5 tons of flacs
Here are some things I learned and remembered on the way to writing a flac decoder in C++11. I actually wrote most of this up in April, but I found this post sitting here basically done, so out it goes.
-
It's convenient to use
bc
for base conversions and general arithmetic. Just enterobase=16
, type in any decimal number andbc
will tell you what it is in hex.ibase=16
is also useful for the reverse conversion. Be careful if you're changingobase
after changingibase
, because you'll have to enter the newobase
in the base specified byibase
. -
hexdump
is cool! The-C
flag is particularly useful since it lets you see bytes in hex and as chars (did you know the first four bytes of a valid flac file spell out 'fLaC'?) -
flac --analyze file.flac
dumps out flac information for you when you get tired of squinting at hexdump output and parsing files manually. -
Flac encodes integers in big-endian format, except when they're encoded in unary format or encoded using an extrapolation of UTF-8 (this is a neat trick; it never occured to me that you could use the byte-saving powers of UTF-8 for something other than...well, unicode characters). Another exception is tags, which store little-endian integers to comply with the vorbis spec. Delightful.
-
Bitfields are not your friend. Especially when the architecture of your dev machine doesn't match with the endianness of the data. They look more idiot-proof than bitshifts and masking, but don't be fooled. Here Be Dragons.
-
STL bitsets are nice...when they're enough. They are frequently not enough, especially if you're trying to slice and dice bytes. They are really convenient for printing out numbers in binary, though. I learned this particular trick from Katy.