Files
audio/audio/vorbis/floors.sakura
T
2026-10-30 16:12:42 -05:00

155 lines
5.0 KiB
Plaintext

#! Contains floor 1, the curve each channel's spectrum is shaped by: its posts'
#! Y values read from a packet, each predicted from its neighbors and moved by
#! what was read, then lines drawn between them, as Vorbis I's seventh section
#! draws them, in integers, so every decoder draws the same curve.
import std::math;
module malunal::audio::vorbis;
# What a floor's Y values, each a step of about 0.55 dB, multiply the spectrum
# by: from about -140 dB at 0 to 1 at 255, as the spec's table gives them.
def inverse_db_table(): f32[256] {
var table: f32[256];
val step := math::log(1.0 / 1.0649863e-07) / 255.0;
for i in 0u..256u {
table[i] = math::exp((i as f64 - 255.0) * step) as f32;
}
return table;
}
const inverse_db: f32[256] = inverse_db_table();
# The Y values' range, by the floor's multiplier.
const floor_ranges: u32[4] = [256, 128, 86, 64];
# Reads a channel's floor from a packet.
# @param ys Where its posts' Y values go, as read.
# @returns Whether the channel has a floor this packet: none where its first
# bit says so, or the packet ended while it was read.
def read_floor1(floor: &floor_setup, books: &vector<codebook>, b: &mut bits, ys: slice<i32>): bool {
return false if b.read(1) == 0;
val width := ilog(floor_ranges[(floor.multiplier - 1) as u64] - 1);
ys[0] = b.read(width) as i32;
ys[1] = b.read(width) as i32;
var post: u64 = 2;
for p in 0..floor.partitions {
val class := floor.classes[p as u64] as u64;
val count := floor.dimensions[class] as u64;
val sub := floor.subclasses[class] as u32;
val one: i32 = 1;
val mask := (one << sub) - 1;
var choice: i32 = 0;
if sub > 0 {
choice = books[floor.masterbooks[class] as u64].decode(b);
return false if choice < 0;
}
for j in 0..count {
val book := floor.subbooks[class * 8 + (choice & mask) as u64];
choice >>= sub;
if book >= 0 {
val y := books[book as u64].decode(b);
return false if y < 0;
ys[post + j] = y;
} else {
ys[post + j] = 0;
}
}
post += count;
}
return !b.past;
}
# Multiplies a channel's spectrum by its floor's curve.
# @param ys The posts' Y values, as read; worked out in place.
# @param flags Where whether each post is drawn to goes.
# @param spectrum The spectrum, of `half` values.
def apply_floor1(floor: &floor_setup, ys: slice<i32>, flags: slice<bool>, spectrum: slice<f32>, half: u32): void {
val range := floor_ranges[(floor.multiplier - 1) as u64] as i32;
val count := floor.posts as u64;
flags[0] = true;
flags[1] = true;
for i in 2..count {
val low := floor.low[i] as u64;
val high := floor.high[i] as u64;
val predicted := render_point(floor.xs[low] as i32, ys[low], floor.xs[high] as i32, ys[high], floor.xs[i] as i32);
val value := ys[i];
val high_room := range - predicted;
val low_room := predicted;
val room := (high_room < low_room ? high_room : low_room) * 2;
if value == 0 {
flags[i] = false;
ys[i] = predicted;
continue;
}
flags[low] = true;
flags[high] = true;
flags[i] = true;
if value >= room {
ys[i] = high_room > low_room ? value - low_room + predicted : predicted - value + high_room - 1;
} else if (value & 1) != 0 {
ys[i] = predicted - (value + 1) / 2;
} else {
ys[i] = predicted + value / 2;
}
}
val multiplier := floor.multiplier as i32;
var lx: i32 = 0;
var ly := ys[0] * multiplier;
for k in 1..count {
val i := floor.order[k] as u64;
continue if !flags[i];
val hx := floor.xs[i] as i32;
val hy := ys[i] * multiplier;
render_line(lx, ly, hx, hy, half as i32, spectrum);
lx = hx;
ly = hy;
}
if lx < half as i32 {
val scale := inverse_db[clamp_y(ly)];
for x in lx as u64..half as u64 {
spectrum[x] *= scale;
}
}
}
# Where the line between two posts is at an X, as the spec rounds it.
def render_point(x0: i32, y0: i32, x1: i32, y1: i32, x: i32): i32 {
val dy := y1 - y0;
val across := x1 - x0;
val off := (dy < 0 ? -dy : dy) * (x - x0) / across;
return dy < 0 ? y0 - off : y0 + off;
}
# Multiplies a spectrum by a line between two posts, from the first's X up to
# the second's, short of `end`, stepping as Bresenham's lines do.
def render_line(x0: i32, y0: i32, x1: i32, y1: i32, end: i32, spectrum: slice<f32>): void {
val dy := y1 - y0;
val across := x1 - x0;
val rise := dy < 0 ? -dy : dy;
val whole := rise / across;
val base := dy < 0 ? -whole : whole;
val step := dy < 0 ? base - 1 : base + 1;
val left := rise - whole * across;
val last := x1 < end ? x1 : end;
return if x0 >= last;
var y := y0;
var error: i32 = 0;
spectrum[x0 as u64] *= inverse_db[clamp_y(y)];
for x in (x0 + 1) as u64..last as u64 {
error += left;
if error >= across {
error -= across;
y += step;
} else {
y += base;
}
spectrum[x] *= inverse_db[clamp_y(y)];
}
}
def clamp_y(y: i32): u64 {
return (y < 0 ? 0 : (y > 255 ? 255 : y)) as u64;
}