Sound spectrum visualizer from a NetStream in AS3
We recently had to work on some sound spectrum in AS3 for DJ ANDY’s new website and the idea was to use the RSS feed of his existing iTunes podcast on the infamous m4a format. Here I found out a silly thing. You can’t load anything else than mp3s in a Sound instance. I know the sound geeks will crucify me but it’s like the old Flash who could only load some jpeg images and was unable to load pngs. Everything looks complicated when it comes to the Sound.

Anyway, I figured I’d use a NetStream to load the m4a song. My google pet told me there were other helpless people asking how to pull a SoundChannel out of a NetStream but it doesn’t seem to be the way to do it.
The answer is rather simple but its implementation is a bit weird :
var ba:ByteArray; //computeSpectrum(output:ByteArray, FFTMode:Boolean=false, stretchFactor:int=0) SoundMixer.computeSpectrum(ba);
Basically you’ll need to start the usual NetConnection and NetStream stuff, grab your song and listen on ENTER_FRAME events to analyze the sound spectrum with this method.
Here’s what the doc says about it :
Takes a snapshot of the current sound wave and places it into the specified ByteArray object. The values are formatted as normalized floating-point values, in the range –1.0 to 1.0. The ByteArray object passed to the outputArray parameter is overwritten with the new values. The size of the ByteArray object created is fixed to 512 floating-point values, where the first 256 values represent the left channel, and the second 256 values represent the right channel.
If you want to use this bytearray as a source for a spectrum visualizer, you’ll have to experiment doing some maths and bitwise calculations, all that in some loops. Using ba.readFloat() will give you the next value in the byte array. So your loop will likely perform 512 (or 256 if you only want one channel) iterations to use all the values of the spectrum.
This method of analyzing the sound spectrum on the fly has two major weaknesses :
- if you got multiple sounds playing, you can’t tell them apart with this method as it gives you a spectrum for all the sounds together.
- if you intend to use it for serious sound applications, you’ll want to enable FFT (using a Fourier transformation) and the calculation will generate a noticeable lag, making your analyzer appear unsynchronized, late.
Luckily for us and in most applications, the sound spectrum is meant as a visual entertainment, nothing more.
There’s a very good sample on todaycreate.com with insightful comments in case you don’t really know where to start.
Additional and detailed explanations are available on the Flex 3 language reference.
A great thing however, is the way you can get a lot of information on an m4a input. There are three events currently supported :
- onTextData gives you the current text of the playing m4a. It could be anything and in our case it’s the name of the current track.
- onImageData gives you the current image of the podcast. Just like the texts, the image can change if your podcast got chapters.
- onMetaData is important to get the duration of the stream. Additionally, the event object tells you of a lot of things about the stream like the sample rate, the channels, the tags, etc.
Depending on how the m4a is documented by the podcast creator and the software one’s using, you could even chapterize a podcast in order to get something similar to cue points in the chapters property in the metadata. You won’t get everything you want though. I would have appreciated to get a list of all the text data and their seek time but you can’t get them beforehand. You must play the stream in order to get the events one by one so if you really need a list of the “text points” (they aren’t cue points), you’ll have to make one manually…
About this entry
You’re currently reading “Sound spectrum visualizer from a NetStream in AS3,” an entry on Théâtre magique
- Published:
- 12.30.09 / 5
- Category:
- Flash/Flex
1 Comment
Jump to comment form | comments rss [?] | trackback uri [?]