Generate sound at a specific frequency

I found this library: http://codebase.es/riffwave/ that generate sounds on the fly in javascript.

I want to have a function that generates a sine wave of a certain frequency.

But the way we use the library is a lot of math, and I could not get it to work.

I tried

for(var i=0;i<10000;i++)data.push(127*(Math.sin(2*Math.PI*f*i))); 

but it didn’t work.

How can i do this?

+4
source share
1 answer

Try the following:

 var freq = 440; // Frequency (cycles per second) var rate = 44100; // Sample rate (samples per second) for (var i = 0; i < 10000; i++) { var time = i / rate; data[i] = 128 + Math.round(127 * (Math.sin(2 * Math.PI * freq * time))); } 
+4
source

All Articles