Get accurate notes with riffwave.js

Does anyone know if it is possible to get the exact note (C, C #, D, Eb, etc.) from a javascript library like riffwave.js ?

The demo makes me think that this is possible, but I'm not sure how to transfer the piano frequency for this note to the data array needed for the generated wave file.

+4
source share
1 answer

Sure! You want to create some key-to-frequency mapping function (maybe just 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(); 
+8
source

All Articles