Skip to content

Commit

Permalink
significantly improve codec2 voice recordings by doing a single encod…
Browse files Browse the repository at this point in the history
…e when the recording is stopped
  • Loading branch information
liamcottle committed Jun 5, 2024
1 parent 4303e83 commit dfa525c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
38 changes: 14 additions & 24 deletions public/assets/js/codec2-emscripten/codec2-microphone-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -57,7 +47,7 @@ class Codec2MicrophoneRecorder {
}
}

stop() {
async stop() {

// disconnect media stream source
if(this.mediaStreamSource){
Expand All @@ -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;

}

Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down

0 comments on commit dfa525c

Please sign in to comment.