Fixed STEM encoding for consistent conversion

This commit is contained in:
patben8 committed 2026-10-19 23:57:28 +02:00
1 parent 3e9d5d5528
commit 21e249ff97
4 files changed
+38 -11

No files matched your search

@@ -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
+15 -7
View File
@@ -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:
+10
View File
@@ -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")