Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
fix: preserve exact AAC container frame duration
Soohong Kimddec03cb2fa9
diff --git a/sprite_gen/audio/processing.py b/sprite_gen/audio/processing.pyindex 093e186..e701d40 100644--- a/sprite_gen/audio/processing.py+++ b/sprite_gen/audio/processing.py@@ -15,10 +15,11 @@ from sprite_gen._deps import npfrom sprite_gen.audio import playbackRATE = 48000+PROCESSING_VERSION = 3def require_tools():- for name in ('ffmpeg',):+ for name in ('ffmpeg', 'ffprobe'):if not shutil.which(name):raise ValueError(f'{name} is required on PATH for sound processing')encoders = _ffmpeg(['-encoders']).stdout.decode(errors='replace')@@ -49,6 +50,19 @@ def _pcm_args():return ['-f', 'f32le', '-ar', str(RATE), '-ac', '2', '-i', 'pipe:0']+def _check_m4a_duration(path, frames):+ result = subprocess.run(['ffprobe', '-v', 'error', '-select_streams', 'a:0',+ '-show_entries', 'stream=duration_ts,time_base', '-of', 'json', str(path)],+ capture_output=True, timeout=180)+ try:+ stream, = json.loads(result.stdout)['streams']+ except (ValueError, KeyError, TypeError):+ stream = None+ if (result.returncode or not isinstance(stream, dict) or+ (stream.get('time_base'), stream.get('duration_ts')) != (f'1/{RATE}', frames)):+ raise ValueError(f'm4a container duration must match the declared {frames} frames at {RATE} Hz')++def measure(samples, target=-18):result = _ffmpeg([*_pcm_args(), '-af', f'loudnorm=I={target}:TP=-2:LRA=11:print_format=json','-f', 'null', '-'], samples.astype('<f4').tobytes())@@ -105,11 +119,15 @@ def export(source: Path, out_dir: Path, *, target_lufs, loop, crossfade):'overlap_frames': round(crossfade*RATE) if loop else 0}, loop)samples, corrections = normalize(samples, target_lufs, loop)files = {}- for fmt, codec in (('ogg', ['-c:a', 'libopus', '-b:a', '160k']), ('m4a', ['-c:a', 'aac', '-b:a', '192k', '-movflags', '+faststart'])):+ for fmt, codec in (('ogg', ['-c:a', 'libopus', '-b:a', '160k']),+ ('m4a', ['-c:a', 'aac', '-b:a', '192k', '-movflags', '+faststart',+ '-movie_timescale', str(RATE)])):path = out_dir / f'sound.{fmt}'encoded_samples, ceiling = samples, -2.0for attempt in range(3):_ffmpeg([*_pcm_args(), '-map_metadata', '-1', *codec, str(path)], encoded_samples.astype('<f4').tobytes())+ if fmt == 'm4a':+ _check_m4a_duration(path, frames['end_frame']-frames['start_frame'])decoded = decode(path)if len(decoded) < len(samples):raise ValueError(f'{fmt} truncated the audio')diff --git a/sprite_gen/audio/sound.py b/sprite_gen/audio/sound.pyindex da07b63..2681b7a 100644--- a/sprite_gen/audio/sound.py+++ b/sprite_gen/audio/sound.py@@ -27,7 +27,7 @@ def generate(*, kind, prompt, duration, event, out_dir, cache_dir=None, loop=Nonraise ValueError('crossfade must be positive and shorter than half the duration')out_dir = Path(out_dir).expanduser().resolve()recipe = {'request': cache.digest(request), 'event': event, 'loop': loop,- 'target_lufs': target, 'crossfade_seconds': crossfade, 'processing_version': playback.VERSION}+ 'target_lufs': target, 'crossfade_seconds': crossfade, 'processing_version': processing.PROCESSING_VERSION}if out_dir.exists():manifest = json.loads((out_dir / 'sound.json').read_text()) if (out_dir / 'sound.json').is_file() else {}if manifest.get('recipe') != recipe:diff --git a/tests/audio/test_sound.py b/tests/audio/test_sound.pyindex 62e5d16..e225197 100644--- a/tests/audio/test_sound.py+++ b/tests/audio/test_sound.py@@ -3,6 +3,7 @@ from concurrent.futures import ThreadPoolExecutorimport ioimport http.clientimport json+import subprocessimport threadingimport urllib.errorimport wave@@ -230,14 +231,50 @@ def test_export_reserves_overlap_for_minimum_playback_rate(tmp_path, monkeypatchprocessing.export(tmp_path/'source', tmp_path, target_lufs=-20, loop=True, crossfade=overlap/48000)-def test_old_processed_output_is_rejected_but_raw_cache_survives(tmp_path, monkeypatch):[email protected]('millisecond_timescale', [False, True])+def test_m4a_container_preserves_declared_frames(tmp_path, monkeypatch, millisecond_timescale):+ frames = 144003+ source = tmp_path/'source.wav'+ source.write_bytes(wav_bytes(frames/48000))+ if millisecond_timescale:+ encode = processing._ffmpeg+ def old_timescale(args, data=None):+ if str(args[-1]).endswith('.m4a'):+ args = [*args[:-1], '-movie_timescale', '1000', args[-1]]+ return encode(args, data)+ monkeypatch.setattr(processing, '_ffmpeg', old_timescale)+ with pytest.raises(ValueError, match='m4a container duration'):+ processing.export(source, tmp_path, target_lufs=-20, loop=True, crossfade=.25)+ return+ result = processing.export(source, tmp_path, target_lufs=-20, loop=True, crossfade=.25)+ probe = subprocess.run(['ffprobe', '-v', 'error', '-select_streams', 'a:0',+ '-show_entries', 'stream=duration_ts,time_base', '-of', 'json',+ str(tmp_path/result['files']['m4a']['path'])],+ capture_output=True, check=True)+ stream = json.loads(probe.stdout)['streams'][0]+ assert stream == {'time_base': '1/48000', 'duration_ts': frames}+ assert result['playback']['end_frame'] == frames+++def test_missing_ffprobe_preflights_before_generation(tmp_path, monkeypatch):+ which = processing.shutil.which+ monkeypatch.setattr(processing.shutil, 'which', lambda name: None if name == 'ffprobe' else which(name))+ monkeypatch.setattr(provider, 'generate', lambda _: pytest.fail('generation before tool preflight'))+ with pytest.raises(ValueError, match='ffprobe is required'):+ sound.generate(kind='sfx', prompt='synthetic', duration=3, event='hit',+ cache_dir=tmp_path/'cache', out_dir=tmp_path/'out')++[email protected]('old_version', [1, 2])+def test_old_processed_output_is_rejected_but_raw_cache_survives(tmp_path, monkeypatch, old_version):calls = []monkeypatch.setattr(provider, 'generate', lambda req: (calls.append(req) or wav_bytes(), {}))kw = dict(kind='bgm', prompt='synthetic', duration=3, event='music',cache_dir=tmp_path/'cache', out_dir=tmp_path/'out')result = sound.generate(**kw)- assert result['version'] == result['recipe']['processing_version'] == 2- old = {**result, 'version': 1, 'recipe': {**result['recipe'], 'processing_version': 1}}+ assert result['version'] == 2+ assert result['recipe']['processing_version'] == 3+ old = {**result, 'version': old_version, 'recipe': {**result['recipe'], 'processing_version': old_version}}(kw['out_dir']/'sound.json').write_text(json.dumps(old))with pytest.raises(ValueError, match='different sound'):sound.generate(**kw)