You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
7.1KB

  1. /*
  2. TODO
  3. u-law encoding
  4. browser feature check (offlineaudiocontext might still be a bit niche)
  5. */
  6. var audioFileChooser = document.getElementById('audioFileChooser');
  7. audioFileChooser.addEventListener('change', readFile);
  8. function readFile() {
  9. for(var i = 0; i < audioFileChooser.files.length; i++) {
  10. var fileReader = new FileReader();
  11. var sampleRateChoice = document.getElementById('sampleRate').value;
  12. if(sampleRateChoice == "auto") sampleRateChoice = null;
  13. else sampleRateChoice = parseInt(sampleRateChoice);
  14. fileReader.readAsArrayBuffer(audioFileChooser.files[i]);
  15. fileReader.addEventListener('load', function(fileName, ev) {
  16. processFile(ev.target.result, fileName, sampleRateChoice);
  17. }.bind(null, audioFileChooser.files[i].name));
  18. }
  19. }
  20. function processFile(file, fileName, sampleRateChoice) {
  21. // determine sample rate
  22. // ideas borrowed from https://github.com/ffdead/wav.js
  23. var sampleRate = 0;
  24. if(!sampleRateChoice) {
  25. try {
  26. var sampleRateBytes = new Uint8Array(file, 24, 4);
  27. for(var i = 0; i < sampleRateBytes.length; i ++) {
  28. sampleRate |= sampleRateBytes[i] << (i*8);
  29. }
  30. } catch(err) {
  31. console.log('problem reading sample rate');
  32. }
  33. if([44100, 22050, 11025].indexOf(sampleRate) == -1) {
  34. sampleRate = 44100;
  35. }
  36. } else {
  37. sampleRate = sampleRateChoice;
  38. }
  39. var context = new OfflineAudioContext(1, 100*sampleRate, sampleRate); // arbitrary 100 seconds max length for now, nothing that long would fit on a Teensy anyway
  40. context.decodeAudioData(file, function(buffer) {
  41. var monoData = [];
  42. if(buffer.numberOfChannels == 1) {
  43. monoData = buffer.getChannelData(0);
  44. } else if(buffer.numberOfChannels == 2) {
  45. var leftData = buffer.getChannelData(0);
  46. var rightData = buffer.getChannelData(1);
  47. for(var i=0;i<buffer.length;i++) {
  48. monoData[i] = (leftData[i] + rightData[i]) / 2;
  49. }
  50. } else {
  51. alert("ONLY MONO AND STEREO FILES ARE SUPPORTED");
  52. // NB - would be trivial to add support for n channels, given that everything ends up mono anyway
  53. }
  54. var padLength;
  55. var compressionCode = '0';
  56. var sampleRateCode;
  57. if(true) compressionCode = '8'; // add u-law support here later
  58. if(sampleRate == 44100) {
  59. padLength = padding(monoData.length, 128);
  60. sampleRateCode = '1';
  61. } else if(sampleRate == 22050) {
  62. padLength = padding(monoData.length, 64);
  63. sampleRateCode = '2';
  64. } else if(sampleRate == 11025) {
  65. padLength = padding(monoData.length, 32);
  66. sampleRateCode = '3';
  67. }
  68. var ulawOut = [];
  69. for(var i = 0; i < monoData.length; i ++) {
  70. ulawOut.push(ulaw_encode(toInteger(monoData[i]*0x7fff)).toString(16));
  71. }
  72. window.ulawOut = ulawOut;
  73. var outputData = createWords(monoData, padLength);
  74. var statusInt = (monoData.length).toString(16);
  75. while(statusInt.length < 6) statusInt = '0' + statusInt;
  76. if(monoData.length>0xFFFFFF) alert("DATA TOO LONG");
  77. statusInt = '0x' + compressionCode + sampleRateCode + statusInt;
  78. outputData.unshift(statusInt);
  79. var outputFileHolder = document.getElementById('outputFileHolder');
  80. var downloadLink1 = document.createElement('a');
  81. var downloadLink2 = document.createElement('a');
  82. var formattedName = fileName.split('.')[0];
  83. formattedName = formattedName.charAt(0).toUpperCase() + formattedName.slice(1).toLowerCase();
  84. downloadLink1.href = generateOutputFile(generateCPPFile(fileName, formattedName, outputData, sampleRate));
  85. downloadLink1.setAttribute('download', 'AudioSample' + formattedName + '.cpp');
  86. downloadLink1.innerHTML = 'Download AudioSample' + formattedName + '.cpp';
  87. downloadLink2.href = generateOutputFile(generateHeaderFile(formattedName, outputData));
  88. downloadLink2.setAttribute('download', 'AudioSample' + formattedName + '.h');
  89. downloadLink2.innerHTML = 'Download AudioSample' + formattedName + '.h';
  90. outputFileHolder.appendChild(downloadLink1);
  91. outputFileHolder.appendChild(document.createElement('br'));
  92. outputFileHolder.appendChild(downloadLink2);
  93. outputFileHolder.appendChild(document.createElement('br'));
  94. });
  95. }
  96. function ulaw_encode(audio)
  97. {
  98. var mag, neg; // both uint32
  99. // http://en.wikipedia.org/wiki/G.711
  100. if (audio >= 0) {
  101. mag = audio;
  102. neg = 0;
  103. } else {
  104. mag = audio * -1;
  105. neg = 0x80;
  106. }
  107. mag += 128;
  108. if (mag > 0x7FFF) mag = 0x7FFF;
  109. if (mag >= 0x4000) return neg | 0x70 | ((mag >> 10) & 0x0F); // 01wx yz00 0000 0000
  110. if (mag >= 0x2000) return neg | 0x60 | ((mag >> 9) & 0x0F); // 001w xyz0 0000 0000
  111. if (mag >= 0x1000) return neg | 0x50 | ((mag >> 8) & 0x0F); // 0001 wxyz 0000 0000
  112. if (mag >= 0x0800) return neg | 0x40 | ((mag >> 7) & 0x0F); // 0000 1wxy z000 0000
  113. if (mag >= 0x0400) return neg | 0x30 | ((mag >> 6) & 0x0F); // 0000 01wx yz00 0000
  114. if (mag >= 0x0200) return neg | 0x20 | ((mag >> 5) & 0x0F); // 0000 001w xyz0 0000
  115. if (mag >= 0x0100) return neg | 0x10 | ((mag >> 4) & 0x0F); // 0000 0001 wxyz 0000
  116. return neg | 0x00 | ((mag >> 3) & 0x0F); // 0000 0000 1wxy z000
  117. }
  118. function createWords(audioData, padLength) {
  119. var totalLength = audioData.length + padLength;
  120. var outputData = [];
  121. for(var i = 0; i < totalLength; i += 2) {
  122. var a = toUint16(i<audioData.length?audioData[i]*0x7fff:0x0000);
  123. var b = toUint16(i+1<audioData.length?audioData[i+1]*0x7fff:0x0000);
  124. var out = (65536*b + a).toString(16);
  125. while(out.length < 8) out = '0' + out;
  126. out = '0x' + out;
  127. outputData.push(out);
  128. }
  129. return outputData;
  130. }
  131. // http://2ality.com/2012/02/js-integers.html
  132. function toInteger(x) {
  133. x = Number(x);
  134. return Math.round(x);
  135. //return x < 0 ? Math.ceil(x) : Math.floor(x);
  136. }
  137. function modulo(a, b) {
  138. return a - Math.floor(a/b)*b;
  139. }
  140. function toUint16(x) {
  141. return modulo(toInteger(x), Math.pow(2, 16));
  142. }
  143. function toUint8(x) {
  144. return modulo(toInteger(x), Math.pow(2, 8));
  145. }
  146. // compute the extra padding needed
  147. function padding(sampleLength, block) {
  148. var extra = sampleLength % block;
  149. if (extra == 0) return 0;
  150. return block - extra;
  151. }
  152. function generateOutputFile(fileContents) {
  153. var textFileURL = null;
  154. var blob = new Blob([fileContents], {type: 'text/plain'});
  155. textFileURL = window.URL.createObjectURL(blob);
  156. return textFileURL;
  157. }
  158. function formatAudioData(audioData) {
  159. var outputString = '';
  160. for(var i = 0; i < audioData.length; i ++) {
  161. if(i%8==0 && i>0) outputString += '\n';
  162. outputString += audioData[i] + ',';
  163. }
  164. return outputString;
  165. }
  166. function generateCPPFile(fileName, formattedName, audioData, sampleRate, encodingType) {
  167. var out = "";
  168. out += '// Audio data converted from audio file by wav2sketch_js\n\n';
  169. out += '#include "AudioSample' + formattedName + '.h"\n\n';
  170. out += '// Converted from ' + fileName + ', using ' + sampleRate + ' Hz, 16 bit PCM encoding\n';
  171. out += 'const unsigned int AudioSample' + formattedName + '[' + audioData.length + '] = {\n';
  172. out += formatAudioData(audioData) + '\n};';
  173. return out;
  174. }
  175. function generateHeaderFile(formattedName, audioData) {
  176. var out = "";
  177. out += '// Audio data converted from audio file by wav2sketch_js\n\n';
  178. out += 'extern const unsigned int AudioSample' + formattedName + '[' + audioData.length + '];';
  179. return out;
  180. }