chore: archive recoverable gateway source baseline
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
export class Resampler {
|
||||
constructor(inputRate, outputRate) { this.step=inputRate/outputRate; this.samples=[]; this.position=0; }
|
||||
push(input) {
|
||||
this.samples.push(...input);
|
||||
const output=[];
|
||||
while(this.position+1<this.samples.length) {
|
||||
const i=Math.floor(this.position), f=this.position-i;
|
||||
output.push(this.samples[i]*(1-f)+this.samples[i+1]*f); this.position+=this.step;
|
||||
}
|
||||
const used=Math.min(Math.floor(this.position),this.samples.length);
|
||||
if(used){this.samples.splice(0,used);this.position-=used;}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
export function pcmFrame(direction,turn,seq,pcm) {
|
||||
const buffer=new ArrayBuffer(12+pcm.length*2), view=new DataView(buffer);
|
||||
view.setUint8(0,76);view.setUint8(1,86);view.setUint8(2,1);view.setUint8(3,direction);
|
||||
view.setUint32(4,turn);view.setUint32(8,seq);
|
||||
for(let i=0;i<pcm.length;i++)view.setInt16(12+i*2,pcm[i],true);
|
||||
return buffer;
|
||||
}
|
||||
export function readFrame(buffer) {
|
||||
const v=new DataView(buffer);
|
||||
if(v.byteLength<14 || v.getUint8(0)!==76 || v.getUint8(1)!==86 || v.getUint8(2)!==1 || v.getUint8(3)!==2 || (v.byteLength-12)%2)throw Error('Invalid audio header');
|
||||
const pcm=new Float32Array((v.byteLength-12)/2);
|
||||
for(let i=0;i<pcm.length;i++)pcm[i]=v.getInt16(12+2*i,true)/32768;
|
||||
return {turn:v.getUint32(4),seq:v.getUint32(8),pcm};
|
||||
}
|
||||
Reference in New Issue
Block a user