Microphone activityLevel in Flash
I wanted to analyse the frequencies of the microphone input in Flash, but guess what? You can’t!
With streaming sound it’s no problem, as this tutorial shows. But surprise: the microphone input is not accessible so you can’t respond to specific frequencies in you live sound input. Crap.
Anyway, after discovering this I set out to simply use the volume of the input, which is represented in the activityLevel property of the Microphone class. When activity is registered (you can set a threshold volume above which it does so), the ActivityEvent.ACTIVITY is dispatched, so you can respond to that.
Get ready for my next surprise: I only see the event being dispatched when my security settings panel is open, and then only when the microphone tab is active! Wtf is going on here?!
After battling with this, and trying out different possible solutions, it turns out that to make it work, you need to have set the loopback setting to true (microphone.setLoopBack(true)). This means that the microphone input will be played back over your speakers.
Needless to say you won’t always want this to happen. In fact, you probably nĂ©ver want this to happen (think of the feedback!), but apparently you have to in order to be able to read the input volume at all.
Luckily the solution is fairly straightforward: set the soundTransform property of the microphone to a SoundTransform instance with volume zero and presto: no mic sound playing back through your speakers, but an ActivityEvent.ACTIVITY event triggering nonetheless.
Good article, I’m trying to analise the frequense of microphone too.
Check this forum to see my progress[pt]:
http://flashmasters.net/forum/index.php?topic=49403.0
If you have some sources, i will apreciate to contribute to this.
Thanks,
Vamoss
Honestly, I haven’t implemented microphone input frequency analysis, but it can be done by using the Flash Media Server (if money is of no importance in your life) or an open source server such as Red5, and write your own service to pass back frequency information. That way you can have the server do the analysis and minimize the amount of data to be sent over the line.
So I have recently been on a search to do the same. That is to have a mic input and display graphics with no sound output.And have succeeded until the last bit,I tryed setting the soundTransform property to zero.
Heres what Ive got sofar
m = Microphone.get();
attachAudio(m);
microphone.gain = 50;
mic.rate =12;
mic.setUseEchoSuppression(true);
mic.setLoopBack(true);
mic.setSilenceLevel(100);
soundTransform = mic.SoundTransform;
trans.volume = 0;
mic.soundTransform = trans;
onEnterFrame = function () {
circle._xscale = circle._yscale = m.activityLevel+10;
};
I was wondering if you could could help me out and tell me what went wrong.Thanks much
Tj New
It seems to me that the first thing that could go wrong is that you’re using Microphone.get(); instead of Microphone.getMicrophone();
In the AS3 language reference (http://help.adobe.com/en_US/AS3LCR/Flash_10.0/index.html) you can find a basic connection example at the bottom of the page.
I used this code to start up the microphone:
mic = Microphone.getMicrophone();
if (mic != null)
{
mic.addEventListener(StatusEvent.STATUS, handleStatus);
if (!mic.muted)
{
connectMic();
}
else
{
Security.showSettings(SecurityPanel.PRIVACY);
}
}
The handleStatus() and connectMic() methods look like this:
private function handleStatus(event : StatusEvent) : void
{
if (event.code == “Microphone.Unmuted”)
{
mic.removeEventListener(StatusEvent.STATUS, handleStatus);
connectMic();
}
}
private function connectMic() : void
{
mic.setLoopBack(true);
mic.soundTransform = new SoundTransform(0, 0);
}
This should do the trick. I think your soundTransform may not work because you’re retrieving mic.SoundTransform and not mic.soundTransform (lowercase ’s’). But maybe the mic doesn’t even have a soundTransform instance here just yet. I simply create a new one and assign it to the microphone instead of trying to adjust the existing one. Work like a charm
Furthermore, the language reference tells you the following: “To prevent the microphone from detecting sound at all, pass a value of 100 for silenceLevel ; the activity event is never dispatched.”
I can see you are setting the silenceLevel to 100, which would prevent any response.
I think with these changes you should be able to fix it.
Good luck!
How counterintuitive. I don’t see any mention of setLoopback as applied to activityLevel in the Adobe docs. I was checking activityLevel and getting -1, and the docs simply said that the microphone wasn’t available because I hadn’t called getMicrophone() yet…how did I get the microphone instance to check the activity level then?!…ridiculous.
Anyway, thank you very much for posting this information.
Thanks for this. I ran into the same issue yesterday. I wonder if it’s a security issue – setLoopBack triggers the security warning for microphone use. Maybe Flash doesn’t want to risk being used to detect the activityLevel for users who haven’t yet accepted the security dialog.
If it were actually a question of security (which I can well imagine), than I would expect to see the security warning trying to connect to the mic rather than when I set the loopback property.
Which is exactly what happens by the way, so why the necessity of using loopback to be able to read the volume? It’s just weird…
Glad I searched for this one instead of banging my head against the wall–I was thinking about the old way to silence audio ( new Sound( instance ) ). Now what I can’t seem to figure out is how to ascertain which mic the user currently has selected. That is, if the user selects their second mic and then visits your application I can’t find a way to show selected (in my combo box listing all mics) their current mic selection.
My current hack is to just set their mic to the first index–which is annoying to the user who may have already selected the second mic.
Any clue? Thanks!