Note that there are some explanatory texts on larger screens.

plurals
  1. POAS3 sound.extract() for waveform
    text
    copied!<p>Good morning!</p> <p>I'm trying to create a visual waveform for an MP3. The code I've included is called on successful load of the MP3. I intend to extract just a few important samples from the sound to create the waveform, rather than extract the entire sound into a bytearray. Even on a good machine, extracting an entire song can cause flash to freeze up for 3-5 seconds (or longer!). For my purposes, this isn't feasible.</p> <p>Unfortunately, the code I've got below is failing to produce any numbers. If I extract the entire song it functions, but extraction of just the key points is giving me nothing. Does performing an extract make the remainder of the sound object invalid for future extracts? If so, is there some way around this that won't freeze flash for an extended period of time during the extract?</p> <p>Some important variables from the rest of the code:</p> <p>waveFormWidth: static width of the wave form sprite.<br> waveFormHeight: static height of the wave form sprite.<br> song: sound object that I'll be using to create the wave form.</p> <pre><code> public function mapWaveForm(e:Event = null):void { // Clear the wave form sprite waveForm.graphics.clear(); waveForm.graphics.lineStyle(0, 0x000000, 1); // Storage for the wave form bit data var byteOutput:ByteArray; var leftPeakSize:Number; var rightPeakSize:Number; var songTotalSamples:int; var thisSample:int; byteOutput = new ByteArray(); // How many samples? songTotalSamples = (song.length * 44.1); // Loop for the wave form width for (var peakCount:int = 0; (peakCount &lt; waveFormWidth); peakCount++) { // Get info at each peak. thisSample = Math.floor(songTotalSamples * (peakCount / waveFormWidth)); song.extract(byteOutput, 1, thisSample); byteOutput.position = 0; trace(thisSample, byteOutput.readFloat()); leftPeakSize = byteOutput.readFloat() / 1.27; rightPeakSize = byteOutput.readFloat() / 1.27; // Turn those peaks into something usable. leftPeakSize = leftPeakSize * (waveFormHeight * .5); rightPeakSize = rightPeakSize * (waveFormHeight * .5); // Make the left channel line waveForm.graphics.moveTo(peakCount, (waveFormHeight * .5)); waveForm.graphics.lineTo(peakCount, (waveFormHeight * .5) - leftPeakSize); // Make the right channel line waveForm.graphics.moveTo(peakCount, (waveFormHeight * .5)); waveForm.graphics.lineTo(peakCount, (waveFormHeight * .5) + rightPeakSize); } } </code></pre> <p>Thanks for your help guys!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload