Solved

Class processing eficiency in playing a sound

Posted on 2011-03-16
4
411 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
scripting, exchange 35 47
How can I do this in Pyhton? 12 117
Convert MSI to MSM 1 49
Control Number of Log Files -Perl 7 56
If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

864 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

21 Experts available now in Live!

Get 1:1 Help Now