November 2008 Archives

(AS3) Dynamic sound generation

| No Comments | No TrackBacks
I've downloaded Flash CS4 today. I think I'll be hooked on this new toy for a while, and I will post a experiments with the new features.

First of all, I tried dynamic sound generation with as3, which is the function I've been awaiting for for a long time.
Here's a very simple sample code that creates sine wave, basically same as the sample on Adobe's online document, but for only one expression :Math.sin((i + event.position) * Math.PI / 100);
Because the sample rate of Sound object is 44,100hz, this code sets the pitch to 440khz that is A4.

var mySound:Sound = new Sound();
mySound.addEventListener(SampleDataEvent.SAMPLE_DATA, sineWaveGenerator);;
var count = 0;

function sineWaveGenerator(event:SampleDataEvent):void
    for (var i:int = 0; i < 8192; i++)
        var n:Number = Math.sin((i + event.position) * Math.PI / 100);;;