Solved

Class processing eficiency in playing a sound

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

695 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