MicRecorder, a tiny microphone library by Thibault Imbert

MicRecorder LibraryWhile working on the second chapter of "What can you do with bytes ?", I came up with a tiny helper class for developers who need to record audio easily from the microphone in their applications.

Here is MicRecorder, which handles internally the ByteArray work with the Event.SAMPLE_DATA event dispatched by the Microphone object introduced in Flash Player 10.1 and the WAV packaging. The WAV encoder is bundled but any other encoder could be used in a near future ;)

Here is a little demo :

A Flash animation should have appeared here, but it seems that your browser has an older version of the Flash Player or it is not installed at all. Please, install the last release of the Flash Player now, then reload this page.

To record audio from the Microphone in your application, just use those few lines :

// volume in the final WAV file will be downsampled to 50%
var volume:Number = .5;
// we create the WAV encoder to be used by MicRecorder
var wavEncoder:WaveEncoder = new WaveEncoder( volume );
// we create the MicRecorder object which does the job
var recorder:MicRecorder = new MicRecorder( wavEncoder );
// starts recording
recorder.record();
// stop recording
recorder.stop();

When recording starts a RecordingEvent.RECORDING event is dispatched giving infos about time. When recording it stopped an Event.COMPLETE is dispatched allowing you to retrieve the Micorder.output bytes and save the audio stream (in this case as a WAV) using a simple FileReference object :

recorder.addEventListener(RecordingEvent.RECORDING, onRecording);
recorder.addEventListener(Event.COMPLETE, onRecordComplete);

private function onRecording(event:RecordingEvent):void
{
     trace ( event.time );
}

private function onRecordComplete(event:Event):void
{
     fileReference.save ( recorder.output, "recording.wav" );
}

You can also replay what has been recorded by passing the raw WAV file to the WavSound object from the nice as3wavsound library :

private function onRecordComplete(event:Event):void
{
     var player:WavSound = new WavSound(recorder.output);
     player.play();
}

The MicRecorder object relies by default on the default Microphone device available, but you can pass any Microphone instance as replacement when creating the MicRecorder object :

// a specific Microphone instance can be passed
var recorder:MicRecorder = new MicRecorder( wavEncoder, microphoneDevice );

You can download everything here.

Comments (16)

  1. Akineton wrote::

    Great lib. Thx.

    Saturday, July 10, 2010 at 6:43 pm #
  2. Victor wrote::

    Cool, thanks!

    J’allais justement en faire une pour un projet sur lequel je travail, mais ça tombe à pic ! (Enfin, si le client accepte le fp 10.1..)

    Cheers.

    Saturday, July 10, 2010 at 7:33 pm #
  3. nicoptere wrote::

    psychopathe :)

    Saturday, July 10, 2010 at 7:34 pm #
  4. jloa wrote::

    thx a lot!
    Just what i was looking for ^_^

    Saturday, July 10, 2010 at 10:50 pm #
  5. makemachine wrote::

    nice work… looking forward to chapter two :)

    Sunday, July 11, 2010 at 5:26 am #
  6. Amazing. How about encoding in mp3?

    Sunday, July 11, 2010 at 4:11 pm #
  7. Hicham Taoufikallah wrote::

    Great! as always ;)

    Monday, July 12, 2010 at 10:38 am #
  8. great article.
    How to save in .mp3 ?????

    Monday, July 12, 2010 at 2:02 pm #
  9. jearum wrote::

    Wow! another amazing lib.

    Tuesday, July 13, 2010 at 8:21 am #
  10. Antymon_SB wrote::

    Can’t wait the 2nd chapter!

    Friday, July 23, 2010 at 1:17 pm #
  11. Patrick wrote::

    Just what I need for an android app idea :)

    Well, not quite everything I need, but I trust that a bundled MP3 encoder is in the works ^^
    http://code.google.com/p/as3lameencoder/

    Thibault, how about including not only one, but 2 encoders, so that we can choose between speed or quality?
    http://code.google.com/p/flash-kikko/

    Friday, July 23, 2010 at 6:42 pm #
  12. drus wrote::

    Great work, thanks a lot!

    Tuesday, July 27, 2010 at 12:37 pm #
  13. jloa wrote::

    Thibault, when will the 2nd chapter of your book be translated or published?

    Thursday, July 29, 2010 at 7:34 pm #
  14. Immo wrote::

    Salut Thibault, I’ve just tried your mic recorder but somehow it doesn’t work for me, the SampleDataEvent never seems to get called and in the end when I trace the byte array I get this value: RIFF. I’ve put in an ActivityEvent and it’s picking up the sound and the OnStatus event triggers as well but no luck recording…

    Tuesday, August 10, 2010 at 7:01 pm #
  15. Immo wrote::

    I’ve noticed SampleData won’t work at run time testing using FDT3.5, I need to export as a final Air app build, it then works ; )

    Tuesday, August 10, 2010 at 9:23 pm #
  16. Superguigui wrote::

    First of all Thanks Thibault for this, it will be pretty handful for me :)

    But I’m dealing with the same problem as Immo, the SampleDataEvent doesn’t seem to fire. I’m using FDT 4 and compiling with Flex SDK4 for FP 10.1 and playing my swf at runtime with FP 10.1
    The thing is that I have two projects using the MicRecorder :
    - one of them is very simple and very similar to Thibault, here the sampleDataEvent is dispatched and my sound is recorded.
    - The other one is a bit more complicated and the recorder is called in a specific module. And with the same configuration and instanciation of MicRecorder than the first project, the SampleDataEvent is dispatched.

    So my question is : is there anything that could possibly block the event, a conflict with a Sound object or something like that ?

    Please Help :’(

    Tuesday, August 17, 2010 at 3:15 pm #

Trackbacks/Pingbacks (3)

  1. [...] Direct Link [...]

     
  2. [...] Thibault Imbert (@thibault_imbert) just came up with a really interesting helper class to easily record your microphone input via AS3. Thibault used the new Microphone Object which comes along with the Flash Player Version 10.1 – so having a look at the code is a good start to get in contact with it. You can check out everything on ByteArray.org. [...]

     
  3. [...] MicRecorder, a tiny microphone library [ by Thibault Imbert ] [...]