A while back I held a contest to see who could come up with the coolest sound visualization using the new SoundMixer.computeSpectrum() method. This gives you a whole bunch of byte data which you can use to create cool sound-driven applications. But what if all you need is the overall amplitude of the sound? Well you can do this with computeSpectrum() by averaging out all the values, but that is a lot of work considering that Flash has a built set of properties just for this situation. The leftPeak and rightPeak values of the SoundChannel class provide amplitude data for each channel of the audio file. The example below shows how to use this data to create an amplitude meter. The ActionScript 3 code is below the graphic and you can get the full source here. Flash Player 9 required.
-
left.mask = leftMask;
-
right.mask = rightMask;
-
-
var s:Sound = new Sound();
-
s.load(new URLRequest(“loop.mp3″));
-
-
var sc:SoundChannel;
-
sc = s.play(0, 1000);
-
-
this.addEventListener(Event.ENTER_FRAME, showAmplitude);
-
-
function showAmplitude(eventArgs:Event)
-
{
-
leftMask.y = 318 + (sc.leftPeak * – 230);
-
rightMask.y = 318 + (sc.rightPeak * – 230);
-
}
Lee









Cool, but here is one thing that i hate in Sound api. You can’t get any value (peak,sound spectrum) for the track in one loop without playing it,like in my example
public function onSoundLoaded(eventL:Event):void
{
for(var i:Number=0;i
Very nice
I think I have some stuff to play with in Flash 9
Thanks
Paul
Nice sample there Lee. Can’t wait till the next official release of Flash… This is so much easier than having to use After Effects for analysing ur audiodata
I actually used this in my submission for the sound spectrum competition. It was used to animate the eyes of a character as if it were blinking with the sound.
I know I wasnt the first to use it, but its cool to see people online start making a big deal about it after I used it before. Almost all of my flash experience has been seeing what others do first, then trying to duplicate it to learn how it was done.
lee, you are one of my heroes! i think you will go down in history as one of the great tinkerers!!
thank you for your efforts in passing along your amazing, cutting-edge understandings. of course, the geniuses behind the software deserve lots of credit. but they get theirs every time we open the packages up.
i have some ideas for using the leftPeak rightPeak values… fun and simple stuff, indeed!
sincerely,
scot electron acklin
So what exactly is the deal with AS3? It only works with Flash 9? Cause i want to start playing with this now!
Hi there,
Deos any one know if it ‘s plausable to run an AS.2 swf and an AS.3 swf alongside each other in an html page.If you hadn’t guessed already i’m kinda new to this.
The reason i say this is is i’m building a flash based website in AS.2 but i’d love to include the computespectrum in an mp3 player for a music section.Obviously an AS.3 swf couldn’t be loaded onto an AS.2 stage(now that would be handy!!!!!) but could they co-exist on a web page? I’ve already got some crude versions working.
Any wisdom?
cheers
Toby
I have played around with this but cannot seem to get it to work with global sound or the sound I import to the timeline. Can someone explain to me if that is possible or not?
I cant get the code to work, I just want it to auto play viual datra of a movie clip.
I’ve gotta turn this baby into a class. Thanks Lee – You’re a star!
I’ve played around with it a bit, using scaleY instead and using a Timer:
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.net.URLRequest;
left.mask = leftMask;
right.mask = rightMask;
var url:String = new String(“Mental_Health_Hotline.mp3″)
var sound:Sound = new Sound(new URLRequest(url));
var sc:SoundChannel;
sc = sound.play();
var peakTimer:Timer = new Timer(33);
peakTimer.addEventListener(TimerEvent.TIMER, showAmplitude);
peakTimer.start();
function showAmplitude(eventArgs:Event):void {
leftMask.scaleY = sc.leftPeak;
rightMask.scaleY = sc.rightPeak;
}
I’m gonna keep playing with this until I’ve got a truly re-usable class. Lee, you’re my hero. I’ve learnt so much from you on gotoAndLearn();
how does mask work?
once i set mask to a mc, it no longer display
maskMC = new Sprite();
maskMC.width = 5;
maskMC.height = 5;
this.addChild(maskMC);
yearText = new YearWorkText();
yearText.mask = maskMC
this.addChild(yearText);
yearText.mask = maskMC
It’s a best example for play audio file.
var numOfTime:Number = 10
var snd:Sound = new Sound();
snd.load(new URLRequest(â€audio/uday.mp3?));
snd.play(0, numOfTime);
Thanks,
Uday
uday_sgh(et)yahoo.com
Hey,
doest this work with a livestream too? It looks like ist would not work with it… any ideas?
Hi i am a Programmer i want to know how to play multiple sound files one after another or in between is it possible that we can play sounds from a specific source directory and then we can play multiple sounds simultaneously
please help me
i want that work in as3
Can anybody provide me solution for microphone to spectrum reading waves realtimes also needs to store each sound wave (byte array) infor in text form to some red5 server of can sednit through http request?
Can anybody provide me solution for microphone to spectrum reading waves realtimes also needs to store each sound wave (byte array) infor in text form to some red5 server of can sednit through http request?
Does anyone know how I can get the leftPeak and rightPeak from a video played with a NetStream?
Here is the code for stream peaks… it work’s..thanks guys
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.media.SoundLoaderContext;
import flash.net.URLRequest;
var s:Sound = new Sound();
var req:URLRequest=new URLRequest(“http://audio.uctradio.cl/clone”);
var context:SoundLoaderContext=new SoundLoaderContext(8000,true);
s.load(req, context);
var sc:SoundChannel;
sc =s.play();
btn_play.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
s.play();
}
left.mask = leftMask;
right.mask = rightMask;
var peakTimer:Timer = new Timer(33);
peakTimer.addEventListener(TimerEvent.TIMER, showAmplitude);
peakTimer.start();
function showAmplitude(eventArgs:Event) {
leftMask.y = 318 + (sc.leftPeak * – 230);
rightMask.y = 318 + (sc.rightPeak * – 230);
}
!kamello
Hi, this maybe a really silly question and I am a flash newbie! But is there a way to stop the music playing when you click on the bars?
Thanks1
Hi
I am trying to create the sound meter, and need help on that.
I want people to scream in front of there mic’s and depending upon how load they are I want my meter to jump at the desired height.
I have check the mic.activityLevel but it is very easy to reach at 100% using it.
Looking forward to hear from you.