Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 1 | """Classes for manipulating audio devices (currently only for Sun and SGI)""" |
Brett Cannon | e5d2cba | 2008-05-06 23:23:34 +0000 | [diff] [blame] | 2 | from warnings import warnpy3k |
| 3 | warnpy3k("the audiodev module has been removed in Python 3.0", stacklevel=2) |
| 4 | del warnpy3k |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 5 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 6 | __all__ = ["error","AudioDev"] |
| 7 | |
Fred Drake | 227b120 | 2000-08-17 05:06:49 +0000 | [diff] [blame] | 8 | class error(Exception): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 9 | pass |
Sjoerd Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 10 | |
| 11 | class Play_Audio_sgi: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 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 |
Sjoerd Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 16 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 17 | classinited = 0 |
| 18 | frameratelist = nchannelslist = sampwidthlist = None |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 19 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 20 | 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 Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 42 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 43 | 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 Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 56 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 57 | 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 Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 64 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 65 | 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 Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 72 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 73 | 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 Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 81 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 82 | 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 Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 90 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 91 | 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 Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 105 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 106 | 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 Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 114 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 115 | 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 Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 127 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 128 | def getfilled(self): |
| 129 | if self.port: |
| 130 | return self.port.getfilled() |
| 131 | else: |
| 132 | return 0 |
Sjoerd Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 133 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 134 | def getfillable(self): |
| 135 | if self.port: |
| 136 | return self.port.getfillable() |
| 137 | else: |
| 138 | return self.config.getqueuesize() |
Sjoerd Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 139 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 140 | # private methods |
| 141 | ## if 0: access *: private |
Sjoerd Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 142 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 143 | def ulaw2lin(self, data): |
| 144 | import audioop |
| 145 | return audioop.ulaw2lin(data, 2) |
Sjoerd Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 146 | |
| 147 | class Play_Audio_sun: |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 148 | ## if 0: access outrate, sampwidth, nchannels, inited_outrate, inited_width, \ |
| 149 | ## inited_nchannels, converter: private |
Sjoerd Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 150 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 151 | 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 Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 161 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 162 | def __del__(self): |
| 163 | self.stop() |
Sjoerd Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 164 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 165 | def setoutrate(self, rate): |
| 166 | self.outrate = rate |
| 167 | self.inited_outrate = 1 |
Sjoerd Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 168 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 169 | def setsampwidth(self, width): |
| 170 | self.sampwidth = width |
| 171 | self.inited_width = 1 |
Sjoerd Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 172 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 173 | def setnchannels(self, nchannels): |
| 174 | self.nchannels = nchannels |
| 175 | self.inited_nchannels = 1 |
Sjoerd Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 176 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 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' |
| 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 Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 197 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 198 | def wait(self): |
| 199 | if not self.port: |
| 200 | return |
| 201 | self.port.drain() |
| 202 | self.stop() |
Sjoerd Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 203 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 204 | def stop(self): |
| 205 | if self.port: |
| 206 | self.port.flush() |
| 207 | self.port.close() |
| 208 | self.port = None |
Sjoerd Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 209 | |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 210 | def getfilled(self): |
| 211 | if self.port: |
| 212 | return self.port.obufcount() |
| 213 | else: |
| 214 | return 0 |
Sjoerd Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | 8710681 | 2001-08-13 15:04:33 +0000 | [diff] [blame] | 216 | ## # Nobody remembers what this method does, and it's broken. :-( |
| 217 | ## def getfillable(self): |
| 218 | ## return BUFFERSIZE - self.getfilled() |
Sjoerd Mullender | aa14837 | 1993-12-17 15:18:37 +0000 | [diff] [blame] | 219 | |
| 220 | def AudioDev(): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 221 | # 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 Rossum | e174c15 | 1994-09-16 10:55:53 +0000 | [diff] [blame] | 237 | |
Guido van Rossum | 27eb14d | 1996-12-13 00:19:56 +0000 | [diff] [blame] | 238 | def test(fn = None): |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 239 | 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 Rossum | e174c15 | 1994-09-16 10:55:53 +0000 | [diff] [blame] | 258 | |
| 259 | if __name__ == '__main__': |
Tim Peters | 146965a | 2001-01-14 18:09:23 +0000 | [diff] [blame] | 260 | test() |