Robots w/Lasers

April 21, 2006

Dynamic Sound in 8.5

Please see this update

As far as I know the company formerly known as Macromedia did not add any kind of dynamic sound generation methods to ActionSciprt3 / Flash 8.5. However, they did add two features we can use to hack it:

  1. ‘ByteArray’ class which is a low level data class
  2. ‘Loader’ class (formerly MovieClipLoader) can load from a ByteArray, as well as the normal loading from a URL.

So the theory is, I generate valid swf into a byteArray that simply contains sound data, then I load that and it plays.

This would involve me understanding the swf binary file format…however that’s a bit of a pain, so I cheated.

  1. Create a .fla in Flash 8 & import a sound into frame 1
  2. Copy & paste the entire data from the .swf into an array in my AS3 project
  3. At runtime, replace the central part of the array with new data (leaving the header and footer of the data intact). Just a bit of trial & error to see how much data needs to be left

Here is a very simple example:

Type a frequency into the text box at the left, then click on the red box on the right. You should here a 1 second tone being generated.

It’s still not perfect, there seems to be a bit of distortion, but it gets the idea across. For reference, the format of the sound seems to be 16 bit signed samples. The sample rate is based on the swf header, and the one I am using is set at 11khz I believe…

Click here to load movie
Requires Flash 8.5 beta player. (does not work on flash 9 player…i’ll update it when I get time.)

You can download the sourcecode if you want.
If you make any improvements, please let me know! Thanks.

Filed under: Flash — davr @ 11:39 am


  1. […] I noticed an awesome hack on Flashcoders today. David from Robots w/Lasers shows off a way to dynamically generate sounds in AS3. He actually builds a SWF in a ByteArray at runtime and passes it into a Loader object. With a little work to refine this approach, ambitious Flash developers could put together some sort of music sequencer. A full game soundtrack would be pretty cool. The sounds have a nice retro feel that would go well with many of the Flash games out there. It reminds me of my days playing with BASIC or Assembly. I’m sure there are hundreds of other possibilities. […]

    Pingback by Dynamic bleeps and bloops in AS3 » Zeus Labs » Flash and Actionscript Insights from a San Diego Developer and Designer — April 21, 2006 @ 3:29 pm

  2. […] I didn’t know this was possible. […]

    Pingback by FatBlog » Holy Crap, Sound Generation in Flash? — April 25, 2006 @ 12:23 pm

  3. interesting little experiment … but isn’t ActionScript
    much too slow to be used for generating any real sounds?
    audio generation and signal processing is already
    CPU-intensive in native assembly code.

    Comment by Rasmus Schultz — May 7, 2006 @ 10:19 pm

  4. Rasmus:

    I feel that AS3 has plenty of speed for things like this. It just depends on what exactly you are trying to do. Is it fast enough to sample and mix a 32 channel orchestral piece? maybe not. But I think with a little work, it could play 2 or 4 channel music in realtime, with plenty of cpu left over. When I get some free time, I’ll play around with it a bit.

    Comment by davr — May 8, 2006 @ 9:48 am

  5. Brilliant work! Never thought of using ByteArray to generate a sound container swf…

    It would be possible to make a sort of SoundProxy class out of this so that adapting it to a (hopefully eventually) proper Flash Sample/SoundData object (Flash10?!) would be less painful.

    As per the comments above I think Flash should be capable of basic synthesis and processing. Just think of plotting Math.sin() into a buffer. Basic synthesis and FM synthesis should be quite viable…

    Comment by Neil Voss — May 9, 2006 @ 11:52 pm

  6. Hi,

    I’ve been working on a Class that was based on your idea.
    I took the concept a bit further and created a realtime circular audio buffer so you can generate audio in realtime without the limit of sample length, as it will ’stream’ the circular buffer.

    It still is a bit unstable, but please take a look at:


    Comment by PJ @ E-Phonic — July 8, 2006 @ 12:03 pm

  7. […] The creator over at seemingly has created the first realtime synth in Flash with AS3.0. Realtime in the meaning that it creates the produced sound completely by itself and not relying on predefined samples. He used the ByteArray and the Loader class (Loader.loadBytes) based on an idea and first experiment from David at Robots /w Lasers. The synth consists of a simple, white emptyness in where you have to click with the mouse and it plays generated sounds depending on the coordinates. The sounds might be still a bit awkward but hey, it’s a milestone in Flash history, isn’t it?! […]

    Pingback by First Realtime Synthesizer in Flash at H1DD3N.R350URC3 — July 9, 2006 @ 1:11 am

  8. […] Synan toteutustapa on yllättävä. Flashissa ei voi luoda dynaamisesti ääniä, mutta ActionScript 3 tukee ulkoisten movieclippien lataamista, joten sen avulla voi ladata tyhjän wav-äänen Flashiin ja muokata ohjelmallisesti tiedoston äänidataa niin, että saadaan ohjelma soittamaan haluttuja ääniä. Idea on ilmeisesti alun perin Davidilta. […]

    Pingback by Kuutio » Syntetisaattori Flashilla — July 27, 2006 @ 11:25 pm

  9. […] Supposing we could generate a sound wrapped in the necessary swf bytecode in memory, compile a swf from it, and play back the sound that we just embedded in it? In a previous article in André Michelle’s blog, he referred to this rather enlightening page about the technique, and after checking that I was barking up the right tree, I set to work. […]

    Pingback by FlashCodersBrighton » Blog Archive » how to make actionscript 3 play generated pcm wave data — August 1, 2006 @ 4:33 pm

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress

Bad Behavior has blocked 776 access attempts in the last 7 days.