Browse Source

looks like web audio api resamples everything to 48kHz? going back to paul's way, reading the wav file manually

dds
Matt Bradshaw 6 years ago
parent
commit
7e332b16d5
2 changed files with 29 additions and 6 deletions
  1. +4
    -0
      extras/wav2sketch/.gitignore
  2. +25
    -6
      extras/wav2sketch/wav2sketch.js

+ 4
- 0
extras/wav2sketch/.gitignore View File

AudioSampleKickULAW.cpp
AudioSampleKickULAW.h
AudioSampleKickPCM.cpp
AudioSampleKickPCM.h
AudioSampleKick.cpp AudioSampleKick.cpp
AudioSampleKick.h AudioSampleKick.h

+ 25
- 6
extras/wav2sketch/wav2sketch.js View File

function processFile(file) { function processFile(file) {
var context = new window.AudioContext(); var context = new window.AudioContext();
context.decodeAudioData(file, function(buffer) { context.decodeAudioData(file, function(buffer) {
var source = context.createBufferSource();
source.buffer = buffer;
source.loop = false;
source.connect(context.destination);
source.start(0);
var monoData = buffer.getChannelData(0); // start with mono, do stereo later
var thisLength = monoData.length;
// AudioPlayMemory requires padding to 2.9 ms boundary (128 samples @ 44100)
var padLength = padding(thisLength, 128);
var arraylen = ((thisLength + padLength) * 2 + 3) / 4 + 1;
console.log(10241, thisLength, padLength, arraylen);
console.log(monoData);
}); });
} }


// compute the extra padding needed
function padding(sampleLength, block) {
var extra = sampleLength % block;
if (extra == 0) return 0;
return block - extra;
}

function generateOutputFile(fileContents) { function generateOutputFile(fileContents) {
var textFileURL = null; var textFileURL = null;
var blob = new Blob([fileContents], {type: 'text/plain'}); var blob = new Blob([fileContents], {type: 'text/plain'});
return textFileURL; return textFileURL;
} }


function generateCPPFile(fileName, audioData) {
var out = "";
out += '// Audio data converted from audio file by wav2sketch_js\n\n';
out += '#include "AudioSample' + fileName + '.h"\n\n';
out += 'const unsigned int AudioSample' + fileName + '[' + audioData.length + '] = {';
out += audioData.join(',') + ',};';
return out;
}

var outputFileHolder = document.getElementById('outputFileHolder'); var outputFileHolder = document.getElementById('outputFileHolder');
var downloadLink = document.createElement('a'); var downloadLink = document.createElement('a');
downloadLink.setAttribute('download', 'test.txt'); downloadLink.setAttribute('download', 'test.txt');
downloadLink.href = generateOutputFile("this is a test");
downloadLink.href = generateOutputFile(generateCPPFile('test.wav',[0,1,2,3]));
downloadLink.innerHTML = 'download link'; downloadLink.innerHTML = 'download link';
outputFileHolder.appendChild(downloadLink); outputFileHolder.appendChild(downloadLink);

Loading…
Cancel
Save