Sunday, 2 March 2008

Converting CCIT μ-law to MP3 using SOX and Lame

This is a problem that has been bugging me for some time, and today I found my solution. Huzzah!

For my work I do a bunch of stuff with the phone network, and the standard that it uses for audio is CCIT μ-law, as described here. Basically this uses an 8khz bit rate, mono PCM (.wav) file. Not quite CD quality, but if you've ever spoken to anyone on the phone, you know that.

Now .wav files aren't really very useful for streaming over the internet, especially as flash doesn't have a great time handling them, especially this weird old-world format optimised for hardware routing. Seems Flash really wants mp3. Which is fair enough, I'm not gonna send a change request to Adobe - it may be that they'll put support in for Pacifica anyway

So, while spending a Sunday afternoon trying to get a demo ready for BlogTalk 2008 (it's last minute, it starts tomorrow!) this is an itch I very much needed to scratch, and preferably using open source tools, and definitely from a command line (which in turn, gets called from ASP.Net using System.Diagnostics.Process.Start)

The tools you'll need are SOX and LAME - you'll need an executable version of LAME, rather than a dll. If you're trying this on an operating system other than Windows this technique might not work in exactly the same way, but the tools are cross platform, so you should find a way.

But both sox.exe and lame.exe in the same folder. Then run this command line (thanks to this old O'Reilly post for pointing the way)

sox -t wav yourinputfile.wav -t wav -s -w -c 1 -r 8000 - rate | lame -b 80 - youroutputfile.mp3

Obviously you'll be changing filenames as you see fit, but the output should be just fine.

Apparently the "rate" switch is deprecated in the version of sox I've got hold of, but frankly, I don't care. This has given me the result I wanted, which is a low quality mp3 file from a CCIT μ-law message that's been left in a kinda voicemail system using the "record" function in CallFlow

Hope this is useful to someone out there! If you feel really brave sox can possibly do this in realtime (ie audio in, audio out, without hitting the file system!) If someone knows how this is done, I'd love them to share.

Now all I need to do is dump the MP3's in an RSS feed. But that should be the easy bit.


Andrew Back said...

"If you feel really brave sox can possibly do this in realtime (ie audio in, audio out, without hitting the file system!) If someone knows how this is done, I'd love them to share."

What as input and what as output if you if not files? On UNIX/Linux devices are represented by files and so grabbing an audio in or line out is easy. Or you could pipe into some other program. Maybe event netcat if you want to crudely send audio across a network...

But, not sure what end-to-end conversion you're trying to achieve if it's not using the FS at all.

Robbie said...

going to put this into the call flow designer?

@Andrew, this is for Call Flow, e.g. record a message using your handset, retrieve the uLaw wav file through the SOAP service, convert to MP3 and publish a feed. By the sounds of it, thats what San1t1 is doing.

Tim Stevens said...

@9600 - talking about real time transcoding. probably the audio arriving over the network. so taking audio in from the phone network, and (possibly) multicasting out as streamed mp3 in _realtime_ (oh, and don't forget I'm the opposite to a *.nix guy, being stuck as I am in Bill Gates little windows on the world)

@robb1e - you got my use case spot on. Doing voice mail to RSS, with MP3 enclosures. But don't see why this would live in the callflow designer - after all this is wav to mp3, not mp3 to wav, so not desperately useful fo that tool.

Andrew Back said...

Would need to understand more about your platform (I.e. what is it running on), and how audio arrives at it from the telephone network.

For true real time you could run an icecast streaming MP3 server and use libshout to stream data to it. And would then need a scheme for creating and protecting the associated URIs.

Guessing you would also need to record to file such that people don't lose messages as a result of not being connected to their stream. Or actually maybe this is what you want and not a real time stream... Just all the conversion done server side with messages contained in MP3 files a la podcasts, and !=Internet radio as real time would be.

Anonymous said...


been working all afternoon to figure out how to use sox to convert u-law (g711) to a standerd .wav file. any chance you might know how to do this ?


sheasie (at) gmail (dot) com

Anonymous said...

ps. nevermind. realizing theproblem that i was having was my source was NOT ulaw errr

Tim Stevens

Tim Stevens
Be Silent