13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with sound<br />

}<br />

g.<strong>en</strong>dFill();<br />

// right channel<br />

g.lineStyle(0, 0xCC0066);<br />

g.beginFill(0xCC0066, 0.5);<br />

g.moveTo(CHANNEL_LENGTH * 2, PLOT_HEIGHT);<br />

for (i = CHANNEL_LENGTH; i > 0; i--)<br />

{<br />

n = (bytes.readFloat() * PLOT_HEIGHT);<br />

g.lineTo(i * 2, PLOT_HEIGHT - n);<br />

}<br />

g.lineTo(0, PLOT_HEIGHT);<br />

g.<strong>en</strong>dFill();<br />

function onPlaybackComplete(ev<strong>en</strong>t:Ev<strong>en</strong>t)<br />

{<br />

removeEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.ENTER_FRAME, onEnterFrame);<br />

}<br />

This example first loads and plays a sound file and th<strong>en</strong> list<strong>en</strong>s for the Ev<strong>en</strong>t.ENTER_FRAME ev<strong>en</strong>t which will trigger<br />

the onEnterFrame() method while the sound plays. The onEnterFrame() method starts by calling the<br />

SoundMixer.computeSpectrum() method, which stores the sound wave data in the bytes ByteArray object.<br />

The sound waveform is plotted using the vector drawing API. A for loop cycles through the first 256 data values,<br />

repres<strong>en</strong>ting the left stereo channel, and draws a line from each point to the next using the Graphics.lineTo()<br />

method. A second for loop cycles through the next set of 256 values, plotting them in reverse order this time, from<br />

right to left. The resulting waveform plots can produce an interesting mirror-image effect, as shown in the following<br />

image.<br />

Last updated 6/6/2012<br />

460

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!