ws-audio2: audio/mpc + codecs/audio/Musepack: Musepack SV7 / SV8 decoding (libmpcdec r475 port, BSD)
Gapless (synthesis delay, SV8 leading silence, SV7 last frame length), seeking (SV8 by packet with one packet of pre-roll: exact; SV7 by frame with 32 frames of pre-roll like libmpcdec), APEv2 tags via codecs/audio/ApeTag (merged from ws/audio2-lossless), ID3v2 skipped. Fixtures build mpcenc / mppenc / a float libmpcdec dumper from the pinned upstream tarballs through nix; 132-140 dB SNR vs libmpcdec float, 82.7 dB vs ffmpeg's s16 decode.
This commit is contained in:
1 parent
b7006dbbed
commit
22615b503e
10 files changed
+2060
No files matched your search
@@ -0,0 +1,130 @@
|
||||
# Musepack fixtures (tools/fixtures.py). There is no Musepack encoder in nixpkgs, so the reference tools are built
|
||||
# here from the official sources through nix (fetchurl with pinned hashes): mpcenc / libmpcdec from musepack_src_r475
|
||||
# (SV8, BSD / LGPL tools), mppenc 1.16 (SV7), and `mpcf32`, a 20-line C program linked against r475's libmpcdec that
|
||||
# writes its float output (gapless, the reference) as f32le, optionally after mpc_demux_seek_sample.
|
||||
# tests/gen/mpc_cases.luau { name, data, version, channels, rate, frames, exact (frames that match libmpcdec
|
||||
# exactly), ref (mpcf32's f32le, the first REF frames), ff? (ffmpeg's s16 decode as f32le,
|
||||
# aligned: ffmpeg is not gapless), tags? }
|
||||
# tests/gen/mpc_seek.luau { name (a case), at (sample), ref (mpcf32 after seeking to `at`, SEEK_REF stereo frames) }
|
||||
import importlib.util, os, subprocess, tempfile, shutil
|
||||
|
||||
_spec = importlib.util.spec_from_file_location("_audio", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_audio.py"))
|
||||
A = importlib.util.module_from_spec(_spec)
|
||||
_spec.loader.exec_module(A)
|
||||
|
||||
INPUTS = ["tools/fixtures/_audio.py"]
|
||||
|
||||
NIX = r'''
|
||||
with import <nixpkgs> {};
|
||||
let
|
||||
flags = "-fcommon -Wno-error -Wno-implicit-function-declaration -Wno-incompatible-pointer-types -Wno-int-conversion";
|
||||
f32 = writeText "mpcf32.c" ''
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <mpc/mpcdec.h>
|
||||
int main(int argc, char **argv) {
|
||||
mpc_reader r; mpc_streaminfo si; MPC_SAMPLE_FORMAT b[MPC_DECODER_BUFFER_LENGTH];
|
||||
if (argc < 2 || mpc_reader_init_stdio(&r, argv[1]) < 0) return 1;
|
||||
mpc_demux *d = mpc_demux_init(&r); if (!d) return 2;
|
||||
mpc_demux_get_info(d, &si);
|
||||
if (argc > 2) mpc_demux_seek_sample(d, (mpc_uint64_t) atoll(argv[2]));
|
||||
for (;;) { mpc_frame_info f; f.buffer = b;
|
||||
if (mpc_demux_decode(d, &f) != MPC_STATUS_OK) { fprintf(stderr, "error\n"); return 3; }
|
||||
if (f.bits == -1) break;
|
||||
fwrite(b, sizeof(float), f.samples * si.channels, stdout); }
|
||||
return 0; }
|
||||
'';
|
||||
in symlinkJoin { name = "musepack-tools"; paths = [
|
||||
(stdenv.mkDerivation {
|
||||
pname = "musepack"; version = "r475";
|
||||
src = fetchurl { url = "https://files.musepack.net/source/musepack_src_r475.tar.gz"; sha256 = "a4b1742f997f83e1056142d556a8c20845ba764b70365ff9ccf2e3f81c427b2b"; };
|
||||
nativeBuildInputs = [ cmake ];
|
||||
NIX_CFLAGS_COMPILE = flags;
|
||||
postPatch = "sed -i -e /mpcgain/d -e /mpcchap/d -e /wavcmp/d -e /mpccut/d CMakeLists.txt";
|
||||
cmakeFlags = [ "-DCMAKE_POLICY_VERSION_MINIMUM=3.5" "-DSHARED=OFF" ];
|
||||
postBuild = "$CC -O2 -I../include ${f32} libmpcdec/libmpcdec_static.a -lm -o mpcf32";
|
||||
installPhase = "mkdir -p $out/bin; cp mpcenc/mpcenc mpcdec/mpcdec mpcf32 $out/bin/";
|
||||
})
|
||||
(stdenv.mkDerivation {
|
||||
pname = "mppenc"; version = "1.16";
|
||||
src = fetchurl { url = "https://files.musepack.net/source/mppenc-1.16.tar.bz2"; sha256 = "f90781856ca7b6ce1ec7033321fadbcd04fe264d0349c20b49ad05f7cb2655ad"; };
|
||||
nativeBuildInputs = [ cmake ];
|
||||
NIX_CFLAGS_COMPILE = flags;
|
||||
cmakeFlags = [ "-DCMAKE_POLICY_VERSION_MINIMUM=3.5" ];
|
||||
installPhase = "mkdir -p $out/bin; cp src/mppenc $out/bin/";
|
||||
})
|
||||
]; }
|
||||
'''
|
||||
|
||||
# name, encoder (mpcenc = SV8, mppenc = SV7), signal, rate, channels, seconds, extra options
|
||||
CASES = [
|
||||
("sv8_q5_stereo", "mpcenc", "music", 44100, 2, 1.2, ["--quality", "5", "--num_frames", "1"]),
|
||||
("sv8_q3_mono32k", "mpcenc", "sweep", 32000, 1, 0.5, ["--quality", "3"]),
|
||||
("sv8_q9_noise48k", "mpcenc", "noise", 48000, 2, 0.4, ["--quality", "9", "--num_frames", "2"]),
|
||||
("sv8_q1_sine", "mpcenc", "sine", 44100, 2, 0.3, ["--quality", "1", "--num_frames", "0",
|
||||
"--artist", "Some One", "--tag", "Title=Mpc Tone", "--tag", "Year=2026"]),
|
||||
("sv7_q5_stereo", "mppenc", "music", 44100, 2, 1.2, ["--quality", "5"]),
|
||||
("sv7_q8_noise", "mppenc", "noise", 44100, 2, 0.3, ["--quality", "8"]),
|
||||
("sv7_q2_sweep", "mppenc", "sweep", 48000, 2, 0.5, ["--quality", "2"]),
|
||||
]
|
||||
REF = 22050 # frames of reference output kept per case (the fixture module stays small)
|
||||
FF = ("sv8_q5_stereo", "sv7_q5_stereo") # cases also compared with ffmpeg
|
||||
SEEKS = [("sv8_q5_stereo", 30000), ("sv8_q9_noise48k", 9000), ("sv7_q5_stereo", 40000)]
|
||||
SEEK_REF = 4096
|
||||
|
||||
|
||||
def tools():
|
||||
out = subprocess.run(["nix", "build", "--impure", "--no-link", "--print-out-paths", "--expr", NIX],
|
||||
capture_output=True, check=True).stdout.decode().strip().split("\n")[-1]
|
||||
return os.path.join(out, "bin")
|
||||
|
||||
|
||||
def generate(ctx):
|
||||
bin_ = tools()
|
||||
d = tempfile.mkdtemp(prefix="fmt-mpc-")
|
||||
cases, seeks, files = [], [], {}
|
||||
try:
|
||||
for name, enc, kind, rate, ch, secs, extra in CASES:
|
||||
frames = int(rate * secs)
|
||||
sig = A.signal(kind, rate, ch, frames, seed=len(name))
|
||||
wav = os.path.join(d, name + ".wav")
|
||||
mpc = os.path.join(d, name + ".mpc")
|
||||
with open(wav, "wb") as f:
|
||||
f.write(A.wav(sig, rate, ch))
|
||||
subprocess.run([os.path.join(bin_, enc)] + extra + ["--silent", "--overwrite", wav, mpc], capture_output=True, check=True)
|
||||
with open(mpc, "rb") as f:
|
||||
data = f.read()
|
||||
files[name] = mpc
|
||||
ref = subprocess.run([os.path.join(bin_, "mpcf32"), mpc], capture_output=True, check=True).stdout
|
||||
got = len(ref) // (4 * ch)
|
||||
assert got == frames, (name, got, frames)
|
||||
# SV7: libmpcdec decodes the zero-filled data past the last frame for the synthesis tail (reading past
|
||||
# its tables); we decode silence there, so only the frames before it compare exactly
|
||||
exact = frames
|
||||
if enc == "mppenc":
|
||||
exact = min(frames, int.from_bytes(data[4:8], "little") * 1152 - 481)
|
||||
c = {"name": name, "data": data, "version": 8 if enc == "mpcenc" else 7, "channels": ch, "rate": rate,
|
||||
"frames": frames, "exact": exact, "ref": ref[:REF * ch * 4]}
|
||||
if name in FF:
|
||||
_, _, ff = A.ffdecode(ctx, data, ext="mpc")
|
||||
# ffmpeg decodes without the gapless trimming: find the lag of the reference inside its output
|
||||
refl = A.from_f32le(ref)
|
||||
best = None
|
||||
for lag in range(0, 2400):
|
||||
if (lag + 200) * ch > len(ff):
|
||||
break
|
||||
e = sum((ff[(lag + i) * ch] - refl[i * ch]) ** 2 for i in range(100, 200))
|
||||
if best is None or e < best[0]:
|
||||
best = (e, lag)
|
||||
c["ff"] = A.f32le(ff[best[1] * ch:(best[1] + REF) * ch])
|
||||
if "--artist" in extra:
|
||||
c["tags"] = {"artist": "Some One", "title": "Mpc Tone", "date": "2026"}
|
||||
cases.append(c)
|
||||
for name, at in SEEKS:
|
||||
ref = subprocess.run([os.path.join(bin_, "mpcf32"), files[name], str(at)], capture_output=True, check=True).stdout
|
||||
data = next(c["data"] for c in cases if c["name"] == name)
|
||||
seeks.append({"name": name, "at": at, "ref": ref[:SEEK_REF * 8]})
|
||||
finally:
|
||||
shutil.rmtree(d, ignore_errors=True)
|
||||
ctx.emit("mpc_cases", cases)
|
||||
ctx.emit("mpc_seek", seeks)
|
||||
Reference in new issue
Block a user