Michel
Michel

Reputation: 11749

Converting data audio to mp3 (or ogg)

I want to convert data audio that I have as a Blob coming from my microphone, to another (less space taking) format.

And I found this project on GitHub, it does something quite close what I want, but the problem is that I get my audio data through:

navigator.mediaDevices.getUserMedia
then
rec = new MediaRecorder(stream);
rec.ondataavailable = e => {
    .......
    audioChunks.push(e.data);
    if (rec.state == "inactive") {
        let blob = new Blob(audioChunks,{type:'audio/x-mpeg-3'});
        .....

while the project I found handles data audio in a different way:

navigator.getUserMedia({audio: true}, function(stream) {
      callback(new RecorderObject(audio_context.createMediaStreamSource(stream), cfg));
}, function(e) {
      console.log("An error occurred"); //Null if something goes wrong
      callback(null);
});

As a result it seems complicated to use what I see in the GitHub project for my own one. I would be glad if someone mastering the subject an possibly knowing the project I am referring to could tell me if there is some way I can adapt what is done it the project to fit my own needs.

Upvotes: 0

Views: 4014

Answers (1)

Brad
Brad

Reputation: 163468

The first example is already doing what you want. The MediaRecorder includes codec support.

The line:

let blob = new Blob(audioChunks, {type: 'audio/x-mpeg-3'});

should be:

let blob = new Blob(audioChunks, {type: rec.mimeType});

https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/mimeType

Also note that for older browsers, the mimeType property may not be present. For those, simply initialize the MediaRecorder with the same type.

new MediaRecorder(stream, {mimeType: 'audio/webm'})

https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/MediaRecorder

The second example is for recording PCM data with the Web Audio API... a technique that is no longer necessary, particularly that the MediaRecorder supports PCM in WebM now.

Upvotes: 2

Related Questions