1 """Classes for manipulating audio devices (currently only for Sun and SGI)"""
2 from warnings
import warnpy3k
3 warnpy3k("the audiodev module has been removed in Python 3.0", stacklevel
=2)
6 __all__
= ["error","AudioDev"]
8 class error(Exception):
12 # Private instance variables
13 ## if 0: access frameratelist, nchannelslist, sampwidthlist, oldparams, \
14 ## params, config, inited_outrate, inited_width, \
15 ## inited_nchannels, port, converter, classinited: private
18 frameratelist
= nchannelslist
= sampwidthlist
= None
22 self
.frameratelist
= [
23 (48000, AL
.RATE_48000
),
24 (44100, AL
.RATE_44100
),
25 (32000, AL
.RATE_32000
),
26 (22050, AL
.RATE_22050
),
27 (16000, AL
.RATE_16000
),
28 (11025, AL
.RATE_11025
),
29 ( 8000, AL
.RATE_8000
),
31 self
.nchannelslist
= [
36 self
.sampwidthlist
= [
45 if not self
.classinited
:
48 self
.params
= [AL
.OUTPUT_RATE
, 0]
49 self
.config
= al
.newconfig()
50 self
.inited_outrate
= 0
52 self
.inited_nchannels
= 0
62 al
.setparams(AL
.DEFAULT_DEVICE
, self
.oldparams
)
69 while self
.port
.getfilled() > 0:
79 al
.setparams(AL
.DEFAULT_DEVICE
, self
.oldparams
)
82 def setoutrate(self
, rate
):
83 for (raw
, cooked
) in self
.frameratelist
:
85 self
.params
[1] = cooked
86 self
.inited_outrate
= 1
89 raise error
, 'bad output rate'
91 def setsampwidth(self
, width
):
92 for (raw
, cooked
) in self
.sampwidthlist
:
94 self
.config
.setwidth(cooked
)
100 self
.inited_width
= 0
101 self
.config
.setwidth(AL
.SAMPLE_16
)
102 self
.converter
= self
.ulaw2lin
104 raise error
, 'bad sample width'
106 def setnchannels(self
, nchannels
):
107 for (raw
, cooked
) in self
.nchannelslist
:
109 self
.config
.setchannels(cooked
)
110 self
.inited_nchannels
= 1
113 raise error
, 'bad # of channels'
115 def writeframes(self
, data
):
116 if not (self
.inited_outrate
and self
.inited_nchannels
):
117 raise error
, 'params not specified'
120 self
.port
= al
.openport('Python', 'w', self
.config
)
121 self
.oldparams
= self
.params
[:]
122 al
.getparams(AL
.DEFAULT_DEVICE
, self
.oldparams
)
123 al
.setparams(AL
.DEFAULT_DEVICE
, self
.params
)
125 data
= self
.converter(data
)
126 self
.port
.writesamps(data
)
130 return self
.port
.getfilled()
134 def getfillable(self
):
136 return self
.port
.getfillable()
138 return self
.config
.getqueuesize()
141 ## if 0: access *: private
143 def ulaw2lin(self
, data
):
145 return audioop
.ulaw2lin(data
, 2)
147 class Play_Audio_sun
:
148 ## if 0: access outrate, sampwidth, nchannels, inited_outrate, inited_width, \
149 ## inited_nchannels, converter: private
155 self
.inited_outrate
= 0
156 self
.inited_width
= 0
157 self
.inited_nchannels
= 0
158 self
.converter
= None
165 def setoutrate(self
, rate
):
167 self
.inited_outrate
= 1
169 def setsampwidth(self
, width
):
170 self
.sampwidth
= width
171 self
.inited_width
= 1
173 def setnchannels(self
, nchannels
):
174 self
.nchannels
= nchannels
175 self
.inited_nchannels
= 1
177 def writeframes(self
, data
):
178 if not (self
.inited_outrate
and self
.inited_width
and self
.inited_nchannels
):
179 raise error
, 'params not specified'
181 import sunaudiodev
, SUNAUDIODEV
182 self
.port
= sunaudiodev
.open('w')
183 info
= self
.port
.getinfo()
184 info
.o_sample_rate
= self
.outrate
185 info
.o_channels
= self
.nchannels
186 if self
.sampwidth
== 0:
188 self
.o_encoding
= SUNAUDIODEV
.ENCODING_ULAW
189 # XXX Hack, hack -- leave defaults
191 info
.o_precision
= 8 * self
.sampwidth
192 info
.o_encoding
= SUNAUDIODEV
.ENCODING_LINEAR
193 self
.port
.setinfo(info
)
195 data
= self
.converter(data
)
196 self
.port
.write(data
)
212 return self
.port
.obufcount()
216 ## # Nobody remembers what this method does, and it's broken. :-(
217 ## def getfillable(self):
218 ## return BUFFERSIZE - self.getfilled()
221 # Dynamically try to import and use a platform specific module.
227 return Play_Audio_sun()
232 raise error
, 'no audio device'
234 return Audio_mac
.Play_Audio_mac()
236 return Play_Audio_sgi()
243 fn
= 'f:just samples:just.aif'
245 af
= aifc
.open(fn
, 'r')
246 print fn
, af
.getparams()
248 p
.setoutrate(af
.getframerate())
249 p
.setsampwidth(af
.getsampwidth())
250 p
.setnchannels(af
.getnchannels())
251 BUFSIZ
= af
.getframerate()/af
.getsampwidth()/af
.getnchannels()
253 data
= af
.readframes(BUFSIZ
)
259 if __name__
== '__main__':