/* TODO multiple files stereo non-44100 sample rates */ var audioFileChooser = document.getElementById('audioFileChooser'); audioFileChooser.addEventListener('change', readFile); function readFile() { for(var i = 0; i < audioFileChooser.files.length; i++) { var fileReader = new FileReader(); fileReader.readAsArrayBuffer(audioFileChooser.files[i]); fileReader.addEventListener('load', function(fileName, ev) { processFile(ev.target.result, fileName); }.bind(null, audioFileChooser.files[i].name)); } } function processFile(file, fileName) { var context = new OfflineAudioContext(1,10*44100,44100); context.decodeAudioData(file, function(buffer) { var monoData = buffer.getChannelData(0); // start with mono, do stereo later var outputData = []; for(var i=0;i=0) uint = Math.round(f * 32767); else uint = Math.round((f + 1) * 32767 + 32768); return uint; } // compute the extra padding needed function padding(sampleLength, block) { var extra = sampleLength % block; if (extra == 0) return 0; return block - extra; } function generateOutputFile(fileContents) { var textFileURL = null; var blob = new Blob([fileContents], {type: 'text/plain'}); textFileURL = window.URL.createObjectURL(blob); return textFileURL; } function generateCPPFile(fileName, formattedName, audioData) { var formattedName = fileName.split('.')[0]; formattedName = formattedName.charAt(0).toUpperCase() + formattedName.slice(1).toLowerCase(); var out = ""; out += '// Audio data converted from audio file by wav2sketch_js\n\n'; out += '#include "AudioSample' + formattedName + '.h"\n\n'; out += 'const unsigned int AudioSample' + formattedName + '[' + audioData.length + '] = {'; out += audioData.join(',') + ',};'; return out; } function generateHeaderFile(formattedName, audioData) { var out = ""; out += '// Audio data converted from audio file by wav2sketch_js\n\n'; out += 'extern const unsigned int AudioSample' + formattedName + '[' + audioData.length + '];'; return out; }