I’ve wanted to make some code to turn MP3s back into the ones and zeros from which they are made ever since reading Eben Moglen’s Anarchism Triumphant. In that Moglen says:
A CD player is a good example[of a digital device]. Its primary input is a bitstream read from an optical storage disk. The bitstream describes music in terms of measurements, taken 44,000 times per second, of frequency and amplitude in each of two audio channels. The player’s primary output is analog audio signals. Like everything else in the digital world, music as seen by a CD player is mere numeric information; a particular recording of Beethoven’s Ninth Symphony recorded by Arturo Toscanini and the NBC Symphony Orchestra and Chorale is (to drop a few insignificant digits) 1276749873424, while Glenn Gould’s peculiarly perverse last recording of the Goldberg Variations is (similarly rather truncated) 767459083268.
I was reminded of this by the trend on twitter for posting binary songs, pointed out by my flatmate, penguin and my friend oxfordbloo. So here’s a very naive implementation; I realise that mp3s don’t work in 32 bit words, perhaps someone else can be arsed to implement the spec properly. Oh, and the comments and code don't match in the video. I was seeing how much parsing in 64 bit blocks affected performance (not much) and forgot to update the comments. How embarrasing.
Of course with perl the implementation is pretty trivial. It goes like this, please help yourself if its useful in some way.
!/usr/bin/perl
use warnings; use strict;
my $file = shift; my $in;
open(F, "< $file") or die "Unable to open file: $!"; binmode(F);
we buffer 4 bytes(i.e. 32 bits) at a time
while(my $len=sysread(F, my $buf,4)) { $in.=byte2bin($buf); } close(F);
print "$in\n";
expects a 32 bit binary word
sub byte2bin { my $str = unpack(’B32’, shift); return $str; }