user3851217
user3851217

Reputation: 15

How to control sound speed AND volume in AS3?

I need to control the sound play speed so I extract sample data from the sound file but how can I control the volume then as SoundTranform.volume has no effect?

private function onSampleData(event:SampleDataEvent):void
    {
        var l:Number;
        var r:Number;

        var outputLength:int = 0;
        while (outputLength < 2048)
        { 
            _loadedMP3Samples.position = int(_phase) * 8; // 4 bytes per float and two channels so the actual position in the ByteArray is a factor of 8 bigger than the phase
            l = _loadedMP3Samples.readFloat(); // read out the left and right channels at this position
            r = _loadedMP3Samples.readFloat(); // read out the left and right channels at this position
            event.data.writeFloat(l); // write the samples to our output buffer
            event.data.writeFloat(r); // write the samples to our output buffer
            outputLength++;
            _phase += _playbackSpeed;
            if (_phase < 0)
                _phase += _numSamples;
            else if (_phase >= _numSamples)
                _phase -= _numSamples;
        }
    }

Upvotes: 0

Views: 588

Answers (1)

Andrew G
Andrew G

Reputation: 2614

Volume:

use say var volume: Number = 1.0 as a field variable. 0.0 for mute, 1.0 for original volume. Alter in other methods. However tweening this variable will be appreciated by listeners.

event.data.writeFloat(volume * l);

event.data.writeFloat(volume * r);

Speed:

You have to resample and use interpolation to define the intermediate values.

It's mathematically involved, but I'm sure there's a ton of libraries that can do this for you. But hey, here's a tutorial that tells you how apparently:

http://www.kelvinluck.com/2008/11/first-steps-with-flash-10-audio-programming/

Edit: Oh, You used this tutorial... You could have said.

modify _playbackSpeed. 1.0 is full speed. 2.0 is double speed.

Upvotes: 1

Related Questions