(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.

http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WSE523B839-C626-4983-B9C0-07CF1A087ED7.html



var mySound:Sound = new Sound();
mySound.addEventListener(SampleDataEvent.SAMPLE_DATA, sineWaveGenerator);
mySound.play();
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);
        event.data.writeFloat(n);
        event.data.writeFloat(n);
       
    }
}

No TrackBacks

TrackBack URL: http://www.kynd.info/cp-bin/mt/mt-tb.cgi/7

Leave a comment