Adding musicbrainz support. First bits.
authorJesus Climent <mooch@debian.org>
Sat, 10 Dec 2005 22:31:58 +0000 (22:31 +0000)
committerJesus Climent <mooch@debian.org>
Sat, 10 Dec 2005 22:31:58 +0000 (22:31 +0000)
abcde
debian/control
examples/musicbrainz-get-tracks [new file with mode: 0755]

diff --git a/abcde b/abcde
index 1ce8ba8..b03d2b1 100755 (executable)
--- a/abcde
+++ b/abcde
@@ -22,7 +22,8 @@ echo "Usage: abcde [options] [tracks]"
 echo "Options:"
 echo "-1     Encode the whole CD in a single file"
 echo "-a <action1[,action2]...>"
-echo "       Actions to perform (cddb,read,normalize,encode,tag,move,replaygain,playlist,clean)"
+echo "       Actions to perform:"
+echo "       cddb,musicbrainz,read,normalize,encode,tag,move,replaygain,playlist,clean"
 #echo "-A     Experimental actions (retag, transcode)"
 echo "-b     Enable batch normalization"
 #echo "-B     Disable batch replaygain (do file by file)"
@@ -2657,6 +2658,7 @@ fi
 # Determine what actions are to be done from $ACTIONS and set the
 # following environment variables for them:
 DOCDDB=n
+DOMUSICBRAINZ=n
 DOREAD=n
 DONORMALIZE=n
 DOPREPROCESS=n
@@ -2672,6 +2674,7 @@ for ACTION in $(echo $ACTIONS | tr , \ )
 do
        case $ACTION in
                cddb) DOCDDB=y;;
+               musicbrainz) DOMUSICBRAINZ=y;;
                read) DOREAD=y;;
                normalize) DONORMALIZE=y; DOREAD=y;;
 #              preprocess) DOPREPROCESS=y; DOREAD=y;;
index f6604c5..c37c985 100644 (file)
@@ -8,7 +8,7 @@ Build-Depends: debhelper
 Package: abcde
 Architecture: all
 Depends: cd-discid, wget, cdparanoia | cdda2wav, vorbis-tools (>= 1.0beta4-1) | lame | flac | bladeenc | speex 
-Suggests: eject, distmp3, id3 (>= 0.12), id3v2, eyed3, normalize-audio, vorbisgain, mkcue, mp3gain
+Suggests: eject, distmp3, id3 (>= 0.12), id3v2, eyed3, normalize-audio, vorbisgain, mkcue, mp3gain, python-musicbrainz
 Description: A Better CD Encoder
  A frontend program to cdparanoia, wget, cd-discid, id3, and your favorite
  Ogg/Vorbis, MP3, FLAC, Ogg/Speex and/or MPP/MP+(Musepack) encoder (defaults 
diff --git a/examples/musicbrainz-get-tracks b/examples/musicbrainz-get-tracks
new file mode 100755 (executable)
index 0000000..1d38480
--- /dev/null
@@ -0,0 +1,123 @@
+#!/usr/bin/python
+
+# Copyright 2005 Decklin Foster, licensed under the same terms as abcde.
+
+import sys
+import getopt
+import musicbrainz
+import musicbrainz.queries as mq
+
+DEF_CD_DEV = '/dev/cdrom'
+
+# TODO:
+#
+# * --cdrom should take an argument, e.g. /dev/cdrom1. musicbrainz.queries
+#   doesn't seem to provide for this.
+
+def get_toc_discid(mb):
+    mb.Query(mq.MBQ_GetCDTOC)
+    return mb.GetResultData(mq.MBE_TOCGetCDIndexId)
+
+def lookup_discid(mb, id):
+    mb.QueryWithArgs(mq.MBQ_GetCDInfoFromCDIndexId, [id])
+
+if __name__ == "__main__":
+    shortopts = 'cd:'
+    longopts = ['cdrom', 'discid=']
+
+    mb = musicbrainz.mb()
+    mb.SetDepth(2)
+
+    opts, args = getopt.getopt(sys.argv[1:], shortopts, longopts)
+
+    id = None
+    if opts:
+        for opt, arg in opts:
+            if opt in ('--cdrom', '-c'):
+                id = get_toc_discid(mb)
+                lookup_discid(mb, id)
+            if opt in ('--discid', '-d'):
+                id = arg
+                lookup_discid(mb, id)
+    else:
+        id = get_toc_discid(mb)
+        lookup_discid(mb, id)
+
+    matches = mb.GetResultInt(mq.MBE_GetNumAlbums)
+    if matches == 0:
+        print >>sys.stderr, "could not find an entry for this CD"
+        sys.exit(1)
+    if matches > 1:
+        print >>sys.stderr, "warning: multiple matches, using first"
+
+    mb.Select1(mq.MBS_SelectAlbum, 1)
+    album = mb.GetResultData(mq.MBE_AlbumGetAlbumName)
+    n = mb.GetResultInt(mq.MBE_AlbumGetNumTracks)
+
+    artistid = mb.GetIDFromURL(mb.GetResultData(mq.MBE_AlbumGetAlbumArtistId))
+    if artistid == mq.MBI_VARIOUS_ARTIST_ID:
+        artist = 'Various Artists'
+    else:
+        try:
+            artist = mb.GetResultData1(mq.MBE_AlbumGetArtistName, 1)
+        except musicbrainz.MusicBrainzError:
+            artist = 'Unknown Artist'
+
+    if args:
+        tracks = map(int, args)
+    else:
+        tracks = range(1, n+1)
+
+    trackinfo = []
+    for i in tracks:
+        if artistid == mq.MBI_VARIOUS_ARTIST_ID:
+            try:
+                tartist = mb.GetResultData1(mq.MBE_AlbumGetArtistName, i)
+            except musicbrainz.MusicBrainzError:
+                tartist = 'Unknown Artist'
+        else:
+            tartist = None
+        try:
+            ttitle = mb.GetResultData1(mq.MBE_AlbumGetTrackName, i)
+        except musicbrainz.MusicBrainzError:
+            tartist = 'Unknown Track'
+        try:
+            tlength = mb.GetResultInt1(mq.MBE_AlbumGetTrackDuration, i)
+        except musicbrainz.MusicBrainzError:
+            tlength = 0
+        trackinfo.append((tartist, ttitle, tlength))
+
+    print "# fake CD database file generated by musicbrainz-get-tracks 0.1"
+    print "#"
+    print "# Track frame offsets:"
+
+    # Assume standard pregap
+    total_len = 2000
+    for t in trackinfo:
+        tartist, ttitle, tlength = t
+        print "#       %d" % (total_len / (1000.0/75.0))
+        total_len += tlength
+
+    print "#"
+    print "# Disc length: %d seconds" % (total_len / 1000)
+    print "#"
+    print "# Revision: 0"
+    print "# Processed by: MusicBrainz"
+    print "# Submitted by: MusicBrainz"
+    print "DISCID=%s" % id
+    print "DTITLE=%s / %s" % (artist, album)
+
+    for i in range(0, len(trackinfo)):
+        tartist, ttitle, tlength = trackinfo[i]
+        if tartist:
+            print "TTITLE%d=%s / %s" % (i, tartist, ttitle)
+        else:
+            print "TTITLE%d=%s" % (i, ttitle)
+
+    print "EXTD="
+
+    for i in range(0, len(trackinfo)):
+        print "EXTD%d=" % i
+
+    print "PLAYORDER="
+    print "."