Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 web audio - convert audio buffer into wav file

I have an audio buffer rendered using webkitOfflineAudioContext. Now, I wish to export it into a WAV file. How do I do it? I tried using recorder.js but couldn't figure out how to use it. Here's my code: http://jsfiddle.net/GBQV8/.

like image 679
pramodtech Avatar asked Mar 21 '14 13:03

pramodtech


1 Answers

Here's a gist that should help: https://gist.github.com/kevincennis/9754325.

I haven't actually tested this, so there might be a stupid typo or something, but the basic approach will work (I've done it before).

Essentially, you're going to use the web worker from Recorder.js directly so that you can process one big AudioBuffer all in one shot, rather than recording it incrementally in real-time.

I'll paste the code here too, just in case something happens to the gist...

// assuming a var named `buffer` exists and is an AudioBuffer instance


// start a new worker 
// we can't use Recorder directly, since it doesn't support what we're trying to do
var worker = new Worker('recorderWorker.js');

// initialize the new worker
worker.postMessage({
  command: 'init',
  config: {sampleRate: 44100}
});

// callback for `exportWAV`
worker.onmessage = function( e ) {
  var blob = e.data;
  // this is would be your WAV blob
};

// send the channel data from our buffer to the worker
worker.postMessage({
  command: 'record',
  buffer: [
    buffer.getChannelData(0), 
    buffer.getChannelData(1)
  ]
});

// ask the worker for a WAV
worker.postMessage({
  command: 'exportWAV',
  type: 'audio/wav'
});
like image 181
Kevin Ennis Avatar answered Oct 20 '22 22:10

Kevin Ennis