Theme changes, fix for unicode conversion errors, misc
[rox-musicbox.git] / plugins / _ogg.py
blob3f3d3f7638196d13c8ef8d8e34d4e3522038dc80
1 """
3 Copyright 2005 Kenneth Hayber <ken@hayber.us>, All rights reserved.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License.
9 This program is distributed in the hope that it will be useful
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 """
19 try:
20 import ogg.vorbis
21 HAVE_OGG = True
22 except:
23 HAVE_OGG = False
24 print 'No OGG support!'
27 def get_info(song):
28 try:
29 tag_info = ogg.vorbis.VorbisFile(song.filename).comment().as_dict()
30 if tag_info.has_key('TITLE'): song.title = tag_info['TITLE'][0]
31 if tag_info.has_key('TRACKNUMBER'): song.track = int(tag_info['TRACKNUMBER'][0])
32 if tag_info.has_key('ALBUM'): song.album = tag_info['ALBUM'][0]
33 if tag_info.has_key('ARTIST'): song.artist = tag_info['ARTIST'][0]
34 if tag_info.has_key('GENRE'): song.genre = tag_info['GENRE'][0]
35 song.length = 0
36 except:
37 pass
40 class OGGDecoder:
41 """
42 A decoder to process OGG sound files. Utilizes pyogg and pyvorbis modules
43 Called from the Player class to read and decode OGG files.
44 """
45 def __init__(self, filename, buffersize):
46 """Initialize the decoder"""
47 self.buffersize = buffersize
48 self.filename = filename
50 def open(self):
51 """Open the file and prepare for decoding"""
52 self.vf = ogg.vorbis.VorbisFile(self.filename)
53 self.vi = self.vf.info()
55 def close(self):
56 """Close the file and do any needed cleanup"""
57 pass
59 def length(self):
60 """Return the length of the file in seconds as an integer"""
61 return int(self.vf.time_total(0))
63 def samplerate(self):
64 """Return the sample rate of the file in samples per second"""
65 return self.vi.rate
67 def channels(self):
68 """Return the number of channels in the file"""
69 return self.vi.channels
71 def read(self):
72 """
73 Read data from the file and decode to PCM data. Return a buffer
74 of data and length, or (None, 0) at EOF
75 """
76 #recent versions don't want buffersize? (buff, bytes, bit) = self.vf.read(self.buffersize)
77 (buff, bytes, bit) = self.vf.read()
78 if bytes == 0:
79 return (None, 0)
80 return (buff, bytes)
82 def tell(self):
83 """Return the current playback position in seconds"""
84 return int(self.vf.time_tell())
86 def seek(self, pos):
87 """Jump to pos as a percentage of the total length of the file"""
88 self.vf.time_seek(float(pos * self.length()))
90 def info(self):
91 """Display some information"""
92 print self.vf.comment().as_dict()