开发者

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!


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);


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

He has some sound synthesis examples.


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜