use webcodecs api (breaking other things)

This commit is contained in:
Jakob Ketterl 2022-06-18 23:24:40 +02:00
parent a6f2b6b31a
commit a779d5d02a
2 changed files with 49 additions and 30 deletions

View File

@ -32,7 +32,7 @@ class ClientAudioChain(Chain):
super().__init__(workers) super().__init__(workers)
def _buildConverter(self): def _buildConverter(self):
return Converter(self.format, self.inputRate, self.clientRate) return Converter(self.format, self.inputRate, 12000)
def _updateConverter(self): def _updateConverter(self):
converter = self._buildConverter() converter = self._buildConverter()

View File

@ -21,9 +21,13 @@ function AudioEngine(maxBufferLength, audioReporter) {
me._start(); me._start();
} }
var onAudio = function(audioData) {
me.playbackAudio(audioData);
}
this.audioCodecs = { this.audioCodecs = {
"adpcm": new ImaAdpcmCodec(), "adpcm": new ImaAdpcmCodec(onAudio),
"opus": new OpusCodec(this.audioContext) "opus": new OpusCodec(onAudio)
}; };
this.compression = 'none'; this.compression = 'none';
@ -281,32 +285,28 @@ AudioEngine.prototype.getSampleRate = function() {
}; };
AudioEngine.prototype.processAudio = function(data, resampler) { AudioEngine.prototype.processAudio = function(data, resampler) {
var me = this;
if (!this.audioNode) return; if (!this.audioNode) return;
this.audioBytes.add(data.byteLength); this.audioBytes.add(data.byteLength);
var audioDataCallback = function(buffer) { if (this.compression !== "none") {
buffer = resampler.process(buffer); this.audioCodecs[this.compression].decodeAsync(new Uint8Array(data));
if (me.audioNode.port) { } else {
this.playbackAudio(new Int16Array(data));
}
}
AudioEngine.prototype.playbackAudio = function(audioData) {
//var buffer = this.resampler.process(audioData);
if (this.audioNode.port) {
// AudioWorklets supported // AudioWorklets supported
me.audioNode.port.postMessage(buffer); this.audioNode.port.postMessage(audioData);
} else { } else {
// silently drop excess samples // silently drop excess samples
if (this.getBuffersize() + buffer.length <= this.maxBufferSize) { if (this.getBuffersize() + buffer.length <= this.maxBufferSize) {
this.audioBuffers.push(buffer); this.audioBuffers.push(buffer);
} }
} }
} };
if (this.compression !== "none") {
//resampling & ADPCM
this.audioCodecs[this.compression].decodeAsync(new Uint8Array(data), audioDataCallback);
} else {
audioDataCallback(new Int16Array(data))
}
}
AudioEngine.prototype.pushAudio = function(data) { AudioEngine.prototype.pushAudio = function(data) {
this.processAudio(data, this.resampler); this.processAudio(data, this.resampler);
@ -330,8 +330,9 @@ AudioEngine.prototype.getBuffersize = function() {
return this.audioBuffers.map(function(b){ return b.length; }).reduce(function(a, b){ return a + b; }, 0); return this.audioBuffers.map(function(b){ return b.length; }).reduce(function(a, b){ return a + b; }, 0);
}; };
function ImaAdpcmCodec() { function ImaAdpcmCodec(callback) {
this.reset(); this.reset();
this.callback = callback;
} }
ImaAdpcmCodec.prototype.reset = function() { ImaAdpcmCodec.prototype.reset = function() {
@ -401,7 +402,7 @@ ImaAdpcmCodec.prototype.decodeAsync = function(data, callback) {
} }
} }
this.skip = index - data.length; this.skip = index - data.length;
callback(output.slice(0, oi)); this.callback(output.slice(0, oi));
}; };
ImaAdpcmCodec.prototype.decodeNibble = function(nibble) { ImaAdpcmCodec.prototype.decodeNibble = function(nibble) {
@ -422,12 +423,30 @@ ImaAdpcmCodec.prototype.decodeNibble = function(nibble) {
return this.predictor; return this.predictor;
}; };
function OpusCodec(context) { function OpusCodec(callback) {
this.context = context; this.decoder = new AudioDecoder({
output: function(audioData) {
var buffer = new Float32Array(audioData.numberOfFrames * audioData.numberOfChannels);
audioData.copyTo(buffer, {planeIndex: 0});
callback(buffer);
},
error: function(e) {
console.error(e);
}
});
this.decoder.configure({
codec: "opus",
sampleRate: 12000,
numberOfChannels: 1
});
} }
OpusCodec.prototype.decodeAsync = function(data, callback) { OpusCodec.prototype.decodeAsync = function(data) {
this.context.decodeAudioData(new ArrayBuffer(data)).then(callback); this.decoder.decode(new EncodedAudioChunk({
type: "key",
data: data,
timestamp: 0
}));
}; };
function Interpolator(factor) { function Interpolator(factor) {