I am combining two AudioBuffer
s to form a single one.
I believe this can be done without using the MediaRecorder
, as real-time recording is not required.
This is my current approach:
concatenateAudio(buffers: AudioBuffer[]): void {
const totalLength = buffers.reduce((acc, buffer) => acc + buffer.length, 0);
const audioContext = new AudioContext();
const audioBuffer = audioContext.createBuffer(1, totalLength, 48000);
const source = audioContext.createBufferSource();
const destination = audioContext.createMediaStreamDestination();
let offset = 0;
buffers.map((buffer) => {
audioBuffer.getChannelData(0).set(buffer.getChannelData(0), offset);
offset += buffer.length;
});
source.buffer = audioBuffer;
source.connect(destination);
source.start();
this.saveRecordedAudio(destination.stream, audioBuffer.duration);
}
In the saveRecordedAudio()
function, I pass the stream to a MediaRecorder
.
The time taken to record the new stream is equivalent to the duration of playing the two AudioBuffer
s sequentially...
Is there an alternative method for accomplishing this?
Appreciate any suggestions. Thank you.