Twiddling the Bits for AAC Audio

Saturday, April 20, 2013, at 05:38PM

By Eric Richardson

Audio Data Transport Stream (ADTS) Header for AAC
Eric Richardson

Working out how the bits work for streaming AAC audio.

I've been doing some work on StreamMachine for KPCC lately, and on a call the other day they mentioned that more of the industry seemed to be standardizing on AAC for audio and moving away from MP3. It's a move that makes sense: the format's newer, a little smarter, and can sound a little better at lower bit rates.

I set to work reading up on it yesterday, just trying to scope out what it would take to add support to StreamMachine. Turns out, not much, but I got to have some fun playing with binary in the process.

In the streaming world, AAC audio frames contain an Audio Data Transport Stream (ADTS) header. It tells you things like what encoding the audio is using and how much of it there is before the next frame header. Conveniently, that's very similar to MP3.

MP3 frame headers are four-bytes, starting with 11 sync bits that are all turned on (all 1's, in the binary world). The next 21 bits tell you things like the sample rate, whether the audio is mono or stereo, etc.

ADTS headers are either seven or nine bytes, depending on whether error protection is enabled. They start with 12 sync bits.

Laid out as zeros and ones, the ADTS header would be either 56 or 72 digits long. That's not how you interact with it in the programming world, though. Instead, bytes (eight bits) are represented as a hexadecimal pair. So instead of 01011100 you get 0x5C (both equal 92 -- either 4+8+16+64 in the binary or "five-sixteens plus 12" in the hex).

That's all well and good, but for something like this I still end up having to go back to the pen and paper and draw out my bits, as you can see above. It may be digital, but sometimes it still takes seeing it on paper to actually understand it.