diff --git a/public/assets/js/codec2-emscripten/codec2-microphone-recorder.js b/public/assets/js/codec2-emscripten/codec2-microphone-recorder.js new file mode 100644 index 0000000..c1f6f86 --- /dev/null +++ b/public/assets/js/codec2-emscripten/codec2-microphone-recorder.js @@ -0,0 +1,102 @@ +/** + * A simple class for recording microphone input and returning the audio encoded in codec2 + */ +class Codec2MicrophoneRecorder { + + constructor() { + + this.sampleRate = 8000; + this.codec2Mode = "1200"; + this.audioChunks = []; + + this.audioContext = null; + this.audioWorkletNode = null; + this.microphoneMediaStream = null; + this.mediaStreamSource = null; + + } + + async start() { + try { + + // load audio worklet module + this.audioContext = new AudioContext({ sampleRate: this.sampleRate }); + await this.audioContext.audioWorklet.addModule('assets/js/codec2-emscripten/processor.js'); + this.audioWorkletNode = new AudioWorkletNode(this.audioContext, 'audio-processor'); + + // 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)); + + }; + + // request access to the microphone + this.microphoneMediaStream = await navigator.mediaDevices.getUserMedia({ + audio: true, + }); + + // send mic audio to audio worklet + this.mediaStreamSource = this.audioContext.createMediaStreamSource(this.microphoneMediaStream); + this.mediaStreamSource.connect(this.audioWorkletNode); + + // successfully started recording + return true; + + } catch(e) { + console.log(e); + return false; + } + } + + stop() { + + // disconnect media stream source + if(this.mediaStreamSource){ + this.mediaStreamSource.disconnect(); + } + + // stop using microphone + if(this.microphoneMediaStream){ + this.microphoneMediaStream.getTracks().forEach(track => track.stop()); + } + + // disconnect the audio worklet node + if(this.audioWorkletNode){ + this.audioWorkletNode.disconnect(); + } + + // close audio context + if(this.audioContext && this.audioContext.state !== "closed"){ + this.audioContext.close(); + } + + // calculate total length + let totalLength = 0; + for(const chunk of this.audioChunks){ + totalLength += chunk.length; + } + + // create a new Uint8Array with the total length + const concatenatedArray = new Uint8Array(totalLength); + + // copy each chunk into the new array + let offset = 0; + for(const chunk of this.audioChunks){ + concatenatedArray.set(chunk, offset); + offset += chunk.length; + } + + return concatenatedArray; + + } + +} diff --git a/public/assets/js/codec2-emscripten/wav-encoder.js b/public/assets/js/codec2-emscripten/wav-encoder.js new file mode 100644 index 0000000..9932b6a --- /dev/null +++ b/public/assets/js/codec2-emscripten/wav-encoder.js @@ -0,0 +1,47 @@ +class WavEncoder { + + static encodeWAV(samples, sampleRate = 8000, numChannels = 1) { + + const buffer = new ArrayBuffer(44 + samples.length * 2); + const view = new DataView(buffer); + + // RIFF chunk descriptor + this.writeString(view, 0, 'RIFF'); + view.setUint32(4, 36 + samples.length * 2, true); // file length + this.writeString(view, 8, 'WAVE'); + + // fmt sub-chunk + this.writeString(view, 12, 'fmt '); + view.setUint32(16, 16, true); // sub-chunk size + view.setUint16(20, 1, true); // audio format (1 = PCM) + view.setUint16(22, numChannels, true); // number of channels + view.setUint32(24, sampleRate, true); // sample rate + view.setUint32(28, sampleRate * numChannels * 2, true); // byte rate + view.setUint16(32, numChannels * 2, true); // block align + view.setUint16(34, 16, true); // bits per sample + + // data sub-chunk + this.writeString(view, 36, 'data'); + view.setUint32(40, samples.length * 2, true); // data chunk length + + // write the PCM samples + this.floatTo16BitPCM(view, 44, samples); + + return buffer; + + } + + static writeString(view, offset, string) { + for(let i = 0; i < string.length; i++){ + view.setUint8(offset + i, string.charCodeAt(i)); + } + } + + static floatTo16BitPCM(output, offset, input) { + for(let i = 0; i < input.length; i++, offset += 2){ + const s = Math.max(-1, Math.min(1, input[i])); + output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true); + } + } + +} diff --git a/public/index.html b/public/index.html index ef5a53a..278e84f 100644 --- a/public/index.html +++ b/public/index.html @@ -17,6 +17,8 @@ + + @@ -661,6 +663,23 @@ + +
+
+
+
+ Audio Recording + {{ formatBytes(newMessageAudio.audio_blob.size) }} +
+
+ + + +
+
+
+
+
@@ -701,6 +720,24 @@ Add Image + +
+ + +
+