Does anyone know if its possible to get a precise note (C, C#, D, Eb, etc) from a javascript library like riffwave.js?
The demo makes me think is possible but I'm not sure how to transpose the piano frequency for a given note into the data array required for the generated wave file.
Sure! You'd want to create some mapping function from key to frequency (could just be a dictionary).
To synthesize a given frequency with riffwave.js, you would do something like this
function simHertz(hz) {
var audio = new Audio();
var wave = new RIFFWAVE();
var data = [];
wave.header.sampleRate = 44100;
var seconds = 1;
for (var i = 0; i < wave.header.sampleRate * seconds; i ++) {
data[i] = Math.round(128 + 127 * Math.sin(i * 2 * Math.PI * hz / wave.header.sampleRate));
}
wave.Make(data);
audio.src = wave.dataURI;
return audio;
}
var audio = simHertz(1000);
audio.play();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With