significantly improve codec2 voice recordings by doing a single encode when the recording is stopped

This commit is contained in:
liamcottle 2024-06-05 12:36:53 +12:00
commit dfa525cbf2
2 changed files with 15 additions and 25 deletions

View file

@ -26,17 +26,7 @@ class Codec2MicrophoneRecorder {
// handle audio received from audio worklet
this.audioWorkletNode.port.onmessage = async (event) => {
// convert audio received from worklet processor to wav
const buffer = WavEncoder.encodeWAV(event.data, this.sampleRate);
// convert wav audio to codec2
const rawBuffer = await Codec2Lib.audioFileToRaw(buffer, "audio.wav");
const encoded = await Codec2Lib.runEncode(this.codec2Mode, rawBuffer);
// pass encoded audio to callback
this.audioChunks.push(new Uint8Array(encoded.buffer));
this.audioChunks.push(event.data);
};
// request access to the microphone
@ -57,7 +47,7 @@ class Codec2MicrophoneRecorder {
}
}
stop() {
async stop() {
// disconnect media stream source
if(this.mediaStreamSource){
@ -79,23 +69,23 @@ class Codec2MicrophoneRecorder {
this.audioContext.close();
}
// calculate total length
let totalLength = 0;
// concatenate all audio chunks into a single array
var fullAudio = [];
for(const chunk of this.audioChunks){
totalLength += chunk.length;
fullAudio = [
...fullAudio,
...chunk,
]
}
// create a new Uint8Array with the total length
const concatenatedArray = new Uint8Array(totalLength);
// convert audio to wav
const buffer = WavEncoder.encodeWAV(fullAudio, this.sampleRate);
// copy each chunk into the new array
let offset = 0;
for(const chunk of this.audioChunks){
concatenatedArray.set(chunk, offset);
offset += chunk.length;
}
// convert wav audio to codec2
const rawBuffer = await Codec2Lib.audioFileToRaw(buffer, "audio.wav");
const encoded = await Codec2Lib.runEncode(this.codec2Mode, rawBuffer);
return concatenatedArray;
return encoded;
}

View file

@ -2482,7 +2482,7 @@
// stop recording microphone and get audio
this.isRecordingAudioAttachment = false;
const audio = this.audioAttachmentMicrophoneRecorder.stop();
const audio = await this.audioAttachmentMicrophoneRecorder.stop();
// do nothing if no audio was provided
if(audio.length === 0){