Name That Tune!
By Marco Nelissen, Developer Technical Support Engineer --
<marcone@be.com>


One of the new filesystem drivers included in BeOS 5 is the
cdda-fs driver, which allows easy access to audio CDs. In this
article I'll present a way to look up the CD and track names
on the Internet, giving you the names as the artists intended
them to be.

First, let's take a closer look at the wonderful world of
simulated writeable filesystems on read-only media. When you
rename a CD or a CD track manually, either from a Terminal
window, or by using Tracker or any other application, cdda-fs
stores these names in files located in ~/config/settings/cdda.
It does this when the filesystem is unmounted. The files are
plain ascii files: the first line of the file is the name of
the CD and subsequent lines are the track names. The name of
the file is the hexadecimal cddb-identifier code.

You'll also find some (lots!) of other files in this folder.
These files have long cryptic names and are used to store
attributes for the faked files on a cdda-fs volume. This allows
attributes on a cdda-fs volume to be writeable (even though the
CD itself is not), which in turn allows you to customize the
way the volume appears in the Tracker, or even add your own
custom attributes to the files.

When you mount a CD for which there is no corresponding track
listing in ~/config/settings/cdda, the filesystem driver writes
a file to ~/config/settings/cdda/TODO that contains all the
information it has about the CD. This file contains a flattened
t_CDFS_TOC structure, which is defined as:

typedef struct CDFS_TOC
{
    long numtracks;
    t_CDFS_TOC_Entry tocentry[100];
    ulong CDDBid;
    ulong CDPlayerkey;
} t_CDFS_TOC;

This structure lists all the information about the CD:

* number of tracks
* an array of structures describing each track
* the 32-bit CDDB identifier
* the 32-bit key that CDPlayer uses

CDDBid and CDPlayerkey can be calculated from the track info and
are included for your convenience.

The structure that describes a single track on the CD looks like
this:

typedef struct CDFS_TOC_Entry
{
    ulong startaddress;
    ulong length;
    ulong realtracknumber;
    char  *name;
} t_CDFS_TOC_Entry;

The members of this structure are pretty much self-explanatory.
Startaddress and length are the offset and length of the track,
and are given in sectors. In "cdda mode" CD sectors are 2352
bytes long, or 588 audio frames. Realtracknumber is the number
of the track on the CD, which may differ from the tracknumber
as it appears in cdda-fs, since cdda-fs will filter out any
datatracks. This will only interfere with name lookups if
there are audio tracks after a datatrack, as is the case for
some game CDs that use Redbook audio for in-game music. Since
these game CDs are generally not listed in any CD database on
the Internet, this is of little consequence to us. Finally, the
"name" member has no meaning in user space, and you should
ignore it.

With all this information, we now know enough to contact our
friendly server at freedb.freedb.org and ask it what CD this is.
I won't go into the details of the required protocol here, but
will instead point you to a very basic sample implementation:
<ftp://ftp.be.com/pub/samples/network_kit/cddblinkd.zip>. This
is a commandline app that does lookups only. Adding a graphical
user interface or making it upload new data to the database is
left as an exercise for the reader. Note that the supplied sample
application will connect to freedb.freedb.org, a royalty-free
service with a database accessible to anyone. While it is
possible to modify the application to use other services, you
should realize that if you do so it is up to you to make sure
that you comply with any licensing restrictions set forth by
the owner of that service.

Since this is a DTS article, I would also like to take this
opportunity to answer a question that is often asked regarding
cdda-fs: "Can you put the tracknumber in the filename, so the
files appear in track order instead of alphabetical order"?
The answer is that we could, but we won't (but now that you
have the source code, you could do it yourself if you think it's
absolutely necessary). The reason for this is that there is a
better way: open the 'wav' folder in the Tracker, go to the
"attribute" menu of the Tracker window, enable the "Track"
attribute, then sort the list on the new column. Tracker will
remember this setting, so you only need to do it once.
