views:

20

answers:

1

I plan to play more than one MP3 at the same time like multi-track does, I know this will caused a huge increase in memory. Is there a way to stream or buffer live?

If I use it on creating a virtual piano, would it be feasible to embedded 88's or 500MB of MP3 for performance or dynamic load?

A: 

Hm...

Stream from files?

It would be possible, but I don't think it would be possible with AIR API... I guess a custom library would be usable here... Basically, you would have a class SoundPlayer, which would have methods addSound and removeSound.

Each time addSound is called, the path needed (id est the note and instrument needed) is used to initialize a FileStream as well as a callback method for a newly created Sound instance. The callback would read (buffer) something from the FileStream instance, for example, 8192 bytes, or the size of the buffer and write that to the Sound buffer.

removeSound would simply remove (close, etc...) the instances of both FileStream and Sound created by addSound.

The instances should be stored in some kind of array.

Also, please note that the dynamic-streaming version of Sound is VERY undocumented... But this is a sample code to understand how it works:

var mySound:Sound = new Sound(); // new one
mySound.addEventListener("sampleData", getData); // the callback event
var myChannel:SoundChannel = mySound.play(); // no arguments for play, channel

// the callback function - it is used to get the new sound buffer
function getData(e:SampleDataEvent):void {
    for (var wi:int = 0; wi < 8192; wi++){
        e.data.writeFloat(Math.random());
    }
}

This generates noise (random).

Here's a sawblade waveform. Just because it's fun: (warning: loud)

var subWi:Number = 0;
for (var wi:int = 0; wi < 8192; wi++){
    e.data.writeFloat(subWi);
    subWi += 0.01;
    if (subWi >= 1){
        subWi = 0;
    }
}

Note: with this code you can do a synth as well :D

Aurel300