Przeglądaj źródła

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

dds
Matt Bradshaw 6 lat temu
rodzic
commit
ea344052e3
2 zmienionych plików z 27604 dodań i 9 usunięć
  1. +27590
    -0
      extras/wav2sketch/testjs/checker.js
  2. +14
    -9
      extras/wav2sketch/wav2sketch.js

+ 27590
- 0
extras/wav2sketch/testjs/checker.js
Plik diff jest za duży
Wyświetl plik


+ 14
- 9
extras/wav2sketch/wav2sketch.js Wyświetl plik

@@ -35,8 +35,8 @@ function processFile(file, fileName) {
}
var outputData = [];
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);
while(out.length < 8) out = '0' + out;
out = '0x' + out;
@@ -72,13 +72,18 @@ function processFile(file, fileName) {
});
}

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

Ładowanie…
Anuluj
Zapisz