forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBa63d087Tahinli2 months ago2151 commits

learn_sfb.py56 lines, 2.0 KiBHistoryRawDownload
1"""Measure the MPEG-1 long-block scalefactor band edges: flat spectrum, one
2band attenuated at a time, read the edges off the recovered coefficients."""
3
4import sys
5import numpy as np
6from mp3probe import BitWriter, Granule, frame_bytes, ffmpeg_decode
7from probe_engine import GLOBAL_GAIN, SCALE
8
9TABLE1 = [(1, 0b1), (3, 0b001), (2, 0b01), (3, 0b000)]
10SC = 13 # slen1 = slen2 = 3
11SLEN = 3
12NBANDS = 21 # scalefactors transmitted for sfb 0..20
13
14
15def probe_bits(band):
16 bw = BitWriter()
17 for b in range(NBANDS):
18 bw.w(7 if b == band else 0, SLEN)
19 ln, code = TABLE1[3] # (1,1)
20 for _ in range(288):
21 bw.w(code, ln)
22 bw.w(0, 1)
23 bw.w(0, 1)
24 return bw.bits
25
26
27def main():
28 Minv = np.load("Minv.npy")
29 for srate_idx, srate in enumerate([44100, 48000, 32000]):
30 stream = b""
31 for band in range(NBANDS):
32 bits = probe_bits(band)
33 g = Granule(part2_3_length=len(bits), big_values=288,
34 global_gain=GLOBAL_GAIN, scalefac_compress=SC,
35 table_select=(1, 1, 1))
36 stream += frame_bytes([g, Granule()], [bits, []], srate_idx=srate_idx)
37 stream += frame_bytes([Granule(), Granule()], [[], []], srate_idx=srate_idx)
38 pcm, err = ffmpeg_decode(stream)
39 assert not err.strip(), err
40 edges = [0]
41 for band in range(NBANDS):
42 xr = (pcm[band * 2304:(band + 1) * 2304] @ Minv)[:576]
43 att = np.abs(xr) / SCALE
44 low = np.where(att < 0.5)[0] # attenuated by 2^-3.5
45 if len(low) == 0:
46 print(f" {srate}: band {band} showed no attenuation")
47 continue
48 edges.append(int(low[-1]) + 1)
49 widths = [edges[i + 1] - edges[i] for i in range(len(edges) - 1)]
50 widths.append(576 - edges[-1])
51 print(f"{srate} long widths ({sum(widths)}): {widths}")
52 return 0
53
54
55if __name__ == "__main__":
56 sys.exit(main())