TeaWeb/js/codec/Codec.ts

51 lines
1.3 KiB
TypeScript
Raw Normal View History

2018-04-11 15:56:09 +00:00
interface CodecCostructor {
new (codecSampleRate: number) : Codec;
}
2018-03-24 22:38:01 +00:00
2018-03-07 18:06:52 +00:00
class BufferChunk {
buffer: AudioBuffer;
2018-03-02 19:39:46 +00:00
index: number;
2018-03-07 18:06:52 +00:00
constructor(buffer: AudioBuffer) {
2018-03-02 19:39:46 +00:00
this.buffer = buffer;
this.index = 0;
}
2018-03-07 18:06:52 +00:00
copyRangeTo(target: AudioBuffer, maxLength: number, offset: number) {
let copy = Math.min(this.buffer.length - this.index, maxLength);
for(let channel = 0; channel < this.buffer.numberOfChannels; channel++) {
target.getChannelData(channel).set(
this.buffer.getChannelData(channel).subarray(this.index, this.index + copy),
offset
);
}
return copy;
}
2018-03-02 19:39:46 +00:00
}
2018-04-11 15:56:09 +00:00
class CodecClientCache {
_chunks: BufferChunk[] = [];
2018-03-02 19:39:46 +00:00
2018-04-11 15:56:09 +00:00
bufferedSamples(max: number = 0) : number {
2018-03-02 19:39:46 +00:00
let value = 0;
2018-03-07 18:06:52 +00:00
for(let i = 0; i < this._chunks.length && value < max; i++)
value += this._chunks[i].buffer.length - this._chunks[i].index;
2018-03-02 19:39:46 +00:00
return value;
}
2018-03-07 18:06:52 +00:00
}
2018-04-11 15:56:09 +00:00
interface Codec {
on_encoded_data: (Uint8Array) => void;
2018-02-27 16:20:49 +00:00
2018-04-11 15:56:09 +00:00
channelCount: number;
samplesPerUnit: number;
2018-03-07 18:06:52 +00:00
2018-04-11 15:56:09 +00:00
name() : string;
initialise();
deinitialise();
2018-02-27 16:20:49 +00:00
2018-04-11 15:56:09 +00:00
decodeSamples(cache: CodecClientCache, data: Uint8Array) : Promise<AudioBuffer>;
encodeSamples(cache: CodecClientCache, pcm: AudioBuffer);
2018-04-11 15:56:09 +00:00
reset() : boolean;
2018-03-10 08:03:29 +00:00
}