Good morning!
I am trying to create a visual cue for MP3. The code I included is called when the MP3 is downloaded successfully. I intend to extract only a few important samples from the sound in order to create a waveform, rather than extract all the sound into a byte. Even on a good machine, extracting the entire song can cause the flash to freeze for 3-5 seconds (or longer!). For my purposes this is not possible.
Unfortunately, the code I received below does not give any numbers. If I extract the whole song in which it works, but extracting only the key points gives me nothing. Does the extract make the rest of the sound object invalid for future extracts? If so, is there any way around this that will not freeze the flash for an extended period of time during extraction?
Some important variables from the rest of the code:
waveFormWidth: The static width of the waveform sprite.
waveFormHeight: The static height of the waveform sprite.
song: sound object, which I will use to create the waveform.
public function mapWaveForm(e:Event = null):void
{
waveForm.graphics.clear();
waveForm.graphics.lineStyle(0, 0x000000, 1);
var byteOutput:ByteArray;
var leftPeakSize:Number;
var rightPeakSize:Number;
var songTotalSamples:int;
var thisSample:int;
byteOutput = new ByteArray();
songTotalSamples = (song.length * 44.1);
for (var peakCount:int = 0; (peakCount < waveFormWidth); peakCount++)
{
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;
leftPeakSize = leftPeakSize * (waveFormHeight * .5);
rightPeakSize = rightPeakSize * (waveFormHeight * .5);
waveForm.graphics.moveTo(peakCount, (waveFormHeight * .5));
waveForm.graphics.lineTo(peakCount, (waveFormHeight * .5) - leftPeakSize);
waveForm.graphics.moveTo(peakCount, (waveFormHeight * .5));
waveForm.graphics.lineTo(peakCount, (waveFormHeight * .5) + rightPeakSize);
}
}
Thanks for the help guys!