Are WAV files really better than FLAC?

Are WAV files really better than FLAC?

Using the command line tools to test out the idea that FLAC audio files are inferior to WAV.

Image credits : 

Open Clip Art Library (public domain). Modified by Jen Wike Huger.

x

Get the newsletter

Join the 85,000 open source advocates who receive our giveaway alerts and article roundups.

If you lurk around enough audiophile-related sites, you may stumble upon writers claiming a difference in sound between the same music played back in FLAC or WAV format. This article on The Well-Tempered Computer does a nice job of exploring this concept, both from the perspective of those who don't believe such a thing is possible and from those who think there is a difference.

Without reprising the whole article, there seem to be two parts to this opinion:

  1. FLAC is somehow inferior to WAV in terms of preserving the original signal.
  2. FLAC takes more processing effort from the playback device and therefore introduces "infelicities" into the digital-analog conversion.

In the past, I mostly ignored this whole debate, but recently I found myself in a discussion with someone I greatly respect who held the opinion that FLAC was inferior to WAV in terms of the music content. Specifically, he believes something is lost in the conversion from WAV to FLAC and that a "second-generation" WAV resulting from the conversion WAV (1st generation) → FLAC → WAV (2nd generation) would be inferior to the original. Moreover, he believes that such loss of quality means you should never purchase FLAC files when WAV files are available because converting the FLAC back to WAV would be a poor copy of the original.

I am sure that some of you reading this will be scratching your heads over such an opinion because the whole point of FLAC is lossless compression. I mean, heck, do we worry that putting our documents in ZIP format might cause us to lose some characters from our sentences? But I really respect this person, who has a great deal of experience in recording and engineering music. So how could I convince someone that FLAC is a safe storage format for precious musical bits?

Using the command line to prove my case

The Well-Tempered Computer article suggests loading both files into an audio editor and subtracting the two tracks. I thought about this for a while; I could load Audacity, mess around to get the starting points aligned, subtract the two, and voilà, the bitstream of zeros would show that the files are equivalent. But I foresaw a fair bit of work in that approach, plus I didn't see how to best present the bitstream of zeros as evidence. So, having Linux on hand, I started a terminal session and proceeded as follows.

First, I used the command-line flac utility to convert a first-generation 24-bit, 44.1kHz WAV file to FLAC, then created a test directory where I could save the FLAC and the subsequent second-generation WAV file.

flac --keep-foreign-metadata 'Immunity-008-Jon Hopkins-Immunity.wav'
mkdir test
mv 'Immunity-008-Jon Hopkins-Immunity.flac' test

Then, I used the command-line flac utility once more to convert the FLAC file into a WAV.

cd test
flac --keep-foreign-metadata -d 'Immunity-008-Jon Hopkins-Immunity.flac'

Finally, I wanted to use the diff command to show the minimal difference between the two generations, but diff is not that informative with binary files. So I used hexdump to convert the two binary files into ASCII files that would better suit diff:

hexdump -v -e '/1 "%02x\n"' ../'Immunity-008-Jon Hopkins-Immunity.wav' >  008_before.hex
hexdump -v -e '/1 "%02x\n"' 'Immunity-008-Jon Hopkins-Immunity.wav' > 008_after.hex
diff 008_before.hex 008_after.hex

Sure enough, diff's output showed the only differences were in the metadata header:

5,6c5,6
< 00
< 63
---
> fe
> 62
57c57
< 12
---
> 10
76,77d75
< 00
< 00

Interpreting the above, bytes 5 and 6, which were originally 00 63, were changed to fe 62; byte 57 was changed from 12 to 10; and the two null bytes at 76 and 77 were dropped.

Personally, I'm OK with metadata changes of this magnitude. And just to confirm this wasn't a lucky choice of source, I've tried this as well on several other files at varying resolutions up to 24 bit / 96kHz, and not seen any changes to the musical content.

The perils of converting with ffmpeg

So if the flac utility does a credible job of preserving signal content across this WAV → FLAC → WAV conversion process, what about other utilities?

I used ffmpeg to convert the 24-bit, 44.1kHz FLAC file back to WAV, with no command-line options, and was surprised to see that ffmpeg downsampled my signal to 16 bits! Here's the output:

Input #0, flac, from 'Immunity-008-Jon Hopkins-Immunity.flac':
  Duration: 00:09:56.76, start: 0.000000, bitrate: 1397 kb/s
    Stream #0:0: Audio: flac, 44100 Hz, stereo, s32 (24 bit)
Stream mapping:
  Stream #0:0 -> #0:0 (flac (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'ffmpegged.wav':
  Metadata:
    ISFT            : Lavf57.83.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s
    Metadata:
      encoder         : Lavc57.107.100 pcm_s16le
[NULL @ 0x55f3b6f51a20] sample/frame number mismatch in adjacent frames
size=  102801kB time=00:09:56.75 bitrate=1411.2kbits/s speed=1.35e+03x

Also, I'm not comfortable with that "sample/frame number mismatch" error message. Hmm. I think I prefer a utility that downsamples when I tell it to, as opposed to by default.

What about sox?

I decided I should try sox to see what the results would be. In this case, there was no output (gotta love the tradition of *nix commands that emit a message only when something goes wrong).

Based on using hexdump and diff on the converted files, it appears that sox is a bit harder on the metadata than flac, at least by default. There were a number of differences in the first 70-odd bytes (too many to list here), but it seems the music payload isn't changed.

This was my first experience with sox; I leafed through its man page to see if there was some way to tell it to be gentle with metadata, and I must say I am stunned by all the cool things you can do with sox. Wow, well done, devs!

My conclusions, for now…

If you buy music in FLAC format and want to convert it to WAV, use the flac command-line tool. Same story if you buy music in WAV format and want to convert it to FLAC. Be prepared to edit your FLAC tags. In ether case, don't worry about the conversion process changing the digital music stream itself—it doesn't.

If you're determined to use ffmpeg, make sure to specify your output format so your file won't get downsampled. Or use sox, but consider taking the time to peruse the documentation and learn more about this wonderful tool.

As for why my contact believes changes are created by the WAV → FLAC → WAV conversion process, perhaps it's due to hs familiarity with real degradations that occur in making copies of analog originals, or in converting DSD to PCM.

And what about the second question above? Do WAV files sound better than FLAC? Well, I'm not going to step into that minefield, except to refer interested parties back to the article on The Well-Tempered Computer.

And more music

Recently, we were fortunate enough to attend a concert by Gli Angeli Genève, where they performed several Bach cantatas from Actus Tragicus as well as some Telemann and Buxtehude. We were moved by the high quality of this performance, and I quickly went looking for a download of Actus Tragicus. On 7digital, a Linux-friendly download store, I bought Vox Luminis' performance in high-resolution FLAC format, and we've been enjoying it since. You can hear some of this music on YouTube.

Another recent find, on that wonderful open source medium, vinyl LP, is Orchestra Baobab's Tribute to Ndiouga Dieng. What a wonderful album! Even better: it came with a download coupon that led to an excellent high-resolution FLAC download. This band has been around for more than 30 years, and wow! They still have the jam. Check out the official video on YouTube.

And, last but not least, I finally shelled out for the high-res download of The Allman Brothers' At Fillmore East, again from 7digital. I've always had a soft spot for The Allman Brothers, but I tend to ignore live albums as, in my experience, their quality varies. This live album is pretty darned wonderful, though. The sound quality is good, the band is at its early best. If you like them but have avoided this album thinking it will be poorly recorded junk, you might want to reconsider!

About the author

Chris Hermansen - Engaged in computing since graduating from the University of British Columbia in 1978, I have been a full-time Linux user since 2005 and a full-time Solaris, SunOS and UNIX System V user before that. On the technical side of things, I have spent a great deal of my career doing data analysis; especially spatial data analysis. I have a substantial amount of programming experience in relation to data analysis, using awk, Python, PostgreSQL, PostGIS and lately Groovy. I have also built a few...