Link to home
Start Free TrialLog in
Avatar of C_Schlottfeldt
C_Schlottfeldt

asked on

Class processing eficiency in playing a sound

Hello,

I would like to ask you what you think of the efficiency of the AS3 class below. I needed to produce a beeping sound  and I acquired this class from somewhere. The thing is: there´s a loop of 8192 iterations and I have no Idea if that consumes more processing power than it´s needed for this simple task! I´m concerned about this because this project is related to milliseconds presentations, and sometimes i feel that the sound flickers a little...

Thanks!
package components
{
	import flash.events.*;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.utils.Timer;


	public class beep
	{
		private const AMP_MULTIPLIER:Number = 0.15;
		private const BASE_FREQ:int = 440;
		private const SAMPLING_RATE:int = 44100;
		private const TWO_PI:Number = 2 * Math.PI;
		private const TWO_PI_OVER_SR:Number = TWO_PI / SAMPLING_RATE;
		private var barulho:Sound = new Sound();
		private var channel:SoundChannel = new SoundChannel();
		private var msegundos:Number = 500;
		private var dur_som:Timer = new Timer(msegundos,1);//500 milisec



		public function beepNow(mseg:Number):void
		{
			// constructor code
			trace("beep");
			msegundos = mseg;
			barulho.addEventListener(SampleDataEvent.SAMPLE_DATA,sineWave1);
			channel = barulho.play();
			dur_som.addEventListener(TimerEvent.TIMER_COMPLETE, TheEnd);
			dur_som.start();
		}

		private function sineWave1(event:SampleDataEvent):void
		{
			var sample:Number;
			for (var i:int=0; i<8192; i++)
			{
				sample = Math.sin((i+event.position) * TWO_PI_OVER_SR * BASE_FREQ);
				event.data.writeFloat(sample * AMP_MULTIPLIER);
				event.data.writeFloat(sample * AMP_MULTIPLIER);
			}
		}
		private function TheEnd(event:TimerEvent):void
		{
			channel.stop();
			trace("Beep playied" + event.target.currentCount * msegundos + " milisec");
			dur_som.reset();
		}
	}

}

Open in new window

Avatar of dgofman
dgofman
Flag of United States of America image

do you want to play beep longer or what?
Avatar of C_Schlottfeldt
C_Schlottfeldt

ASKER

No, the beep should play for a small fixed time, 100miliseconds should be fine, but in that Fixed time and without delay or flicker....
ASKER CERTIFIED SOLUTION
Avatar of dgofman
dgofman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of TommySzalapski
TommySzalapski
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial