oampo/audiofile.js

Sample scaling is incorrect

Barabas5532 opened this issue · 1 comments

The code for unsigned to signed integer conversion is incorrect.

Paste this into console, and the result is wrong.

function scale(value, bitDepth)
{
    var range = 1 << bitDepth - 1;
    if (value >= range) {
        value |= ~(range - 1);
    }
    return value;
}

[0, 1, 2, 3].forEach(function(n) {console.log(n + ", " + scale(n, 2))})
Output:
0, 0
1, 1
2, -2
3, -1

Moreover, according to stack overflow, greater than 8 bits per sample is already signed, so there is no need to do this conversion.

Hmm, it seems this actually does sign extension on the data, so only the comment is wrong. 8-bits per sample (always unsigned) will be broken after sign extension though.