#! Contains the frames a stream the tests make should decode to, worked out as #! the spec defines them, directly: each block's spectrum, its residue, the #! coupled channels taken apart; its inverse MDCT as its sum; windowed, the #! window's slopes short beside a short block; and laid at its place on one #! timeline, read from the first block's middle to the last's. import std::math; # The frames a stream of `pattern`'s blocks decodes to, each packet's residue # given, `trimmed` frames cut from its end. def defined_frames(channels: u32, trimmed: u64, residues: &vector>): vector { val count := residues.length; val sizes: u64[2] = [64, 256]; var centers := vector::::new(); var center := sizes[pattern[(0) % 9] ? 1 : 0] / 2; for k in 0..count { if k > 0 { center += sizes[pattern[(k - 1) % 9] ? 1 : 0] / 4 + sizes[pattern[(k) % 9] ? 1 : 0] / 4; } centers.append(center); } val length := centers[count - 1] + 128; var timeline := vector::::new(); timeline.resize(length * channels as u64); for k in 0..count { val long := pattern[(k) % 9]; val n := sizes[long ? 1 : 0]; val half := n / 2; val start := centers[k] - half; for c in 0..channels as u64 { var spectrum := vector::::new(); spectrum.resize(half); for i in 0..half { if channels == 1 { spectrum[i] = residues[k][i] as f64; } else { # Taken apart, as the magnitude and angle were coupled. val m := residues[k][2 * i] as f64; val a := residues[k][2 * i + 1] as f64; var mag := m; var ang := a; if m > 0 { if a > 0 { ang = m - a; } else { ang = m; mag = m + a; } } else { if a > 0 { ang = m + a; } else { ang = m; mag = m - a; } } spectrum[i] = c == 0 ? mag : ang; } } val before_short := long && k > 0 && !pattern[(k - 1) % 9]; val after_short := long && k + 1 < count && !pattern[(k + 1) % 9]; for t in 0..n { var sum: f64 = 0; for i in 0..half { sum += spectrum[i] * math::cos(math::tau / n as f64 * (t as f64 + 0.5 + n as f64 / 4) * (i as f64 + 0.5)); } timeline[(start + t) * channels as u64 + c] += sum * window_at(n, t, before_short, after_short); } } } val first := centers[0]; val total := centers[count - 1] - first - trimmed; var expected := vector::::new(); for f in 0..total { for c in 0..channels as u64 { expected.append(timeline[(first + f) * channels as u64 + c] as f32); } } return expected; } # Vorbis's window, at a sample of a block of a size, its slopes short beside a # short block, as the spec defines it. def window_at(n: u64, t: u64, before_short: bool, after_short: bool): f64 { val short: u64 = 64; val half := n / 2; var rise_from: u64 = 0; var rise := half; if before_short { rise_from = n / 4 - short / 4; rise = short / 2; } var fall_from := half; var fall := half; if after_short { fall_from = n * 3 / 4 - short / 4; fall = short / 2; } if t < rise_from { return 0; } if t < rise_from + rise { val s := math::sin(((t - rise_from) as f64 + 0.5) / rise as f64 * math::pi / 2); return math::sin(math::pi / 2 * s * s); } if t < fall_from { return 1; } if t < fall_from + fall { val s := math::sin(((t - fall_from) as f64 + 0.5) / fall as f64 * math::pi / 2 + math::pi / 2); return math::sin(math::pi / 2 * s * s); } return 0; }