Question

I want to monitor the microphone audio input with flash ( as3 ).

This is just a tiny part of my code, but actually the problem is in there.

var mic:Microphone = Microphone.getMicrophone();
mic.setLoopBack(true);

addEventListener( Event.ENTER_FRAME, loop );

function loop( event:Event ):void {
    trace( mic.activityLevel );
}

If i use the code like it is, i can trace the activityLevel and actually can see some values.. ( i think it is the volume ? )

Well, the only problem is, that the audio is also outputed to the speakers, what i DON'T want... ( mic.setLoopBack(true); )

But when i try mic.setLoopBack(false);, flash doesn't ask for microphone premissions anymore and the traced activityLevel stays "-1".....

So what can i do to disable the audio loopback OR to just monitor the audio data from the mic. ?

( when i say "audio data", i mean all the data that is necessary for later BPM detection... i think it's an byte array of the audio, isn't it ? )

Was it helpful?

Solution 2

Instead of using setLoopBack(), you just need to listen for SampleDataEvent's from the Microphone. Note the section titled "Detecting Microphone Activity" in this Adobe article, and in particular this note that talks about ways you can listen for microphone activity:

Note: A Microphone object only dispatches Activity events when your application is monitoring the microphone. Thus, if you do not call setLoopBack( true ), add a listener for sample data events, or attach the microphone to a NetStream object, then no activity events are dispatched.

var mic:Microhpone = Microphone.getMicrophone;
mic.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);

function onSampleData(event:SampleDataEvent):void
{
    trace("activity from: " + mic.name + " level: " + mic.activityLevel);
}

This should be a more optimal solution, as the SampleDataEvent's are only dispatched when the microphone detects sound, as opposed to your current approach that works on every frame.

OTHER TIPS

As a temp solution to mute the mic you can try :

var st:SoundTransform = new SoundTransform(0);
mic.soundTransform = st;

You should still see the activity level.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top