1 """Classes for manipulating audio devices (currently only for Sun and SGI)"""
3 class error(Exception):
7 # Private instance variables
8 ## if 0: access frameratelist, nchannelslist, sampwidthlist, oldparams, \
9 ## params, config, inited_outrate, inited_width, \
10 ## inited_nchannels, port, converter, classinited: private
13 frameratelist
= nchannelslist
= sampwidthlist
= None
17 self
.frameratelist
= [
18 (48000, AL
.RATE_48000
),
19 (44100, AL
.RATE_44100
),
20 (32000, AL
.RATE_32000
),
21 (22050, AL
.RATE_22050
),
22 (16000, AL
.RATE_16000
),
23 (11025, AL
.RATE_11025
),
24 ( 8000, AL
.RATE_8000
),
26 self
.nchannelslist
= [
31 self
.sampwidthlist
= [
40 if not self
.classinited
:
43 self
.params
= [AL
.OUTPUT_RATE
, 0]
44 self
.config
= al
.newconfig()
45 self
.inited_outrate
= 0
47 self
.inited_nchannels
= 0
57 al
.setparams(AL
.DEFAULT_DEVICE
, self
.oldparams
)
64 while self
.port
.getfilled() > 0:
74 al
.setparams(AL
.DEFAULT_DEVICE
, self
.oldparams
)
77 def setoutrate(self
, rate
):
78 for (raw
, cooked
) in self
.frameratelist
:
80 self
.params
[1] = cooked
81 self
.inited_outrate
= 1
84 raise error
, 'bad output rate'
86 def setsampwidth(self
, width
):
87 for (raw
, cooked
) in self
.sampwidthlist
:
89 self
.config
.setwidth(cooked
)
96 self
.config
.setwidth(AL
.SAMPLE_16
)
97 self
.converter
= self
.ulaw2lin
99 raise error
, 'bad sample width'
101 def setnchannels(self
, nchannels
):
102 for (raw
, cooked
) in self
.nchannelslist
:
104 self
.config
.setchannels(cooked
)
105 self
.inited_nchannels
= 1
108 raise error
, 'bad # of channels'
110 def writeframes(self
, data
):
111 if not (self
.inited_outrate
and self
.inited_nchannels
):
112 raise error
, 'params not specified'
115 self
.port
= al
.openport('Python', 'w', self
.config
)
116 self
.oldparams
= self
.params
[:]
117 al
.getparams(AL
.DEFAULT_DEVICE
, self
.oldparams
)
118 al
.setparams(AL
.DEFAULT_DEVICE
, self
.params
)
120 data
= self
.converter(data
)
121 self
.port
.writesamps(data
)
125 return self
.port
.getfilled()
129 def getfillable(self
):
131 return self
.port
.getfillable()
133 return self
.config
.getqueuesize()
136 ## if 0: access *: private
138 def ulaw2lin(self
, data
):
140 return audioop
.ulaw2lin(data
, 2)
142 class Play_Audio_sun
:
143 ## if 0: access outrate, sampwidth, nchannels, inited_outrate, inited_width, \
144 ## inited_nchannels, converter: private
150 self
.inited_outrate
= 0
151 self
.inited_width
= 0
152 self
.inited_nchannels
= 0
153 self
.converter
= None
160 def setoutrate(self
, rate
):
162 self
.inited_outrate
= 1
164 def setsampwidth(self
, width
):
165 self
.sampwidth
= width
166 self
.inited_width
= 1
168 def setnchannels(self
, nchannels
):
169 self
.nchannels
= nchannels
170 self
.inited_nchannels
= 1
172 def writeframes(self
, data
):
173 if not (self
.inited_outrate
and self
.inited_width
and self
.inited_nchannels
):
174 raise error
, 'params not specified'
176 import sunaudiodev
, SUNAUDIODEV
177 self
.port
= sunaudiodev
.open('w')
178 info
= self
.port
.getinfo()
179 info
.o_sample_rate
= self
.outrate
180 info
.o_channels
= self
.nchannels
181 if self
.sampwidth
== 0:
183 self
.o_encoding
= SUNAUDIODEV
.ENCODING_ULAW
184 # XXX Hack, hack -- leave defaults
186 info
.o_precision
= 8 * self
.sampwidth
187 info
.o_encoding
= SUNAUDIODEV
.ENCODING_LINEAR
188 self
.port
.setinfo(info
)
190 data
= self
.converter(data
)
191 self
.port
.write(data
)
207 return self
.port
.obufcount()
211 def getfillable(self
):
212 return BUFFERSIZE
- self
.getfilled()
215 # Dynamically try to import and use a platform specific module.
221 return Play_Audio_sun()
226 raise error
, 'no audio device'
228 return Audio_mac
.Play_Audio_mac()
230 return Play_Audio_sgi()
237 fn
= 'f:just samples:just.aif'
239 af
= aifc
.open(fn
, 'r')
240 print fn
, af
.getparams()
242 p
.setoutrate(af
.getframerate())
243 p
.setsampwidth(af
.getsampwidth())
244 p
.setnchannels(af
.getnchannels())
245 BUFSIZ
= af
.getframerate()/af
.getsampwidth()/af
.getnchannels()
247 data
= af
.readframes(BUFSIZ
)
253 if __name__
== '__main__':