|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { spawnSync } from 'node:child_process'; |
| 3 | +import { test } from 'node:test'; |
| 4 | +import { buildTimelineAudioMixFilter } from './timeline-audio-filter'; |
| 5 | + |
| 6 | +const ffmpeg = process.env.BEATDESIGN_FFMPEG?.trim() || 'ffmpeg'; |
| 7 | +const available = spawnSync(ffmpeg,['-version'],{stdio:'ignore'}).status === 0; |
| 8 | +function render(count:number, overlap=false) { |
| 9 | + const args=['-hide_banner','-loglevel','error']; |
| 10 | + for(let i=0;i<count;i++)args.push('-f','lavfi','-i','sine=frequency=440:sample_rate=48000:duration=1'); |
| 11 | + const filters=Array.from({length:count},(_,i)=>`[${i}:a]volume=${overlap?10:1},adelay=${overlap?0:i*1000}:all=1[a${i}]`); |
| 12 | + filters.push(Array.from({length:count},(_,i)=>`[a${i}]`).join('')+buildTimelineAudioMixFilter(count,overlap?1:count)+'[out]'); |
| 13 | + args.push('-filter_complex',filters.join(';'),'-map','[out]','-ac','1','-c:a','pcm_f32le','-f','f32le','pipe:1'); |
| 14 | + const result=spawnSync(ffmpeg,args,{maxBuffer:4*1024*1024}); |
| 15 | + assert.equal(result.status,0,result.stderr?.toString()); |
| 16 | + const samples=new Float32Array(result.stdout.buffer.slice(result.stdout.byteOffset,result.stdout.byteOffset+result.stdout.byteLength)); |
| 17 | + return samples; |
| 18 | +} |
| 19 | +function rms(samples:Float32Array,start:number,end:number) { |
| 20 | + let sum=0;for(let i=start;i<end;i++)sum+=samples[i]**2; |
| 21 | + return Math.sqrt(sum/(end-start)); |
| 22 | +} |
| 23 | + |
| 24 | +test('sequential delayed clips retain unity gain as later inputs are added',{skip:!available},()=>{ |
| 25 | + const one=render(1),three=render(3),expected=rms(one,4800,43200); |
| 26 | + assert.ok(expected>0.08); |
| 27 | + for(let i=0;i<3;i++)assert.ok(Math.abs(rms(three,48000*i+4800,48000*i+43200)-expected)<0.0001); |
| 28 | + assert.equal(three.length,3*48000); |
| 29 | +}); |
| 30 | + |
| 31 | +test('overlapping loud clips are limited without automatic makeup or timeline delay',{skip:!available},()=>{ |
| 32 | + const samples=render(2,true); |
| 33 | + let peak=0;for(const sample of samples)peak=Math.max(peak,Math.abs(sample)); |
| 34 | + assert.ok(peak<=0.951 && peak>0.90,`Peak was ${peak}`); |
| 35 | + assert.equal(samples.length,48000); |
| 36 | +}); |
0 commit comments