How do I play a generated waveform as a sound in Flash?
If I have a sound waveform stored as a ByteArray in actionscript 3, how would I go about converting this into a Sound object that can be p开发者_如何学JAVAlayed?
Note that the array is full of sound samples - a complete generated waveform. It isn't an array containing an mp3 or other compressed data.
I have written quite a few tutorials on sound synthesis in Flash, some of which are about wavetable synthesis.
The general concept is that you need to fill an audio buffer ( Sound object ) with audio data at a regular interval. A Sound object will dispatch an event when it needs audio data. The block of data can be anywhere between 2048 and 8192 samples. Below is some pseudo-code that might help you out. It will create an audio loop from the data in the array.
var readIndex:int = 0;
var data:Array = yourData;
var sound:Sound = new Sound()
sound.addEventListener( SampleDataEvent.SAMPLE_DATA, onSampleData );
sound.play();
function onSampleData( event:SampleDataEvent ):void
{
for( var i:int = 0; i < 2048; i++ )
{
if( readIndex + 1 > data.length )
{
readIndex = 0;
} else {
readIndex++;
}
event.data.writeFloat( data[i] );
event.data.writeFloat( data[i] );
}
}
As I mentioned, I've written many articles on working with sound in Flash. Here is one such article: http://labs.makemachine.net/2010/07/slice-tool-looper/
Well I don't really have the answer but I got also very interested in this problem as I read you thread and here is what I found. Sounds like a lot overhead but very interesting solution.
I hope this helps!
精彩评论