Browse Source

clearer uint16 conversion, basic test script to track down rounding errors

dds
Matt Bradshaw 6 years ago
parent
commit
ea344052e3
2 changed files with 27604 additions and 9 deletions
  1. +27590
    -0
      extras/wav2sketch/testjs/checker.js
  2. +14
    -9
      extras/wav2sketch/wav2sketch.js

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


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

} }
var outputData = []; var outputData = [];
for(var i=0;i<monoData.length;i+=2) { for(var i=0;i<monoData.length;i+=2) {
var a = floatToUnsignedInt16(monoData[i]);
var b = floatToUnsignedInt16(monoData[i+1]);
var a = toUint16(monoData[i]*0x7fff);
var b = toUint16(monoData[i+1]*0x7fff);
var out = (65536*b + a).toString(16); var out = (65536*b + a).toString(16);
while(out.length < 8) out = '0' + out; while(out.length < 8) out = '0' + out;
out = '0x' + out; out = '0x' + out;
}); });
} }


function floatToUnsignedInt16(f) {
f = Math.max(-1, Math.min(1, f));
var uint;
// horrible hacks going on here - basically i don't understand bitwise operators
if(f>=0) uint = Math.round(f * 32767);
else uint = Math.round((f + 1) * 32767 + 32768);
return uint;
// http://2ality.com/2012/02/js-integers.html
function toInteger(x) {
x = Number(x);
return x < 0 ? Math.ceil(x) : Math.floor(x);
}

function modulo(a, b) {
return a - Math.floor(a/b)*b;
}

function toUint16(x) {
return modulo(toInteger(x), Math.pow(2, 16));
} }


// compute the extra padding needed // compute the extra padding needed

Loading…
Cancel
Save