[4] | 1 | #if 0 /* |
---|
| 2 | # ----------------------------------------------------------------------- |
---|
| 3 | # audioinfo.py - support for audio cds |
---|
| 4 | # ----------------------------------------------------------------------- |
---|
| 5 | # $Id: audioinfo.py,v 1.22 2004/05/28 19:32:59 dischi Exp $ |
---|
| 6 | # |
---|
| 7 | # ----------------------------------------------------------------------- |
---|
| 8 | # Copyright (C) 2003 Thomas Schueppel, Dirk Meyer |
---|
| 9 | # |
---|
| 10 | # This program is free software; you can redistribute it and/or modify |
---|
| 11 | # it under the terms of the GNU General Public License as published by |
---|
| 12 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 13 | # (at your option) any later version. |
---|
| 14 | # |
---|
| 15 | # This program is distributed in the hope that it will be useful, but |
---|
| 16 | # WITHOUT ANY WARRANTY; without even the implied warranty of MER- |
---|
| 17 | # CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
| 18 | # Public License for more details. |
---|
| 19 | # |
---|
| 20 | # You should have received a copy of the GNU General Public License along |
---|
| 21 | # with this program; if not, write to the Free Software Foundation, Inc., |
---|
| 22 | # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 23 | # |
---|
| 24 | # ----------------------------------------------------------------------- */ |
---|
| 25 | #endif |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | from mmpython import mediainfo |
---|
| 29 | import mmpython |
---|
| 30 | import discinfo |
---|
| 31 | import DiscID |
---|
| 32 | import CDDB |
---|
| 33 | import cdrom |
---|
| 34 | |
---|
| 35 | _debug = mediainfo._debug |
---|
| 36 | |
---|
| 37 | class AudioDiscInfo(discinfo.DiscInfo): |
---|
| 38 | def __init__(self,device): |
---|
| 39 | discinfo.DiscInfo.__init__(self) |
---|
| 40 | self.context = 'audio' |
---|
| 41 | self.offset = 0 |
---|
| 42 | self.valid = self.isDisc(device) |
---|
| 43 | self.mime = 'audio/cd' |
---|
| 44 | self.type = 'CD' |
---|
| 45 | self.subtype = 'audio' |
---|
| 46 | |
---|
| 47 | def isDisc(self, device): |
---|
| 48 | if discinfo.DiscInfo.isDisc(self, device) != 1: |
---|
| 49 | return 0 |
---|
| 50 | |
---|
| 51 | disc_id = DiscID.disc_id(device) |
---|
| 52 | if mmpython.USE_NETWORK: |
---|
| 53 | try: |
---|
| 54 | (query_stat, query_info) = CDDB.query(disc_id) |
---|
| 55 | except: |
---|
| 56 | # Oops no connection |
---|
| 57 | query_stat = 404 |
---|
| 58 | else: |
---|
| 59 | query_stat = 404 |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | if query_stat == 210 or query_stat == 211: |
---|
| 63 | # set this to success |
---|
| 64 | query_stat = 200 |
---|
| 65 | |
---|
| 66 | for i in query_info: |
---|
| 67 | if i['title'] != i['title'].upper(): |
---|
| 68 | query_info = i |
---|
| 69 | break |
---|
| 70 | else: |
---|
| 71 | query_info = query_info[0] |
---|
| 72 | |
---|
| 73 | elif query_stat != 200: |
---|
| 74 | _debug("failure getting disc info, status %i" % query_stat) |
---|
| 75 | |
---|
| 76 | if query_stat == 200: |
---|
| 77 | qi = query_info['title'].split('/') |
---|
| 78 | self.artist = qi[0].strip() |
---|
| 79 | self.title = qi[1].strip() |
---|
| 80 | for type in ('title', 'artist'): |
---|
| 81 | if getattr(self, type) and getattr(self, type)[0] in ('"', '\'') \ |
---|
| 82 | and getattr(self, type)[-1] in ('"', '\''): |
---|
| 83 | setattr(self, type, getattr(self, type)[1:-1]) |
---|
| 84 | (read_stat, read_info) = CDDB.read(query_info['category'], |
---|
| 85 | query_info['disc_id']) |
---|
| 86 | # id = disc_id + number of tracks |
---|
| 87 | #self.id = '%s_%s' % (query_info['disc_id'], disc_id[1]) |
---|
| 88 | |
---|
| 89 | if read_stat == 210: |
---|
| 90 | for i in range(0, disc_id[1]): |
---|
| 91 | mi = mediainfo.MusicInfo() |
---|
| 92 | mi.title = read_info['TTITLE' + `i`] |
---|
| 93 | mi.album = self.title |
---|
| 94 | mi.artist = self.artist |
---|
| 95 | mi.genre = query_info['category'] |
---|
| 96 | mi.codec = 'PCM' |
---|
| 97 | mi.samplerate = 44.1 |
---|
| 98 | mi.trackno = i+1 |
---|
| 99 | mi.trackof = disc_id[1] |
---|
| 100 | self.tracks.append(mi) |
---|
| 101 | for type in ('title', 'album', 'artist', 'genre'): |
---|
| 102 | if getattr(mi, type) and getattr(mi, type)[0] in ('"', '\'') \ |
---|
| 103 | and getattr(mi, type)[-1] in ('"', '\''): |
---|
| 104 | setattr(mi, type, getattr(mi, type)[1:-1]) |
---|
| 105 | else: |
---|
| 106 | _debug("failure getting track info, status: %i" % read_stat) |
---|
| 107 | # set query_stat to somthing != 200 |
---|
| 108 | query_stat = 400 |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | if query_stat != 200: |
---|
| 112 | _debug("failure getting disc info, status %i" % query_stat) |
---|
| 113 | self.no_caching = 1 |
---|
| 114 | for i in range(0, disc_id[1]): |
---|
| 115 | mi = mediainfo.MusicInfo() |
---|
| 116 | mi.title = 'Track %s' % (i+1) |
---|
| 117 | mi.codec = 'PCM' |
---|
| 118 | mi.samplerate = 44.1 |
---|
| 119 | mi.trackno = i+1 |
---|
| 120 | mi.trackof = disc_id[1] |
---|
| 121 | self.tracks.append(mi) |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | # read the tracks to generate the title list |
---|
| 125 | device = open(device) |
---|
| 126 | (first, last) = cdrom.toc_header(device) |
---|
| 127 | |
---|
| 128 | lmin = 0 |
---|
| 129 | lsec = 0 |
---|
| 130 | |
---|
| 131 | num = 0 |
---|
| 132 | for i in range(first, last + 2): |
---|
| 133 | if i == last + 1: |
---|
| 134 | min, sec, frames = cdrom.leadout(device) |
---|
| 135 | else: |
---|
| 136 | min, sec, frames = cdrom.toc_entry(device, i) |
---|
| 137 | if num: |
---|
| 138 | self.tracks[num-1].length = (min-lmin)*60 + (sec-lsec) |
---|
| 139 | num += 1 |
---|
| 140 | lmin, lsec = min, sec |
---|
| 141 | device.close() |
---|
| 142 | |
---|
| 143 | # correct bad titles for the tracks, containing also the artist |
---|
| 144 | for t in self.tracks: |
---|
| 145 | if not self.artist or not t.title.startswith(self.artist): |
---|
| 146 | break |
---|
| 147 | else: |
---|
| 148 | for t in self.tracks: |
---|
| 149 | t.title = t.title[len(self.artist):].lstrip('/ \t-_') |
---|
| 150 | |
---|
| 151 | # correct bad titles for the tracks, containing also the title |
---|
| 152 | for t in self.tracks: |
---|
| 153 | if not self.title or not t.title.startswith(self.title): |
---|
| 154 | break |
---|
| 155 | else: |
---|
| 156 | for t in self.tracks: |
---|
| 157 | t.title = t.title[len(self.title):].lstrip('/ \t-_') |
---|
| 158 | return 1 |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | mmpython.registertype( 'audio/cd', mediainfo.EXTENSION_DEVICE, mediainfo.TYPE_AUDIO, |
---|
| 162 | AudioDiscInfo ) |
---|