blob: b6831a692f5d6f6170e11ce7592298b607c6539f [file] [log] [blame]
Guido van Rossum4acc25b2000-02-02 15:10:15 +00001"""Classes for manipulating audio devices (currently only for Sun and SGI)"""
Brett Cannone5d2cba2008-05-06 23:23:34 +00002from warnings import warnpy3k
3warnpy3k("the audiodev module has been removed in Python 3.0", stacklevel=2)
4del warnpy3k
Guido van Rossum4acc25b2000-02-02 15:10:15 +00005
Skip Montanaroe99d5ea2001-01-20 19:54:20 +00006__all__ = ["error","AudioDev"]
7
Fred Drake227b1202000-08-17 05:06:49 +00008class error(Exception):
Tim Peters146965a2001-01-14 18:09:23 +00009 pass
Sjoerd Mullenderaa148371993-12-17 15:18:37 +000010
11class Play_Audio_sgi:
Tim Peters146965a2001-01-14 18:09:23 +000012 # 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
Sjoerd Mullenderaa148371993-12-17 15:18:37 +000016
Tim Peters146965a2001-01-14 18:09:23 +000017 classinited = 0
18 frameratelist = nchannelslist = sampwidthlist = None
Guido van Rossumb6775db1994-08-01 11:34:53 +000019
Tim Peters146965a2001-01-14 18:09:23 +000020 def initclass(self):
21 import AL
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),
30 ]
31 self.nchannelslist = [
32 (1, AL.MONO),
33 (2, AL.STEREO),
34 (4, AL.QUADRO),
35 ]
36 self.sampwidthlist = [
37 (1, AL.SAMPLE_8),
38 (2, AL.SAMPLE_16),
39 (3, AL.SAMPLE_24),
40 ]
41 self.classinited = 1
Sjoerd Mullenderaa148371993-12-17 15:18:37 +000042
Tim Peters146965a2001-01-14 18:09:23 +000043 def __init__(self):
44 import al, AL
45 if not self.classinited:
46 self.initclass()
47 self.oldparams = []
48 self.params = [AL.OUTPUT_RATE, 0]
49 self.config = al.newconfig()
50 self.inited_outrate = 0
51 self.inited_width = 0
52 self.inited_nchannels = 0
53 self.converter = None
54 self.port = None
55 return
Sjoerd Mullenderaa148371993-12-17 15:18:37 +000056
Tim Peters146965a2001-01-14 18:09:23 +000057 def __del__(self):
58 if self.port:
59 self.stop()
60 if self.oldparams:
61 import al, AL
62 al.setparams(AL.DEFAULT_DEVICE, self.oldparams)
63 self.oldparams = []
Sjoerd Mullenderaa148371993-12-17 15:18:37 +000064
Tim Peters146965a2001-01-14 18:09:23 +000065 def wait(self):
66 if not self.port:
67 return
68 import time
69 while self.port.getfilled() > 0:
70 time.sleep(0.1)
71 self.stop()
Sjoerd Mullenderaa148371993-12-17 15:18:37 +000072
Tim Peters146965a2001-01-14 18:09:23 +000073 def stop(self):
74 if self.port:
75 self.port.closeport()
76 self.port = None
77 if self.oldparams:
78 import al, AL
79 al.setparams(AL.DEFAULT_DEVICE, self.oldparams)
80 self.oldparams = []
Sjoerd Mullenderaa148371993-12-17 15:18:37 +000081
Tim Peters146965a2001-01-14 18:09:23 +000082 def setoutrate(self, rate):
83 for (raw, cooked) in self.frameratelist:
84 if rate == raw:
85 self.params[1] = cooked
86 self.inited_outrate = 1
87 break
88 else:
89 raise error, 'bad output rate'
Sjoerd Mullenderaa148371993-12-17 15:18:37 +000090
Tim Peters146965a2001-01-14 18:09:23 +000091 def setsampwidth(self, width):
92 for (raw, cooked) in self.sampwidthlist:
93 if width == raw:
94 self.config.setwidth(cooked)
95 self.inited_width = 1
96 break
97 else:
98 if width == 0:
99 import AL
100 self.inited_width = 0
101 self.config.setwidth(AL.SAMPLE_16)
102 self.converter = self.ulaw2lin
103 else:
104 raise error, 'bad sample width'
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000105
Tim Peters146965a2001-01-14 18:09:23 +0000106 def setnchannels(self, nchannels):
107 for (raw, cooked) in self.nchannelslist:
108 if nchannels == raw:
109 self.config.setchannels(cooked)
110 self.inited_nchannels = 1
111 break
112 else:
113 raise error, 'bad # of channels'
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000114
Tim Peters146965a2001-01-14 18:09:23 +0000115 def writeframes(self, data):
116 if not (self.inited_outrate and self.inited_nchannels):
117 raise error, 'params not specified'
118 if not self.port:
119 import al, AL
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)
124 if self.converter:
125 data = self.converter(data)
126 self.port.writesamps(data)
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000127
Tim Peters146965a2001-01-14 18:09:23 +0000128 def getfilled(self):
129 if self.port:
130 return self.port.getfilled()
131 else:
132 return 0
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000133
Tim Peters146965a2001-01-14 18:09:23 +0000134 def getfillable(self):
135 if self.port:
136 return self.port.getfillable()
137 else:
138 return self.config.getqueuesize()
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000139
Tim Peters146965a2001-01-14 18:09:23 +0000140 # private methods
141## if 0: access *: private
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000142
Tim Peters146965a2001-01-14 18:09:23 +0000143 def ulaw2lin(self, data):
144 import audioop
145 return audioop.ulaw2lin(data, 2)
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000146
147class Play_Audio_sun:
Tim Peters146965a2001-01-14 18:09:23 +0000148## if 0: access outrate, sampwidth, nchannels, inited_outrate, inited_width, \
149## inited_nchannels, converter: private
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000150
Tim Peters146965a2001-01-14 18:09:23 +0000151 def __init__(self):
152 self.outrate = 0
153 self.sampwidth = 0
154 self.nchannels = 0
155 self.inited_outrate = 0
156 self.inited_width = 0
157 self.inited_nchannels = 0
158 self.converter = None
159 self.port = None
160 return
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000161
Tim Peters146965a2001-01-14 18:09:23 +0000162 def __del__(self):
163 self.stop()
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000164
Tim Peters146965a2001-01-14 18:09:23 +0000165 def setoutrate(self, rate):
166 self.outrate = rate
167 self.inited_outrate = 1
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000168
Tim Peters146965a2001-01-14 18:09:23 +0000169 def setsampwidth(self, width):
170 self.sampwidth = width
171 self.inited_width = 1
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000172
Tim Peters146965a2001-01-14 18:09:23 +0000173 def setnchannels(self, nchannels):
174 self.nchannels = nchannels
175 self.inited_nchannels = 1
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000176
Tim Peters146965a2001-01-14 18:09:23 +0000177 def writeframes(self, data):
178 if not (self.inited_outrate and self.inited_width and self.inited_nchannels):
179 raise error, 'params not specified'
180 if not self.port:
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:
187 info.o_precision = 8
188 self.o_encoding = SUNAUDIODEV.ENCODING_ULAW
189 # XXX Hack, hack -- leave defaults
190 else:
191 info.o_precision = 8 * self.sampwidth
192 info.o_encoding = SUNAUDIODEV.ENCODING_LINEAR
193 self.port.setinfo(info)
194 if self.converter:
195 data = self.converter(data)
196 self.port.write(data)
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000197
Tim Peters146965a2001-01-14 18:09:23 +0000198 def wait(self):
199 if not self.port:
200 return
201 self.port.drain()
202 self.stop()
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000203
Tim Peters146965a2001-01-14 18:09:23 +0000204 def stop(self):
205 if self.port:
206 self.port.flush()
207 self.port.close()
208 self.port = None
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000209
Tim Peters146965a2001-01-14 18:09:23 +0000210 def getfilled(self):
211 if self.port:
212 return self.port.obufcount()
213 else:
214 return 0
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000215
Guido van Rossum87106812001-08-13 15:04:33 +0000216## # Nobody remembers what this method does, and it's broken. :-(
217## def getfillable(self):
218## return BUFFERSIZE - self.getfilled()
Sjoerd Mullenderaa148371993-12-17 15:18:37 +0000219
220def AudioDev():
Tim Peters146965a2001-01-14 18:09:23 +0000221 # Dynamically try to import and use a platform specific module.
222 try:
223 import al
224 except ImportError:
225 try:
226 import sunaudiodev
227 return Play_Audio_sun()
228 except ImportError:
229 try:
230 import Audio_mac
231 except ImportError:
232 raise error, 'no audio device'
233 else:
234 return Audio_mac.Play_Audio_mac()
235 else:
236 return Play_Audio_sgi()
Guido van Rossume174c151994-09-16 10:55:53 +0000237
Guido van Rossum27eb14d1996-12-13 00:19:56 +0000238def test(fn = None):
Tim Peters146965a2001-01-14 18:09:23 +0000239 import sys
240 if sys.argv[1:]:
241 fn = sys.argv[1]
242 else:
243 fn = 'f:just samples:just.aif'
244 import aifc
245 af = aifc.open(fn, 'r')
246 print fn, af.getparams()
247 p = AudioDev()
248 p.setoutrate(af.getframerate())
249 p.setsampwidth(af.getsampwidth())
250 p.setnchannels(af.getnchannels())
251 BUFSIZ = af.getframerate()/af.getsampwidth()/af.getnchannels()
252 while 1:
253 data = af.readframes(BUFSIZ)
254 if not data: break
255 print len(data)
256 p.writeframes(data)
257 p.wait()
Guido van Rossume174c151994-09-16 10:55:53 +0000258
259if __name__ == '__main__':
Tim Peters146965a2001-01-14 18:09:23 +0000260 test()