Under The Microscope

The Piper At The Gates Of Dawn

Now we can proceed to a more complex example: broadcasting. Suppose you want your friends to hear about this new band of soon-to-be-rock-stars, Bitchslap. With personal Internet radio servers like Shoutcast or Icecast that run on OS X, anyone can become a DJ to the world. But how do you get audio to the server? As you might have guessed, we’ll use Pipe Dream again!I’ve wanted to make something like the new Pipe Dream plugin, released with Audio Hijack Pro 1.2, ever since the OS X Public Beta came out. I even made a never-released input plug-in for MacAmp Lite X last year, called Crack Pipe. (For the record, all these drug references are just for humor and don’t reflect my personal habits. My only drugs of choice are nicotine and coding). So what’s with all these pipes, you ask? I’ll try to explain it in this article.Pipes in the Unix world (as you may know, MacOS X has Unix hidden under the hood) are involved in a great many tasks you’ve probably never seen. The pipe is one of the Inter Process Communication methods used to chain commands together. It allows the output of one program to be sent to another program (we’re talking about Unix programs, on the command-line.) For example, (a pretty useless example, but hey, its just a tutorial), we need to find out the number of processes running on our machine. The Unix command to list processes currently running is “ps” or with some useful options to list all processes is “ps aux”. If you run this command in Terminal.app, you will see a bunch of lines you have to count to get the total (each line represents one process.) But we have another Unix command “wc -l” which can count lines for us. So we need to somehow instruct “wc” to count lines in the “ps aux” output. Pipes to the rescue!

We can accomplish this with “ps aux | wc -l”. The vertical bar here is a symbolic representation of a pipe, the whole meaning of this command in plain English would be “run the ps command with ‘aux’ options, and send the output to a ‘wc’ command, which will count lines in its input and print the total out”. Actually, wc will count the header line put by “ps” too, so that number should be reduced
by 1, but we omit it for simplicity.


[gutter:~] alex% ps aux | wc -l
44

“OK”, you say, counting running processes might be more fun to do than watching paint dry, but what does this have to do with Audio Hijack? Well, the Pipe Dream plugin bundled with Audio Hijack Pro 1.2 allows you to do something that has never been possible before. With Pipe Dream, you can output raw audio hijacked by AH Pro to Unix commands.

You’ve probably heard of the LAME encoder, the best free MP3 encoder in the world, and you might even have it installed on your system. Maybe you wanted to make an MP3 recording of the highest quality possible for some of your hijacked tracks, but AH Pro uses the standard LAME quality settings when it records to an MP3 file. Instead, we use the Pipe Dream plugin to record. When you load Pipe Dream into AH Pro DSP patch you will be presented with its Editor window (if you have “Automatically show DSP Editor windows” checkbox enabled in AHP preferences, otherwise click the editor button on the Pipe Dream plugin tile.) The editor window is used to enter commands which will be later executed by the system shell when you click Pipe Dream’s “Start” button. So we clear all the text in Editor and enter this line:

<&0 /usr/local/bin/lame --preset insane -r - ~/Desktop/insane_quality.mp3

This cryptic line means: "launch the '/usr/local/bin/lame' command with the encoding preset set to 'insane' , instruct it to look for input audio data on the standard input (AH Pro sends raw audio there) and write the encoded data to the file 'insane_quality.mp3' in my Desktop folder". Now, when you click "Start" button in Pipe Dream's editor window, you will be recording 320Kbps MP3 of hijacked audio.

Now we can proceed to a more complex example: broadcasting. Suppose you want your friends to hear about this new band of soon-to-be-rock-stars, Bitchslap. With personal Internet radio servers like Shoutcast or Icecast that run on OS X, anyone can become a DJ to the world. But how do you get audio to the server from your audio player? As you might have guessed, we'll use Pipe Dream again!

Prerequisites: LAME installed and shoutcast or icecast 1 server installed and configured. Check their respective websites or Google for help setting that up.

What we'll do is turn our computer into a broadcasting station. First, we start the shoutcast/icecast server. This server acts like a re-transmitter of our audio, so clients connected to this server will get the audio stream we send to it. Next, set up AH Pro to hijack iTunes (or your choice of audio player) and insert the Pipe Dream plugin in the DSP area. In Pipe Dream's Editor window, type the following simple perl script (simple by perl standards, anyhow), changing the appropriate values (port and password) to match your setup.

exec perl -x $0
#!/usr/bin/perl
use Socket;
$hostname = "localhost";
$port = 8001;
$password = "changeme";
$proto = getprotobyname('tcp');
socket(SOCK, PF_INET, SOCK_STREAM, $proto);
$iaddr = gethostbyname($hostname);
$sin = sockaddr_in($port, $iaddr);
connect(SOCK, $sin) || die "Failed to connect!";
print SOCK "$password\r\n";
print SOCK "icy-br: 24\r\nicy-name: my music\r\n\r\n";
open(FH, "/usr/local/bin/lame -q 5 --resample 22.05 -cbr -b 24 -r - - |");
while (read(FH, $buf, 1024)) { print SOCK $buf; }

(If you dont want to type all of that, download Pipe Dream preset with the code. Click a little 'up' arrow in the Pipe Dream editor window to load preset.)

Explanation:
Line 1-11 of the script prepare and establish the connection to a local broadcast server.
Line 12-13 logs into the server by sending the password and some info about our stream. After a successful login, our server is ready to receive MP3 encoded audio data.
Line 14 starts the lame encoder with the following settings: encoding quality=5, output stream sample rate =22.05KHz, constant bitrate of 24Kbps, accept audio data from standard input (AH Pro sends the raw audio there), output encoded data to standard output (our script will get it from there.)
Line 15 reads data from the standard output of lame encoder (remember, we instructed lame to send its encoded data there) and sends this MP3 encoded data to our broadcasting server, using the connection we established earlier in our script.

Click the "Start" button, and if everything is OK, you'll be sending the MP3 stream of your hijacked audio to the local shoutcast/icecast server. Play some audio in your hijacked application, and anyone can connect to it and listen with his own MP3 player. The address for the stream
would be "http://your_external_ip_address:8000" (or "http://your_external_ip_address:8000/icy_0" if you use icecast.)

Voila, your broadcasting server is ready to serve listeners. Suddenly, Broadcasting is as simple as clicking play in iTunes (once we've set up a few steps beforehand).

Have fun with pipes,
Alex Lagutin
Chief Pipe Advocate
Rogue Amoeba Software, LLC.

Comments for this post have been closed. Thanks for reading!


Our Software