5

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!

Lucas
  • 558
  • 11
  • 28

3 Answers3

8

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);
Adam Smith
  • 1,917
  • 12
  • 14
2

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

He has some sound synthesis examples.

John Giotta
  • 16,432
  • 7
  • 52
  • 82
2

I have written many articles on the topic of audio synthesis in Flash on my blog. Here are a few good places to start:

http://labs.makemachine.net/2010/06/note-frequency/

http://labs.makemachine.net/2010/06/sine-square-waves/

http://labs.makemachine.net/category/audio/

JeremyFromEarth
  • 14,344
  • 4
  • 33
  • 47