Introduction
This article shows how to compile sox with lame and libmad to enable mp3 support for Windows.
Background
Many of us have an idea about the audio tool sox, through which one can edit the audio files in various formats. But it is pretty unfortunate for developers to find out that sox does not have mp3 support.
Sox is an open source project of SourceForge, which was built on the source code of Lame, another open source. Now it is quite interesting that Lame does have mp3 support but sox does not. But sox has more audio editing features than lame, which makes sox so useful.
Now to enable mp3 support, we have to compile Sox with Lame and Libmad. Linux server has its process to compile, but Windows has its own way. If you Google this, you can find one basic article written by “Jason Smestad”. But when I tried that out I found some problems, some steps didn’t appear as they seemed to be written.
I don’t know why that happens, but I tried out a slightly different way after that and succeeded.
In this article, there is no credit for me, all credit goes to “Jason Smestad”, but I only publish this so that some developers like me may get it right.
Using the Code
Prequisities
- Visual Studio 2005 / 2008
- Cmake 2.6
- Windows XP, SP-2 [It should work with others, but I haven’t tried]
- Sox 14.0.1
- Lame 3.97
- Libmad 0.15.1b
Step-1 [Compiling Lame]
- Unzip the LAME source code and open it as a Visual Studio project.
- Solution Explorer -> Right Click on libmp3lame -> Configuration Properties
-> C/C++ -> Code Generation -> Runtime Library" -> Change the value to Multi-threaded DLL (/MD).
- Solution Explorer -> Right Click on mpglib -> Configuration Properties
-> C/C++ -> Code Generation -> Runtime Library" -> Change the value to Multi-threaded DLL (/MD)
- Build libmp3lame
- Build mpglib
- Search libmp3lame.lib within that folder and rename it as mp3lame.lib
- Create a folder outside and name it as you wish, for now let's consider, lameforsox
- Create a folder inside it and name it as “lame” and now search “lame.h” within lame source code folder and copy that within this lame folder
- Copy mp3lame.lib and mpglib.lib within lameforsox
Step-2 [Compiling Libmad]
- Unzip the Libmad source code and open it as a Visual Studio project
- Solution Explorer -> Right Click on each file of “source file” folder -> Configuration Properties -> C/C++ -> Code Generation -> Runtime Library" -> Change the value to Multi-threaded DLL (/MD)
- Build Libmad
- Search mad.h within that folder and copy it
- Create another folder outside the application and name it as you wish. For now, let’s say “madforsox” and create another folder named “mad” within it.
- Copy mad.h within “mad"
- Search libmad.lib within Libmad source folder and rename it as mad.lib
- Copy mad.lib within “madforsox”
Step-3 [Compiling Sox with Lame and Mad Library]
- Open Visual Studio. Tools -> Options -> Projects and Solutions -> VC++ Directories -> Include Directories -> Click on the “…” button -> now browse and add the lame.h and mad.h if these headers are not there.
- Library Files -> click on “…” button -> add those lib files i.e. mp3lame.lib, mpglib.lib and mad.lib.
- Visit sox source folder, find CMakeLists.txt
- Open it and find if (
NEED_LIBM
)
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} -lm)
It should be within line number 62-65. Leave some space after that and add:
set(CMAKE_REQUIRED_LIBRARIES {CMAKE_REQUIRED_LIBRARIES} mpglib)
optional(HAVE_LAME_LAME_H lame/lame.h mp3lame lame_init mp3)
And edit “lame/lame.h” to “lame.h”, i.e.: the line should look like this.
optional(HAVE_LAME_LAME_H lame.h mp3lame lame_init mp3)
- Run -> Cmd -> change the path into Sox directory
- Type cmake.exe ./ -G "Visual Studio 9 2008" if you are using Visual Studio 2005, then make it cmake.exe ./ -G "Visual Studio 8 2005"
- If everything is ok, then the following will appear:
...
include files HAVE_MAD_H1 – found
mad_stream_buffer in madmad_stream_buffer in mad – found
include files HAVE_LAME_LAME_H1 – found
lame_init in mp3lamelame_init in mp3lame - found
- Now open Visual Studio and open the solution file [.sln] for sox
- Rebuild “All Build”, if “File Modification Detected” appears then “Reload”, otherwise let the rebuild complete
- Solution Explorer -> Right click on libsox -> "Preprocessor Definitions" (right click on libsox -> Properties -> Configuration Properties -> C/C++ -> Preprocessor)
HAVE_STRING_H
before the first line. Set the "Runtime Library" to Multi-threaded DLL (/MD) as described above.
- Do the same for sox after that Linker -> Input -> Additional Dependencies -> click on “…” button -> after the last line add mpglib.lib
- Now build libsox
- Build sox
- Run -> cmd -> change to sox directory -> type sox –h
- Now you will find "mp3, mp2" in the usage output in the section "SUPPORTED FILE FORMATS"
Points of Interest
I haven't tried with the latest version of sox. I might provide an update in the near future.