Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to synthesize exact frequencies on flash?

I've researched a bit and I discovered a way to generate sounds dynamically on flash:

import flash.media.Sound;

var mySound:Sound = new Sound();

mySound.addEventListener(SampleDataEvent.SAMPLE_DATA, sineGenerateSound);

mySound.play();

function sineGenerateSound(event:SampleDataEvent):void{ 

 for(var i:int=0;i<4092;i++){  

  var n:Number = Math.sin((i+event.position)/Math.PI/4); 
  event.data.writeFloat(n)
  event.data.writeFloat(n)
 } 
}

I would just like to know how I can make it generate the exact frequency I need, for example 100Hz.

Thanks!

like image 202
Lucas Avatar asked Apr 05 '11 17:04

Lucas


2 Answers

Assuming 44.1kHz sample rate:

var freq:Number = 100; // example, 100 Hz, set this somewhere outside the for loop   
var n:Number = Math.sin((i+event.position)*freq*2.0*Math.PI/44100.0);
like image 151
Adam Smith Avatar answered Oct 03 '22 07:10

Adam Smith


If you haven't already, check out http://lab.andre-michelle.com/. The man does some cool stuff.

He has some sound synthesis examples.

like image 27
John Giotta Avatar answered Oct 03 '22 07:10

John Giotta