Solved

Class processing eficiency in playing a sound

Posted on 2011-03-16
4
416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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