problem with loop Sound and a class

Explorer ,
Jan 27, 2021 Jan 27, 2021

Copy link to clipboard

Copied

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

Views

154

Likes

translate

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
Adobe Community Professional ,
Jan 28, 2021 Jan 28, 2021

Copy link to clipboard

Copied

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

Likes

translate

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
Explorer ,
Jan 28, 2021 Jan 28, 2021

Copy link to clipboard

Copied

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;

    /**
     * @author 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.

 

Likes

translate

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
Adobe Community Professional ,
Jan 28, 2021 Jan 28, 2021

Copy link to clipboard

Copied

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.)

Likes

translate

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
Adobe Community Professional ,
Jan 28, 2021 Jan 28, 2021

Copy link to clipboard

Copied

looks like andre has the same issue in both these files, Solved: SoundChannel event.sound_complete not working? hel... - Adobe Support Community - 2538295

 

use:

 

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;

	/**
	 * @author 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, completeF);
			_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 completeF(event: Event): void {
		
			_mp3.addEventListener(SampleDataEvent.SAMPLE_DATA, sampleData);
			_myChannel = _mp3.play(0, int.MAX_VALUE);
			_myChannel.addEventListener(Event.SOUND_COMPLETE, completeF);
		}

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

Likes

translate

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
Explorer ,
Jan 29, 2021 Jan 29, 2021

Copy link to clipboard

Copied

Hi. Thank you very much for the answer. I have tested the code and it works !! I see that it doesn't use the _sound variable by substituting it for the __mp3 variable.

But other problems arise:
- I cannot change the pitch of the sounds by invoking the .rate method in my instance. I have tried to change the _mp3 and _sound variables in your code again and the rate method works but does NOT repeat. I've made a lot of changes trying to fix it but I can't.

and another thing
- I would like to be able to control the number of times the sound is repeated. This is very important for my application since in the trial version it will not be repeated infinitely but will be repeated 3 or 4 times.

Thanks so much for the help!!!

Likes

translate

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
Adobe Community Professional ,
Jan 30, 2021 Jan 30, 2021

Copy link to clipboard

Copied

you need to think about your design.  do you want to fix the repeats to 3 (or 4) for every sound?

 

do you want to change the pitch during the repeat sequence?  do you want a stop (the sound) button?  do you want a stop the repeats button? etc

Likes

translate

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
Explorer ,
Jan 31, 2021 Jan 31, 2021

Copy link to clipboard

Copied

Hi there!. What I want to achieve is the following:
- The BotonViola MovieClip turns on a "ViolaGround.mp3" sound (a sound with a different pitch than the original sound) and turns off the "TiorbaGround.mp3" sound.
- The MovieClip BotonTiorba turns on a "TiorbaGround.mp3" sound (a sound with a different pitch from the original sound) and turns off the "ViolaGround.mp3" sound.
- Both sounds have a slider to modify the volume.
- I need two possibilities: the sound loops 4 times and another that the sound loops indefinitely.

Working with Mitchell's code, I've accomplished all of this, but it only sounds once: when I put two sound variables in the code (_sound and _mp3), the pitch change is done perfectly, but it only sounds once. When I put in the code a sound variable (_mp3) the sound loops indefinitely but does not carry out the transport.

 

In the link you sent me
https://community.adobe.com/t5/animate/soundchannel-event-sound-complete-not-working-help/td-p/25382...

bennos89 had the same problem as me, the loop worked fine with one sound variable and the speed (and pitch) change with two sound variables

 

I have modified Mitchell's code a bit: I load the sound and turn it on from the actions panel. I do this so that I can have the soundChannel class in the action panel and be able to relate the sound to my slider. To stop the music I use the StopAll method (using Stop related to SoundChannel works for me too). I send my code from the action panel. I do not put the piece of the slider, I think it is not necessary.:

function activarSonidoViola(e:MouseEvent):void{
SoundMixer.stopAll();
viola = new MP3Pitch1();
viola.rate +=0.5
viola._req = new URLRequest ("ViolaGround.mp3");
viola._mp3.load(viola._req);
myChannelViola = viola._sound.play(0, int.MAX_VALUE);
BotonViola.removeEventListener (MouseEvent.CLICK, activarSonidoViola);
BotonTiorba.addEventListener(MouseEvent.CLICK, activarSonidoTiorba);
}


function activarSonidoTiorba(e:MouseEvent):void{
SoundMixer.stopAll();
var tiorba:MP3Pitch1 = new MP3Pitch1 ();
tiorba._req = new URLRequest ("TiorbaGround.mp3");
tiorba._mp3.load(tiorba._req);
tiorba.rate +=0.4
myChannelTiorba = tiorba._sound.play(0,int.MAX_VALUE);
BotonTiorba.removeEventListener (MouseEvent.CLICK, activarSonidoTiorba);
BotonViola.addEventListener(MouseEvent.CLICK, activarSonidoViola);
}
BotonViola.addEventListener(MouseEvent.CLICK, activarSonidoViola);
BotonTiorba.addEventListener(MouseEvent.CLICK, activarSonidoTiorba);

 

 I am not sending the Mitchell code because it is not redundant.

 

Thanks for the help

Likes

translate

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
Explorer ,
Jan 31, 2021 Jan 31, 2021

Copy link to clipboard

Copied

I forgot the beginning of my action panel code, sorry. MP3Pitch1 is the name of my class.

var myChannelViola:SoundChannel = new SoundChannel();
var viola:MP3Pitch1;

 

Likes

translate

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
Adobe Community Professional ,
Feb 01, 2021 Feb 01, 2021

Copy link to clipboard

Copied

i'm busy right now with other projects, and while i don't think it would take more than an hour or two to do you what you want, that's more time than i usually offer unless i'm hired.

 

others may help for free, if you wait.  or, if you want to hire me, send a private message.

Likes

translate

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