0

所以基本上我有一个用 Flash CS5 制作的音板,是否可以仅使用 Flash 来交替库的音频文件的声音?就像让剪辑听起来更深或更快,这就是重点。但如果不是小便

4

1 回答 1

0

你不能在 AS2 中做到这一点。但是是的,您可以在 AS3 中更改声音的音高和播放速度。事实上,你可以做的不止这些(搜索 Audiotool),但这展示了它是如何完成的:http: //plasticsturgeon.com/2012/05/changeing-the-pitch-of-a-looped-sound-at-runtime -with-actionscript/

如果我的博客出现故障或其他情况,这里是改变声音音高的相关类:

    package sound
{
    import flash.events.Event;
    import flash.events.SampleDataEvent;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.media.SoundTransform;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;

/**
 * @author Andre Michelle (andr...@gmail.com)
     * Modified by Zach Foley aka The Plastic Sturgeon
 */
public class MP3Pitch
{
    private const BLOCK_SIZE: int = 3072;

    private var _mp3: Sound;
    private var _sound: Sound;

    private var _target: ByteArray;

    private var _position: Number;
    private var _rate: Number;
    private var repeat:SoundChannel;
    private var _volume:Number = 1;
    private var byteArray:ByteArray;

            // Pass in your looped Sound
    public function MP3Pitch( pitchedSound: Sound)
    {
        _target = new ByteArray();
        _mp3 =  pitchedSound;

        _position = 0.0;
        _rate = 0.0;

        _sound = new Sound();
        _sound.addEventListener( SampleDataEvent.SAMPLE_DATA, sampleData );
        repeat = _sound.play();
    }

    public function get rate(): Number
    {
        return _rate;
    }

            // Also added a handy volume setter
    public function set volume( value: Number ): void
    {
        _volume = value;
        repeat.soundTransform = new SoundTransform(_volume);
    }

            // use this to set the pitch of your sound
    public function set rate( value: Number ): void
    {
        if( value < 0.0 )
            value = 0;

        _rate =  value;
    }

    private function sampleData( event: SampleDataEvent ): void
    {
        //-- REUSE INSTEAD OF RECREATION
        _target.position = 0;

        //-- SHORTCUT
        var data: ByteArray = event.data;

        var scaledBlockSize: Number = BLOCK_SIZE * _rate;
        var positionInt: int = _position;
        var alpha: Number = _position - positionInt;

        var positionTargetNum: Number = alpha;
        var positionTargetInt: int = -1;

        //-- COMPUTE NUMBER OF SAMPLES NEED TO PROCESS BLOCK (+2 FOR INTERPOLATION)
        var need: int = Math.ceil( scaledBlockSize ) + 2;

        //-- EXTRACT SAMPLES
        var read: int = _mp3.extract( _target, need, positionInt );

        var n: int = read == need ? BLOCK_SIZE : read / _rate;

        var l0: Number;
        var r0: Number;
        var l1: Number;
        var r1: Number;

        for( var i: int = 0 ; i < n ; ++i )
        {
            //-- AVOID READING EQUAL SAMPLES, IF RATE < 1.0
            if( int( positionTargetNum ) != positionTargetInt )
            {
                positionTargetInt = positionTargetNum;

                //-- SET TARGET READ POSITION
                _target.position = positionTargetInt << 3;                      //-- READ TWO STEREO SAMPLES FOR LINEAR INTERPOLATION                   l0 = _target.readFloat();                   r0 = _target.readFloat();                   l1 = _target.readFloat();                   r1 = _target.readFloat();               }                               //-- WRITE INTERPOLATED AMPLITUDES INTO STREAM              data.writeFloat( l0 + alpha * ( l1 - l0 ) );                data.writeFloat( r0 + alpha * ( r1 - r0 ) );                                //-- INCREASE TARGET POSITION               positionTargetNum += _rate;                                 //-- INCREASE FRACTION AND CLAMP BETWEEN 0 AND 1                alpha += _rate;                 while( alpha &gt;= 1.0 ) --alpha;
        }

        //-- FILL REST OF STREAM WITH ZEROs
        if( i < BLOCK_SIZE )
        {
            while( i < BLOCK_SIZE )                 {                   data.writeFloat( 0.0 );                     data.writeFloat( 0.0 );                                         ++i;                }           }           //-- INCREASE SOUND POSITION            _position += scaledBlockSize;                        // My little addition here:            if (_position &gt; _mp3.length * 44.1) {
            _position = 0;
            _target.position = 0;
        }
    }
}
}
于 2013-01-16T22:10:29.593 回答