diff --git a/CLAUDE.md b/CLAUDE.md index 93f585e..f67aafe 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -205,8 +205,13 @@ Two independent apps that talk over HTTP: 8-20 kHz bands for moments (twoloop: never). The separation is a CALLBACK — the backend never imports torch; the engine (below) supplies it, the tests a fake. Measured traps: FFmpeg's resampler upmixes mono at -3 dB, - so mono is duplicated by hand; resampling does not move time; both - encoders (`aac_at` on macOS, `aac` elsewhere) are timing-exact but decode + so mono is duplicated by hand; resampling does not move time; the + encoder is FFmpeg's `aac` on EVERY OS, never Apple's `aac_at` + (`default_encoder`): Traktor on macOS honours the MP4 edit list but on + WINDOWS skips a fixed 1024 samples of priming, so `aac_at`'s 2112 put a + stem file ~25 ms early on a PC (2026-10-19: exactly the 9 of 148 stems on + a stick that Konduktor had encoded with `aac_at` were off, Reset fixed + each; `aac` and every commercial stem prime 1024). Both decode up to ~2 frames LONGER, so length checks allow 0..2048 samples and the duration reported is the source's; AAC pre-echo fools threshold onsets, so alignment is checked by cross-correlation. **Two JSON literals**: the diff --git a/backend/konduktor/adapters/traktor/timebase.py b/backend/konduktor/adapters/traktor/timebase.py index bf4ba62..70b3526 100644 --- a/backend/konduktor/adapters/traktor/timebase.py +++ b/backend/konduktor/adapters/traktor/timebase.py @@ -11,8 +11,12 @@ stores — grid markers, cues, loops — sits samples LATER than the same moment in the decoded audio: ~51.2 ms at 44.1 kHz, ~47.0 ms at 48 kHz. The rule is in SAMPLES. An MP3 WITHOUT a header is decoded the same way by both sides (nothing to skip, nothing to trim), so its offset is -0 — as is every other format: WAV, AIFF, FLAC and AAC (Traktor honours the MP4 -edit list) line up to the sample. +0 — as is every other format: WAV, AIFF, FLAC and AAC line up to the sample. +AAC with a caveat this module cannot fix: Traktor on macOS honours the MP4 +edit list, Traktor on WINDOWS skips a fixed 1024 samples instead (2026-10-19: +Apple-encoded files, priming 2112, sat ~25 ms early on a PC; 1024-primed ones +did not). One collection serves both, so the offset stays 0 and Konduktor +writes its own stem files at 1024 (`stem_file.default_encoder`). Measured 2026-09-30 in Traktor 4.5 ("kit 4", see the stem-conversion discussion log): hotcues Konduktor wrote on a synthetic kick, blinded, landed on it only diff --git a/backend/konduktor/core/stem_file.py b/backend/konduktor/core/stem_file.py index 68033e5..18fecca 100644 --- a/backend/konduktor/core/stem_file.py +++ b/backend/konduktor/core/stem_file.py @@ -5,8 +5,10 @@ stem files (see the stem-conversion discussion log): an `ipod` MP4 with FIVE AAC-LC stereo 44.1 kHz streams — stream 0 the full mix and 1-4 drums / bass / synths / vox — streams 1-4 not enabled (`disposition` 0, so a player that knows nothing of stems plays the mix), and a `moov/udta/stem` box holding JSON that -names and colours the stems. Traktor 4.5 loads such a file as stems and honours -the MP4 edit list, so a stem file's time base is the decoded audio's own. +names and colours the stems. Traktor 4.5 loads such a file as stems. On macOS +it honours the MP4 edit list; on Windows it skips a fixed 1024 samples of +priming — so the file must prime exactly 1024 (`default_encoder`), and then its +time base is the decoded audio's own on both. Platform-independent on purpose: the separation itself is a callback (`separate`) so this module never imports torch — the stem ENGINE supplies it @@ -39,7 +41,6 @@ import fractions import json import queue import struct -import sys import threading from dataclasses import dataclass from pathlib import Path @@ -117,11 +118,18 @@ class Written: def default_encoder() -> str: - """Apple's AudioToolbox encoder on macOS (the better AAC encoder there), - FFmpeg's own elsewhere. Both are timing-exact in Traktor (kit 1).""" - import av + """FFmpeg's own `aac`, on EVERY platform — never Apple's `aac_at`. - return "aac_at" if sys.platform == "darwin" and "aac_at" in av.codecs_available else "aac" + Both are timing-exact in Traktor on macOS (kit 1), which honours the MP4 + edit list. Traktor on WINDOWS does not: it skips a fixed 1024 samples of + priming, which is what `aac` (and every commercial stem file measured) + primes — but `aac_at` primes 2112, so its files sat 1088 samples (~25 ms) + early on a PC. Found 2026-10-19: of 148 stem files on one stick, exactly the + 9 Konduktor had encoded with `aac_at` were off in Windows Traktor, every one, + and Reset fixed each; a 1024-primed one beside them was not. The cost is + speed: `aac_at` encodes several times faster on a Mac. + """ + return "aac" def choose_bitrate(source: Source) -> int: diff --git a/backend/test_stem_file.py b/backend/test_stem_file.py index dddee8f..b471b1a 100644 --- a/backend/test_stem_file.py +++ b/backend/test_stem_file.py @@ -204,6 +204,16 @@ for enc in encoders: covr = MP4(str(out)).tags.get("covr") check(f"{enc}: the cover art is copied", bool(covr) and bytes(covr[0]) == COVER) +# Traktor on Windows skips a FIXED 1024 samples of priming where macOS honours +# the edit list: Apple-encoded stems (2112) sat ~25 ms early on a PC +# (2026-10-19). So the default encoder is FFmpeg's on every platform, and what +# it writes primes exactly 1024 — the commercial stems' value. +from konduktor.core import mp4_edit # noqa: E402 +check("the default encoder is FFmpeg's aac, on every platform", sf.default_encoder() == "aac") +w = sf.build(MP3, tmp / "Default.stem.m4a", fake_separate) +prime = round((mp4_edit.priming_seconds(w.path) or 0) * SR) +check("a default-built stem file primes exactly 1024 samples", prime == 1024, str(prime)) + w = sf.build(MP3_128, tmp / "low.stem.m4a", fake_separate, encoder="aac") check("a 128 kbps source is encoded at the 256 kbps floor", w.bit_rate == 256_000, str(w.bit_rate)) w = sf.build(FLAC_SRC, tmp / "Lossless.stem.m4a", fake_separate, encoder="aac")