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 (46)

  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 #
  17. infocyde wrote::

    Thanks! I can see a lot of uses for this.

    Monday, November 1, 2010 at 7:30 pm #
  18. Martyg wrote::

    How the heck to I get this compnent added under Flash CS5? Can’t seem to use the normal linking methods.

    Friday, November 12, 2010 at 3:22 am #
  19. Nice job.
    Now I’m not sure if you purposely turned my voice into that of Micky Mouse or not… maybe it’s a bug because that’s what it sounded like when I replayed it – like on Helium and 4 times the speed.

    Monday, November 15, 2010 at 10:50 pm #
  20. Sandra wrote::

    How to save file into server please ?????????????????

    Friday, November 19, 2010 at 4:21 pm #
  21. Ralf wrote::

    Hi.
    Really nice job!!!
    I’m just playing with it and Wont to save it as mp3 to the server,
    I’m using https://github.com/kikko/Shine-MP3-Encoder-on-AS3-Alchemy
    To chenge wav to mp3. And it isn’t working. Have some one manage to save it on the server as mp3? Regards Ralf

    Thursday, December 2, 2010 at 10:46 pm #
  22. Martin wrote::

    Same problem here. I’m trying to use the class on Flash Builder 4 but the sampledata event is not being triggered

    Sunday, December 5, 2010 at 10:36 am #
  23. aizat wrote::

    i tried it and it doesnt work

    Wednesday, February 16, 2011 at 3:43 am #
  24. Eddwin Paz wrote::

    Can you show a simple sample with Flex? for the web please

    Thursday, February 17, 2011 at 12:36 am #
  25. Iskren wrote::

    First sorry form my English.
    Be carful with that liblary.
    If you want to use it, stage width and height must be bigger from Adobe Flash Player Security Dialog Box (who allow Microphone or Cam access). otherwise it’s not working and you will wonder why and you will become crazy :)

    Thursday, February 24, 2011 at 4:09 pm #
  26. Iskren wrote::

    Question – when I play sound with player.play();

    how to stop that shound?
    I tried with

    var player:Sound;
    var playerSoundControl:SoundChannel = new SoundChannel();

    function playRecordedSound():void
    {
    player = new WavSound(recorder.output);
    playerSoundControl = player.play();
    }

    function stopRecordedSound():void
    {
    playerSoundControl.stop();
    }

    but it’s not working.

    Thursday, February 24, 2011 at 5:37 pm #
  27. Edith wrote::

    I’m totally new to Flash. May I know how can I run your as file? I tried to create a fla file with an empty stage and set the document class to the as file. In the fla file, I wrote “var rec:TestRecorder = new TestRecorder();”. When I run it, I got lots of errors as follows:
    Wavsound.as, Line 161 – 1020: Method marked override must override another method.
    AudioSamples.as, Line13 – 1084: Syntax error: expecting identifier before lessthan.
    etc.
    Thanks for your help.

    Friday, March 4, 2011 at 11:45 am #
  28. Mansoor wrote::

    LOLz, the issue for sampledata not dispatched is …
    it works only in release mode :P i.e u have to create the .air file

    Wednesday, April 6, 2011 at 10:03 am #
  29. Leigh wrote::

    Anyone know where there’s an idiot guide to implementing this.

    I’m a good php coder but I dont know how to link the files i downloaded to my flash file.

    Friday, May 13, 2011 at 5:09 pm #
  30. Ismail Khan wrote::

    is it available for windows or only mac

    Monday, May 16, 2011 at 12:02 pm #
  31. If you need a Flash CS5 version then download it here:

    http://cl.ly/2Z3P2O1r1n0q331B1Q2a

    (all I’ve done is set-up directly in Flash and added comments, but I’m sure must be confusing to users who don’t know Adobe Flex at all).

    Also, I’ve read that if you want to save a file to your server you’ll need to use flash.net.URLRequest to send the data to a script on the server that saves the data to a file – I’m not sure how well that’ll translate with data that is a .wav file?

    Monday, June 6, 2011 at 6:49 pm #
  32. Shubho wrote::

    Wonderful library! Is it available to convert the voice data to a Base64 encoded string and get it back from the encoded string? Can you please provide a piece of code for that?

    Wednesday, June 29, 2011 at 8:57 am #
  33. Dima wrote::

    Thank you!

    Friday, July 8, 2011 at 6:30 am #
  34. Jesse wrote::

    This example does not work in google chrome. The audio plays very fast so that you sound likea chipmunk.

    Thursday, August 4, 2011 at 12:55 am #
  35. Naveen wrote::

    Hi its seems there was an error in ur Script,

    when i opened ur page i got alert saying there was eeror in the scriupt in FF and in GOOGLE CHOROME no bugs but i could not able to view the flash with my natural eyes please check ur script.

    D.Naveen Rahul
    skype(naveen_rahul)

    Friday, September 16, 2011 at 9:16 am #
  36. Gareth Foote wrote::

    Hi there,

    Appreciate the useful class you have written and released here.

    I have one slight issue which relates to the duration accuracy of the recording. I have set my silenceLevel to 0 and timeout to 99999 so that there is always Mic activity but there seems to be a slight (10th of a second or so) delay when calling MicRecorder.record() and the first floats being written to the buffer. I believe it is because of a short delay before the the first SampleDataEvent.SAMPLE_DATA event is fired and the first floats being written.

    I am speculating but I thought a potential solution (of hack??) would be to set a Timer or ENTER_FRAME listener to start writing ‘silence floats’ to the buffer as soon as MicRecorder.record() is called and when SampleDataEvent.SAMPLE_DATA is fired to write the data in this event to the buffer instead of the silence. I am not sure how you go about writing floats to the buffer that represent silence though.

    If someone could advise me on this I would be most grateful.

    Thanks,
    Gareth

    Monday, September 19, 2011 at 1:11 pm #
  37. Super,
    J’étais en train de lire ton bouquin et au chapitre 20 “son et vidéo” p 812 tu regrettais que la classe Microphone ne puisse être dirigé vers la class sound. Tu as résolu ton pb. Bravo.

    Question cela marche vers une importation iphone via air ?

    A propos ton livre est d’une clarté transcendante. Vivement une mise à jour.

    Tuesday, October 25, 2011 at 8:50 am #
  38. nd wrote::

    “1020: Method marked override must override another method.”-this eror shows in the
    public override function extract(target:ByteArray, length:Number, startPosition:Number = -1): Number {

    at WaveSound.as file

    Friday, November 11, 2011 at 3:18 am #
  39. latch0 wrote::

    @Naveen
    Hi its seems there was an error in ur Script.

    It seems you don’t master the basics of compiling actionscript, so first get your act together before posting BS.

    Monday, November 14, 2011 at 1:06 pm #
  40. Tom wrote::

    allright, nice tutorial, but im new to this so i undestand jackshit, i undestand that this example needs that tinylibrary to run properly, how do i import it? plz help asap. I need to save the WAV and send to a server.

    Tuesday, December 6, 2011 at 9:44 pm #
  41. Matt Bury wrote::

    Hi Thibault,

    Once again, thanks for the great code. Isn’t it amazing what you can do with byte arrays? Everything works fine and I’ve been having a great time experimenting with it.

    All the best,

    Matt

    Friday, December 23, 2011 at 5:59 am #
  42. houz wrote::

    thx

    Wednesday, December 28, 2011 at 10:11 pm #
  43. Yuri F wrote::

    Hi, is anybody else having speed\pitch issues when recording using Chrome ?
    it seems that the MicRecorder works great in IE and Firefox versions of Flash player, but for some reason the Chrome version creates a very fast and high-pitch audio.

    If somebody can help with this issue (solution is probably in AS3) it will be great:)

    Sunday, February 26, 2012 at 5:26 pm #
  44. Jan wrote::

    Thank you! Great job!

    Saturday, March 17, 2012 at 2:55 am #
  45. Cozzbie wrote::

    First of all…lovely lib, love it alot, just have one issue — the white noise produced by each recording — How does one eliminate the white noise produced by the recordings saved using this library?

    Wednesday, June 5, 2013 at 4:36 am #
  46. botchniaque wrote::

    First of all thanks for the great library. It is very helpful for me.
    I was interested in WaveEncoder only and have had some trouble encoding 8bit WAV files.

    Posted an issue http://code.google.com/p/micrecorder/issues/detail?id=1 together with a solution :)

    Thanks for the lib once again.

    Saturday, November 2, 2013 at 6:53 pm #

Trackbacks/Pingbacks (17)

  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 ] [...]

     
  4. Demo Set: Community Audio Libraries in Flash | codebass.net on Wednesday, January 12, 2011 at 2:50 pm

    [...] MicRecorder, a tiny microphone library MicRecorder 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. [...]

     
  5. AS3 Code Libraries (APIs) | bytecoderz blog on Wednesday, February 2, 2011 at 10:42 am

    [...] MicRecorder – A tiny microphone library http://www.bytearray.org/?p=1858 [...]

     
  6. [...] もうひとつ残念なお知らせはじっくり探してみるとライブラリがすでにあったことです。 bytearray.org : MicRecorder, a tiny microphone library [...]

     
  7. zero point nine » Blog Archive » FLV Encoder with Audio on Sunday, March 13, 2011 at 5:51 am

    [...] I'm doing in the sample implementation above. I'm using a modified version of Thibault Imbert's MicRecorder class to collect the audio data into one big audio ByteArray and then chunking out the pieces of it [...]

     
  8. Brando » Record Your Roar: Behind the Scenes on Thursday, April 7, 2011 at 12:43 pm

    [...] -        MicRecorder, which allowed us to save the audio from the mic as wav [...]

     
  9. [...] For saving the sound data to wave file I’m using wave encoder from bytearray.org [...]

     
  10. Anonymous on Wednesday, May 11, 2011 at 12:16 pm

    [...] [...]

     
  11. [...] заголовки, и получается обычный Wav-файл. А вот MicRecorder и маленькая, прекрасная библиотека, которая [...]

     
  12. [...] MicRecorder — маленькая библиотека для работы с микрофоном. Shine-MP3-Encoder-on-AS3-Alchemy — Flash/Alchemy порт небольшого кодировщика Shine MP3. Pitch MP3 — изменение скорости звука в реальном времени (КРУТО!!!). PitchShifter — изменение тональности звука, черз pitch-shifting (я думаю, что этот термин лучше оставить без перевода), т.е. без изменения длины звука. soundtouch-as3 — AS3 порт библиотеки для обработки звука. [...]

     
  13. 类库收集 | 嘟嘟晨的个人站点 on Thursday, June 7, 2012 at 10:38 am

    [...] http://www.bytearray.org/?p=1858 [...]

     
  14. [转]AS3 Code Libraries (APIs) | 南龙的小站 on Sunday, July 8, 2012 at 6:30 pm

    [...] MicRecorder – A tiny microphone libraryhttp://www.bytearray.org/?p=1858 [...]

     
  15. AS3 Class Library on Monday, October 22, 2012 at 4:48 am

    [...] MicRecorder – A tiny microphone library http://www.bytearray.org/?p=1858 [...]

     
  16. FLASH录音 | catproj on Sunday, November 18, 2012 at 7:37 am

    [...] http://www.bytearray.org/?p=1858 [...]

     
  17. [...] MicRecorder – A tiny microphone library http://www.bytearray.org/?p=1858 [...]