Last active
February 17, 2024 14:05
-
-
Save kevincennis/9754325 to your computer and use it in GitHub Desktop.
Buffer to WAV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment