Saturday, 15 October 2011

Microphone class in Actionscript 3

The Microphone class in Actionscript 3 is one of the few classes that I rarely use, so I thought I would write a post about it. If you take a quick look at the documentation for the Microphone class, you can see that the class is very small and simple. The main functionality of this class is the activity level of the microphone which is basically the amount of sound that is detected. The activity level ranges from 0 (no sound) to 100 (maximum sound).

This example uses the enter frame event to display the current activity level. I have used a vector image of a car to visualise the activity level, so the louder sound the further the car will move across the stage area.

//Instance of the microphone class
var microphone:Microphone = Microphone.getMicrophone();

//Increase the amplification of the sound
microphone.gain = 50;

//Set the microphone's input to loop back through the speaker, so the 
//activity level can be registered
microphone.setLoopBack(true);

//Add an enter frame event to the stage
addEventListener(Event.ENTER_FRAME, ef);

var distance:int = (stage.stageWidth-car.width) / 100;

function ef(e:Event):void{
car.x = Math.abs(distance * microphone.activityLevel); 
}



Another way to check the activity level is to use the ActivityLevel event from the Microphone class. The ActivityLevel event has a Boolean value called ‘activating’ that only dispatch an event when the sound has reached a certain threshold. To set the threshold you use the setSilenceLevel() method. This method has two parameters the first parameter is the threshold of the activity level, so when the activity level goes over the threshold the Activity event will fire. The second parameter is the timeout value which will ignore any activity events for this specified amount of time.

This example uses the Activity level event to change the frame number of the movie clip when the threshold is above 50.

//Instance of the micophone class
var microphone:Microphone = Microphone.getMicrophone();

//Set the activity level threshold to 50 and the delay to 1 second.
microphone.setSilenceLevel(50, 500);

//Increase the amplication of the sound
microphone.gain = 50;

//Set the microphone's input to loop back through the speaker, so the 
//activity level can be registered
microphone.setLoopBack(true);

//Add activity event to the micrphone
microphone.addEventListener(ActivityEvent.ACTIVITY, activityHandler);

function activityHandler(e:ActivityEvent):void{
 
 if(e.activating){
  circle_mc.gotoAndStop(2);
 }else{
  circle_mc.gotoAndStop(1);
 }
} 

4 comments:

nolimit966 10 January 2012 at 18:55  

I have re-drawn a vector car on the stage and named it 'car' in the movieclip properties but i cant work out what is distance is in your actionscript code? is it a variable name with an assigned number?
Thanks

iliketo 11 January 2012 at 04:03  

@nolimit996

It's the line of code underneath the enter frame listener.

Emma Taylor 9 September 2012 at 23:51  

With the car example, how would you make it so the car continues to move along the x-axis, instead of resetting to the start when there is no activity level.

iliketo 11 September 2012 at 11:37  

@Emma,

Look at the line of code inside the ef() function?

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP