Audio-enabled Physics Engine Example
June 9, 2008 on 4:16 am | In Flex, Music, Programming |I thought I’d put up a fun demo SWF from my “Flash Bang” talk of a couple of weeks ago. This is an example of what can be done with the new FlashPlayer 10 (”Astro”) Sound API; it synthesizes sounds on the fly that are synchronized with colliding objects in a simple physical simulation. The volume and pitch of the sounds are controlled by the collision force and the nature of the colliding objects.
To run this demo, get hold of the FP10 standalone player, then use it to open this URL:
http://www.joeberkovitz.com/projects/StandingWave/physicalSound/physicalSound.swf
(If you try this with FP9, you’ll get a runtime error dialog, or the app won’t open. And at some point soon the FP10 API will probably change, invalidating this app and obliging me to rebuild it, so in the near future YMMV.)
The sync between the sounds and the visuals is quite delicate and if it is off by even a little bit, the results look terrible. On my MacBook Pro, the auto-calculation of the audio latency is near perfect, but on Windows the player API’s Sound.position property seems to be off by about 150 msec, enough to ruin everything. You can play around with the latency in this example by moving the slider.
I didn’t post source because I am using a proprietary library, but the sound code in the example is not very interesting or complex actually. The trickiness in this app is all in the audio/visual synchronization. I am actually running two identical physical simulations, one of which runs ahead of the other, generating each collision sound exactly the right time interval before the other simulation displays the same collision. You can see both simulations by clicking the checkbox in the upper left of the example.
I used an open source physics engine called APE (Actionscript Physics Engine) available, at http://www.cove.org/ape/index.htm. I picked it because it was very easy to modify the collision detection mechanism to add audio hooks, but I haven’t done enough research to compare it to other engines for this purpose.
Here’s a screenshot (without sound of course!):

No Comments yet »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Entries and comments feeds.
Valid XHTML and CSS.
All content copyright (c) 2006-2007 Joseph Berkovitz. All Rights Reserved.