Capturing sound input

The Microphone class lets your application connect to a microphone or other sound input device on the user’s system. An application can broadcast the input audio to that system’s speakers or send the audio data to a remote server, such as the Flash Media Server. You cannot access raw audio data from the microphone; you can only send audio to the system’s speakers or send compressed audio data to a remote server. You can use either Speex or Nellymoser codec for data sent to a remote server. (The Speex codec is available in AIR 1.5.)

Accessing a microphone

The Microphone class does not have a constructor method. Instead, you use the static Microphone.getMicrophone() method to obtain a new Microphone instance, as the following example shows:

var mic = air.Microphone.getMicrophone(); 

Calling the Microphone.getMicrophone() method without a parameter returns the first sound input device discovered on the user’s system.

A system can have more than one sound input device attached to it. Your application can use the Microphone.names property to get an array of the names of all available sound input devices. Then it can call the Microphone.getMicrophone() method with an index parameter that matches the index value of a device’s name in the array.

A system might not have a microphone or other sound input device attached to it. You can use the Microphone.names property or the Microphone.getMicrophone() method to check whether the user has a sound input device installed. If the user doesn’t have a sound input device installed, the names array has a length of zero, and the getMicrophone() method returns a value of null .

Routing microphone audio to local speakers

Audio input from a microphone can be routed to the local system speakers by calling the Microphone.setLoopback() method with a parameter value of true .

When sound from a local microphone is routed to local speakers, there is a risk of creating an audio feedback loop. This can cause loud squealing sounds and can potentially damage sound hardware. Calling the Microphone.setUseEchoSuppression() method with a parameter value of true reduces, but does not completely eliminate, the risk that audio feedback will occur. Adobe recommends that you always call Microphone.setUseEchoSuppression(true) before calling Microphone.setLoopback(true) , unless you are certain that the user is playing back the sound using headphones or something other than speakers.

The following code shows how to route the audio from a local microphone to the local system speakers:

var mic = air.Microphone.getMicrophone(); 
mic.setUseEchoSuppression(true); 
mic.setLoopBack(true);

Altering microphone audio

Your application can alter the audio data that comes from a microphone in two ways. First, it can change the gain of the input sound, which effectively multiplies the input values by a specified amount. This creates a louder or quieter sound. The Microphone.gain property accepts numeric values from 0 through 100. A value of 50 acts like a multiplier of one and specifies normal volume. A value of zero acts like a multiplier of zero and effectively silences the input audio. Values above 50 specify higher than normal volume.

Your application can also change the sample rate of the input audio. Higher sample rates increase sound quality, but they also create denser data streams that use more resources for transmission and storage. The Microphone.rate property represents the audio sample rate measured in kilohertz (kHz). The default sample rate is 8 kHz. You can set the Microphone.rate property to a value higher than 8 kHz if your microphone supports the higher rate. For example, setting the Microphone.rate property to 11 sets the sample rate to 11 kHz; setting it to 22 sets the sample rate to 22 kHz, and so on. The sample rates available depend on the selected codec. When you use the Nellymoser codec, you can specify 5, 8, 11, 16, 22 and 44 kHz as the sample rate. When you use Speex codec (available in AIR 1.5), you can only use 16 kHz.

Detecting microphone activity

To conserve bandwidth and processing resources, the runtime tries to detect when a microphone transmits no sound. When the microphone’s activity level stays below the silence level threshold for a period of time, the runtime stops transmitting the audio input and dispatches an activity event. If you use the Speex codec (available in AIR 1.5), set the silence level to 0, to ensure that the application continuously transmits audio data. Speex voice activity detection automatically reduces bandwidth.

Three properties of the Microphone class monitor and control the detection of activity:

  • The read-only activityLevel property indicates the amount of sound the microphone is detecting, on a scale from 0 to 100.

  • The silenceLevel property specifies the amount of sound needed to activate the microphone and dispatch an activity event. The silenceLevel property also uses a scale from 0 to 100, and the default value is 10.

  • The silenceTimeout property describes the number of milliseconds that the activity level must stay below the silence level before an activity event is dispatched. The default silenceTimeout value is 2000.

Both the Microphone.silenceLevel property and the Microphone.silenceTimeout property are read only, but their values can be changed by using the Microphone.setSilenceLevel() method.

In some cases, the process of activating the microphone when new activity is detected can cause a short delay. Keeping the microphone active at all times can remove such activation delays. Your application can call the Microphone.setSilenceLevel() method with the silenceLevel parameter set to zero. This keeps the microphone active and gathering audio data, even when no sound is detected. Conversely, setting the silenceLevel parameter to 100 prevents the microphone from being activated at all.

The following example displays information about the microphone and reports on activity events and status events dispatched by a Microphone object:

var deviceArray = air.Microphone.names; 
air.trace("Available sound input devices:"); 
for (i = 0; i < deviceArray.length; i++) 
{ 
    air.trace("   " + deviceArray[i]); 
} 
 
var mic = air.Microphone.getMicrophone(); 
mic.gain = 60; 
mic.rate = 11; 
mic.setUseEchoSuppression(true); 
mic.setLoopBack(true); 
mic.setSilenceLevel(5, 1000); 
     
mic.addEventListener(air.ActivityEvent.ACTIVITY, this.onMicActivity); 
     
var micDetails = "Sound input device name: " + mic.name + '\n'; 
micDetails += "Gain: " + mic.gain + '\n'; 
micDetails += "Rate: " + mic.rate + " kHz" + '\n'; 
micDetails += "Muted: " + mic.muted + '\n'; 
micDetails += "Silence level: " + mic.silenceLevel + '\n'; 
micDetails += "Silence timeout: " + mic.silenceTimeout + '\n'; 
micDetails += "Echo suppression: " + mic.useEchoSuppression + '\n'; 
air.trace(micDetails); 
 
function onMicActivity(event) 
{ 
    air.trace("activating=" + event.activating + ", activityLevel=" +  
        mic.activityLevel); 
}

When you run the preceding example, speak or make noises into your system microphone and watch the resulting trace statements appear in the console.

Sending audio to and from a media server

Additional audio capabilities are available when using a streaming media server such as Flash Media Server.

In particular, your application can attach a Microphone object to a runtime.flash.net.NetStream object and transmit data directly from the user’s microphone to the server. Audio data can also be streamed from the server to an AIR application.

AIR 1.5 introduces support for the Speex codec. To set the codec used for compressed audio sent to the media server, set the codec property of the Microphone object. This property can have two values, which are enumerated in the SoundCodec class. Setting the codec property to SoundCodec.SPEEX selects the Speex codec for compressing audio. Setting the property to SoundCodec.NELLYMOSER (the default) selects the Nellymoser codec for compressing audio.

For more information, see the Flash Media Server documentation online at http://www.adobe.com/support/documentation .

// Ethnio survey code removed