Mercurial > cpython-lru_cache-weakref
annotate Lib/audiodev.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 3c7ad5037ab1 |
| children | 4cdf21a0dbbc ffdb5f4139fe |
| rev | line source |
|---|---|
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10818
diff
changeset
|
1 """Classes for manipulating audio devices (currently only for Sun and SGI)""" |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
10818
diff
changeset
|
2 |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15857
diff
changeset
|
3 __all__ = ["error","AudioDev"] |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15857
diff
changeset
|
4 |
|
14022
d44603f2b928
Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents:
11691
diff
changeset
|
5 class error(Exception): |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
6 pass |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
7 |
|
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
8 class Play_Audio_sgi: |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
9 # Private instance variables |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
10 ## if 0: access frameratelist, nchannelslist, sampwidthlist, oldparams, \ |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
11 ## params, config, inited_outrate, inited_width, \ |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
12 ## inited_nchannels, port, converter, classinited: private |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1434
diff
changeset
|
13 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
14 classinited = 0 |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
15 frameratelist = nchannelslist = sampwidthlist = None |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
16 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
17 def initclass(self): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
18 import AL |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
19 self.frameratelist = [ |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
20 (48000, AL.RATE_48000), |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
21 (44100, AL.RATE_44100), |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
22 (32000, AL.RATE_32000), |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
23 (22050, AL.RATE_22050), |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
24 (16000, AL.RATE_16000), |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
25 (11025, AL.RATE_11025), |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
26 ( 8000, AL.RATE_8000), |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
27 ] |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
28 self.nchannelslist = [ |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
29 (1, AL.MONO), |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
30 (2, AL.STEREO), |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
31 (4, AL.QUADRO), |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
32 ] |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
33 self.sampwidthlist = [ |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
34 (1, AL.SAMPLE_8), |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
35 (2, AL.SAMPLE_16), |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
36 (3, AL.SAMPLE_24), |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
37 ] |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
38 self.classinited = 1 |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
39 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
40 def __init__(self): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
41 import al, AL |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
42 if not self.classinited: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
43 self.initclass() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
44 self.oldparams = [] |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
45 self.params = [AL.OUTPUT_RATE, 0] |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
46 self.config = al.newconfig() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
47 self.inited_outrate = 0 |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
48 self.inited_width = 0 |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
49 self.inited_nchannels = 0 |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
50 self.converter = None |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
51 self.port = None |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
52 return |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
53 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
54 def __del__(self): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
55 if self.port: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
56 self.stop() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
57 if self.oldparams: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
58 import al, AL |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
59 al.setparams(AL.DEFAULT_DEVICE, self.oldparams) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
60 self.oldparams = [] |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
61 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
62 def wait(self): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
63 if not self.port: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
64 return |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
65 import time |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
66 while self.port.getfilled() > 0: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
67 time.sleep(0.1) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
68 self.stop() |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
69 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
70 def stop(self): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
71 if self.port: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
72 self.port.closeport() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
73 self.port = None |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
74 if self.oldparams: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
75 import al, AL |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
76 al.setparams(AL.DEFAULT_DEVICE, self.oldparams) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
77 self.oldparams = [] |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
78 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
79 def setoutrate(self, rate): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
80 for (raw, cooked) in self.frameratelist: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
81 if rate == raw: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
82 self.params[1] = cooked |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
83 self.inited_outrate = 1 |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
84 break |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
85 else: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
86 raise error, 'bad output rate' |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
87 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
88 def setsampwidth(self, width): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
89 for (raw, cooked) in self.sampwidthlist: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
90 if width == raw: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
91 self.config.setwidth(cooked) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
92 self.inited_width = 1 |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
93 break |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
94 else: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
95 if width == 0: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
96 import AL |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
97 self.inited_width = 0 |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
98 self.config.setwidth(AL.SAMPLE_16) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
99 self.converter = self.ulaw2lin |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
100 else: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
101 raise error, 'bad sample width' |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
102 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
103 def setnchannels(self, nchannels): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
104 for (raw, cooked) in self.nchannelslist: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
105 if nchannels == raw: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
106 self.config.setchannels(cooked) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
107 self.inited_nchannels = 1 |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
108 break |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
109 else: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
110 raise error, 'bad # of channels' |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
111 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
112 def writeframes(self, data): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
113 if not (self.inited_outrate and self.inited_nchannels): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
114 raise error, 'params not specified' |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
115 if not self.port: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
116 import al, AL |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
117 self.port = al.openport('Python', 'w', self.config) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
118 self.oldparams = self.params[:] |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
119 al.getparams(AL.DEFAULT_DEVICE, self.oldparams) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
120 al.setparams(AL.DEFAULT_DEVICE, self.params) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
121 if self.converter: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
122 data = self.converter(data) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
123 self.port.writesamps(data) |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
124 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
125 def getfilled(self): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
126 if self.port: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
127 return self.port.getfilled() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
128 else: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
129 return 0 |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
130 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
131 def getfillable(self): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
132 if self.port: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
133 return self.port.getfillable() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
134 else: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
135 return self.config.getqueuesize() |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
136 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
137 # private methods |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
138 ## if 0: access *: private |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
139 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
140 def ulaw2lin(self, data): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
141 import audioop |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
142 return audioop.ulaw2lin(data, 2) |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
143 |
|
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
144 class Play_Audio_sun: |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
145 ## if 0: access outrate, sampwidth, nchannels, inited_outrate, inited_width, \ |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
146 ## inited_nchannels, converter: private |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
147 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
148 def __init__(self): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
149 self.outrate = 0 |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
150 self.sampwidth = 0 |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
151 self.nchannels = 0 |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
152 self.inited_outrate = 0 |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
153 self.inited_width = 0 |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
154 self.inited_nchannels = 0 |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
155 self.converter = None |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
156 self.port = None |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
157 return |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
158 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
159 def __del__(self): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
160 self.stop() |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
161 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
162 def setoutrate(self, rate): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
163 self.outrate = rate |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
164 self.inited_outrate = 1 |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
165 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
166 def setsampwidth(self, width): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
167 self.sampwidth = width |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
168 self.inited_width = 1 |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
169 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
170 def setnchannels(self, nchannels): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
171 self.nchannels = nchannels |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
172 self.inited_nchannels = 1 |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
173 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
174 def writeframes(self, data): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
175 if not (self.inited_outrate and self.inited_width and self.inited_nchannels): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
176 raise error, 'params not specified' |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
177 if not self.port: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
178 import sunaudiodev, SUNAUDIODEV |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
179 self.port = sunaudiodev.open('w') |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
180 info = self.port.getinfo() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
181 info.o_sample_rate = self.outrate |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
182 info.o_channels = self.nchannels |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
183 if self.sampwidth == 0: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
184 info.o_precision = 8 |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
185 self.o_encoding = SUNAUDIODEV.ENCODING_ULAW |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
186 # XXX Hack, hack -- leave defaults |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
187 else: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
188 info.o_precision = 8 * self.sampwidth |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
189 info.o_encoding = SUNAUDIODEV.ENCODING_LINEAR |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
190 self.port.setinfo(info) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
191 if self.converter: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
192 data = self.converter(data) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
193 self.port.write(data) |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
194 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
195 def wait(self): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
196 if not self.port: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
197 return |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
198 self.port.drain() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
199 self.stop() |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
200 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
201 def stop(self): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
202 if self.port: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
203 self.port.flush() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
204 self.port.close() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
205 self.port = None |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
206 |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
207 def getfilled(self): |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
208 if self.port: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
209 return self.port.obufcount() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
210 else: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
211 return 0 |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
212 |
|
19221
3c7ad5037ab1
Commenting out the getfillable() method -- it's broken, and nobody
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
213 ## # Nobody remembers what this method does, and it's broken. :-( |
|
3c7ad5037ab1
Commenting out the getfillable() method -- it's broken, and nobody
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
214 ## def getfillable(self): |
|
3c7ad5037ab1
Commenting out the getfillable() method -- it's broken, and nobody
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
215 ## return BUFFERSIZE - self.getfilled() |
|
1434
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
216 |
|
46c217337bde
Interface to audio hardware for SGI and Sun.
Sjoerd Mullender <sjoerd@acm.org>
parents:
diff
changeset
|
217 def AudioDev(): |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
218 # Dynamically try to import and use a platform specific module. |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
219 try: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
220 import al |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
221 except ImportError: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
222 try: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
223 import sunaudiodev |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
224 return Play_Audio_sun() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
225 except ImportError: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
226 try: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
227 import Audio_mac |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
228 except ImportError: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
229 raise error, 'no audio device' |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
230 else: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
231 return Audio_mac.Play_Audio_mac() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
232 else: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
233 return Play_Audio_sgi() |
|
1893
aba51621244e
audiodev.py: Mac port.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
234 |
|
4710
7b9a6166281a
Added support for 4-channel on SGI.
Guido van Rossum <guido@python.org>
parents:
4078
diff
changeset
|
235 def test(fn = None): |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
236 import sys |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
237 if sys.argv[1:]: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
238 fn = sys.argv[1] |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
239 else: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
240 fn = 'f:just samples:just.aif' |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
241 import aifc |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
242 af = aifc.open(fn, 'r') |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
243 print fn, af.getparams() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
244 p = AudioDev() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
245 p.setoutrate(af.getframerate()) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
246 p.setsampwidth(af.getsampwidth()) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
247 p.setnchannels(af.getnchannels()) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
248 BUFSIZ = af.getframerate()/af.getsampwidth()/af.getnchannels() |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
249 while 1: |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
250 data = af.readframes(BUFSIZ) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
251 if not data: break |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
252 print len(data) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
253 p.writeframes(data) |
|
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
254 p.wait() |
|
1893
aba51621244e
audiodev.py: Mac port.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
255 |
|
aba51621244e
audiodev.py: Mac port.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
256 if __name__ == '__main__': |
|
15857
0a8da6f397b7
Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents:
14022
diff
changeset
|
257 test() |
