Selaa lähdekoodia

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

dds
Matt Bradshaw 6 vuotta sitten
vanhempi
commit
7e332b16d5
2 muutettua tiedostoa jossa 29 lisäystä ja 6 poistoa
  1. +4
    -0
      extras/wav2sketch/.gitignore
  2. +25
    -6
      extras/wav2sketch/wav2sketch.js

+ 4
- 0
extras/wav2sketch/.gitignore Näytä tiedosto

@@ -1,2 +1,6 @@
AudioSampleKickULAW.cpp
AudioSampleKickULAW.h
AudioSampleKickPCM.cpp
AudioSampleKickPCM.h
AudioSampleKick.cpp
AudioSampleKick.h

+ 25
- 6
extras/wav2sketch/wav2sketch.js Näytä tiedosto

@@ -14,14 +14,24 @@ function readFile() {
function processFile(file) {
var context = new window.AudioContext();
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) {
var textFileURL = null;
var blob = new Blob([fileContents], {type: 'text/plain'});
@@ -29,9 +39,18 @@ function generateOutputFile(fileContents) {
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 downloadLink = document.createElement('a');
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';
outputFileHolder.appendChild(downloadLink);

Loading…
Peruuta
Tallenna