FLAC
Free Lossless Audio Codec (or FLAC) is just that. The reference implementation is available at http://flac.sf.net/.
Contents
Encoding
Typical:
flac --verify --best -o dst.flac src.wav
flac's encoder seems to have at least some default. It is okay to encode a WAV file by
flac src.wav
which results in src.flac. If src.flac already exists, flac won't write without -f set.
flac will encode multiple source files if no output filename is set.
flac *.wav
You'll probably want --verify (that result is same as original) and --best (compression) set.
flac --verify --best *.wav
If archiving, --delete-input-file may be useful. This option won't delete the input if the encode/decode doesn't go as planned.
Specifying --output-prefix=somedir/ will prepend "somedir/" to the normal output filename, effectively specifying an output directory.
-o or --output-name= allows specifying an output file.
--tag=FIELD=VALUE adds a Vorbis comment. More than one of these can be specified per run. However, all tags are applied to all encodings done for that run.
Decoding
Typical:
flac --decode src.flac -o dst.wav
This is fairly simple. If you need raw samples, set output file extension to .raw, or use --force-raw-format.
Using with SoX
It is probably easiest just to use flac to convert to and from WAV format, since WAV files carry their own samplerate/channels/bits-per-sample information. However, messing with raw data isn't out of the question. If it is necessary to take WAV files entirely out of the process, flac's -a option produces an analysis file from which the information can be retrieved; however, this takes the same amount of time as a decode.
Decoding FLAC for use with SoX
It's possible to use raw output from flac in SoX, but the samplerate, channels, and bits per sample must be known in advance. If the file is known, for example, to be 16000Hz, mono, 16-bit, then the result of
flac --decode src.flac -o dst.raw
can be processed in SoX using
sox [-V] -r 16000 -c 1 -s -w dst.raw ...
The default raw output of flac appears to be signed.
Encoding raw SoX output with FLAC
The options to be set when encoding raw data from SoX depends on the format of the data itself. For example, with raw data specified as -r 44100 (samplerate 44100Hz), -c 2 (2 channels), -s (signed) -w (word = 16 bits), these arguments seem to work:
flac --endian=little --sample-rate=44100 --channels=2 --sign=signed --bps=16 src.raw -o dst.flac
It is possible that the endian might need to be "big" instead of "little" if SoX was run on a big-endian machine; I haven't tested this. If in doubt, just convert to WAV first.