Andre Michelle and others have been telling Adobe to Make Some Noise, and Adobe has listened!
A preview release of Flash Player 10 “Astro” was released today, and in it is the capability to dynamically create sounds. See this demo (of course you’ll need the new player installed):
http://www.bit-101.com/astro/sound01.html
Here I’m creating two wave forms, one controlled by the mouse’s X position, and one by the Y, and combining them into a single dynamic sound, also visualized.
The way the new sound creation works is you listen for a “SAMPLES_CALLBACK” event on the sound object. This is fired when the playing sound object is almost out of sound to play. In the event handler for this event, you fill up the sound’s sampleCallbackData property with a bunch of new sample. This property is a byte array, so you write a bunch of floating point numbers to it. One for the left channel, then one for the right channel.
Here is about the simplest example:
[as]var sound:Sound = new Sound();
sound.addEventListener(Event.SAMPLES_CALLBACK, onSamplesCallback);
sound.play();
var freq:Number = 440;
var rate:Number = 44100;
var phase:Number = 0;
function onSamplesCallback(event:Event):void
{
for(var i:int = 0; i < 512; i++)
{
phase += freq / rate;
var phaseAngle:Number = phase * Math.PI * 2;
var sample:Number = Math.sin(phaseAngle);
sound.samplesCallbackData.writeFloat(sample);
sound.samplesCallbackData.writeFloat(sample);
}
}[/as]
Note that these are beta APIs and are likely to change. If you are viewing this post any time after spring / early summer 2008, the above code will probably not compile, and I can't even guarantee that the SWF will work in future versions of the player. But this is a glimpse of what is to come.
freq is the frequency of the sound you want to create. rate is 44100 samples per second, which is what the new model uses. Use phase to hold a number, multiplied times 2PI to give you the angle, take the sine of that angle to get a sine wave. We write that to samplesCallbackData twice - once for left, once for right. You could write different values here to go stereo. Here, we write 512 samples at a time. When those are almost used up, we'll get another callback and write another 512, so the sound will continue to play.
Thanks to Tinic Uro for pushing this one through! A lot more (and more accurate and better coded) information here: http://www.kaourantin.net/2008/05/adobe-is-making-some-noise-part-2.html
How do you compile it?
You can’t right now. When some version of Flash CS4 is released, you’ll be able to.
How do YOU Keith Peters compile it?
haha. OK, OK, sorry to be the big mystery man. 🙂
Obviously, I have an unreleased version of a Flash authoring program that will compile to this player. Adobe has been cool enough to let those with access to this post samples to show off the new player features, but has asked that we limit the discussion to the player features, not the authoring program. Hopefully they will release some kind of public beta at some point so that everyone can play with it, not just drool over it.
Are you allowed to share any documentation you were given? Would be nice to see the specific new classes/functions that are available.
hmm… I was probably pushing it showing code, as the APIs are likely going to change, but Tinic had already released stuff. I’ll wait for Adobe people to release code and documentation.
Hi ….
are you planing to show more demos in the near future? like demo about the new text engine .
thanks
yes, I’m sure I’ll be showing more stuff.
Keith,
How much faster is F10 when compiled to it? For example, a regular for loop in F9 vs a regular for loop in F10…
Does your confidential authoring tool support ECMA 4 (or AS4)??
This is so exciting!
Yesss!!! As a musician and flash lover, this excites me terribly.
Keith, will you be able to show samples of the p2p and local load/save file abilities?
Get your wiimote and be the first to build a truly authentic flash theremin.
this is cool news! anyone know if and where a standalone version of astro is available?
Why hasn’t anyone asked the Flash Platform Evangelist!?!
http://theflashblog.com/
Good news!
Flashdevelop with FP10 support:
http://www.flashdevelop.org/community/viewtopic.php?t=3039
@lunetta: A theremin- what an awesome idea!
i think everyone knows this but no one posted it…
you can compile F10 using the flex sdk
Awsome!.
fp10 is going to be great.
Kieth come by my blog. well everyone is invited.
Would it be easy sb to update this code to the new Sound API, please?