Wednesday, April 22, 2009

Ustream, Soundflower, and iTunes

So, it turns out the version of Soundflower I was using has been superseded, and the newest (1.4.3 as of this writing) has some nice new features. One of these is the ability to tap the output directly, from a supplied GUI program, without the need to set up an extra bus in Logic the way I did in my previous article.

Let's say, for example, you want to send audio from your iTunes library up to Ustream. This is now easier than it was.

1. Download and install Soundflower (I'm testing with 1.4.3; look for the latest release here. You'll need to reboot, so bookmark this blog entry and come back when you're done.

2. Open up your Applications folder and look under utilities. You're looking for Audio MIDI Setup. You'll probably want to drag it to your dock.

3. While you're in the Applications folder, look for a folder called Soundflower. Open it up and drag Soundflowerbed to the dock as well.

4. Run Audio MIDI Setup. Change the Default Output (upper right in the window) to Soundflower (2ch). Think of the two-channel (stereo, left and right) Soundflower bus as a virtual set of two wires you can tap into. This setting means that everything the computer sends to the default audio output will go into these wires. You can also take a look at the properties for Soundflower (2ch) but the defaults are really already what you want: 44.1 KHz sample rate, 2ch-32bit (the only option), and
volumes all the way up (for now, at least).

5. Now launch Soundflowerbed. It doesn't appear to do much, but you will notice a little flower icon in your menu bar. Make two settings. Choose the Soundflower (2ch) submenu, go under buffer size, and set it to 64. Then change the built in routing for Soundflower (2ch) from None (OFF) to Built-in Output. What this means is that you are hooking up those two virtual "wires" to your computer's built-in output (on this laptop, the built-in speaker or headphones). By setting the buffer size the the smallest available, you are ensuring that the audio doesn't lag too much. If you hear stuttering or dropouts you will want to increase that buffer size, but assuming your system is reasonably fast and not doing a lot of other work, that size should be fine.

6. Now launch iTunes and get some music playing. Turn the output from iTunes all the way up (if it is too loud, turn down the speaker using the volume control in your menu bar). The reason for this is that you don't want the audio going into the bus to be attenuated; you want it to be full-scale.

7. Now go to ustream.tv, log in, and hit the "Broadcast Now" button to bring up the broadcast console. Of course, I'm assuming you have a show set up already; if you don't, you'll have to do that first. Set the broadcast audio source to Soundflower (2ch) and do whatever you like with the video (I'm leaving it off and turning off local monitoring). I also set the volume level on the console to 50% and tell it that I am using a mixer. I set the audio quality all the way up, to 44.1KHz. The Ustream interface has a few quirks with the way it redraws the chat room and user list, or fails to, so experiment.

And now you've got an internet radio show! Well, sort of. It's up to you to get some listeners, and up to you to provide some content. I hope this helps you get started. Let me know if you're doing it like this yourself, or if you find a better way. Note that there are other little tricks you can do -- if you use Soundflowerbed a lot, you can make it a startup item. See the README file that comes with it for more information.

UPDATE: you will need to be careful with the volume controls. Now that you have set the default audio output to the two-channel Soundflower bus, this means that your Mac's volume pop-up, in the menu bar, and the volume keys on your keyboard, if you have them, now control the SoundFlower output volume. That makes a certain amount of sense -- normally, if you are playing through an audio interface of some sort, you would want these controls to affect what you were actually listening to, not just the built-in speakers or headphone output. But unfortunately that means if you turn the volume down, your stream will become quieter. This is probably not what you want. (For something conceptually pretty simple, there are sure a lot of quirks to this process! It would be nice if this were fully configurable somewhere.)

To work around this, you can bring down your speaker or headphone monitoringvolume by using the Audio MIDI Setup application again, selecting properties for Built-in Output, and adjusting the volume down to about -24 dB (or whatever monitoring level seems reasonable to you). Make sure the input and output volumes for Soundflower (2ch) are all the way up; if not, your stream volume will be attenuated. Better to leave the digital signal at full scale, as long as you avoid digital clipping, so that you maintain your source's dynamic range, and let your audience members choose how loud they want it. Again, I think this is at about a 50% volume level on the Ustream broadcast console. At least, that is the level that seems to work well for me. But please correct me if you think there is a better way to achieve full volume without clipping.

Sunday, April 19, 2009

iTunes Plus Fail

So, recently Apple has been gradually updating all its protected content to unprotected (though tagged) files. I've been buying the upgrades as new versions become available.

But the process has kind of petered out lately, and now a banner on the iTunes Store home claims "Music is Now 100% iTunes Plus."

So does this mean I have upgraded all my songs -- all ummm, wow, thousand-plus?

Well, not exactly. I have a "residue" of about 65 songs that I apparently can't upgrade. Let's look at the different situations here.

Some of them are from completed albums that just don't exist on iTunes anymore. Here are a few examples: Otis Taylor's album Below the Fold is just gone. The Orb's Bicycles and Tricycles, just gone, as well as the EP entitled Aftermath. Tommy Keene's album Crashing the Ether. So there's no "upgrade path" to these tracks.


For whatever reason, the DRM-free versions were not made available, and for the sake of -- what, DRMological purity? -- the old songs were not kept around as DRM files, but just... purged. Tossing out whole albums seems like a funny way to achieve the goal of 100% DRM-free music.

Should I expect iTunes Plus versions of these to reappear at some point in the future? I feel like it is incumbent on Apple to propose a solution here, especially if they have a goal of eventually decommissioning their DRM playback authorization service. That would be very uncool, because these tracks would then no longer play, unless I bypass the DRM in one of the various methods available.

Another case: the first big album by Eurythmics, Sweet Dreams (Are Made of This). The original is not available as iTunes Plus tracks because it's been replaced by the remastered album, which has a few extra tracks. I guess I shouldn't expect to get a low-cost upgrade to remastered tracks; the labels themselves push this kind of upgrade. But still, it might have been nice to see Apple upgrade the original non-remastered tracks. This was a very popular album; I wonder how many people who purchased it now find themselves in my position?


Some other cases are more confusing. I own a whole bunch of loose tracks purchased from compilations -- and those compilations albums are now gone. The songs themselves are most likely all still available on iTunes, but in their original albums, or in different compilations, so there's no inexpensive upgrade, because their system doesn't think they are the same song.

This seems to be the case even for some tracks that aren't from compilations. I have a single track from Michael Jackson's album Off the Wall. The album is there in the store, and it doesn't seem to be a remaster or contain any change to the track listing. There is no apparent way to upgrade it aside from buying the new track. Why?

I guess it isn't incumbent on Apple to provide me with an upgrade path, but it's annoying; I was able to buy upgrades for almost all of my material.

Then there are some cases that seem like mistakes. I own the DRM version of Rush's album Exit... Stage Left (the remastered edition). It's there in the iTunes catalog as iTunes Plus, but there isn't an upgrade option. And five or six of the tracks are missing. There's a similar situation with their album All the World's a Stage, except in that case only four tracks out of the ten or so are available. Following the link from the protected album in my library to the iTunes Store doesn't even take me to the album, or the band, just to "Rock." So maybe it's a database problem?


I can't complain too much -- it looks like I was able to upgrade more than 90% of my purchased iTunes music. The audio quality is better, and there's no DRM. But having it mostly work also serves to highlight the parts that didn't work. And having music just disappear from iTunes -- well, it makes me nervous, like seeing books disappear from the bookstore, for unclear reasons. Because they just didn't sell well enough to earn their keep? That's not what I'd expect for an entire online store that is 90% or more "long tail."

How about you -- if you owned any protected content from iTunes, have you upgraded it? How did the process go for you?

UPDATE 1: a few days later, I am still seeing new iTunes Plus ugrades to existing tracks become available. So, apparently Apple has indeed, at least in some cases, removed the DRM tracks before providing their iTunes Plus replacements. Also, I noticed that the example I showed of a "broken" album can't even be purchased as an album or partial album with one click, just track-by-track. Do albums appear piecemeal in the iTunes store as they are added, even if they are not complete?

UPDATE 2: it isn't just some old rock albums. Kate Bush's album Aerial -- a fantastic album, by the way -- is listed as a "partial album." Only 7 tracks are available, out of sixteen, and you have to buy them individually. I don't get it!

Monday, April 13, 2009

Hardware's Sense of Humor

I just had to throw out a cheap USB keyboard because several keys on the bottom row had stopped working, including the "b" and "n" key.

Of course, I didn't find this out the easy way. After repeated failure to log in, I had pretty much decided that my system had been compromised and was ready to wake up our system administrator, and make him start digging through firewall logs looking for evidence of a network security breach...

Ubuntu Wastes My Time and Money

So, in a previous post I described how I spent the better part of a day replacing a hard drive in my PC because Ubuntu reported persistent disk failures.

It turns out the drive wasn't bad. There was nothing at all wrong with the drive.

No -- what I was seeing was some kind of recently added diagnostic behavior in the FAT32 version of Ubuntu. When my system was booting, if the drive was marked dirty, Ubuntu would run its fsck, which is a script, which would launch the FAT32-appropriate binary. Apparently when that tool runs now, among the checks it does is a check to verify that my backed-up boot sector matches the real boot sector. The problem is that if it doesn't, which it didn't, it doesn't just produce a diagnostic to this effect, it dumps a list of non-matching inodes or block numbers or whatever the hell you call several pages of fsck's diagnostic numbers. Diagnostic numbers that look exactly like what you see when you have a persistently corrupt directory structure.

Naturally, since the file itself wasn't bad, repairing the file system repeatedly didn't fix this problem, so every time I'd boot, I'd see several pages of fsck garbage spew past.

Which, incidentally, is exactly what it looks like when your hard drive is going bad.

So I replaced it.

Yes, I could have found the actual error in the appropriate log. But after using Linux on and off going back to about, er, 1994? I thought I knew what the system was telling me.

Now I'm just pissed. The actual fix involved running the tool with sudo and allowing it to back up the boot sector.

It doesn't help that my machine also can't shut down without locking up; I get a hang about half the time after the line "running local boot scripts /etc/rc.local." And of course there was the time I had to waste to get the system, in 2009, to let me use my dispay's full resolution.

I might mention that this is about as generic a PC as they come; I built it myself, from an Antec Sonata case, an Intel motherboard with built-in video, an Intel CPU, Crucial RAM, and Seagate hard drives.

Except that now it has one Western Digital drive that I never wanted to buy at all, but it was the only EIDE drive the local Best Buy had in stock.

I could go on a rant about the user interfaces of command-line tools that overwhelm you with useless information while telling you almost nothing you need to know, but perhaps I should save that for another time.

Sunday, April 05, 2009

Hard Drive Weekend

Aside from being a weekend of UStream studio setup, it has also been a weekend of dealing with hard drives.

I have a PC I built for myself a few years ago, from parts chosen to maximize reliability (an Intel motherboard, Seagate drives, Crucial RAM). It is a dual-boot system with Windows 2000 and Ubuntu, which I've more-or-less continually upgraded. It's been in my closet, but last week at work I needed a Linux box quick for some testing and coding, so I dragged it in, and picked up a used HP flat panel at Affordable Computers on Industrial. Much to my delight I found that the 20" flat panel does 1600x1200 and can be rotated into the portrait aspect ratio, which is fantastic for programming; if the built-in video had a second output, I'd seriously consider a second one. But I'd have to add a video card, and disable the onboard video, and that sounded like too much money and work for the moment, so there it sits.

Anyway, the drive with Windows 2000 on it seems to be failing, although I'm reminded again just how badly operating systems are at letting you know this rather important bit of information. Ubuntu always found lots of problems on it when scanning the disk, and so did Windows, but it did not seem to be able to fix them despite its best efforts, so I decided to pull it and put in a different drive.

I finally figured out how to use partimage to save the whole partition onto an external drive. You have to segment it, so you don't hit the file size limit on FAT32 volumes. You apparently can't just use dd, which was my first thought. I had to re-learn how to use parted. Both these tools have somewhat cryptic error messages (the source drive for partimage has to be un-mounted, but you wouldn't know it from the error message). I used a Knoppix boot CD-R burned for the purpose. Knoppix has apparently gone all 3-D animated, which annoyed the crap out of me; I have no idea why a CD distribution designed for system administration needs to shatter your window into dozens of translucent pieces when you close it. That's just dumb. All this magic also probably contributed to the two hard lockups I experienced trying to get the backup and restore completed.

I had to do the MBR thing, and then the GRUB thing, and dredge up memories of how to fix a dual-boot when it comes up and just says GRUB. I have fixed them before, but I don't do it frequently enough to remember what to do when it goes south. I finally found some documentation from Red Hat and it involved a series of 3 GRUB commands. GRUB appears to have a reasonable parser, but it really isn't reasonable at all; if you type "setup(hda)" it isn't the same as typing "setup (hda)" or something like that; it doesn't handle whitespace in a reasonable way at all.

That shocked me a bit, as handling whitespace in text strings is not that hard; in fact, writing a program to break up a line of differently-formatted pieces of text with arbitrary whitespace is, or at least used to be, a very common assignment for a beginning programming class. And there are parsing libraries that make it a no-brainer. No, the author had to _work_ at making the tool intolerant of deviations in whitespace. I remember writing loops in VAX Pascal to skip whitespace circa 1985. But open-source command-line tools are rarely optimized towards making it easy for the end-user.

Anyway, after doing the dd trick for saving and restoring the MBR, the GRUB commands that fixed the boot were along these lines:

grub> find /boot/grub/stage1

grub> root (hda,0)

grub> setup (hda)

GRUB is also monstrously complicated so don't expect the man page to be much assistance.

As usual, though, the hardest part of swapping out one hard drive and swapping in another was dealing with the physical cabling in the case. I have an Antec Sonata case, which is a very nice case, but even so, you can't get the drive cage fully free of the box; you have to twist and wrap and fold ribbon cables in a nightmare origami configuration; there isn't room behind the drive cage for the power and ribbon cables to actually fit neatly, so you have to smash them flat and hope that you haven't broken any conductors; and even the best PC case I've ever seen still has hiding in it somewhere razor-sharp sheet metal. I have yet to do any kind of a PC upgrade -- a CPU, memory, or drives -- that doesn't involve sacrificing a small amount of blood.

What would have been really helpful is a case where the _back side_ came off, to allow me to get my hands behind the drives. Barring that, if the entire drive cage came out, that would have been extremely helpful as well. But no, on my Sonata case it is welded in place.

It really, really makes me appreciate the thought Apple put into the Mac Pro (and prior to that, the G5) case!

I've never heard anyone take that into account when they talk about the "Apple Tax," have you?

I Stream, UStream, We All Stream

So, this weekend I figured out how to do a live broadcast via ustream.tv that is a little bit more sophisticated than just turning on my webcam and playing my guitar in front of it.

What I wanted was to avoid relying on the single mono microphone built in to the iSight webcam I'm using, keeping in mind that even though it is sounds pretty decent, the ideal place to put a microphone is not necessarily the same as the ideal place to put a camera. What I wanted was the ability to fully mix live audio sources, and that's mostly what I got.

If you haven't seen UStream, it's a really cool site that allows you to follow live streaming audio and/or video from any of a bazillion live contributors at any given time, and makes it easy to set up you own streaming upload. It provides a very nice Java application which, on my Mac, worked very seamlessly with either my wife's laptop with built-in iSight camera (which it considers a USB camera) or one of the discontinued iSight cameras I picked up recently. The Java application looks like this:


Let me point out a few key features. First, there is the webcam image. You can view either the local image or the one coming back down from the server. The broadcast console has some amazing features: can set set the image quality along a whole range of values, which corresponds to the level of pixelation (how grainy the image is). You can set the frame rate down as low as one frame per second. If you have a slow connection, or if as in my case you want to crank up the audio quality and don't care much about the video quality, you can tweak it just the way you like it. The audio quality can be set from CD quality (44.1 KHz) down to phone quality or less (5 KHz).

Note also that you can choose to stream video, audio, or both. So, if you want to set up your own internet radio station, there you go.

There's only one minor problem: the console application is designed to work with a webcam. You can tell it what audio and video source to use, but if, for example, you want to send audio from iTunes into your stream, you can't, because iTunes, or the system audio, does not show up as a source. And what if you want to play DJ and mix the camera audio over the iTunes audio, or use a separate microphone, or, like me, play guitar into an audio interface and sing on another microphone into the audio interface? You can't.

(Note that the following instructions apply only to Macs; there are presumably similar situations for Windows or Linux on PC hardware, but I have not investigated them).

What you need to do is set up another audio bus -- a virtual bus. The ticket here is a free application from Cycling '74 called Soundflower (Google for it). This will give you two new audio devices, both of which are virtual (they have no hardware associated with them). One is a two-channel bus and one is 16-channel bus. This virtual device can be set up to 96KHz and uses 32-bit samples, so you won't lose audio quality needlessly by passing 16-bit or 24-bit audio through it.

Now, what I really wanted to do was to allow Logic Pro to work with one big audio device. I wanted to use the audio inputs on my Apogee Ensemble for guitar, microphone, and CD player, and the audio outputs for headphones and speakers for monitoring, but I _also_ wanted to use the SoundFlower two-channel device as a way to send exactly what I wanted, all mixed together, to my audio stream. Logic and CoreAudio only work with one device at a time, so this seems impossible.

Apple's solution is to create something called an Aggregate Device. If you run the Audio Midi Setup utility, in the Audio menu you'll see a command called "Open Aggregate Device Editor." You can then create a mash-up of audio devices. In this case, a real hardware device and a virtual device can all be combined. We're using 8 inputs and outputs on the Ensemble, which include four mic preamps, two of which can be used to plug in guitars directly. The two virtual channels from Soundflower are 9 and 10. The aggregate device looks like this in the editor:


So, there's my virtual device. Now Logic can use it; you have to do a little tweaking in Preferences/Audio, but wham, it was pretty simple. And now I can set up my devices.

The way I did it was to configure channel 1 as a guitar input, set up with virtual effects. Channel 4 is my vocal mic, configured with more virtual effects. Channels 7 and 8 are wired to the combo CD/tape player in my rack, so I wanted to be able to use that as an input source as well. Outputs 1 and 2 are the Apogee headphone outputs. Outputs 7 and 8 are the Apogee outputs to some monitor speakers, although to avoid feedback and strange delay issues I generally just listen to headphones.

Now things get a little stranger: inputs 9 and 10 correspond to the 2-channel virtual SoundFlower device, and outputs 9 and 10 represent the outgoing stream.

So, I have lots of options. In Logic, I created a virtual mixer to help me keep this all straight:


Let me try to give you a quick tour of that mixer. From left to right, channel 1 get input 1, which is my guitar, and the inserts are guitar effects. Instead of setting it up with a fixed output, I configured two aux busses, 1 and 2. Bus 1 represents my headphone monitoring. Bus 2 represents the outgoing stream. So the mixer is currently set up to feed the guitar, mic, and CD/tape deck into my headphones.

The fourth channel strip from the left is interesting. Its purpose in life is to allow me to feed the output of iTunes into Logic and mix it in to my headphones as well. For this purpose it has as its input channels 9 and 10, which is the Soundflower virtual bus. It has a send to the headphone bus, but no send to the Soundflower output. The reason for this is that I don't want iTunes audio to be going into the stream twice, with different latencies. I am not sure what might happen there but it looked like it could get ugly, so I thought it best to avoid that situation.

One last thing: iTunes does not have a configurable output; I can't tell it to play on a particular audio device and channel. Instead, it always plays on the default output. So if I want it to feed audio to SoundFlower, that means I have to set the default output to Soundflower, using the Audio MIDI Setup application. I left the system output to 1 and 2 on the Ensemble so that any beeps or what-not produced by my Mac did not wind up on the stream.

If you decide to play audio from iTunes into your UStream show, keep in mind that the 50% mark on the volume control on the Ustream console seems to be digital full scale or 0 dB, when the iTunes volume control is all the way up. This seems to give you the fullest range of volumes. If you go any higher than that you will likely get digital clipping, which sounds very nasty. Experiment with boosting the sources you feed into Soundflower up by adding gain or compression, rather than pushing the final volume past 50%.

Note that you don't have to use Logic; you could use MainStage, or if you want a lighter-weight setup, I think you could probably do most or all of what I'm doing using the wonderful Audio Hijack Pro. And you don't need an Apogee Ensemble; I have one for doing recording with its nice-sounding preamps and low noise, but for live use a much less expensive audio interface would be nearly indistinguishable. You could try the Duet, also by Apogee, or something like an Edirol FA-66, or something like a MOTU traveler. I can't recommend any USB audio interfaces, but this Firewire combination seems to work very well for me.

A word on processing power: I'm doing this all live on a Mac Mini with a G4 processor. However, when I turn on guitar and vocal effects, it starts to bog down. The effects use quite a bit of CPU time. Handling live video also makes the G4 work. If you have anything faster than this -- a G5 or something with an Intel processor -- it should be plenty fast enough to do what I'm doing. You may hear the fan ramp up. One way that I could save some processor power is by moving all the guitar effects to outboard pedals. I don't have an outboard compressor or voice channel strip so the voice processing will have to stay in the computer for now. It does make me want to trade in my G4 Mac Mini for a newer Intel version, which would be able to run MainStage and which would presumably have quite a bit more CPU horsepower to handle audio effects. Oh, and if you are using a laptop, make sure to set it so it doesn't go to sleep or try to cycle down the processor. This might make it get hot, so be sure you have adequate airflow and your studio environment is not too warm.

Here is a picture showing pretty much my entire live setup, minus the guitars and pedals;


Looks impressive, right? Well, except that everything on the rack shelf was very dusty from disuse, and I had not cleaned it all up yet. Also, when you see a shot from the other side, taken while I was setting it all up, and notice the dirty socks on the floor... and the beer... maybe not so much!


And, yes, I did play harmonica on my punk version of Jewel's song "I'm Sensitive." (If this were Twitter, I would then type LOLZ).

You might be saying "wow, this is very complicated," and you wouldn't be wrong. However, I am very impressed, and almost amazed, that it worked. Apple really has their system software together now, and these tools are incredibly flexible. It's particularly nice to be able to do all this mixing and routing right inside the box. And you don't need a setup this elaborate; I know folks using a cheap webcam and microphone and their live streams sound fine for casual listening. We aren't producing a professional-quality video for Blu-Ray here. If I were setting up something from scratch, I'd consider a laptop, an iSight, and an Edirol FA-66, and a mic; the FA-66 can be had for under $300, and a Shure SM-58 microphone for under $100. Add a few cables and spend some time on the setup and you have near-broadcast audio quality (although, of course, not video quality, but with the proper lighting the iSight can produce some impressive results).

Finally, if you would like to listen to one of my shows, my Ustream channel can be found here. I post notes on Twitter and Facebook, or on my chat, indicating when I expect to be on. So far I have done two kinds of shows -- live music with video, and canned playlists from iTunes. But as you can see, just about anything is possible!