blob: 234514597ca0b163128f8ccc707f14175bb26201 [file] [log] [blame]
Guido van Rossum4acc25b2000-02-02 15:10:15 +00001"""Classes for manipulating audio devices (currently only for Sun and SGI)"""
2
Skip Montanaroe99d5ea2001-01-20 19:54:20 +00003__all__ = ["error","AudioDev"]
4
Fred Drake227b1202000-08-17 05:06:49 +00005class error(Exception):
Tim Peters146965a2001-01-14 18:09:23 +00006 pass
Sjoerd Mullenderaa148371993-12-17 15:18:37 +00007
8class Play_Audio_sgi:
Tim Peters146965a2001-01-14 18:09:23 +00009 # Private instance variables
10## if 0: access frameratelist, nchannelslist, sampwidthlist, oldparams, \
11## params, config, inited_outrate, inited_width, \
12## inited_nchannels, port, converter, classinited: private
Sjoerd Mullenderaa148371993-12-17 15:18:37 +000013
Tim Peters146965a2001-01-14 18:09:23 +000014 classinited = 0
15 frameratelist = nchannelslist = sampwidthlist = None
Guido van Rossumb6775db1994-08-01 11:34:53 +000016
Tim Peters146965a2001-01-14 18:09:23 +000017 def initclass(self):
18 import AL
19 self.frameratelist = [
20 (48000, AL.RATE_48000),
21 (44100, AL.RATE_44100),
22 (32000, AL.RATE_32000),
23 (22050, AL.RATE_22050),
24 (16000, AL.RATE_16000),
25 (11025, AL.RATE_11025),
26 ( 8000, AL.RATE_8000),
27 ]
28 self.nchannelslist = [
29 (1, AL.MONO),
30 (2, AL.STEREO),
31 (4, AL.QUADRO),
32 ]
33 self.sampwidthlist = [
34 (1, AL.SAMPLE_8),
35 (2, AL.SAMPLE_16),
36 (3, AL.SAMPLE_24),
37 ]
38 self.classinited = 1
Sjoerd Mullenderaa148371993-12-17 15:18:37 +000039
Tim Peters146965a2001-01-14 18:09:23 +000040 def __init__(self):
41 import al, AL
42 if not self.classinited:
43 self.initclass()
44 self.oldparams = []
45 self.params = [AL.OUTPUT_RATE, 0]
46 self.config = al.newconfig()
47 self.inited_outrate = 0
48 self.inited_width = 0
49 self.inited_nchannels = 0
50 self.converter = None
51 self.port = None
52 return
Sjoerd Mullenderaa148371993-12-17 15:18:37 +000053
Tim Peters146965a2001-01-14 18:09:23 +000054 def __del__(self):
55 if self.port:
56 self.stop()
57 if self.oldparams:
58 import al, AL
59 al.setparams(AL.DEFAULT_DEVICE, self.oldparams)
60 self.oldparams = []
Sjoerd Mullenderaa148371993-12-17 15:18:37 +000061
Tim Peters146965a2001-01-14 18:09:23 +000062 def wait(self):
63 if not self.port:
64 return
65 import time
66 while self.port.getfilled() > 0:
67 time.sleep(0.1)
68 self.stop()
Sjoerd Mullenderaa148371993-12-17 15:18:37 +000069
Tim Peters146965a2001-01-14 18:09:23 +000070 def stop(self):
71 if self.port:
72 self.port.closeport()
73 self.port = None
74 if self.oldparams:
75 import al, AL
76 al.setparams(AL.DEFAULT_DEVICE, self.oldparams)
77 self.oldparams = []
Sjoerd Mullenderaa148371993-12-17 15:18:37 +000078
Tim Peters146965a2001-01-14 18:09:23 +000079 def setoutrate(self, rate):
80 for (raw, cooked) in self.frameratelist:
81 if rate == raw:
82 self.params[1] = cooked
83 self.inited_outrate = 1
84 break
85 else:
86 raise error, 'bad output rate'
Sjoerd Mullenderaa148371993-12-17 15:18:37 +000087
Tim Peters146965a2001-01-14 18:09:23 +000088 def setsampwidth(self, width):
89 for (raw, cooked) in self.sampwidthlist:
90 if width == raw:
91 self.config.setwidth(cooked)
92 self.inited_width = 1
93 break
94 else:
95 if width == 0:
96 import AL
97 self.inited_width = 0
98 self.config.setwidth(AL.SAMPLE_16)
99 self.converter = self.ulaw2lin
100 else:
101 raise error, 'bad sample width'
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000102
Tim Peters146965a2001-01-14 18:09:23 +0000103 def setnchannels(self, nchannels):
104 for (raw, cooked) in self.nchannelslist:
105 if nchannels == raw:
106 self.config.setchannels(cooked)
107 self.inited_nchannels = 1
108 break
109 else:
110 raise error, 'bad # of channels'
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000111
Tim Peters146965a2001-01-14 18:09:23 +0000112 def writeframes(self, data):
113 if not (self.inited_outrate and self.inited_nchannels):
114 raise error, 'params not specified'
115 if not self.port:
116 import al, AL
117 self.port = al.openport('Python', 'w', self.config)
118 self.oldparams = self.params[:]
119 al.getparams(AL.DEFAULT_DEVICE, self.oldparams)
120 al.setparams(AL.DEFAULT_DEVICE, self.params)
121 if self.converter:
122 data = self.converter(data)
123 self.port.writesamps(data)
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000124
Tim Peters146965a2001-01-14 18:09:23 +0000125 def getfilled(self):
126 if self.port:
127 return self.port.getfilled()
128 else:
129 return 0
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000130
Tim Peters146965a2001-01-14 18:09:23 +0000131 def getfillable(self):
132 if self.port:
133 return self.port.getfillable()
134 else:
135 return self.config.getqueuesize()
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000136
Tim Peters146965a2001-01-14 18:09:23 +0000137 # private methods
138## if 0: access *: private
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000139
Tim Peters146965a2001-01-14 18:09:23 +0000140 def ulaw2lin(self, data):
141 import audioop
142 return audioop.ulaw2lin(data, 2)
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000143
144class Play_Audio_sun:
Tim Peters146965a2001-01-14 18:09:23 +0000145## if 0: access outrate, sampwidth, nchannels, inited_outrate, inited_width, \
146## inited_nchannels, converter: private
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000147
Tim Peters146965a2001-01-14 18:09:23 +0000148 def __init__(self):
149 self.outrate = 0
150 self.sampwidth = 0
151 self.nchannels = 0
152 self.inited_outrate = 0
153 self.inited_width = 0
154 self.inited_nchannels = 0
155 self.converter = None
156 self.port = None
157 return
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000158
Tim Peters146965a2001-01-14 18:09:23 +0000159 def __del__(self):
160 self.stop()
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000161
Tim Peters146965a2001-01-14 18:09:23 +0000162 def setoutrate(self, rate):
163 self.outrate = rate
164 self.inited_outrate = 1
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000165
Tim Peters146965a2001-01-14 18:09:23 +0000166 def setsampwidth(self, width):
167 self.sampwidth = width
168 self.inited_width = 1
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000169
Tim Peters146965a2001-01-14 18:09:23 +0000170 def setnchannels(self, nchannels):
171 self.nchannels = nchannels
172 self.inited_nchannels = 1
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000173
Tim Peters146965a2001-01-14 18:09:23 +0000174 def writeframes(self, data):
175 if not (self.inited_outrate and self.inited_width and self.inited_nchannels):
176 raise error, 'params not specified'
177 if not self.port:
178 import sunaudiodev, SUNAUDIODEV
179 self.port = sunaudiodev.open('w')
180 info = self.port.getinfo()
181 info.o_sample_rate = self.outrate
182 info.o_channels = self.nchannels
183 if self.sampwidth == 0:
184 info.o_precision = 8
185 self.o_encoding = SUNAUDIODEV.ENCODING_ULAW
186 # XXX Hack, hack -- leave defaults
187 else:
188 info.o_precision = 8 * self.sampwidth
189 info.o_encoding = SUNAUDIODEV.ENCODING_LINEAR
190 self.port.setinfo(info)
191 if self.converter:
192 data = self.converter(data)
193 self.port.write(data)
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000194
Tim Peters146965a2001-01-14 18:09:23 +0000195 def wait(self):
196 if not self.port:
197 return
198 self.port.drain()
199 self.stop()
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000200
Tim Peters146965a2001-01-14 18:09:23 +0000201 def stop(self):
202 if self.port:
203 self.port.flush()
204 self.port.close()
205 self.port = None
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000206
Tim Peters146965a2001-01-14 18:09:23 +0000207 def getfilled(self):
208 if self.port:
209 return self.port.obufcount()
210 else:
211 return 0
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000212
Tim Peters146965a2001-01-14 18:09:23 +0000213 def getfillable(self):
214 return BUFFERSIZE - self.getfilled()
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000215
216def AudioDev():
Tim Peters146965a2001-01-14 18:09:23 +0000217 # Dynamically try to import and use a platform specific module.
218 try:
219 import al
220 except ImportError:
221 try:
222 import sunaudiodev
223 return Play_Audio_sun()
224 except ImportError:
225 try:
226 import Audio_mac
227 except ImportError:
228 raise error, 'no audio device'
229 else:
230 return Audio_mac.Play_Audio_mac()
231 else:
232 return Play_Audio_sgi()
Guido van Rossume174c151994-09-16 10:55:53 +0000233
Guido van Rossum27eb14d1996-12-13 00:19:56 +0000234def test(fn = None):
Tim Peters146965a2001-01-14 18:09:23 +0000235 import sys
236 if sys.argv[1:]:
237 fn = sys.argv[1]
238 else:
239 fn = 'f:just samples:just.aif'
240 import aifc
241 af = aifc.open(fn, 'r')
242 print fn, af.getparams()
243 p = AudioDev()
244 p.setoutrate(af.getframerate())
245 p.setsampwidth(af.getsampwidth())
246 p.setnchannels(af.getnchannels())
247 BUFSIZ = af.getframerate()/af.getsampwidth()/af.getnchannels()
248 while 1:
249 data = af.readframes(BUFSIZ)
250 if not data: break
251 print len(data)
252 p.writeframes(data)
253 p.wait()
Guido van Rossume174c151994-09-16 10:55:53 +0000254
255if __name__ == '__main__':
Tim Peters146965a2001-01-14 18:09:23 +0000256 test()