Download cddb.tar.gz (7K)
NAME
cddb.tcl - Communicate with CDDB servers using the CDDB Protocol
SYNOPSIS
source cddb.tcl
scan [cddb_query] "%s %s" category disc_id
if ![info exists category] {
return
}
array set mycd [cddb_read $category $disc_id]
if [info exists mycd(artist)] {
puts ""
puts "Artist : $mycd(artist)"
puts "Album : $mycd(album)"
puts ""
}
for { set i 1 } { $i <= $mycd(tracks) } { incr i } {
puts [format "%2d : %s" $i $mycd($i)]
}
DESCRIPTION
cddb.tcl is nearly a direct port of Net::CDDB, a Perl module which
can be found at http://www.wcnet.org/~jzawodn/perl/Net-CDDB/. It
provides easy to use CDDB routines for Tcl. It uses CDDB-P version 1
to interact with Internet CDDB servers.
VARIABLES
cddb.tcl uses only a small number of global variables. They all
begin with cddb to avoid possible conflicts.
$cddb_server
The fully-qualified domain name (or IP address) of the CDDB server.
The default is cddb.cddb.com.
$cddb_port
The port on which the CDDB server is listening. The default is 888,
the standard CDDB port.
$cddb_localuser
The username you'd like to use to identify yourself when cddb.tcl
connects to the CDDB server. Most CDDB servers combine $cddb_localuser
and $cddb_localhost to come up with your e-mail address. However, I
like anonymity so the default is nobody. CDDB server maintainers
may not like this so I reccomend changing it to reflect the username
portion of your e-mail address.
$cddb_localhost
The hostname you'd like to use to identify yourself when cddb.tcl
connects to the CDDB server. Most CDDB servers combine $cddb_localuser
and $cddb_localhost to come up with your e-mail address. However, I
like anonymity so the default is localhost.localdomain. CDDB server
maintainers may not like this so I reccomend changing it to reflect the
server portion of your e-mail address.
$cddb_debug
A non-zero value for $cddb_debug will enable debugging. CDDB debugging
will print all CDDB client input and CDDB server output. This can
be helpful for troubleshooting a problem.
METHODS
All methods are defined in the global namespace. Shame on me! The
next revision will be better by using namespaces. Most of the methods
support a small library of CDDB verbs. These methods are prefixed with
an underscore and aren't meant to be used directly. However, since
they are defined in the global namespace, they are briefly described here
to avoid naming conflicts:
_cddb_hello
Say "hello" to the CDDB server (first step in CDDB transactions.)
_connect
Open a connection with the CDDB server.
_disconnect
Close the connection with the CDDB server.
_extract_stuff
Extract CDDB information from CDDB server output.
_read_line, _read_lines
Methods for reading output from the CDDB server.
_write_line
Methods for writing input to the CDDB server.
The remaining methods are prefixed with cddb and represent the methods
which should be used when writing cddb.tcl scripts.
cddb_query ()
Performs a CDDB query based on the disc id and other information
read from the CD table of contents. See TOOLS section.
For example, disc information from Ani DiFranco's Imperfectly album
looks like this (wrapped for readability):
af0a540d 13 150 14715 36107 50120 64152 75500 93337 106972 121985
130082 149545 168550 181557 2644
The first two items are the disc ID and the number of tracks. The
last item is the disc length in seconds. The remaining items are
track offsets in frames (75 frames = 1 second.)
cddb_query () returns a list with the following elements:
discid category artist title
Often its enough just to scan off the first two elements since those
are the only elements needed for cddb_read ().
If an inexact match is found, cddb_inexact () returns the first
choice found. If you find this is routinely returning incorrect
matches, you may want to edit cddb_inexact () to provide all the
choices. See cddb_inexact () for more information.
If cddb_query () does not return a match, it will return a null string.
cddb_read ($category,$discid)
Performs a CDDB read command on a given category and discid.
cddb_read () uses the array get command to return an array.
Therefore, an array set command should be used to capture the
return value of a cddb_read () command:
array set mycd [cddb_read $category $discid]
For example, if the Ani DiFranco CD above is used for the CDDB read,
the following array would result:
[parray mycd]
mycd(1) = What If No One's Watching
mycd(10) = Make Them Apologize
mycd(11) = The Waiting Song
mycd(12) = Served Faithfully
mycd(13) = Imperfectly
mycd(2) = Fixing Her Hair
mycd(3) = In Or Out
mycd(4) = Every State Line
mycd(5) = Circle Of Light
mycd(6) = If It Isn't Her
mycd(7) = Good, Bad, Ugly
mycd(8) = I'm No Heroine
mycd(9) = Coming Up
mycd(album) = Imperfectly
mycd(artist) = Ani Difranco
mycd(category) = folk
mycd(disc_id) = af0a540d
mycd(tracks) = 13
cddb_inexact ($line)
An inexact match may occur when information for a particular discid
may be present in more than one category. The response from the
CDDB server is a list of choices in the form:
category discid artist / title
Each choice is on a separate line with the last line containing
only a '.' to denote the end of choices.
In this first writing of cddb.tcl, the first choice is used. This
is because in my testing, the choices matched the CD being queried
and differed only by the category they were listed under. If
this algoritm isn't what you want, you may want to turn on debugging
to see the server response and edit cddb_inexact () to match.
Since only the first choice is used in cddb_inexact, a line is read
from the server output and fed to cddb_inexact as follows:
cddb_inexact [_read_line $sock]
$sock is the return value of the _connect method.
cddb_lscat ()
Retrieve a category list from the CDDB server. Requires no arguments
and returns a list of music categories.
cddb_sites ()
Retrieve a list of the sites the CDDB server knows about. Requires
no arguments and returns a list of sites.
CDDB INFO
Taken directly from Net::CDDB man page:
CDDB is an acronym for "CD Database". It also refers to the CDDB
protocol (TCP port 888) which is used to query CDDB databases to retrieve
and update information about music CD's.
For more information about CDDB and the CDDB protocol spec, please
visit http://www.cddb.com/ which is the official CDDB web site.
This package implements most of the CDDB protocol version 1.
TOOLS
The author of Net::CDDB is generous enough to allow the redistribution
of his discid tool for retrieving the disc info CDDB requires. It
can also be acquired from:
http://www.wcnet.org/~jzawodn/c/discid/
Make sure discid is in your PATH or cddb.tcl will fail.
Also distributed in this package are a few example scripts to
provide some code examples:
query.tcl
Place a CD in your CDROM drive and run this script. It will print
out CDDB information for that CD.
test.tcl
Contains the disc info for Ani DiFranco's Imperfectly album (the
last album I was ripping when I wrote this doc) and demonstrates
how a cddb.tcl script can be written without the discid utitlity.
This may be useful if you want to write your own discid tool or
insert someone else's tool in its place.
rename.tcl
A simple script to rename all those pesky audio_*.mp3 files
from a non-CDDB aware ripper/encoder. I found that after ripping
half a dozen CD's, I had all these mp3's like audio_01.mp3 that
I wasn't about to go back and rename by hand. This script will
prompt you for a source directory (where the generic mp3 filenames
can be found), the filename prefix (ie audio) and the filename
suffix (ie mp3). It assumes the file to be renamed is:
$source_dir/$prefix_$track.$suffix
where $track is the track number. It also assumes that tracks 1 - 9
are prefaced with a zero in the filename, ie audio_01.mp3.
BUGS
Since this is basically a direct port of Net::CDDB, many of the
same bugs have been ported too.
The error reporting could use some work.
If an album has multiple discids in the database, cddb_read() will
return a comma separated list. This is not handled properly.
cddb_inexact () only uses the first choice returned. This should
be changed to provide an array of choices.
All methods are defined in the global namespace. This package will
be rewritten to use namespaces in a future revision.
The cddb_server, cddb_port, cddb_localuser, and cddb_localhost
variables are hardcoded into cddb.tcl. A future revision will use
an install script to customize these at installation and provide
better methods to override these in script development.
There isn't much in the way of socket validation so if the _connect
method fails, any of the other methods may still try to use a
non-existent socket. In each procedure which reads or writes from
a socket, there should be a check to see if that socket is good.
This package does not provide methods for submitting records to a
CDDB server. I could not find any information on CDDB version 1 that
says how to do this so I didn't implement it. If you know how, please
mail me at straycat000@yahoo.com.
SPECIAL THANKS
Special thanks go to Jeremy Zawodny for his Net::CDDB module. Without
it, there would be no cddb.tcl.
Net::CDDB web site is located at:
http://www.wcnet.org/~jzawodn/perl/Net-CDDB/
AUTHOR AND COPYRIGHT
Copyright 1999, Matthew Levine
cddb.tcl is covered under GNU Public License (GPL).
The cddb.tcl web site is located at:
http://www.eds.org/~straycat/cddb/
Mail comments and bug reports to straycat000@yahoo.com.
This page was last updated April 8, 2000