2

I'm gathering audio from the browser using MediaRecorder and the audio file being produced has a codec that is incompatible with IBM Watson Speech to Text. The codec is "matroska" and I need it to be raw PCM codec.

How do I set the codec of an audio file that I'm gathering from the browser? Here's some of the code that I'm using:

var options = {
  audioBitsPerSecond : 32000
}
var mediaRecorder = new MediaRecorder(audioStream, options);
mediaRecorder.start();

var data = [];
mediaRecorder.ondataavailable = e => console.log(mediaRecorder.audioBitsPerSecond);
mediaRecorder.ondataavailable = e => e.data.size && data.push(e.data);
mediaRecorder.onstop = () => process(data);

function process(data) {
  var int16Array = [];
  const blob = new Blob(data, {
    type: 'audio/wav'
  })
asynts
  • 2,213
  • 2
  • 21
  • 35
The Windhover
  • 312
  • 3
  • 16

1 Answers1

0

You can set the codec by setting it in the second variable of the mimeType argument of the new MediaRecorder(). Thanks to Jaromanda X for pointing out my mistake with looking at the new blob type. Do this like so...

var options = {
    audioBitsPerSecond : 32000,
    mimeType: 'audio/webm;codecs=pcm',
}
var mediaRecorder = new MediaRecorder(audioStream,options);

Check out the MDN Docs on it: MDN Network: MediaRecorder

You can see if a mimetype is supported with isTypeSupported, MediaRecorder.isTypeSupported().

So, just what codecs are supported? I have looked extensively for this answer, but, the best I have found is another stackoverflow post: Where is a comprehensive list of supported media types when recording with the Media * API?

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
  • if the audio is recorded using a different mimetype, would creating a blob convert the audio to the specified mime type? how is `codec=opus` going to create PCM encoded format? – Jaromanda X Oct 02 '18 at 14:52
  • @JaromandaX Ah, you're right! Thank you, updated and credited. – HoldOffHunger Oct 02 '18 at 14:58
  • Please note that `'audio/webm;codecs=pcm'` is only supported in Chrome as far as I can see - definitely not in FireFox, Edge doesn't even support MediaRecord (so that's a moot point) ... not sure about other browsers – Jaromanda X Oct 02 '18 at 15:02
  • Also ...`audio/webm;codecs=pcm` is not RAW pcm ... it's PCM inside a webm audio container - and I can't see any documentation that audio/webm even allows a PCM codec!! – Jaromanda X Oct 02 '18 at 15:03
  • Thanks for your replies, but none of the solutions worked because they don't produce raw PCM. We found an alternate method of gathering audio from the browser that produces the correct audio file. – The Windhover Oct 03 '18 at 15:44
  • @TheWindhover Excellent! Sometimes these things cannot be solved unless you are there. By all means, would be interested in seeing what you did if you have an answer. – HoldOffHunger Oct 03 '18 at 15:45
  • 1
    @TheWindhover, Which alternate solution you found? – Jayna Tanawala Dec 07 '20 at 10:16