Yesterday I wanted to try the Sound.extract() method added in Flash Player 10, I came to this (Flash Player 10 only) little test, where a simple waveform of an external mp3 is rendered.
The power of Sound.extract() is that you can render a waveform of a sound before playing it, which was not the case of SoundMixer.computeSpectrum(). More than that, Sound.extract() is more precise than computeSpectrum().
As you can imagine, you have millions of samples in a usual sound, so if you want to render it you would normally need a huge bitmap that we could scroll.
In fact there are different approaches for handling this, mine was very simple, trick on x-axis and jump some values to get an average wave which is enough here. Just to play with it, you can select an area to loop.
Well in fact this is the kind of thing where we could never stop adding stuff
Sources (saved for Flash CS3 but to be compiled with Flash CS4)

Comments (50)
Hi Thibault,
Thanks for this sources because the documentation about Sound.extract is poor.
I like your loop function.
Can you thing it’s possible to save on the desktop a selected area of the sound ?
Thanks
JP
Hi jeanphilippe,
Happy you like it, yes it possible to extract any samples from the playing sound and save it on the hard disk. Any filter you could also be applied to the selection made
best,
Thibault
Er wow!
Nice work Mr T.
Good work, you keep it really simple.
Now waiting for the real fast Fourier transform class for waveform display ?
Hi Tek,
Yes ! Don’t tempt me !
Merci Thibault,
I use FileReference to save() a part of a sound on my hard disk. But i can’t read it, it’s not a MP3
Thanks
JP
@Tek
I’m with you on that one! Something that actually renders the spectrum as expected would be lovely…
I dream of such a beautiful day!
Bonjour Thibault,
i’ve been playing with the Sound.extract() function as well, but in a scratchy way and it’s been quite fun !
see there : http://www.minimal.be/scratch/
@jeanphilippe : ce n’est plus du mp3, mais plutôt du wave, puisque c’est décompressé.
merci boblemarin,
mais je ne trouve pas de solution
@Tek – It is a very usefull feature to rendering the spectrums! Thx for your comment, it gives me the opinion to understand the program a liddle bit more:)
greets , Flug
I wrote about this some time ago as well, although by just taking one sample every so often you end up showing sympathetic frequencies to the rate of your sub-sampling, resulting in a false waveform. I discussed it on my blog here-> http://blog.efnx.com/?p=75 as well as the fact that crunching through the entire array of samples all at once causes the player to lag and refuse input. Awesome highlight and loop though, sounds great!
Hi Schell,
Nice article !
Yes, you are right, I think it would be nice to implement a zooming feature. This way, we would be able to render the full waveform without any modification
best,
Thibault
Hi Thibault. I’ve just written a class that does just that! Check out my new post here-> Flash Actionscript 3 Waveform Generation Class. Let me know what you think. I used your method of looping using a Timer object, it works MUCH better than using a SampleDataEvent callback!
Hi Schell,
Very nice demo
The zooming behavior is great ! I am happy to see that you liked the looping feature
Is there any way to control speed of the mp3 played through as3 (Normalization)
hi,
Thank you so much for this great source Thibault.
I see in your code you use the same instruction (samples.readFloat()*128)for left/right channel. is it normal ?
I have some difficulties understanding of the ByteArray structure for an extracted sound. If I’m right, for example for a sound of 1 second we would extract a ByteArray.length of 44100 (44khz sound) ? Or 88200 (left/right) ?
Thank you
for one second, 88200 !
Stereo sounds, each channel has 44100 sample per second.
If Thimbault use twice the same instructions, that’s because both channel are in one only byteArray. The first data picked is for the left channel, then the next one, right channel, and the next one, next sample for left channel .. right .. left .. etc etc
Quand un ingé Flash rencontre à la cafet un ingé du département ex Syntrillium..
Enfin, j’imagine..
Mais cette classe permet-elle d’en retirer les FFT??
Si oui, on peut se passer de l’ancienne classe, sinon.. ben non.
Hey guys, i made visualizer based on Sound.extract()
]
look: http://borovsky.pl/projects/own/visualizer
[FP10
my mail: fl.borovsky [ at ] gmail.com
Umm how are supposed to read the frequences out of the data the method returns ?
For creating a visualizer or comparing samples the wavespectrum isn’t nearly as interesting as a nice FFT’d bytearray.
Anyway, great work !
-Shui (mal_shuir@hotmail.com)
Siiiiiiiick. Thanks for posting
Thibault, can you please advise how it is possible to extract any samples from the playing sound and save it on the hard disk? What method should be used? Thank you very much.
Hi there, amazing work! Although I’m having a problem with your sources both in CS3 and CS4 Flashes, when I try to publish the new .swf from it I get this error msg saying:
“1061: Call to a possibly undefined method extract through a reference with static type flash.media:Sound.”
It’s sourced from line 66 “var lng:Number = sound.extract(samples,extract);”
Any ideas what I’m missing here to work with it?
sk- i had this problem too! In the properties for your flash project, it defaults to Flash 9 compatibility. Change it to 10, and everything will be great.
Oh, thank you so much for posting this.
It didn’t hit me until today that Soundmixer.computeSpectrum() only operates on the global sound output. Sound.extract() looked promising, but thanks to you I can implement sound-specific spectrums confidently knowing I’m not barking up the wrong tree.
First thibault, your player is awesome.
Lately, I’ve been trying to program a progress bar in as3 that uses sound.extract, I have managed to make it show the whole song in wave but I still get a major memory system issue. If anybody could help me on that I would love it.
http://www.levortex.tv/mp3player/mp3player.html
that’s what it looks like for now.
I think it’s because of my byteArray…
Great , thanks a lot for share it.
Is really nice you saved the source for CS3, I publish for player 10 from Flex, and I haven’t upgraded to CS4
please kill my previous post,
thanks
formatting goes terribly wrong
Nice work!
I need to use this with really long audio-files over 15 min is it possible to split the sound.extract handling in to smaller chunks?
When i use this with a 15 min file. CPU and memory peaks and some times the browser runs out of memory.
//Olof
I have no experience with CS and Flash. Someone can tell me how to see and modify the source of this software?
Which SDK should I use?
Thanks
Gianluca
I’m having a similar question like Olof above. Is it possible to read chunks and display the rendered waveform while buffering the mp3? I’ve experimented a bit with this and been trying the SoundLoaderContext but without any luck. If you have time I would be very grateful if you could give some hint.
This stuff is just a bit over my head, but it looks to me like Sound.extract() need to have the audio completely loaded before it can run – is that right? I’d like to make a visualizer that interprets streaming audio. The computeSpectrum() function doesn’t really seem to be ideal in that it fires when your timer fires, rather than based on the tempo of the song. Feel free to correct me if I’ wrong. Any guidance – pretend your guiding a 4-year-old – would be a huge help.
Thanks,
Josh
I totally love this and all your work. This is exactly what I need for a current project. Any one know how to do something similar with video from a loaded f4v?
Big thanks!!! I search some script full day! This is simple and good code!!!
For those who are struggling to understand what those ambiguous numbers meant in the source code, I’ve done my own reinterpretation with commented codes & variable names to it.
See: http://www.copypastecode.com/42860/
I found this code, maybe it helps…..http://gerrybeauregard.wordpress.com/2010/08/03/an-fft-in-as3/
Hi, I just downloaded the source and tried to compile and I get this:
1061: Call to a possibly undefined method extract through a reference with static type flash.media:Sound.
@carl welch: In the properties of your flash project, default player is Flash 9, Change it to 10, and everything will be great.
Me again.
This is so great, exaclty what I was looking for, thanks!
When I use one of my own audio files, it still needs some time to do the analysis. I wouldn’t need it as precise as it is at the moment, just very rough.
Do you have an idea to get the same (just less precise) result with less data being analyzed?
I have a joomla based website that is in real need for an audio player like Sound.extract(). I’m not a developer — just a tinkerer. What is the best way to get this tool working on my site.
Duane (whitcombmusic.com)
Great work and thanks a lot for the source.
Hi Thibault.
First of all, your example is very good and you really helpt me working with the extract()-method! (Personally I needed some more information, but Yanser – post 35 – documented it nicely)
I’m just wondering how I would make a waveform that is drawn ‘absolute’ instead of ‘relative’ to a certain waveform width?
example:
I have two clip; one of 4 sec and one of 8 sec.
When drawing those 2 waveform the length is almost identical.
What I like to get is that the length of the wave of 8 sec is double as long as the one of 4 sec?
Can you help me with this please? I’m making a audio-editor application for my school?
Kind regards
@Devaraj
Try http://www.sonoport.com – AS3 pitch shifting & time stretching libraries. Been very simple to use so far.
@Dieter : I am also looking for a similar kind of requirement. Where u able to complete the task?
Hi nice post is there a way to combine
multiple sound data like mixing them
and save them to hard drive ?
Hi,
your implementation of waveform and control is excellent.
Can you share the action script & mxml
can you provide flex version of this
Awesome article Thibault. I decided to contribute a bit to the topic having beginner programmers in mind. Check my article: http://www.marinbezhanov.com/web-development/14/actionscript-3-sound-extract-demystified-or-how-to-draw-a-waveform-in-flash/
I know, this post is soooooo old.
But i’ve found an issue.
If the file is shorter than one second, the extract method return 0.
C’est fâcheux…
Hello all, i am new at bytearray. I am building an application for sound mixing. when the user upload a mp3 file the application check that weather the file is clipped or not clipped, a clipped sound file are those which touch odb again and again. have you guys any idea how to achieve this in Flash as3.
Trackbacks/Pingbacks (6)
[...] came across Using Sound.extract() in FP10 which is damn [...]
[...] > Rendering spectrums with Sound.extract() [ by Thibault Imbert ] < ByteArray.org [...]
[...] to the Summit Projects Flash Blog and Thibault Imbert at ByteArray for their comments and input on the different techniques that went into my [...]
[...] sound.extract(); This can take the sound object and turn it into a byteArray. I came across an example by Thibault Imbert and of course the example worked very well to visualize a sound spectrum from a byte array – [...]
[...] Sound.extract() ( http://www.bytearray.org/?p=329): apparently with flashplayer 10 it is possible to analyze sounds before they are [...]
[...] jetzt hab ich doch glatt was gefunden: Rendering spectrums with Sound.extract() – ByteArray.org Da wird alles beim Laden ausgelesen. Juhuuu! __________________ -> Showcase | Nonverblog [...]