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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Working with sound<br />

var mySound:Sound = new Sound();<br />

mySound.addEv<strong>en</strong>tList<strong>en</strong>er(SampleDataEv<strong>en</strong>t.SAMPLE_DATA, sineWaveG<strong>en</strong>erator);<br />

mySound.play();<br />

function sineWaveG<strong>en</strong>erator(ev<strong>en</strong>t:SampleDataEv<strong>en</strong>t):void<br />

{<br />

for (var i:int = 0; i < 8192; i++)<br />

{<br />

var n:Number = Math.sin((i + ev<strong>en</strong>t.position) / Math.PI / 4);<br />

ev<strong>en</strong>t.data.writeFloat(n);<br />

ev<strong>en</strong>t.data.writeFloat(n);<br />

}<br />

}<br />

Wh<strong>en</strong> you call Sound.play(), the application starts calling your ev<strong>en</strong>t handler, requesting sound sample data. The<br />

application continues to s<strong>en</strong>d ev<strong>en</strong>ts as the sound plays back until you stop providing data, or until you call<br />

SoundChannel.stop().<br />

The lat<strong>en</strong>cy of the ev<strong>en</strong>t varies from platform to platform, and could change in future versions of Flash Player and AIR.<br />

Do not dep<strong>en</strong>d on a specific lat<strong>en</strong>cy; calculate it instead. To calculate the lat<strong>en</strong>cy, use the following formula:<br />

(SampleDataEv<strong>en</strong>t.position / 44.1) - SoundChannelObject.position<br />

Provide from 2048 through 8192 samples to the data property of the SampleDataEv<strong>en</strong>t object (for each call to the<br />

ev<strong>en</strong>t list<strong>en</strong>er). For best performance, provide as many samples as possible (up to 8192). The fewer samples you<br />

provide, the more likely it is that clicks and pops will occur during playback. This behavior can differ on various<br />

platforms and can occur in various situations—for example, wh<strong>en</strong> resizing the browser. Code that works on one<br />

platform wh<strong>en</strong> you provide only 2048 sample might not work as well wh<strong>en</strong> run on a differ<strong>en</strong>t platform. If you require<br />

the lowest lat<strong>en</strong>cy possible, consider making the amount of data user-selectable.<br />

If you provide fewer than 2048 samples (per call to the sampleData ev<strong>en</strong>t list<strong>en</strong>er), the application stops after playing<br />

the remaining samples. The SoundChannel object th<strong>en</strong> dispatches a SoundComplete ev<strong>en</strong>t.<br />

Modifying sound from mp3 data<br />

Flash Player 10 and later, Adobe AIR 1.5 and later<br />

You use the Sound.extract() method to extract data from a Sound object. You can use (and modify) that data to<br />

write to the dynamic stream of another Sound object for playback. For example, the following code uses the bytes of a<br />

loaded MP3 file and passes them through a filter function, upOctave():<br />

Last updated 6/6/2012<br />

449

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

Saved successfully!

Ooh no, something went wrong!