Mr Pablo
Mr Pablo

Reputation: 4188

[AS3][Air for Android] Get streaming mic input?

Is there a way to get true mic streaming input?

The example code I have at the moment looks to be getting the mic input data, and saving it to a sound object and playing it right away.

Is there a way to stream it properly?

If not, in my example, is there a way to get the mic input data, but mute the audio, as it is causing a feedback loop (despite setLoopBack being set to false..)

Code below:

import flash.display.*;
import flash.events.*;
import flash.media.*;
import flash.media.SoundTransform;
import flash.utils.*;

var _soundBytes:ByteArray = new ByteArray();
var _micBytes:ByteArray;
var son:Sound;
var sc:SoundChannel;
var pow:int = 0;
var myBar:Sprite;

stage.quality = "LOW";

// this code ended up muting the mic input oddly?
//SoundMixer.soundTransform = new SoundTransform(0);

init();

function init()
{
    myBar = new Sprite;
    micInit();
    soundInit();

    addEventListener(Event.ENTER_FRAME, visualise);
}

function micInit()
{
    var mic:Microphone = Microphone.getMicrophone();

    if(mic != null) {
        //mic.setUseEchoSuppression(true);
        mic.setLoopBack(false);
        mic.setSilenceLevel(0);
        mic.rate = 44;
        mic.gain = 60;
        mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
    }
}

function micSampleDataHandler(event:SampleDataEvent):void
{
    _micBytes = event.data;
    sc = son.play();
}

function soundInit():void {
    son = new Sound();
    son.addEventListener(SampleDataEvent.SAMPLE_DATA, soundSampleDataHandler);
}

function soundSampleDataHandler(event:SampleDataEvent):void {
    for (var i:int = 0; i < 8192 && _micBytes.bytesAvailable > 0; i++) {
        var sample:Number = _micBytes.readFloat();
        event.data.writeFloat(sample);
        event.data.writeFloat(sample);
    }
}

function drawLines(e:Event):void{

    SoundMixer.computeSpectrum(_soundBytes, true);
    myBar.graphics.clear();
    myBar.graphics.lineStyle(2,0xabc241);
    for (var i=0; i < 256; i++) {
        pow = _soundBytes.readFloat()*200;
        pow = Math.abs(pow);
        myBar.graphics.drawRect(i*2, 0, 2, pow);
        addChild(myBar);
    }
}

Hope you can help!

Upvotes: 3

Views: 1861

Answers (1)

Jason Reeves
Jason Reeves

Reputation: 1716

To use acoustic echo cancellation, call Microphone.getEnhancedMicrophone() to get a reference to an enhanced Microphone object. Set the Microphone.enhancedOptions property to an instance of the MicrophoneEnhancedOptions class. Here is an article that discusses it all. Article about enhanced microphone options at Adobe

EDIT: I spoke too soon. I have used enhanced mic many times before, but I decided to read the article myself to see if there were any interesting things I could learn new from it... and I found this near the end

AEC is computationally expensive. Currently, only desktop platforms are supported for Flash Player and AIR

Although I just looked at the date... last year, so maybe give it a try and it is supported now?!?

Upvotes: 2

Related Questions