3 ms·
My ears! I know it's not the point but his audio synthesis sounds terrible, and that makes me feel like nit-picking: - 8-bits at 20 kHz really? - He's actual
by radarsat1 15y ago
My ears!
I know it's not the point but his audio synthesis sounds terrible, and that makes me feel like nit-picking:
- 8-bits at 20 kHz really?
- He's actually specifying 20480 Hz in the code, so it's not in tune
- He's using the range 255, but it's 8 bits _signed_ so it should be 127, so he's getting lots of distortion.
His wav header should be:
0x44,0xAC,0,0, // 44,100 samples per second
0x88,0x58,0x01,0, // byte rate: two bytes per sample
2, 0, // aligned on every two bytes
16, 0, // 16 bits per sample
and then in the synthesis:
var samplespercycle = 44100 / frequency;
var samples = new Uint16Array(44100 * duration);
var da = 2 * Math.PI / samplespercycle;
for(var i = 0, a = 0; i < samples.length; i++, a += da) {
samples[i] = Math.floor(Math.sin(a) * 32768);
}