Flash 9 Sound Spectrum!
If you watched my last tutorial you know how much of pain it was to read audio data in Flash. The old “analyze in AE” method is now a thing of the past. The new SoundMixer.computeSpectrum() method reads audio data at runtime allowing you to create audio-driven animations. Click on the image below to see my first experiment. This only took 10 minutes! Get ready to see some crazy stuff from the Flash community. Source is below as well.
Flash Player 9 required!
[as]var s:Sound = new Sound();
var sc:SoundChannel;
var ba:ByteArray = new ByteArray();
var array:Array;
s.load(new URLRequest(“mix.mp3″));
sc = s.play(0,1000);
this.addEventListener(Event.ENTER_FRAME, spectrum);
var a:Number = 0;
function spectrum(event:Event)
{
a = 0;
graphics.clear();
SoundMixer.computeSpectrum(ba,true,0);
for(var i=0; i < 256; i=i+8)
{
a = ba.readFloat();
var num:Number = a*360;
graphics.lineStyle(num/15,0×0066FF|(num << 8));
graphics.drawCircle(stage.stageWidth/2,stage.stageHeight/2,i);
}
} [/as]
Lee




very cool!!
I can’t wait to play with this. How do you compile/publish this script?
Thanks.
F
Cool, it came out today and you are already working with it at an advanced level in my eyes. you think can you make a tutorial on GotoAndLearn About this?
-Otto
Superb! Nice choice of music too…
Yeah, very nice mix.
The new API rocks!!!
I can’t recreate your example. Did you build this using Flex or Flash9 AS3 Preview?
that is much easier. can you do an update tut on it. I think I got how it works but just in case some of us are missing something witht the AS3 sintax
Yes I am using the Flash 9 preview to compile this. What errors are you getting?
I got an error about sandbox security, somethin’ or other. I think it’s connected to the fact that the actionscript file is pointing to a file that is a relative path:
6. s.load(new URLRequest(“mix.mp3″));
I was able to input the actionscript into my alpha version of Flash 9 and change the url to an mp3 on my computer and it worked great.
Cool stuff man!
oooh! oooh! I have an idea! how about you do the same thing you did on GotoAndLearn, but instead of using after effects, use this. I will love to do something like that ( I dont undrerstand a single thing about as3 syntax).
This will be the greatest upgrade to Flash, and the way Adobe is moving forward with the Flash API and Flex is awe inspiring.
Still you cannot use the soundMixer for dynamically created sound objects, right? I mean is it possible to have this effect on an mp3 player where the user points and listens to any mp3 off the net?
I am not sure I am as stoked about the release of the new player. It seems that there are many demands on viewers to date and I am concerned that flash 9 is going to have a slower adoption rate unless some really big sites adopt it right away with detection and installation. I have released a few videos that require flash 8 and I have witnessed many email from people hearing only my music. I have sneaker detection and installation but on some machines it requres a reboot…so I think this is really starting to be a bit of a problem.
Lee, you are great!
With AS3 we dont need to parse sound Spectrum. computeSpectrum works really perfect. There is a version that i did with flash 8 in my website with your tut; http://www.ozguraltay.com //coming soon
Now i can update it…
@Jake Hawkes; you are right about the flash detection. I am using the Macromedia’s flash detection kit
Windows machines dont need reboot, just opening a new browser should be fine. But as you say, to push people to DL the latest version of flash sometimes is a nightmare…
[...] lerede begyndt at dukke en del eksperimenter med den rundt omkring, blandt andre disse to: Flash 9 Sound Spectrum Flash 9 Sound Spectrum – Part 2 Udfra koden at dømme ser det jo super nemt ud, og det åbn [...]
Very very nice. I’m looking forward to having a play.
Was hoping to make some sort of game which uses beat detection, then you have to press certain keys, but it doesnt *look* like you can analyse sound before you play it, unless i’m missing something…
Also Lee, am I right in thinking that your example is only using the first 1/8th of the spectrum?
trying to get my head around all this ByteArray stuff!
Very good ,I have successed to run it !
VERY COOL !
Thanks Lee.
hi
Very good example. but found prob. with FLASH 9 .i m useing the same above code but i got problem when loding and playing the mp3 songs. The last 10 to 15% portion of mp3 file is not getting loaded into memory/buffer so that the song is not playing completly .Song is stop before the end length(duration) of mp3. As a result Waveform is not generated completly
if any idea to solve this prob. pls help me. its very urgent to m
Cool man, I’ve constructed a xml playlist around the as3code, it works great, now I have a jukebox with vis, Thanks Lee.
Greets, Daniel, The Netherlands
that’s so cool!
I like this
It’s won’t be real in as2
I can’t wait to make one
Where do we get this so called as3 update cause im not finding it?
I am going to try it. Flash 8 is too much FUN!!!!!!!!!
I HAVE A PROBLEM THO. I built an MP3 Player and it runs perfect on my machine, as soon as I upload to the web it doesn’t produce any sound, I tried it on a couple of browsers but no luck. Any advice??????
THanks, I like this
I am to having this problem were the player does not play the sound completetly. In my case it actually does play until end if the sound was not interrupted while playing. If I do a pause/resume while playing then the sound does not finish and I do not get a SOUND_COMPLETE event from the channel and channel.position never reaches snd.length.
Any ideas?
When I put this code into Flash 9 Public Alpha, I get an error message that says “Failure to initialize Java Runtime Environment, you may need to reinstall Flash.”
O_o what’s that all about?
Get an error when I try the code. Down loaded the flash 9 alpha. Here is the error.
namespace internal.
var sc:SoundChannel;
ReferenceError: Error #1065: Variable Timeline0_82462e9c65e6af488b1f2418cf2ceca is not defined.
I think the Flash Player update killed this…
I did also get this error:
When I put this code into Flash 9 Public Alpha, I get an error message that says “Failure to initialize Java Runtime Environment, you may need to reinstall Flash.”
WHY!!!
Flash 9 Public Alpha is incompatible with Intel Macs, which is likely why you got “Failure to initialize Java Runtime Environment, you may need to reinstall Flash.”
It only runs on a 10.4 Mac as far as I know.
I’m running on OS 10.4.8 and I see the “Failure to initialize Java Runtime Environment…” I’m on an Intel Macbook Pro, so maybe that has something to do with it.
great !
I made this 4 years ago using flashamp, but know that you can do it realtime is awsome
http://www.r-a-n-d-o-m.com/combustion.html (looks a bit the same)
I was wondering if we could do the same for sound captured from the Mic. Couldn’t find a way yet. Anyone?
Yeah, im wondering the same. I want to make an audio visualizer that captures audio from my Audio Input, and visualizes it live. Is this possible? If, does anyone know of any examples, sources, tutorials?
[...] http://theflashblog.com/?p=181# [...]
make sure u use action script 3 and compile using flash player 9. Note that the variable sc is not necessarily needed. the variable a contains the peak!
Yo for all us noobs out there I hacked it for flash compiling
package {
import flash.display.Sprite;
import flash.events.*;
import flash.net.URLRequest;
import flash.media.*;
import flash.utils.*;
public class Visualizer extends Sprite {
private var s:Sound = new Sound();
private var sc:SoundChannel = new SoundChannel();
private var ba:ByteArray = new ByteArray();
private var array:Array;
public var a:Number = 0;
public function Visualizer() {
s.load(new URLRequest(“Marie Antoinette Soundtrack – 05 – Jynweythek Ylow.mp3″));
sc = s.play(0,1000);
this.addEventListener(Event.ENTER_FRAME, spectrum);
}
function spectrum(event:Event):void {
a = 0;
graphics.clear();
SoundMixer.computeSpectrum(ba,true,0);
for(var i=0; i
for(var i=0; i
Chris – is this a hack for actionscript 2.0? Looks very useful, but it seems to be cut short….. Or does the code just continue as in the initial example…?
Thanks in advance
[...] http://theflashblog.com/?p=181# [...]
Brilliant!
any ideas how to use that on a video stream?
any way of capturing the soundchannel on a video, or is there any alternative.
thx
Lee, will there ever be a change to the behaviour of Flash Player, so that computeSpectrum and soundmixer work on a per player instance basis? At the moment, the computespectrum function throws errors if you have youtube (for example) open in another tab. This post explains the bug/feature:
http://www.soledadpenades.com/2007/09/24/security-sandbox-violation-how-come/
Do you know why this demo doesnt display in Firefox? I even opened the swf directly in the browser, and nothing!
Nevermind. I just read the post above. That was my problem.
Thanks! Ive been looking for something like this.
http://www.alyse-designs.com/
I made a few adjustments, You can spend hours playing with this.
[...] [?????] AS3????????????????? – flashrod The Flash Blog » Flash 9 Sound Spectrum! [...]
[...] AS3?SoundMixer.computeSpectrum()?????????????????????????????????? [...]
[...] Found this great little example on manipulating sound data with Flash 9. This makes it a whole lot easier to manipulate sound in Flash. I see some very interesting projects being created with this in future. http://theflashblog.com/?p=181# [...]
I have a question: Howa I can show whole spectrum of mp3 file. It could not be showing dinamicly but when I load mp3 file it shows me spectrum of whole file. How I can do that.
P.S This same effect like I need to do is when we drag mp3 file onto flash form and it showns in library with full spectrum.
new to AS3…
having trouble, it says “label must be simple identifier”
in the line graphic.lineStyle
help anyone.
This is not related to this site but i wanna to ask a query. I am designing a new bloging site. The design which i have made is too simple and i m not satisfied with it. Will u plz suggest the goo design for that.
flash player 10 lets you access the soundTransform of the microphone. with dynamic sound Generation API, i can see its gona get really really crazy
Hi!Nice work really!
There is a possibility to associate jpg images, ui would like to build a soundspectrum who works with images taken from flickr, is it possible?
Thanks in advance
i try to explain me better. I would like to associate pictures and music and create an interection amongst them.
[...] http://theflashblog.com/?p=181# [...]
[...] SoundMixer?????????????????? http://theflashblog.com/?p=181# [...]
[...] soundbasic play stop Simple playback sound load Komplet Sound-gennemgang fra republicofcode Simple playback med volumeslider Nem tutorial med playback og volume interessant script med spectrum [...]
[...] ??????????????????????????????? [...]
stumbled on this trying to work though an issue I’m having. posted the code here for easier copy and paste.
it doesn’t work if a youtube player is in another tab
[...] ?????? ByteArray http://livedocs.adobe.com/flex/3_jp/langref/flash/utils/ByteArray.html Flash 9 Sound Spectrum! [...]
Wow… nice
Hi there,
Is it possible to use 2 different SoundMixer.computeSpectrum() analysis with 2 different sounds (.mp3)?
Thanks