Copy link to clipboard
Copied
I HAVE 2 questions!
1. How do I trigger sound when microphone detects high volume input in AS3?
2. Currently, I have this codes as follows that lowers the volume, as microphone activity increase. However, the the change is not gradual and smooth. What am I doing wrong?
package
{
import flash.display.Sprite;
import flash.events.ActivityEvent;
import flash.events.TimerEvent;
import flash.media.Microphone;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.utils.Timer;
import flash.media.SoundTransform;
public class Main extends Sprite
{
var mic:Microphone;
var myTimer:Timer = new Timer(100);
var volumeLevel:int = 10;
var audioFile:Sound = new Rollindeep();
var chan:SoundChannel;
var sndtranform:SoundTransform;
public function Main() //this is the constructor
{
//trace("Hello World"); //test to see if it runs
//var jojo:Audio;
mic = Microphone.getMicrophone(); //to get computer microphone
mic.gain = 60;
mic.rate = 11;
mic.setUseEchoSuppression(true);
mic.setLoopBack(true);
mic.setSilenceLevel(5,1000);
var transform1:SoundTransform = mic.soundTransform;
transform1.volume = 0;
mic.soundTransform = transform1;
mic.setSilenceLevel(10);
mic.addEventListener(ActivityEvent.ACTIVITY, micLevel);
myTimer.addEventListener(TimerEvent.TIMER,test);
myTimer.start();
chan = new SoundChannel();
sndtranform = new SoundTransform();
chan = audioFile.play();
}
private var maxVolumeChange = 0.05;
public function test(e:TimerEvent) {
//trace ("mic level "+mic.activityLevel);
//trace ("music vol "+sndtranform.volume);
var newVolume = (20-mic.activityLevel)/10;
var volumeChange = sndtranform.volume - newVolume;
/*
if(volumeChange < 0) {
if(volumeChange < -1*maxVolumeChange) {
newVolume = -1*maxVolumeChange;
}
} else {
if(volumeChange > maxVolumeChange) {
newVolume = maxVolumeChange;
}
}
*/
sndtranform.volume = newVolume;
//trace("TEST: "+mic.activityLevel);
chan.soundTransform = sndtranform;
trace(sndtranform.volume+"AUDIOLEVEL");
trace(mic.activityLevel+"MIC LEVEL");
}
public function micLevel(e:ActivityEvent) {
trace(mic.activityLevel);
trace("blah");
}
}
}
Copy link to clipboard
Copied
1. use a loop and conditional to check mic.activityLevel or you mic.soundTransform.volume
2. your newVolume should be the current volume (sndtranform) minus maxVolumeChange. check if the current volume is where you want it to terminate the quietting loop.