• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

cambiar el pitch de un sonido para una app con as3

Contributor ,
Dec 02, 2020 Dec 02, 2020

Copy link to clipboard

Copied

Buenos días. Ando desarrollando una app de música y necesito un código cambie la afinación (lo que es la altura o el pitch) de un sonido. Parece que aquí hay un código que hace eso, pero como as3 no es mi fuerte, agradezco cualquier ayuda, ya que es lo único que me hace falta para lanzar mi app. Necesito:

 

Saber dónde exactamente coloco el código y...

 

cómo relaciono este códico con o el o los audios (están en mp3) los cuales quiero cambiar el pitch. vamos, que en qué sitio del código hay que poner el nombre de archivo.

 

envío dos opciones de código

 

Muchísimas gracias de antemano

el primero es más corto:

var source:Sound = …;
var output:Sound = new Sound();

var soundTouch:SoundTouch = new SoundTouch();
soundTouch.pitchSemitones = -6;

var filter:SimpleFilter = new SimpleFilter(sound, soundTouch);
output.addEventListener(SampleDataEvent.SAMPLE_DATA, filter.handleSampleData);

output.play();

 

el segundo más largo

 

package components
{
    import flash.events.Event;
    import flash.events.SampleDataEvent;
    import flash.media.Sound;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;

    /**
     * @author Andre Michelle (andre.michelle@gmail.com)
     */
    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;

        public function MP3Pitch( url: String )
        {
            _target = new ByteArray();

            _mp3 = new Sound();
            _mp3.addEventListener( Event.COMPLETE, complete );
            _mp3.load( new URLRequest( url ) );

            _position = 0.0;
            _rate = 1.0;

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

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

        public function set rate( value: Number 😞 void
        {
            if( value < 0.0 )
                value = 0;

            _rate = value;
        }

        private function complete( event: Event 😞 void
        {
            _sound.play();
        }

        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 >= 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;
        }
    }
}

 

 

esta es otra opcion de código

Views

109

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 03, 2020 Dec 03, 2020

Copy link to clipboard

Copied

LATEST

there's more to the first sniipet but the second snippet looks complete and ready to accept mp3's.  what's the problem?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines