MP3 encoder in Flash with Alchemy
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:
Go Bernard go! Nice work dude!
Thnx for the heads up Erik!
Nice work!
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
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
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
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.
@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.
@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!
Hello, I’d love to see that source code. It looks very interesting, please put it online if you have some free time. Thanks!
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!
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?
@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.
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.
how long it takes to encode 30 sec of mp3?
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
Way to go! This looks awesome – hope you find the time to finish it. Rockstar status guaranteed
Benz
Hi, I really would like to see the source code for this, its great!
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
awesome! i need this right now. anyway you can release the source as is?
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?
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!
Sorry by MP3Encoder I meant MP3 8hz
Well actually it’s kind of fixed. I just directly make the swc from the sources with the makefile!
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/
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.
Is it possible to convert audio files with nellymoser codec (flv e.g. created by FMS) using this library?
@kikko
Nice work!
@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.
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!
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
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.
@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).
Hey, the “first version” isn’t visible
Also, could you suggest a decent mp3 encoder for AS3??
-Pranav
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.
@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
@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.
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