[flac-dev] Gapless Support
Brian Willoughby
brianw at sounds.wa.com
Sun Feb 12 03:20:14 PST 2012
Hi Richard,
Your question spans two or three standards, and thus a complete
answer is complicated.
First of all, there is no information on a CD that indicates whether
the audio is gapless or not. All audio on a CD is technically
gapless (if not conceptually gapless) and must fall into track
boundaries that start on multiples of the 2352-byte block (588 stereo
samples). The first track of a CD can easily be aligned at the start
of the audio, but the remaining tracks cannot precisely align the
start of the audio with the start of the track without editing the
audio. As a result, some tracks will have a random amount of silence
between the start of the track and the start of the audio. If there
is a conceptual gap in the audio, then the CD mastering engineer
might increase or decrease the silence to align the start of the
audio with the start of the track, but there is no indication in the
data on the CD that this was done. If the original audio is gapless,
then the CD mastering engineer only has two choices: allow some audio
from the previous track to be heard at the start of the next track,
or miss some of the audio for a track in order to avoid hearing the
tail of the previous track. We're talking about 1/75th of a second
at most, so usually these problems are easily missed or ignored.
But, all the same, these problem exist, and if the mastering engineer
adds a few samples of silence, then nothing in the CD format will
tell you that this was done. Likewise, if the engineer deletes a few
samples, nothing in the data will tell you. Audio from a CD source
always comes in groups of 588 stereo samples. The bottom line is
that if you think you "know" whether a pair of tracks on a CD have a
gapless segue, then you will not always be correct. This is a fact
that has nothing to do with CD, MP3, or FLAC. It's about the
relation between the original audio and the limitations of the CD
format. There are a few other details of the CD format that have to
do with the imprecise specification of track markers and mute bits -
information which is purposely vague in order to allow both cheap CD
player hardware and more advanced CD players to follow the same loose
guidelines. But these further details are a bit too far off topic
for a FLAC mailing list.
Second of all, the biggest issue with gapless playback is the
mismatch between the CD block size of 588 stereo samples versus the
MP3 block (frame) size of 1152 stereo samples. An even bigger issue
is that while the audio samples in a CD block are not related to each
other after they have been decoded, the samples in an MP3 frame are
related. If you somehow knew that an audio track ended 100 samples
before the end of a CD block (and you'll never know this) then you
could just toss those samples and keep the preceding ones. However,
you cannot discard MP3 samples without getting a glitch, because all
1152 samples in a frame affect each other. This is part of the
definition of lossy encoding. Not only do you lose the precision of
the original audio, but trying to use partial frames results in even
further glitches. Thus, the origin of all problems with gapless
playback are due to the mismatch between CD and MP3 block/frame
sizes. The workaround for MP3 is to have extra tags in the file to
remember the actual length of the CD track in multiples of 588 stereo
samples, and then recreate the lossy audio without the extra samples
in the last frame, even though that creates a small glitch. But not
all MP3 encoders put the original CD block length in the file, so you
really have trouble.
Now, when you get to FLAC, you have a default block size of 4096 or
1152 for levels 0, 1, and 2. Neither of these match CD (4116 and
1176 are the closest). Fortunately, because FLAC is lossless, you
can add or remove excess samples at the end of the last block without
any glitches. Also, because FLAC is lossless, all encodings preserve
the exact length of the original data, regardless of whether the
final encoded block is padded or is of a smaller size.
So, armed with all of the above information, you can see that all you
need for gapless playback of FLAC is to honor the actual size of the
audio data as stored in the FLAC file. Then, on playback, just make
sure that the first sample of a new track is immediately following
the last sample of the previous track. The only piece of information
that is missing is whether these tracks are "supposed" to be played
right after each other. However, that information is conceptual and
is missing from the original CD data, so the problem has nothing to
do with FLAC at all.
The only way to "know" whether a track is gapless or not is to have
some human being enter the information manually. There is no
universal specification for this information. Of course, Apple has
their own format in iTunes for gapless tracks, and I assume some
other audio developers have their solutions, but my point is that you
cannot depend upon the CD data itself to provide this information
automatically. And, if the information cannot be obtained
automatically from the CD, then there's no way for the FLAC format to
automatically provide "gapless" information. At best, all you can do
is recreate the original CD audio data as it existed on the original
disc. FLAC is perfect for this because it preserves all the data
that actually exists (all but the track lead-in and mute bits, which
are not available to most CD ripping hardware anyway).
Brian Willoughby
Sound Consulting
On Jan 17, 2012, at 00:44, Richard Schülein wrote:
> We are currently try to add Gapless support on our device… If we
> rip an CD with our device, we can find out, that one track follow
> after another so we can recognize, that the tracks are gapless or not.
>
> But how can we find that out on already existing FLAC files (or
> other formats). Is there a marker inside the file, who tell me,
> that this File is a gapless file? Also gapless make only sense, if
> you have the follow up file also…
>
> Example:
>
> CD
>
> Track 1 not gapless
>
> Track2 Gapless
>
> Track 3 Gapless
>
> Track 4 not gapless
>
> If I rip now all 4 titles and play them later the player must know,
> that Song 1 is not gapless but Song2 and 3 “Fit” together.
>
> What will happen, if I delete Song3…. Is the player playing now
> from Song2 to Song4 gapless, which is not correct…
>
> Anybody a good description for me, how this is solved on FLAC?
More information about the flac-dev
mailing list