Solved

Class processing eficiency in playing a sound

Posted on 2011-03-16
4
410 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:C_Schlottfeldt
  • 2
4 Comments
 
LVL 29

Expert Comment

by:dgofman
ID: 35150500
do you want to play beep longer or what?
0
 

Author Comment

by:C_Schlottfeldt
ID: 35150862
No, the beep should play for a small fixed time, 100miliseconds should be fine, but in that Fixed time and without delay or flicker....
0
 
LVL 29

Accepted Solution

by:
dgofman earned 350 total points
ID: 35150996
The first problem if you will call you funtion beepNow multiple times you will add more than one event listeners. You should call removeEventListener or better in your case move to constructor

public function beep(){
    barulho.addEventListener(SampleDataEvent.SAMPLE_DATA,sineWave1);
    dur_som.addEventListener(TimerEvent.TIMER_COMPLETE, TheEnd);
}

Open in new window


I don't know why you added local a variable "msegundos"?

if you want to assign delay you can set in your timer.

public function beepNow(mseg:Number):void
{
	channel = barulho.play();
	dur_som.delay = mseg;
	dur_som.start();
}

Open in new window


And to get timer delay value use event property

private function TheEnd(event:TimerEvent):void
{
	channel.stop();
	trace("Beep playied" + event.target.currentCount * event.target.delay + " milisec");
	dur_som.reset();
}

Open in new window

0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 150 total points
ID: 35151353
8000 iterations is nothing. You have, what, a >2.5 gigahertz processor? That's microseconds.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
The viewer will learn how to count occurrences of each item in an array.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now