20 lines
1.2 KiB
JavaScript
20 lines
1.2 KiB
JavaScript
import { MusicError } from '../src/core/controller.mjs';
|
|
// Strict local-fixture decoder only. No file/network fetch or third-party codec.
|
|
export function* pcmFixtureFrames(input) {
|
|
const data=Buffer.from(input);
|
|
if(data.length<44 || data.length>9000000 || data.toString('ascii',0,4)!=='RIFF' || data.toString('ascii',8,12)!=='WAVE' || data.readUInt32LE(4)+8!==data.length)throw new MusicError('unsupported');
|
|
let format=false,pcm;
|
|
for(let offset=12;offset+8<=data.length;){
|
|
const kind=data.toString('ascii',offset,offset+4),length=data.readUInt32LE(offset+4);offset+=8;
|
|
if(offset+length>data.length)throw new MusicError('unsupported');
|
|
if(kind==='fmt '){
|
|
if(length<16 || data.readUInt16LE(offset)!==1 || data.readUInt16LE(offset+2)!==2 || data.readUInt32LE(offset+4)!==24000 || data.readUInt16LE(offset+12)!==4 || data.readUInt16LE(offset+14)!==16)throw new MusicError('unsupported');
|
|
format=true;
|
|
}
|
|
if(kind==='data'){if(pcm)throw new MusicError('unsupported');pcm=data.subarray(offset,offset+length);}
|
|
offset+=length+(length%2);
|
|
}
|
|
if(!format || !pcm?.length || pcm.length%1920!==0)throw new MusicError('unsupported');
|
|
for(let offset=0;offset<pcm.length;offset+=1920)yield Uint8Array.from(pcm.subarray(offset,offset+1920));
|
|
}
|