scratched tallies on the prison wall

Sox, “the Swiss Army knife of sound processing programs”, has a jolly useful feature where it can read an audio file and dump out the contents as data; exactly what you need to create a waveform image of an mp3. I needed to do this for a particualr client project; there are a whole bunch of pages explaining how to create a waveform of an mp3 in PHP, and they pretty much all rely on using lame to convert the mp3 to a wav file and then reading and decoding the wav file with PHP. That seems like a waste to me, since I don’t care about the wav file, and sox does precisely what I want.

However, if you’re writing PHP apps, it’s quite possible that you (as I am, for this project) are hosting the web app in some sort of shared hosting environment, which is fairly sharply limited. So what I wanted was a static build of sox; that is, a single binary with the dependencies I need (mp3-reading, primarily) compiled into it. I managed to put together a static Linux binary by (roughly) following the ggkarman instructions (which are for OS X, but that doesn’t matter here). However, once I had said binary, it didn’t run on the hosting environment; I got the error ./sox: /lib/x86_64-linux-gnu/libc.so.6: version GLIBC_2.14 not found (required by ./sox). What this means is that the binary was built against the version of glibc, the system C library on which basically everything depends, that was on the machine I built it on — my Ubuntu 14.04 desktop. The hosting service has an older version of glibc, and so things didn’t work.

At this point I went to ask questions on #ubuntu-uk, and popey said: can’t you just build it in an older version of Linux — say, a Debian wheezy chroot? I said, dunno, can I? And popey, hero of the revolution that he is, had a wheezy chroot lying around the place and so tried it, and it works.

Below is how we built the static binary. There are probably a bunch of complexities in here that will make it work for you; in particular, the web hosting machine was 64-bit and so was popey’s build environment, and if that’s not the case for you then epic fail lies ahead. But it may help.

And now you have a sox binary, which can be used to convert an mp3 to numeric data with sox whatever.mp3 output.dat, and doesn’t need other libraries so you can just drop it into your web hosting and run it with PHPexec(). Hooray.