Alexey Spiridonov's homepage

Peelable, Lossless Audio Coding

(using Ogg Vorbis & FLAC)

- warning! -

Tim Phipps correctly points out that different versions of the Ogg decoder are almost certainly not guaranteed to produce bit-for-bit identical results. Therefore, if you want to use this as a storage solution, please be sure to keep a copy of your Ogg decoder (source, compilation settings, binary, and libraries and all) decoder together with the FLAC files.

- why? -

CDs degrade over time; they are bulky and inconvenient. As a result, many people rip them to hard disk, both for back-up/archival, and because it's more convenient to listen to them using music software like amaroK, Winamp, iTunes, and XMMS.

There's a trade-off involved in ripping a CD. Ideally, one should use a lossless format like FLAC, LA or Monkey's Audio. Files in this format are typically 40-55% of the size of the CD (280-400 MB), and are every bit as good as the CD[*]. However, many people don't have hard drives so large that 300 MB albums are a non-issue. Size is especially important for laptop users. So, it makes sense to use lossy, psychoacoustically tuned codecs like MP3 or Ogg Vorbis. Encoded files are, for most purposes indistinguishable from the original at around 180kbps (7.6x compression over a CD's 1378kbps, 3.5x compression over the lossless codecs). That gives about 85 MB for a typical CD.

Although lossy compression works pretty well, it's still nice to have easy access to the original: what if you get better audio equipment, and start hearing compression artifacts? What if you want to re-encode your music in the latest and greatest format, or look for hidden pictures?

Having to re-rip your CDs does not constitute "easy access": you might lose metadata, directory structure, you have to load and unload a gazillion disks. One solution is to keep losslessly encoded files on an external hard drive or DVD-R. This way, you'd be duplicating 85 MB worth of data for every 300 MB. That's not too bad, but this page presents a "better" way.

- how? -

We'll compress every CD track into two files: a high-quality .ogg file, and a "leftover" .flac file. The idea is to avoid duplicating the information that's already recorded in the .ogg file. You'll keep the .ogg file on your computer, and the .flac file on archival media (external HD, DVD-R, etc).

The procedure is simple:

  1. Rip CD to WAV
  2. Compress WAV to OGG
  3. Decode ogg to another WAV file
  4. Subtract the first WAV from the second, sample-by-sample, to make a third WAV file
  5. Compress the third WAV file to FLAC

Moreover, it seems to work pretty well. I tested on a couple of tracks (Schubert symphonies), and got the following results.

Track 1 just flac: 49.31 MB
ogg quality (-q) size (MB) overhead (%)
ogg flac total
10 32.07 23.83 55.90 13.35
9 21.27 31.04 52.31 6.08
8 16.34 34.76 51.10 3.63
7 14.32 36.47 50.80 3.01
6 13.17 37.46 50.63 2.67
5 11.18 39.80 50.98 3.38
4 9.00 41.98 50.98 3.39
3 7.86 43.42 51.28 3.99
2 6.37 44.60 50.97 3.37
1 5.11 45.79 50.91 3.24
0 4.01 46.52 50.53 2.47

I omitted the extremes of the quality spectrum from the second test.

Track 2 just flac: 24.32 MB
ogg quality (-q) size (MB) overhead (%)
ogg flac total
8 9.52 16.31 25.83 6.19
7 8.36 17.28 25.64 5.39
6 7.69 17.85 25.54 4.98
5 6.50 19.25 25.76 5.89
4 5.20 20.54 25.75 5.84
3 4.55 21.33 25.88 6.40

So, the overhead numbers are lowest right around -q 6 (-q 0 is not reasonable). That's what I'd recommend -- it's nearly transparent, and gives good results (use -q 7 if you're feeling snooty).

- code! -

I wrote a quick-and-dirty implementation of this idea. It uses the bash shell and the GNU Octave language. Octave is pretty slow. You won't want to use it to encode your entire music collection. If you want to do that, spend 10 minutes, and write the subtraction code in C (share it, and I'll post it). Here are the files:

You'll want to run encode.sh. Listen to the "leftover" flac. It's kind of freaky :)

Please send comments and corrections to Alexey Spiridonov.

- footnotes -

[*] Better, actually. When you play your CD on a consumer-grade computer CD drive, it usually outputs the music through an analog channel. Because the audio circuitry in the drive tends to be junk, this analog channel usually has noise from the drive's electonics and motor. The ripping process, in contrast, is digital, and the sound you get is as good as your sound card. Use cdparanoia to make high-quality rips, including some scratch and skip correction.

Valid XHTML 1.0!