Browse Source

sample rate detection working

dds
Matt Bradshaw 7 years ago
parent
commit
fe54aab301
3 changed files with 28 additions and 27611 deletions
  1. +0
    -27590
      extras/wav2sketch/testjs/checker.js
  2. +5
    -12
      extras/wav2sketch/wav2sketch.html
  3. +23
    -9
      extras/wav2sketch/wav2sketch.js

+ 0
- 27590
extras/wav2sketch/testjs/checker.js
File diff suppressed because it is too large
View File


+ 5
- 12
extras/wav2sketch/wav2sketch.html View File

<h1>Teensy Audio Wav2Sketch Utility</h1> <h1>Teensy Audio Wav2Sketch Utility</h1>
<p>Upload an audio file to convert it into Teensy audio format.</p> <p>Upload an audio file to convert it into Teensy audio format.</p>
<form> <form>
<label>Input sample rate</label>
<select>
<option>Auto-detect</option>
<option>44100</option>
<option>22050</option>
<option>11025</option>
</select><br/><br/>
<label>Output sample rate</label> <label>Output sample rate</label>
<select>
<option>Use input sample rate</option>
<option>44100</option>
<option>22050</option>
<option>11025</option>
<select id="sampleRate">
<option value="auto">Auto</option>
<option value="44100">44100</option>
<option value="22050">22050</option>
<option value="11025">11025</option>
</select><br/><br/> </select><br/><br/>
<input id="audioFileChooser" name="audioFileChooser" type="file" accept="audio/wav" multiple> <input id="audioFileChooser" name="audioFileChooser" type="file" accept="audio/wav" multiple>
<div id="outputFileHolder"></div> <div id="outputFileHolder"></div>

+ 23
- 9
extras/wav2sketch/wav2sketch.js View File

function readFile() { function readFile() {
for(var i = 0; i < audioFileChooser.files.length; i++) { for(var i = 0; i < audioFileChooser.files.length; i++) {
var fileReader = new FileReader(); var fileReader = new FileReader();
var sampleRateChoice = document.getElementById('sampleRate').value;
if(sampleRateChoice == "auto") sampleRateChoice = null;
else sampleRateChoice = parseInt(sampleRateChoice);
fileReader.readAsArrayBuffer(audioFileChooser.files[i]); fileReader.readAsArrayBuffer(audioFileChooser.files[i]);
fileReader.addEventListener('load', function(fileName, ev) { fileReader.addEventListener('load', function(fileName, ev) {
processFile(ev.target.result, fileName);
processFile(ev.target.result, fileName, sampleRateChoice);
}.bind(null, audioFileChooser.files[i].name)); }.bind(null, audioFileChooser.files[i].name));
} }
} }


function processFile(file, fileName) {
function processFile(file, fileName, sampleRateChoice) {
// determine sample rate // determine sample rate
// ideas borrowed from https://github.com/ffdead/wav.js // ideas borrowed from https://github.com/ffdead/wav.js
var sampleRate = 0; var sampleRate = 0;
try {
var sampleRateBytes = new Uint8Array(file, 24, 4);
for(var i = 0; i < sampleRateBytes.length; i ++) {
sampleRate |= a[i] << (i*8);
if(!sampleRateChoice) {
try {
var sampleRateBytes = new Uint8Array(file, 24, 4);
for(var i = 0; i < sampleRateBytes.length; i ++) {
sampleRate |= sampleRateBytes[i] << (i*8);
}
} catch(err) {
console.log('problem reading sample rate');
} }
if([44100, 22050, 11025].indexOf(sampleRate) == -1) { if([44100, 22050, 11025].indexOf(sampleRate) == -1) {
sampleRate = 44100; sampleRate = 44100;
} }
} catch(err) {
sampleRate = 44100;
} else {
console.log('using chosen sample rate of ' + sampleRateChoice);
sampleRate = sampleRateChoice;
} }


var context = new OfflineAudioContext(1, 100*sampleRate, sampleRate); // 100 seconds for now var context = new OfflineAudioContext(1, 100*sampleRate, sampleRate); // 100 seconds for now
} }
} else { } else {
alert("ONLY MONO AND STEREO FILES ARE SUPPORTED"); alert("ONLY MONO AND STEREO FILES ARE SUPPORTED");
// NB - would be trivial to add support for n channels, given that everything ends up mono anyway
} }
var outputData = []; var outputData = [];
for(var i=0;i<monoData.length;i+=2) { for(var i=0;i<monoData.length;i+=2) {
var statusInt = (outputData.length*2).toString(16); var statusInt = (outputData.length*2).toString(16);
while(statusInt.length < 6) statusInt = '0' + statusInt; while(statusInt.length < 6) statusInt = '0' + statusInt;
if(outputData.length*2>0xFFFFFF) alert("DATA TOO LONG"); if(outputData.length*2>0xFFFFFF) alert("DATA TOO LONG");
statusInt = '0x81' + statusInt;
var compressionCode = '0';
if(true) compressionCode = '8';
var sampleRateCode = '1';
if(sampleRate == 22050) sampleRateCode = '2';
if(sampleRate == 11025) sampleRateCode = '3';
statusInt = '0x' + compressionCode + sampleRateCode + statusInt;
outputData.unshift(statusInt); outputData.unshift(statusInt);


for(var i=0;i<padLength;i++) { for(var i=0;i<padLength;i++) {

Loading…
Cancel
Save