I am building an electron app that requires recording audio and saving it in the directory of my main.js. I got the recording as a blob and then turned it into an array buffer in order to send it to my backend.
In the startRecording function I use the mediarecorder to save the audio and then send it to main
const startRecording = async () => {
console.log('Recording started');
chunks = [];
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = event => {
chunks.push(event.data);
};
mediaRecorder.onstop = () => {
const blob = new Blob(chunks, { type: 'audio/wav' });
// Convert Blob to buffer
blob.arrayBuffer().then(buffer => {
// Sending audio data to the main process using the function exposed in the preload script
window.sendAudioToMain.send(buffer);
}).catch(error => {
console.error('Error converting Blob to buffer:', error);
});
const audioUrl = URL.createObjectURL(blob);
const audioElement = document.getElementById('audioElement');
audioElement.src = audioUrl;
};
mediaRecorder.start();
};
ipcMain.on('save-audio', async (event, buffer) => {
try {
// Convert the buffer back into a Blob
const audioBlob = new Blob([buffer], { type: 'audio/wav' });
// Print the MIME type of the blob
console.log('MIME Type:', audioBlob.type);
} catch (error) {
console.error('Error:', error);
}
});
In my code block I have the array buffer turned back into a blob, but what I would really like to know is how I can convert this into a wav file