I'm trying to create a parametric equalizer in Flash. I've been looking for a way to read audio data and mess around with the samples before Flash plays them on the fly. Loading a sound in one Sound object and using Sound.extract() to read the data, processing it, then play a second second empty Sound object and writing the data to its sampleData event seems to be the way to do it (please correct me if I am wrong or there is a better way).
Is there a way to use Sound.extract() while the Sound object is still loading a sound file? I don't want to have to wait for the entire sound file to load before it plays. Unfortunately, whenever I use Sound.extract() while the Sound object is still loading, it returns a zero-length byte array.
Is there a way to wait for enough samples to load first before playing? I imagine I'd have the same problem again when the Flash movie eats through all the loaded samples while the sound file is still loading.
Here's a simplified version of my code. It's working so far, but only when I wait for the Sound object to fire an Event.COMPLETE event.
var inputSound:Sound = new Sound();
inputSound.load("somefile.mp3");
inputSound.addEventListener(Event.COMPLETE, loadComplete);
var outputSound:Sound = new Sound();
outputSound.addEventListener(SampleDataEvent.SAMPLE_DATA, processSamples);
var sc:SoundChannel;
/*if I called ouputSound.play() right now, it wouldn't work.*/
function loadComplete(e:Event) : void
{
sc = outputSound.play();
}
function processSamples(e:SampleDataEvent) : void
{
var samples:ByteArray = new ByteArray();
var len:int = snd.extract(samples, 8192);
var sample:Number;
var i:int = 0;
trace(len.toString());
samples.position = 0;
//TODO: Sound Processing here
//The following code plays a sine wave over the input sound as a test
while (samples.bytesAvailable)
{
i++;
sample = samples.readFloat();
sample += Math.sin(i * Math.PI / 256) * 0.5;
e.data.writeFloat(sample);
sample = samples.readFloat();
sample += Math.sin(i * Math.PI / 256) * 0.5;
e.data.writeFloat(sample);
}
}
EDIT: If I try using the PROGRESS event, I'm going to need to do a lot more low level stuff to implement buffering and whatnot (anything else I need to account for?). Could someone help me out with that? Also, is there a way to tell the position of a sample in milliseconds? Do I have to assume that all sound files are 44.1 kHz stereo (they may not be), or is there a better way?