Home > Actionscript, Alchemy, Flash > MP3 encoder in Flash with Alchemy

MP3 encoder in Flash with Alchemy

March 24th, 2009

A week ago I started playing with Adobe Alchemy. First just a few return values or simple calculations.
After a few days of playing and testing I thought it would be nice to have a MP3 Encoder in flash.
With the new Sound.extract method in Actionscript you can get raw data like a "WAV" but there is no easy way to compress this data.

I started searching for C code which implements a MP3 Encoder and found the 8Hz MP3 implementation.
I could have used LAME, but hey.. have you looked at the file count!
The 8Hz implementation uses about 10 files, so it's readable. It's not the best encoder, but good enough to test the possibilities.

I was very surprised that the 8Hz code would compile with llvm-gcc (Alchemy)! That is a good starting point, now I only have to rewrite some code to get input from and output to Actionscript.

The original source used fopen to read and write files. This isn't possible with Alchemy so I replaced fopen with funopen and gave a pointer to Actionscript bytearrays.

The next problem I encountered was the execution timeout in Flash. When encoding large WAV files (more then 30 seconds of sound on my MacBook Pro) the flashplayer will trigger an error about exceeding the maximum execution time. I tried to alter it with the compiler option "-default-script-limits" but no luck (maybe it has something to do with Alchemy).
But it forced me to take a deeper look in the encoding source. I've rewritten the layer3.c file to process a few frames and then give feedback to Actionscript. This way no timeout occurs.

I'm still working on speed optimization, because encoding is slow at the moment.
I will keep you updated!

Here is the first version:

Bernard Actionscript, Alchemy, Flash

  1. March 24th, 2009 at 17:05 | #1

    Go Bernard go! Nice work dude!

  2. March 24th, 2009 at 17:11 | #2

    Thnx for the heads up Erik!

  3. March 25th, 2009 at 08:29 | #3

    Nice work!

  4. Michel
    March 30th, 2009 at 00:17 | #4

    Great Job.
    I have similar issue for writing an image stream after having extracted it from a pdf using alchemy.
    How did you manage to write it on the disk using funopen. Did you use gluegen or only pure alchemy ? How to pass the C stream as a byte array baak to actionscript ?
    Example welcome if possible ;-)
    Thanks

  5. March 30th, 2009 at 11:26 | #5

    I don’t do diskwriting in C, it’s done in Actionscript.
    Also I didn’t use Gluegen.
    I will put some examples online this evening.

    //edit: Will be tomorrow evening :)

  6. Michel
    March 31st, 2009 at 16:17 | #6

    so you succeeded to encode WAV into mp3 in C then pass it back to actionscript (using bytearray ?) so it can be written clicking the button.
    I am looking forward your examples
    thanks

  7. May 11th, 2009 at 11:59 | #7

    Just tried it but it returned some strange garble (original file was 6sec of mono 44.1kHz 16bit wav).

    By the way, do you know of any client-side mp3 encoding in flash? I was looking for that and found your page.

  8. May 11th, 2009 at 15:08 | #8

    @Steven
    Maybe I should mention that I fixed the input at stereo, 44.1kHz 16 bits.
    Everything could work, but I didn’t implement reading the WAV header yet.

    And about your client-side question. This is client-side.

  9. May 11th, 2009 at 18:08 | #9

    @Bernard my bad. I tried again with a stereo file and it worked. I must say I’m impressed, I haven’t seen this before. Great job!

  10. Gaby
    May 11th, 2009 at 21:36 | #10

    Hello, I’d love to see that source code. It looks very interesting, please put it online if you have some free time. Thanks!

  11. jmp909
    May 19th, 2009 at 00:26 | #11

    Excellent work! I hope you can release this to the community one day.

    (In case you haven’t seen it the sazameki flash 10 libraries contain some useful classes for dealing with WAV encoding/decoding and other stuff for dynamically creating audio, sample player instruments etc http://sazameki.org/index.php?id=3)

    we are currently downloading audio from this to desktop as WAV but hoped for MP3. At least we can see that target in sight now!

  12. agladshteyn
    June 29th, 2009 at 15:42 | #12

    For some reason this isn’t working for me. When I load a WAV file, both buttons are just greyed out and I can’t save it as MP3. Any ideas?

  13. July 6th, 2009 at 19:36 | #13

    @agladshteyn
    I will check this out. At the moment I’m on vacation so not in a good position to check the code.
    But one thing that may work is uploading a stereo 44.1kHz WAV.
    That’s the only format I implemented at the moment.
    The output is fixed at 256kbits stereo (not joined).

    //EDIT:
    The implementation needs Flash Player 10 to work.

  14. Alex
    July 7th, 2009 at 15:25 | #14

    Thanks for the reply, Bernard. Didn’t mean to interrupt the vacation :) Whenever you get a chance, I am really really interested in seeing the code where you extract raw audio data from WAV and convert it into MP3 file format using Actionscript. If you could post the code or point me to where I can get it, that would be very greatly appreciated. Thanks again.

  15. yaniv
    July 13th, 2009 at 23:04 | #15

    how long it takes to encode 30 sec of mp3?

  16. Will Streeter
    August 4th, 2009 at 02:04 | #16

    Just wondering where I might be able to find the code. I see from the comments that some people have actually been able to test it out. Would love to give it a spin…

    Thanks

  17. August 24th, 2009 at 23:10 | #17

    Way to go! This looks awesome – hope you find the time to finish it. Rockstar status guaranteed ;-)

    Benz

  18. philip andrew
    September 10th, 2009 at 06:00 | #18

    Hi, I really would like to see the source code for this, its great!

  19. Karthik
    November 24th, 2009 at 09:35 | #19

    This looks great, encoding and conversion from wav to mp3. Really looking forward to see the source code to know how the process had been done.

    Karthik

  20. judah
    February 14th, 2010 at 22:51 | #20

    awesome! i need this right now. anyway you can release the source as is?

  21. Tomfat
    March 2nd, 2010 at 15:44 | #21

    I guess,I’m trying to do the same thing.
    I’ve found some opensouced Mp3Encoder implemented by c. And I’m going to translate it into as. If you’ve already done some job on this, would you mind to share your code with me?

  22. March 23rd, 2010 at 23:29 | #22

    This is awesome! Thanks for sharing these infos!

    I’ve been trying for hours to get this version of MP3Encoder http://mp3-tech.org/programmer/encoding.html to work with alchemy. But with little success yet.. :(
    After some tweaking of the Makefile I got it to compile as .a and .l.bc files but I still have an error when I try to initialize : “Undefined sym: _DetermineByteOrder”. This seems to be the first function called that is outside the main.c file. Did you come accross this ?

    Thanks for your help!

  23. March 23rd, 2010 at 23:47 | #23

    Sorry by MP3Encoder I meant MP3 8hz :)

  24. March 24th, 2010 at 14:24 | #24

    Well actually it’s kind of fixed. I just directly make the swc from the sources with the makefile!

  25. March 26th, 2010 at 18:32 | #25

    Hey thank you again for sharing all these informations!
    Thanks to it, I managed to build a little mp3 encoder for flash (with sources and examples) : http://code.google.com/p/flash-kikko/

  26. cbsienda
    May 25th, 2010 at 08:08 | #26

    hello everyone, do i need to produce l.bc files? i was compiling a library which happen to use libtool, but not producing any l.bc files. do i heve to tweak to makefile to produce l.bc, or does .a files enough. thanks. :D

  27. SirHenry
    June 7th, 2010 at 17:06 | #27

    Is it possible to convert audio files with nellymoser codec (flv e.g. created by FMS) using this library?

  28. June 7th, 2010 at 20:42 | #28

    @kikko
    Nice work! :D

  29. June 7th, 2010 at 20:43 | #29

    @SirHenry
    No it’s not. This code and Kikko’s code both use WAV as input.
    You could convert nellymoser to wav and then convert it to MP3 with code like this.

  30. Cesar
    October 14th, 2010 at 22:01 | #30

    Hello, there.

    Nice work. I’m don’t completely get it. Does it make 44 KHz 16-bit audio files? I’m actually needing that for a project I’m working on. Are you planning to publish those sources? Could you send them to me for free/money? Does it work with audio got from a Sound.extract method? I already tried with kikko’s script but didn’t work.

    Thanks!

  31. April 23rd, 2011 at 09:32 | #31

    Hi Kikio,

    Really great work. I am trying to record data from mic and then passing this data to convert to wav file using codes from here http://www.bytearray.org/?p=1858 , but the file size is too big as I am sending this file to server. If I use your code to convert back my wav data to mp3 it doesnt work. It says invalid format. What should i do.
    If I try to pass raw pcm data to your function, even that also doesnt work.

    Your help will be appreciated.

    Regards
    Parminder

  32. Subbu
    June 1st, 2011 at 10:54 | #32

    Hi,
    This is really helpful. Thanks for all the posts.
    I am looking of a way to mix two mp3 songs and save them as one mp3. Is this possible? Any pointers are highly appreciated.

  33. June 1st, 2011 at 10:59 | #33

    @Subbu
    To mix two or more mp3 songs, you could load them with the Sound object and extract the samples. Then it’s not much more then adding the values and write them to an array (or similar). Then you could use an mp3 encoder in flash (mine is a bit slow).

  34. Pranav
    August 15th, 2011 at 10:18 | #34

    Hey, the “first version” isn’t visible :)
    Also, could you suggest a decent mp3 encoder for AS3??

    -Pranav

  35. Supriya Tenany
    September 14th, 2011 at 12:22 | #35

    Hi,

    I already have a code to generate a .wav output, but somehow I am unable to use your code. It always gets stuck at ‘import cmodule.shine.CLibInit’. I can’t find it even. Please help.

  36. September 22nd, 2011 at 22:58 | #36

    @Supriya Tenany
    Do you have a solution for this problem?
    ‘import cmodule.shine.CLibInit’

    Thanks for your repply

  37. chris
    October 4th, 2011 at 17:53 | #37

    Raul :
    @Supriya Tenany
    Do you have a solution for this problem?
    ‘import cmodule.shine.CLibInit’
    Thanks for your repply

    jeah, i have the same problem… how to use this libary???

    THX
    Chris

  38. October 17th, 2011 at 23:49 | #38

    @Raul and @Supriya

    Be sure to add the swc to the library in your IDE. For Flash CS5, you will need to go to Publish Settings -> Flash Tab -> Script Settings -> Library Path Tab. In there you can either add the path to the folder containing the swc, or you can add the swc itself.

  39. Nick
    November 14th, 2011 at 18:06 | #39

    Great work here guys. I’d like to try increasing the quality of the MP3 file produced by increasing the bit rate. Is that possible? which file would need to be changed?
    Many thanks,
    Nick

  40. Igor
    May 23rd, 2012 at 09:38 | #40

    I have been looking at this example for days and its very interesting.
    I used this http://code.google.com/p/flash-kikko/ to encode wav file and it works great except it takes like a minute and a half to encode 2min wav and that is really slow. Also UI is not completely blocked but really lags while encoding is done it’s almost unusable. Is this implementation better considering performance or does anyone have any ideas how to optimize Kikko encoder?

  41. dingo
    December 10th, 2012 at 03:04 | #41

    where is you src files?can you send me?

  42. BDogg
    February 13th, 2013 at 08:32 | #42

    really good job well done! Tell me does the file get uploaded to the server before it is encoded? or is it just done by the users computer with the code and apps on this page?

  1. No trackbacks yet.