blob: adfeb96a551342cd29dc52885e26592016ce86f1 [file] [log] [blame]
Guido van Rossum17448e21995-01-30 11:53:55 +00001error = 'audiodev.error'
2
3class Play_Audio_sgi:
4 # Private instance variables
5 access frameratelist, nchannelslist, sampwidthlist, oldparams, \
6 params, config, inited_outrate, inited_width, \
7 inited_nchannels, port, converter, classinited: private
8
9 classinited = 0
10 frameratelist = nchannelslist = sampwidthlist = None
11
12 def initclass(self):
13 import AL
14 Play_Audio_sgi.frameratelist = [
15 (48000, AL.RATE_48000),
16 (44100, AL.RATE_44100),
17 (32000, AL.RATE_32000),
18 (22050, AL.RATE_22050),
19 (16000, AL.RATE_16000),
20 (11025, AL.RATE_11025),
21 ( 8000, AL.RATE_8000),
22 ]
23 Play_Audio_sgi.nchannelslist = [
24 (1, AL.MONO),
25 (2, AL.STEREO),
26 ]
27 Play_Audio_sgi.sampwidthlist = [
28 (1, AL.SAMPLE_8),
29 (2, AL.SAMPLE_16),
30 (3, AL.SAMPLE_24),
31 ]
32 Play_Audio_sgi.classinited = 1
33
34 def __init__(self):
35 import al, AL
36 if not self.classinited:
37 self.initclass()
38 self.oldparams = []
39 self.params = [AL.OUTPUT_RATE, 0]
40 self.config = al.newconfig()
41 self.inited_outrate = 0
42 self.inited_width = 0
43 self.inited_nchannels = 0
44 self.converter = None
45 self.port = None
46 return
47
48 def __del__(self):
49 if self.port:
50 self.stop()
51 if self.oldparams:
52 import al, AL
53 al.setparams(AL.DEFAULT_DEVICE, self.oldparams)
54 self.oldparams = []
55
56 def wait(self):
57 if not self.port:
58 return
59 import time
60 while self.port.getfilled() > 0:
61 time.sleep(0.1)
62 self.stop()
63
64 def stop(self):
65 if self.port:
66 self.port.closeport()
67 self.port = None
68 if self.oldparams:
69 import al, AL
70 al.setparams(AL.DEFAULT_DEVICE, self.oldparams)
71 self.oldparams = []
72
73 def setoutrate(self, rate):
74 for (raw, cooked) in self.frameratelist:
75 if rate == raw:
76 self.params[1] = cooked
77 self.inited_outrate = 1
78 break
79 else:
80 raise error, 'bad output rate'
81
82 def setsampwidth(self, width):
83 for (raw, cooked) in self.sampwidthlist:
84 if width == raw:
85 self.config.setwidth(cooked)
86 self.inited_width = 1
87 break
88 else:
89 if width == 0:
90 import AL
91 self.inited_width = 0
92 self.config.setwidth(AL.SAMPLE_16)
93 self.converter = self.ulaw2lin
94 else:
95 raise error, 'bad sample width'
96
97 def setnchannels(self, nchannels):
98 for (raw, cooked) in self.nchannelslist:
99 if nchannels == raw:
100 self.config.setchannels(cooked)
101 self.inited_nchannels = 1
102 break
103 else:
104 raise error, 'bad # of channels'
105
106 def writeframes(self, data):
107 if not (self.inited_outrate and self.inited_nchannels):
108 raise error, 'params not specified'
109 if not self.port:
110 import al, AL
111 self.port = al.openport('Python', 'w', self.config)
112 self.oldparams = self.params[:]
113 al.getparams(AL.DEFAULT_DEVICE, self.oldparams)
114 al.setparams(AL.DEFAULT_DEVICE, self.params)
115 if self.converter:
116 data = self.converter(data)
117 self.port.writesamps(data)
118
119 def getfilled(self):
120 if self.port:
121 return self.port.getfilled()
122 else:
123 return 0
124
125 def getfillable(self):
126 if self.port:
127 return self.port.getfillable()
128 else:
129 return self.config.getqueuesize()
130
131 # private methods
132 access *: private
133
134 def ulaw2lin(self, data):
135 import audioop
136 return audioop.ulaw2lin(data, 2)
137
138class Play_Audio_sun:
139 access outrate, sampwidth, nchannels, inited_outrate, inited_width, \
140 inited_nchannels, converter: private
141
142 def __init__(self):
143 self.outrate = 0
144 self.sampwidth = 0
145 self.nchannels = 0
146 self.inited_outrate = 0
147 self.inited_width = 0
148 self.inited_nchannels = 0
149 self.converter = None
150 self.port = None
151 return
152
153 def __del__(self):
154 self.stop()
155
156 def setoutrate(self, rate):
157 self.outrate = rate
158 self.inited_outrate = 1
159
160 def setsampwidth(self, width):
161 self.sampwidth = width
162 self.inited_width = 1
163
164 def setnchannels(self, nchannels):
165 self.nchannels = nchannels
166 self.inited_nchannels = 1
167
168 def writeframes(self, data):
169 if not (self.inited_outrate and self.inited_width and self.inited_nchannels):
170 raise error, 'params not specified'
171 if not self.port:
172 import sunaudiodev, SUNAUDIODEV
173 self.port = sunaudiodev.open('w')
174 info = self.port.getinfo()
175 info.o_sample_rate = self.outrate
176 info.o_channels = self.nchannels
177 if self.sampwidth == 0:
178 info.o_precision = 8
179 self.o_encoding = ENCODING_ULAW
180 else:
181 info.o_precision = 8 * self.sampwidth
182 info.o_encoding = SUNAUDIODEV.ENCODING_LINEAR
183 self.port.setinfo(info)
184 if self.converter:
185 data = self.converter(data)
186 self.port.write(data)
187
188 def wait(self):
189 if not self.port:
190 return
191 self.port.drain()
192 self.stop()
193
194 def stop(self):
195 if self.port:
196 self.port.flush()
197 self.port.close()
198 self.port = None
199
200 def getfilled(self):
201 if self.port:
202 return self.port.obufcount()
203 else:
204 return 0
205
206 def getfillable(self):
207 return BUFFERSIZE - self.getfilled()
208
209def AudioDev():
210 try:
211 import al
212 return Play_Audio_sgi()
213 except ImportError:
214 try:
215 import sunaudiodev
216 return Play_Audio_sun()
217 except ImportError:
218 try:
219 import Audio_mac
220 return Audio_mac.Play_Audio_mac()
221 except ImportError:
222 raise error, 'no audio device'
223
224def test(fn = 'f:just samples:just.aif'):
225 import aifc
226 af = aifc.open(fn, 'r')
227 print fn, af.getparams()
228 p = AudioDev()
229 p.setoutrate(af.getframerate())
230 p.setsampwidth(af.getsampwidth())
231 p.setnchannels(af.getnchannels())
232 BUFSIZ = af.getframerate()/af.getsampwidth()/af.getnchannels()
233 while 1:
234 data = af.readframes(BUFSIZ)
235 if not data: break
236 print len(data)
237 p.writeframes(data)
238 p.wait()
239
240if __name__ == '__main__':
241 test()