Theme changes, fix for unicode conversion errors, misc
[rox-musicbox.git] / plugins / _wav.py
blob6255c089812178fe349ae432a9755531057e364b
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 wave
21 HAVE_WAV = True
22 except:
23 HAVE_WAV = False
24 print 'No WAV support'
27 def get_info(song):
28 #TODO or NOP?
29 pass
32 class WAVDecoder:
33 """
34 A decoder to process WAV sound files. Utilizes the standard python wave module
35 Called from the Player class to read and decode WAV files.
36 """
37 def __init__(self, filename, buffersize):
38 """Initialize the decoder"""
39 self.buffersize = buffersize
40 self.filename = filename
42 def open(self):
43 """Open the file and prepare for decoding"""
44 self.wv = wave.open(self.filename, 'r')
46 def close(self):
47 """Close the file and do any needed cleanup"""
48 pass
50 def length(self):
51 """Return the length of the file in seconds as an integer"""
52 return self.wv.getnframes() / self.wv.getframerate()
54 def samplerate(self):
55 """Return the sample rate of the file in samples per second"""
56 return self.wv.getframerate()
58 def channels(self):
59 """Return the number of channels in the file"""
60 return self.wv.getnchannels()
62 def read(self):
63 """
64 Read data from the file and decode to PCM data. Return a buffer
65 of data and length, or (None, 0) at EOF
66 """
67 buff = self.wv.readframes(self.buffersize)
68 bytes = len(buff)
69 if not bytes:
70 buff = None
71 return (buff, bytes)
73 def tell(self):
74 """Return the current playback position in seconds"""
75 return self.wv.tell() / self.wv.getframerate()
77 def seek(self, pos):
78 """Jump to pos as a percentage of the total length of the file"""
79 self.wv.setpos(int(pos * self.wv.getnframes()))
81 def info(self):
82 """Print some info about this WAV file"""
83 print "no info for WAV files"