Cambiata
Cambiata

Reputation: 3845

Details about as3 Sound.extract() format?

Using the sound object .extract() method, it's possible to retrieve the uncompressed sound data as a ByteArray. What is the format of the extracted sound data? Custom internal format, or established format? Endian type?

I would like to be able to load sounddata directly into a byteArray and feed sound playback from this data (bypassing step 1 below).

Here are my experiments so far:

Step 1. Here I load a sound, extract the data content and save it to disc:

private var sound:Sound;
private var byteArray:ByteArray;

// loading a sound...
private function init():void {
    sound = new Sound();            
    sound.addEventListener(Event.COMPLETE, onComplete);
    sound.load(new URLRequest('short.mp3'));
}

// saving it to disc...
private function onComplete(e:Event):void {
    byteArray = new ByteArray();
    sound.extract(byteArray, 100000000);            
    byteArray.position = 0;
    var file:FileReference = new FileReference();
    file.save(byteArray, 'short.data');             
}

Step 2. Here I load the data from disc and feed the playback of a new sound object with this data:

private var sound:Sound;
private var byteArray:ByteArray;

// loading a sound...
private function init():void {

    file = new FileReference();
    file.addEventListener(Event.COMPLETE, onLoaded);
    file.addEventListener(Event.SELECT, onSelected);
    file.browse();

}

private function onSelected(e:Event):void {
    file.load();
}

private function onLoaded(e:Event):void {
    trace('loaded');
    trace(file.data.length);
    byteArray = file.data;

    sound2 = new Sound();
    sound2.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);     
    sound2.play();      
}   

private function onSampleData(e:SampleDataEvent):void {
    trace(e.position);
    for (var i:int = 0; i < 2048; i++) {
        var left:Number = byteArray.readFloat();
        var right:Number = byteArray.readFloat();               
        e.data.writeFloat(left * 0.2);
        e.data.writeFloat(right * 0.2);
    }
}

I would like to be able of accomplishing this second step (loading data from disc and feeding a sound object playback) by using an external converter utility (sox or something).

/ Jonas

Upvotes: 0

Views: 735

Answers (1)

Mikael Lindqvist
Mikael Lindqvist

Reputation: 797

It should be standard 32bit floats, one per channel per sample. So I would try -e floating-point for sox, I haven't tried though but I would do it with a certain level of confidence... :) Floats are endian-independent, I believe...

Upvotes: 1

Related Questions