Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.
fix: prepare audio loop buffers after browser decoding
Soohong Kim4e2d851ee669
diff --git a/pyproject.toml b/pyproject.tomlindex fe4aa62..f067b86 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -99,4 +99,4 @@ include = ["sprite_gen*"]# not feared: adding curator/src/deep/new.js under the old patterns built a wheel without it.# `curator/**/*` derives the answer from the filesystem, the same fix as packages.find above.[tool.setuptools.package-data]-sprite_gen = ["serve/curator/**/*", "serve/composer/**/*"]+sprite_gen = ["serve/curator/**/*", "serve/composer/**/*", "audio/*.mjs"]diff --git a/sprite_gen/audio/manifest.py b/sprite_gen/audio/manifest.pyindex dc835b2..d850796 100644--- a/sprite_gen/audio/manifest.py+++ b/sprite_gen/audio/manifest.py@@ -8,6 +8,8 @@ import mathfrom pathlib import Pathimport re+from sprite_gen.audio import playback+def event_name(value):if not isinstance(value, str) or not re.fullmatch(r'[A-Za-z0-9][A-Za-z0-9_.:-]{0,127}', value):@@ -24,17 +26,17 @@ def _number(value, name, low, high):def load(path, max_bytes):path = Path(path).resolve()value = json.loads(path.read_text())- if not isinstance(value, dict) or value.get('kind') != 'sprite-gen-sound' or value.get('version') != 1:- raise ValueError('audio source must be a sprite-gen-sound version 1 manifest')+ if not isinstance(value, dict) or value.get('kind') != 'sprite-gen-sound' or value.get('version') != playback.VERSION:+ raise ValueError('audio source must be a sprite-gen-sound version 2 manifest')if value.get('audio_kind') not in ('sfx', 'bgm') or type(value.get('loop')) is not bool:raise ValueError('invalid audio kind or loop flag')- duration = _number(value.get('duration_seconds'), 'duration', .001, 601)+ frames = playback.validate(value.get('playback'), value['loop'])+ duration = playback.duration(frames)+ if value.get('duration_seconds') != duration:+ raise ValueError('audio duration must be derived from playback frames')result = {'event': event_name(value.get('event')), 'audio_kind': value['audio_kind'], 'loop': value['loop'],'duration_seconds': duration, 'target_lufs': _number(value.get('target_lufs'), 'target LUFS', -36, -9),- 'loop_start_seconds': _number(value.get('loop_start_seconds'), 'loop start', 0, duration),- 'loop_end_seconds': _number(value.get('loop_end_seconds'), 'loop end', .001, duration), 'files': {}}- if result['loop_start_seconds'] >= result['loop_end_seconds']:- raise ValueError('audio loop interval must have positive duration')+ 'playback': frames, 'files': {}}files = value.get('files')if not isinstance(files, dict) or set(files) != {'ogg', 'm4a'}:raise ValueError('audio source requires ogg and m4a files')@@ -54,7 +56,7 @@ def load(path, max_bytes):if hashlib.sha256(data).hexdigest() != file.get('sha256'):raise ValueError('audio file digest does not match its manifest')record = {'path': name, 'sha256': file['sha256'],- 'duration_seconds': _number(file.get('duration_seconds'), 'file duration', duration-2/48000, 602),+ 'duration_seconds': _number(file.get('duration_seconds'), 'file duration', frames['end_frame']/frames['sample_rate']-2/frames['sample_rate'], 602),'lufs': _number(file.get('lufs'), 'LUFS', -70, 0),'true_peak_dbfs': _number(file.get('true_peak_dbfs'), 'true peak', -100, -1)}if abs(record['lufs'] - result['target_lufs']) > 1:diff --git a/sprite_gen/audio/playback.py b/sprite_gen/audio/playback.pynew file mode 100644index 0000000..6252e00--- /dev/null+++ b/sprite_gen/audio/playback.py@@ -0,0 +1,33 @@+# SPDX-License-Identifier: Apache-2.0+"""Frame contract and the browser player shipped with every audio pack."""+from __future__ import annotations++import hashlib+from pathlib import Path++VERSION = 2+PLAYER_PATH = Path(__file__).with_name('sprite-gen-audio-v2.mjs')+++def validate(value, loop):+ if not isinstance(value, dict) or value.get('version') != VERSION:+ raise ValueError('audio playback requires frame contract version 2')+ fields = ('sample_rate', 'start_frame', 'end_frame', 'overlap_frames')+ if any(type(value.get(k)) is not int for k in fields):+ raise ValueError('audio playback coordinates must be integer frames')+ rate, start, end, overlap = (value[k] for k in fields)+ if not 8000 <= rate <= 192000 or not 0 <= start < end <= 601*rate:+ raise ValueError('invalid audio playback interval')+ if (loop and not 2 <= overlap < (end-start)/2) or (not loop and overlap != 0):+ raise ValueError('invalid audio playback overlap')+ return {'version': VERSION, **{k: value[k] for k in fields}}+++def duration(value):+ return (value['end_frame']-value['start_frame']-value['overlap_frames'])/value['sample_rate']+++def player():+ data = PLAYER_PATH.read_bytes()+ return {'path': PLAYER_PATH.name, 'sha256': hashlib.sha256(data).hexdigest(),+ 'export': 'prepareAudioBuffer'}, datadiff --git a/sprite_gen/audio/processing.py b/sprite_gen/audio/processing.pyindex 52c63d8..7bf402e 100644--- a/sprite_gen/audio/processing.py+++ b/sprite_gen/audio/processing.py@@ -1,5 +1,5 @@# SPDX-License-Identifier: Apache-2.0-"""PCM loop overlap, EBU R128 normalization and measured browser audio encodes."""+"""EBU R128 normalization and unspliced browser audio encodes."""from __future__ import annotationsimport hashlib@@ -12,8 +12,9 @@ import subprocessimport numpy as np+from sprite_gen.audio import playback+RATE = 48000-CODEC_TAPER_SECONDS = .005def require_tools():@@ -44,22 +45,6 @@ def decode(path):return samples-def make_loop(samples, seconds):- count = round(seconds * RATE)- if count < 2 or count * 2 >= len(samples):- raise ValueError('loop crossfade must be positive and shorter than half the source')- weight = (.5 - .5 * np.cos(np.linspace(0, np.pi, count)))[:, None]- overlap = samples[-count:] * (1 - weight) + samples[:count] * weight- output = np.concatenate((samples[count:-count], overlap)).astype('<f4')- # Lossy encoders have no preceding signal at the beginning of a standalone file.- # A short, smooth zero boundary prevents that priming error becoming a wrap impulse.- guard = min(round(CODEC_TAPER_SECONDS * RATE), len(output)//4)- taper = (.5 - .5*np.cos(np.linspace(0, np.pi, guard)))[:, None]- output[:guard] *= taper- output[-guard:] *= taper[::-1]- return output--def _pcm_args():return ['-f', 'f32le', '-ar', str(RATE), '-ac', '2', '-i', 'pipe:0']@@ -115,11 +100,10 @@ def normalize(samples, target, loop):def export(source: Path, out_dir: Path, *, target_lufs, loop, crossfade):samples = decode(source)- source_seconds = len(samples) / RATE- if loop:- samples = make_loop(samples, crossfade)+ frames = playback.validate({'version': playback.VERSION, 'sample_rate': RATE,+ 'start_frame': 0, 'end_frame': len(samples),+ 'overlap_frames': round(crossfade*RATE) if loop else 0}, loop)samples, corrections = normalize(samples, target_lufs, loop)- duration = len(samples) / RATEfiles = {}for fmt, codec in (('ogg', ['-c:a', 'libopus', '-b:a', '160k']), ('m4a', ['-c:a', 'aac', '-b:a', '192k', '-movflags', '+faststart'])):path = out_dir / f'sound.{fmt}'@@ -129,8 +113,8 @@ def export(source: Path, out_dir: Path, *, target_lufs, loop, crossfade):decoded = decode(path)if len(decoded) < len(samples) - 2:raise ValueError(f'{fmt} truncated the audio')- playback = decoded[:len(samples)] # AAC may retain a final partial codec frame.- measured = measure(np.tile(playback, (3, 1)) if loop else playback, target_lufs)+ interval = decoded[:len(samples)] # AAC may retain a final partial codec frame.+ measured = measure(np.tile(interval, (3, 1)) if loop else interval, target_lufs)lufs, peak = measured['input_i'], measured['input_tp']if math.isfinite(lufs) and abs(lufs - target_lufs) <= 1 and peak <= -1:break@@ -144,10 +128,5 @@ def export(source: Path, out_dir: Path, *, target_lufs, loop, crossfade):'lufs': lufs, 'true_peak_dbfs': peak, 'encode_passes': attempt + 1,'pcm_peak_limit_dbtp': ceiling,'sha256': hashlib.sha256(path.read_bytes()).hexdigest()}- if loop:- files[fmt]['seam_delta'] = float(np.max(np.abs(playback[-1] - playback[0])))- return {'files': files, 'duration_seconds': duration, 'source_duration_seconds': source_seconds,- 'sample_rate': RATE, 'loop_start_seconds': 0, 'loop_end_seconds': duration,- 'crossfade_seconds': crossfade if loop else 0,- 'codec_taper_seconds': CODEC_TAPER_SECONDS if loop else 0,+ return {'files': files, 'duration_seconds': playback.duration(frames), 'playback': frames,'normalization': {'method': 'loudnorm-two-pass', 'peak_limit_dbtp': -2, 'gain_limiter_passes': corrections}}diff --git a/sprite_gen/audio/sound.py b/sprite_gen/audio/sound.pyindex 88b05fe..da07b63 100644--- a/sprite_gen/audio/sound.py+++ b/sprite_gen/audio/sound.py@@ -10,7 +10,7 @@ from pathlib import Pathimport shutilimport tempfile-from sprite_gen.audio import cache, processing, provider+from sprite_gen.audio import cache, processing, provider, playbackfrom sprite_gen.audio.manifest import event_name, load as load_soundfrom sprite_gen.util.cli_kwargs import namespace_from_kwargs@@ -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': 1}+ 'target_lufs': target, 'crossfade_seconds': crossfade, 'processing_version': playback.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:@@ -49,7 +49,7 @@ def generate(*, kind, prompt, duration, event, out_dir, cache_dir=None, loop=Nonsource.write_bytes(data)details = processing.export(source, stage, target_lufs=target, loop=loop, crossfade=crossfade)source.unlink()- manifest = {'kind': 'sprite-gen-sound', 'version': 1, 'event': event, 'audio_kind': kind,+ manifest = {'kind': 'sprite-gen-sound', 'version': playback.VERSION, 'event': event, 'audio_kind': kind,'loop': loop, 'target_lufs': target, 'recipe': recipe, 'provider': receipt, **details}(stage/'sound.json').write_text(json.dumps(manifest, indent=2, allow_nan=False)+'\n')stage.rename(out_dir)diff --git a/sprite_gen/audio/sprite-gen-audio-v2.mjs b/sprite_gen/audio/sprite-gen-audio-v2.mjsnew file mode 100644index 0000000..c0b0f5a--- /dev/null+++ b/sprite_gen/audio/sprite-gen-audio-v2.mjs@@ -0,0 +1,64 @@+// SPDX-License-Identifier: Apache-2.0+// Frame contract v2. No processing is applied before decoding or on each repeat.+const prepared = new WeakMap();+const cached = new WeakMap();++export function prepareAudioBuffer(decoded, playback) {+ const { version, sample_rate: rate, start_frame: start, end_frame: end,+ overlap_frames: overlap } = playback;+ if (version !== 2 || ![rate, start, end, overlap].every(Number.isSafeInteger) ||+ rate < 8000 || rate > 192000 || start < 0 || end <= start || end > 601 * rate ||+ overlap < 0 || (overlap !== 0 && (overlap < 2 || overlap * 2 >= end - start))) {+ throw new RangeError('Invalid audio playback frame contract');+ }+ const key = [version, rate, start, end, overlap].join(':');+ if (prepared.has(decoded)) {+ if (prepared.get(decoded) !== key) throw new RangeError('AudioBuffer is already prepared');+ return decoded;+ }+ if (cached.get(decoded)?.has(key)) return cached.get(decoded).get(key);+ const scale = decoded.sampleRate / rate;+ const first = Math.round(start * scale);+ const last = first + Math.round((end - start) * scale);+ // Round the declared final duration once. Independent rounding of N and M can+ // change N-M by a frame; derive the overlap from the mapped interval instead.+ const length = Math.round((end - start - overlap) * scale);+ const count = last - first - length;+ if (last > decoded.length || length < 1 || count < 0 ||+ (overlap > 0 && (count < 2 || count * 2 >= last - first))) {+ throw new RangeError('Decoded audio does not contain the playback interval');+ }+ const output = new AudioBuffer({ numberOfChannels: decoded.numberOfChannels,+ length, sampleRate: decoded.sampleRate });+ for (let channel = 0; channel < decoded.numberOfChannels; channel++) {+ const source = decoded.getChannelData(channel);+ const target = output.getChannelData(channel);+ if (!overlap) {+ target.set(source.subarray(first, first + length));+ continue;+ }+ const middle = last - first - 2 * count;+ target.set(source.subarray(first + count, last - count));+ for (let i = 0; i < count; i++) {+ const headWeight = 0.5 - 0.5 * Math.cos(Math.PI * i / (count - 1));+ target[middle + i] = source[last - count + i] * (1 - headWeight) ++ source[first + i] * headWeight;+ }+ }+ prepared.set(output, key);+ if (!cached.has(decoded)) cached.set(decoded, new Map());+ cached.get(decoded).set(key, output);+ return output;+}++export function createAudioSource(context, decoded, audio) {+ if (typeof audio.loop !== 'boolean' || audio.loop !== (audio.playback.overlap_frames > 0)) {+ throw new RangeError('Audio loop flag does not match playback overlap');+ }+ const source = context.createBufferSource();+ source.buffer = prepareAudioBuffer(decoded, audio.playback);+ source.loop = audio.loop;+ source.loopStart = 0;+ source.loopEnd = source.buffer.duration;+ return source;+}diff --git a/sprite_gen/compose/game_pack.py b/sprite_gen/compose/game_pack.pyindex d6d17bf..fbc4d8c 100644--- a/sprite_gen/compose/game_pack.py+++ b/sprite_gen/compose/game_pack.py@@ -14,6 +14,7 @@ import tempfilefrom PIL import Imagefrom sprite_gen.audio.manifest import load as load_sound+from sprite_gen.audio.playback import player as audio_playerfrom sprite_gen.spec.assets import finite, load_asset_statesfrom sprite_gen.util.resample import resize_cell@@ -239,6 +240,10 @@ def pack(request: Path, out_dir: Path, *, page_size=MAX_PAGE_SIZE, max_bytes=MAXraise ValueError("output directory must not contain any source file")result, payloads = _encode(_layout(frames, page_size), animations, max_bytes)if audio:+ helper, module = audio_player()+ audio_payloads[helper["path"]] = module+ for record in audio.values():+ record["player"] = helperresult["audio"] = dict(sorted(audio.items()))payloads.update(audio_payloads)payloads["game-pack.json"] = (json.dumps(result, ensure_ascii=False, separators=(",", ":"), allow_nan=False) + "\n").encode()diff --git a/tests/audio/render_playback.mjs b/tests/audio/render_playback.mjsnew file mode 100644index 0000000..a014a97--- /dev/null+++ b/tests/audio/render_playback.mjs@@ -0,0 +1,136 @@+// SPDX-License-Identifier: Apache-2.0+// node tests/audio/render_playback.mjs <cases.json> <output-dir>+// PLAYWRIGHT_MODULE may name an installed Playwright entry point.+import assert from 'node:assert/strict';+import { readFile, mkdir, writeFile } from 'node:fs/promises';+import { createHash } from 'node:crypto';+import { dirname, resolve } from 'node:path';+const { chromium, webkit } = await import(process.env.PLAYWRIGHT_MODULE || 'playwright');+const [caseFile, outputDir] = process.argv.slice(2);+const cases = JSON.parse(await readFile(caseFile, 'utf8'));+await mkdir(outputDir, { recursive: true });+const modulePath = resolve('sprite_gen/audio/sprite-gen-audio-v2.mjs');+const moduleBytes = await readFile(modulePath);+const digest = createHash('sha256').update(moduleBytes).digest('hex');+const reports = [];+for (const [name, engine] of Object.entries({ chromium, webkit })) {+ const browser = await engine.launch({ headless: true });+ try {+ const page = await browser.newPage();+ await page.goto('about:blank');+ for (const rate of [44100, 48000]) {+ const synthetic = await page.evaluate(async ({ code, rate }) => {+ const { prepareAudioBuffer, createAudioSource } = await import(code);+ const check = (ok, message) => { if (!ok) throw new Error(message); };+ // Non-zero origin and a frame count whose independent rounding differs.+ const meta = { version: 2, sample_rate: 48000, start_frame: 13,+ end_frame: 144014, overlap_frames: 12001 };+ const first = Math.round(meta.start_frame * rate / 48000);+ const n = Math.round((meta.end_frame-meta.start_frame)*rate/48000);+ const length = Math.round((meta.end_frame-meta.start_frame-meta.overlap_frames)*rate/48000);+ const count = n-length;+ const input = new AudioBuffer({ sampleRate: rate, length: first+n, numberOfChannels: 2 });+ for (let c=0;c<2;c++) {+ const x=input.getChannelData(c);+ for (let i=0;i<x.length;i++) x[i]=.2*Math.cos(2*Math.PI*(c ? 73 : 1000)*i/rate)+.1*i/x.length;+ }+ const before = input.getChannelData(0).slice();+ const out = prepareAudioBuffer(input, meta);+ check(out.length === length, 'one-frame rounding drift');+ check(prepareAudioBuffer(input, meta) === out, 'decoded input was prepared twice');+ check(prepareAudioBuffer(out, meta) === out, 'prepared output was crossfaded twice');+ check(input.getChannelData(0).every((v,i)=>v===before[i]), 'decoded source mutated');+ for (let c=0;c<2;c++) {+ const x=input.getChannelData(c), y=out.getChannelData(c);+ const expected=x[first+count]-x[first+count-1];+ check((y[0]-y.at(-1))-expected === 0, 'added wrap error');+ const middle=n-2*count;+ check((y[middle]-y[middle-1])-(x[first+n-count]-x[first+n-count-1])===0, 'added join error');+ // The detector must reject an artificial discontinuity, even though+ // a high-frequency signal's normal adjacent difference is allowed.+ const control=y.slice(); control[0]+=.125;+ check(Math.abs((control[0]-control.at(-1))-expected)>.12, 'broken negative control');+ }+ let rejected=false;+ try { prepareAudioBuffer(out, { ...meta, overlap_frames: 12002 }); } catch { rejected=true; }+ check(rejected, 'changed metadata reused prepared audio');+ const ctx=new OfflineAudioContext(2, length*3, rate);+ const source=createAudioSource(ctx,input,{loop:true,playback:meta});+ check(source.buffer===out && source.loop && source.loopStart===0 && source.loopEnd===out.duration, 'source bypasses preparation');+ source.connect(ctx.destination); source.start();+ const rendered=await ctx.startRendering();+ let maxError=0;+ for(let c=0;c<2;c++) for(let i=0;i<rendered.length;i++) {+ maxError=Math.max(maxError,Math.abs(rendered.getChannelData(c)[i]-out.getChannelData(c)[i%length]));+ }+ check(maxError<1e-7, 'rendered looping differs from prepared buffer');+ return { length, count, added_wrap_error:0, added_join_error:0, control_detected:true, render_error:maxError };+ }, { code: `data:text/javascript;base64,${moduleBytes.toString('base64')}`, rate });+ reports.push({ browser:name, browser_version:browser.version(), rate, synthetic });+ for (const item of cases) {+ const manifestPath = resolve(dirname(caseFile), item.manifest);+ const pack = JSON.parse(await readFile(manifestPath,'utf8'));+ const record = item.id ? pack.audio[item.id] : pack;+ const shipped = record.player ? await readFile(resolve(dirname(manifestPath), record.player.path)) : moduleBytes;+ assert.equal(createHash('sha256').update(shipped).digest('hex'), digest);+ if (record.player) assert.equal(record.player.sha256, digest);+ for (const fmt of ['ogg','m4a']) {+ const bytes = await readFile(resolve(dirname(manifestPath),record.files[fmt].path));+ assert.equal(createHash('sha256').update(bytes).digest('hex'),record.files[fmt].sha256);+ const result = await page.evaluate(async ({code,encoded,record,rate}) => {+ const { prepareAudioBuffer, createAudioSource } = await import(code);+ const data=Uint8Array.from(atob(encoded),c=>c.charCodeAt(0));+ const decoder=new OfflineAudioContext(2,1,rate);+ const decoded=await decoder.decodeAudioData(data.buffer);+ const buffer=prepareAudioBuffer(decoded,record.playback);+ const m=record.playback, scale=decoded.sampleRate/m.sample_rate;+ const first=Math.round(m.start_frame*scale), n=Math.round((m.end_frame-m.start_frame)*scale);+ const length=Math.round((m.end_frame-m.start_frame-m.overlap_frames)*scale), count=n-length;+ if (buffer.length!==length) throw new Error('wrong prepared duration');+ const context=new OfflineAudioContext(buffer.numberOfChannels,length*3,rate);+ const source=createAudioSource(context,decoded,record);+ source.connect(context.destination); source.start();+ const rendered=await context.startRendering();+ let wrap=0, join=0, renderError=0, controls=0;+ for (let c=0;c<buffer.numberOfChannels;c++) {+ const x=decoded.getChannelData(c), y=buffer.getChannelData(c), r=rendered.getChannelData(c);+ if (record.loop) {+ const baseline=x[first+count]-x[first+count-1];+ for(let lap=1;lap<3;lap++) {+ const at=lap*length;+ wrap=Math.max(wrap,Math.abs((r[at]-r[at-1])-baseline));+ if(Math.abs((r[at]+.125-r[at-1])-baseline)>.12) controls++;+ }+ const at=n-2*count;+ join=Math.max(join,Math.abs((y[at]-y[at-1])-(x[first+n-count]-x[first+n-count-1])));+ }+ for(let i=0;i<r.length;i++) {+ const expected=record.loop ? y[i%length] : (i<length ? y[i] : 0);+ renderError=Math.max(renderError,Math.abs(r[i]-expected));+ }+ }+ const interleaved=new Float32Array(rendered.length*rendered.numberOfChannels);+ for(let i=0;i<rendered.length;i++) for(let c=0;c<rendered.numberOfChannels;c++)+ interleaved[i*rendered.numberOfChannels+c]=rendered.getChannelData(c)[i];+ const u8=new Uint8Array(interleaved.buffer);+ let binary=''; for(let i=0;i<u8.length;i+=16384) binary+=String.fromCharCode(...u8.subarray(i,i+16384));+ return { decoded_frames:decoded.length, length, overlap:count, channels:rendered.numberOfChannels,+ added_wrap_error:wrap, added_join_error:join, render_error:renderError, controls_detected:controls,+ pcm:btoa(binary) };+ },{code:`data:text/javascript;base64,${shipped.toString('base64')}`,encoded:bytes.toString('base64'),record,rate});+ assert.equal(result.added_wrap_error,0);+ assert.equal(result.added_join_error,0);+ assert.ok(result.render_error<1e-7);+ if(record.loop) assert.equal(result.controls_detected,result.channels*2);+ const pcmName=`${item.name}-${name}-${rate}-${fmt}.f32`;+ await writeFile(resolve(outputDir,pcmName),Buffer.from(result.pcm,'base64'));+ delete result.pcm;+ reports.push({ name:item.name,browser:name,rate,fmt,target_lufs:record.target_lufs,pcm:pcmName,...result });+ await writeFile(resolve(outputDir,'render.json'),JSON.stringify({module_sha256:digest,reports},null,2));+ }+ }+ }+ } finally { await browser.close(); }+}+await writeFile(resolve(outputDir,'render.json'),JSON.stringify({module_sha256:digest,reports},null,2));+console.log(JSON.stringify({passed:reports.length,module_sha256:digest}));diff --git a/tests/audio/test_sound.py b/tests/audio/test_sound.pyindex 8096b20..07b7935 100644--- a/tests/audio/test_sound.py+++ b/tests/audio/test_sound.py@@ -89,15 +89,6 @@ def test_cache_serializes_paid_requests_and_survives_uncertain_results(tmp_path)cache.obtain(db, {'a':2}, lambda: pytest.fail('ambiguous POST resent'))-def test_loop_overlaps_ends_without_discontinuity():- t = np.arange(48000 * 4) / 48000- x = np.stack([.1*np.sin(2*np.pi*317.25*t) + .2*t/4]*2, axis=1)- y = processing.make_loop(x, .2)- assert len(y) == len(x) - 9600- assert np.max(np.abs(y[-1] - y[0])) < .01- assert np.max(np.abs(np.diff(y, axis=0))) < .01--def test_actual_exports_are_normalized_and_reused(tmp_path, monkeypatch):called = []monkeypatch.setattr(provider, 'generate', lambda req: (called.append(req) or wav_bytes(), {}))@@ -133,11 +124,11 @@ def test_short_effect_and_loop_encoded_playback(tmp_path, monkeypatch, kind, durresult = sound.generate(kind=kind, prompt='synthetic', duration=duration, event='sample',cache_dir=tmp_path/'cache', out_dir=tmp_path/'out')assert result['loop'] is loop- assert abs(result['duration_seconds'] - (duration-result['crossfade_seconds'])) < .001- if loop:- for file in result['files'].values():- decoded = processing.decode(tmp_path/'out'/file['path'])[:round(result['loop_end_seconds']*48000)]- assert np.max(np.abs(decoded[-1]-decoded[0])) < .025+ meta = result['playback']+ assert meta == {'version': 2, 'sample_rate': 48000, 'start_frame': 0,+ 'end_frame': round(duration*48000), 'overlap_frames': 12000 if loop else 0}+ assert result['duration_seconds'] == (meta['end_frame']-meta['overlap_frames'])/48000+ assert 'loop_end_seconds' not in result and 'codec_taper_seconds' not in resultdef test_preflight_does_not_claim_and_cached_source_works_without_key(tmp_path, monkeypatch):@@ -205,18 +196,31 @@ def test_codec_reconstruction_keeps_a_broadband_transient_below_peak_ceiling(tmpassert abs(file['lufs']+18) <= 1-def test_loop_codec_boundary_at_nonzero_stereo_phase(tmp_path, monkeypatch):+def test_export_keeps_source_frames_until_browser_preparation(tmp_path, monkeypatch):t = np.arange(48000*3)/48000- x = np.stack([.1*np.sin(2*np.pi*121*t+.5), .4*np.cos(2*np.pi*73*t)], axis=1)- stream = io.BytesIO()- with wave.open(stream, 'wb') as f:- f.setnchannels(2); f.setsampwidth(2); f.setframerate(48000)- f.writeframes((x*32767).astype('<i2').tobytes())- monkeypatch.setattr(provider, 'generate', lambda _: (stream.getvalue(), {}))- result = sound.generate(kind='bgm', prompt='synthetic stereo loop', duration=3, event='music',- out_dir=tmp_path/'out', cache_dir=tmp_path/'cache')- for file in result['files'].values():- decoded = processing.decode(tmp_path/'out'/file['path'])[:round(result['duration_seconds']*48000)]- # Codec priming must not turn a large nonzero start into a wrap impulse.- assert np.max(np.abs(decoded[-1]-decoded[0])) < .003- assert np.max(np.abs(decoded[[0,-1]])) < .003+ x = np.stack([.1*np.sin(2*np.pi*121*t+.5), .4*np.cos(2*np.pi*73*t)], axis=1).astype('<f4')+ monkeypatch.setattr(processing, 'decode', lambda path: x.copy())+ seen = []+ def normalize(samples, target, loop):+ seen.append(samples.copy())+ return samples, 0+ monkeypatch.setattr(processing, 'normalize', normalize)+ monkeypatch.setattr(processing, 'measure', lambda *a: {'input_i': -20, 'input_tp': -2})+ processing.export(tmp_path/'source', tmp_path, target_lufs=-20, loop=True, crossfade=.25)+ np.testing.assert_array_equal(seen[0], x)+++def test_old_processed_output_is_rejected_but_raw_cache_survives(tmp_path, monkeypatch):+ 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}}+ (kw['out_dir']/'sound.json').write_text(json.dumps(old))+ with pytest.raises(ValueError, match='different sound'):+ sound.generate(**kw)+ monkeypatch.delenv('ELEVENLABS_API_KEY')+ fresh = sound.generate(**{**kw, 'out_dir': tmp_path/'new'})+ assert fresh['cache_hit'] and len(calls) == 1diff --git a/tests/compose/test_game_pack_audio.py b/tests/compose/test_game_pack_audio.pyindex 460a15c..2c072a4 100644--- a/tests/compose/test_game_pack_audio.py+++ b/tests/compose/test_game_pack_audio.py@@ -20,9 +20,10 @@ def inputs(tmp_path):(source/f'sound.{fmt}').write_bytes(data)files[fmt] = {'path':f'sound.{fmt}', 'duration_seconds':1, 'lufs':-18,'true_peak_dbfs':-2, 'sha256':hashlib.sha256(data).hexdigest()}- manifest = {'kind':'sprite-gen-sound','version':1,'event':'combat.hit', 'audio_kind':'sfx',+ manifest = {'kind':'sprite-gen-sound','version':2,'event':'combat.hit', 'audio_kind':'sfx','loop':False,'duration_seconds':1,'target_lufs':-18,'files':files,- 'loop_start_seconds':0, 'loop_end_seconds':1}+ 'playback': {'version':2, 'sample_rate':48000, 'start_frame':0,+ 'end_frame':48000, 'overlap_frames':0}}(source/'sound.json').write_text(json.dumps(manifest))path = tmp_path/'request.json'path.write_text(json.dumps(request))@@ -30,7 +31,7 @@ def inputs(tmp_path):def test_optional_audio_preserves_image_only_bytes_and_packs_events(tmp_path):- path, request, _ = inputs(tmp_path)+ path, request, original = inputs(tmp_path)pack(path, tmp_path/'before')request['audio'] = []path.write_text(json.dumps(request))@@ -44,6 +45,11 @@ def test_optional_audio_preserves_image_only_bytes_and_packs_events(tmp_path):assert audio['event'] == result['animations']['hero']['events'][0]['name']assert (tmp_path/'with'/audio['files']['ogg']['path']).read_bytes() == b'synthetic-ogg'assert audio['files']['m4a']['path'] == 'audio-hit.m4a'+ assert audio['playback'] == original['playback']+ helper = audio['player']+ assert helper['path'] == 'sprite-gen-audio-v2.mjs'+ assert helper['export'] == 'prepareAudioBuffer'+ assert hashlib.sha256((tmp_path/'with'/helper['path']).read_bytes()).hexdigest() == helper['sha256']assert pack(path, tmp_path/'with') == resulttotal = sum(p.stat().st_size for p in (tmp_path/'with').iterdir())with pytest.raises(ValueError, match='bytes'):@@ -51,7 +57,7 @@ def test_optional_audio_preserves_image_only_bytes_and_packs_events(tmp_path):assert not (tmp_path/'small').exists()[email protected]('defect', ['duplicate', 'traversal', 'digest', 'nonfinite', 'badloop'])[email protected]('defect', ['duplicate', 'traversal', 'digest', 'nonfinite', 'badloop', 'old', 'fraction', 'overlap'])def test_broken_audio_cannot_publish_a_partial_pack(tmp_path, defect):path, request, manifest = inputs(tmp_path)request['audio'] = [{'id':'hit','source':'audio/sound.json'}]@@ -60,6 +66,9 @@ def test_broken_audio_cannot_publish_a_partial_pack(tmp_path, defect):if defect == 'digest': manifest['files']['ogg']['sha256'] = '0'*64if defect == 'nonfinite': manifest['target_lufs'] = float('nan')if defect == 'badloop': manifest['loop'] = 'false'+ if defect == 'old': manifest['version'] = 1+ if defect == 'fraction': manifest['playback']['end_frame'] = 48000.5+ if defect == 'overlap': manifest['playback']['overlap_frames'] = 24000path.write_text(json.dumps(request))(tmp_path/'audio'/'sound.json').write_text(json.dumps(manifest))with pytest.raises(ValueError): pack(path, tmp_path/'bad')diff --git a/tests/packaging/test_curation_asset_packaging.py b/tests/packaging/test_curation_asset_packaging.pyindex 1bfcdf8..9606c8c 100644--- a/tests/packaging/test_curation_asset_packaging.py+++ b/tests/packaging/test_curation_asset_packaging.py@@ -209,3 +209,7 @@ def test_the_package_never_reaches_into_the_scripts_wrappers(module: str) -> Nonf"{module} builds a path into scripts/ ({offenders}) — those wrappers are not "f"installed, so this works in a checkout and fails in an install. Shell out with "f"`-m sprite_gen.<module>` instead.")+++def test_built_wheel_carries_browser_audio_player(wheel_names: set[str]) -> None:+ assert 'sprite_gen/audio/sprite-gen-audio-v2.mjs' in wheel_names