Skip to main content
Inspiring
January 28, 2021
Question

problem with loop Sound and a class

  • January 28, 2021
  • 9 replies
  • 588 views

Good Morning. I'm working with a class made by a professional actionscript sound specialist (Andre Michelle).
The class loads a sound, changes its pitch, and plays it.
I want it to loop. I have added the (0, int.MAX_VALUE) in the play method but it only plays once.

Thank you very much for the help.

package {
	
	import flash.display.MovieClip;
	import flash.events.Event;
    import flash.events.SampleDataEvent;
    import flash.media.Sound;
	import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;
	import flash.events.MouseEvent;
	
	
	public class Bucle extends Sound 
{
        private const BLOCK_SIZE: int = 3072;

        private var _mp3: Sound;
        private var _sound: Sound;
		private var _myChannel :SoundChannel;

        private var _target: ByteArray;

        private var _position: Number;
        private var _rate: Number;

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

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

            _position = 0.0;
            _rate = 1.0;
			_myChannel = new SoundChannel ();
            _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
        {
			_myChannel = _sound.play (0, int.MAX_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 >= 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;
        }
    }
}
    This topic has been closed for replies.

    9 replies

    kglad
    Community Expert
    Community Expert
    January 28, 2021

    in complete() add a _mychannel soundcomplete listener that calls complete().

    Inspiring
    January 28, 2021

    Hi. Thank you very much for the reply. I have put what you told me. In complete I have put this:

     

            private function complete( event: Event 😞 void
            {
                _myChannel = _sound.play(0, int.MAX_VALUE);
    			_myChannel.addEventListener( Event.SOUND_COMPLETE, complete );
            }

     

    I put the complete code

     

    package
    {
        import flash.events.Event;
        import flash.events.SampleDataEvent;
        import flash.media.Sound;
        import flash.net.URLRequest;
        import flash.utils.ByteArray;
        import flash.media.SoundChannel;
    
        /**
         * @7111211 Andre Michelle (andre.michelle@gmail.com)
         */
        public class Bucle 
        {
            private const BLOCK_SIZE: int = 3072;
    
            private var _mp3: Sound;
            private var _sound: Sound;
    		private var _myChannel:SoundChannel;
    
            private var _target: ByteArray;
    
            private var _position: Number;
            private var _rate: Number;
    
            public function Bucle( 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();
    			_myChannel = new SoundChannel ();
                _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
            {
                _myChannel = _sound.play(0, int.MAX_VALUE);
    			_myChannel.addEventListener( Event.SOUND_COMPLETE, complete );
            }
    
            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;
            }
        }
    }

     

    and to call the class:

     

    Thanks for the attempt

     

     

    var pepe:Bucle = new Bucle ("ViolaGround.mp3");
    pepe.rate += 0.3;

     

    But, as before, it only sounds once.

     

    kglad
    Community Expert
    Community Expert
    January 28, 2021

    check if complete is being called the first time.

     

    (and you have a lot of code errors possible because the forum is mangling your code.)