Search code examples
actionscript-3flashactionscriptmicrophone

Two microphone simultaneous input in Flash?


I am creating a project but I am having some issues , the project consists in 2 persons screaming on two microphones, and filling a bar with that scream. The problem is how can flash work with these two inputs simultaneous,this is the code.

import flash.events.ActivityEvent;
import flash.events.StatusEvent;
import flash.media.Microphone
var myTimer:Timer = new Timer(500);
myTimer.addEventListener(TimerEvent.TIMER, onMicActivity);
myTimer.start();

var deviceArray:Array = Microphone.names;
var micaux = 0;
var micaux2 = 0;
trace("Available sound input devices:");

for (var i:int = 0; i < deviceArray.length; i++)
{
    trace(" " + deviceArray[i]+ " " + i);
}

var mic:Microphone = Microphone.getMicrophone();
var mic2:Microphone = Microphone.getMicrophone(1);
trace("mic1 "+mic.name);
trace("mic2 "+mic2.name);
mic.gain = 60;
mic.rate = 11;
mic.setUseEchoSuppression(true);
mic.setLoopBack(true);
mic.setSilenceLevel(5, 1000);
mic2.gain = 60;
mic2.rate = 11;
mic2.setUseEchoSuppression(true);
mic2.setLoopBack(true);
mic2.setSilenceLevel(5, 1000);

function onMicActivity(event:TimerEvent):void
{
    //trace("activating="  + ", activityLevel=" +  
    // mic.activityLevel);
    if (mic.activityLevel > 3) {
    micaux = micaux + mic.activityLevel;
    }
    if (mic2.activityLevel >3){


    micaux2 = micaux2 + mic2.activityLevel; }
    trace("mic1 " +mic.activityLevel);
    trace("mic 2" +mic2.activityLevel);
    if (micaux>0){
        soundbox.text = String(micaux);
        healthBar.barColor.x +=  mic.activityLevel / 50;
        healthBar.barColor.scaleX +=  mic.activityLevel / 50;}
    if (micaux2>0){ 
        soundbox2.text = String(micaux2);
        healthBar2.barColor.x +=  mic2.activityLevel / 50;
        healthBar2.barColor.scaleX +=  mic2.activityLevel / 50;
    }

Soundbox is where appears the score of the user, and healthbar.barcolor is the bar being filled with that score, when i run this code it appear all ok, but only 1 microphone works when i open the settings of that swf the two work ok :\

Hardware is being connected thru two different usb ports.

NETSTREAM didn't solved

Thanks in advance


Solution

  • I think the problem is that you cannot enable the microphone loopback for more than one microphone at a time. My experience is that the the last microphone you do setLoopBack(true) with is the one that is capturing audio.

    One solution is to instead add a listener for SampleDataEvent.SAMPLE_DATA. This is the event you would use if you wanted to capture the audio and do something with it. Using this event, you can listen for data from multiple microphones. I've built a "camera/microphone" wizard this that showed activity level from multiple microphones at the same time. The only drawback is that unlike using setLoopBack(true), it doesn't play the audio back to you. But since you are getting the SampleDataEvent.SAMPLE_DATA, you could construct a sound object and play that back to the user if you really needed to.

    So instead of enabling loopback mode and using a Timer to query the activity level for each mic, add listeners for SampleDataEvent.SAMPLE_DATA:

    mic1.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
    mic2.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
    
    function onSampleData(event:SampleDataEvent):void
    {
        var mic:Microphone = event.target as Microphone;
        trace("activity from: " + mic.name + " level: " + mic.activityLevel);
    }
    

    This actually works better than using a Timer, in the sense that if your timer is running every 500 milliseconds, it may miss occasions when the user is speaking. If the timer fires when the user isn't saying anything, no sound is detected. The SampleDataEvent method doesn't have that problem.

    References: Capturing Sound Output -- note the section titled "Detecting Microphone Activity", and in particular the "Note:" about the 3 different ways you can detect activity: by using setLoopBack(true), adding a listener for SampleDataEvent.SAMPLE_DATA, and by attaching the mic to a NetStream. This page also shows how could create a Sound object from the samples.