-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy patherror.py
More file actions
164 lines (138 loc) · 3.86 KB
/
Copy patherror.py
File metadata and controls
164 lines (138 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"""
PyAudio Example: Test for a variety of error conditions. This example
demonstrates exception handling with PyAudio.
"""
import pyaudio
p = pyaudio.PyAudio()
# get invalid sample size
try:
p.get_sample_size(10)
except ValueError as e:
assert e.args[1] == pyaudio.paSampleFormatNotSupported
print("OK: %s" % e.args[0])
else:
assert False, "sample size"
# get format from invalid width
try:
p.get_format_from_width(8)
except ValueError as e:
print("OK: invalid format from width")
else:
assert False, "invalid format"
# try to get an invalid device
try:
p.get_host_api_info_by_type(-1)
except IOError as e:
assert e.args[1] == pyaudio.paHostApiNotFound
print("OK: %s" % e.args[0])
else:
assert False, "invalid host type"
# try to get host api info by index
try:
p.get_host_api_info_by_index(-1)
except IOError as e:
assert e.args[1] == pyaudio.paInvalidHostApi
print("OK: %s" % e.args[0])
else:
assert False, "invalid host api index"
# try good host api device index
try:
p.get_device_info_by_host_api_device_index(0, -1)
except IOError as e:
assert ((e.args[1] == pyaudio.paInvalidDevice) or \
(e.args[1] == pyaudio.paInvalidHostApi))
print("OK: %s" % e.args[0])
else:
assert False, "device info by host api device idnex"
# try bad host api and good device index
try:
p.get_device_info_by_host_api_device_index(-1, 0)
except IOError as e:
assert ((e.args[1] == pyaudio.paInvalidDevice) or \
(e.args[1] == pyaudio.paInvalidHostApi))
print("OK: %s" % e.args[0])
else:
assert False, "device info by host api device idnex"
# bad device index
try:
p.get_device_info_by_index(-1)
except IOError as e:
assert e.args[1] == pyaudio.paInvalidDevice
print("OK: %s" % e.args[0])
else:
assert False, "bad device index"
### now for some real work ###
stream = p.open(channels = 1,
rate = 44100,
format = pyaudio.paInt16,
input = True,
start = False)
# (note that we didn't start the stream!)
try:
data = stream.read(2)
except IOError as e:
print("OK: %s" % e.args[0])
assert e.args[1] == pyaudio.paStreamIsStopped, e.args[1]
else:
assert False, "Should have caused exception"
stream.start_stream()
# try to write to the input stream
try:
stream.write('foobar')
except IOError as e:
assert e.args[1] == pyaudio.paCanNotWriteToAnInputOnlyStream
print("OK: %s" % e.args[0])
else:
assert False, "write to input stream"
# read some negative data
try:
data = stream.read(-1)
except ValueError as e:
print("OK: Invalid frames")
else:
assert False, "invalid frames"
# read some real data
try:
data = stream.read(2)
except IOError as e:
# some slower machines might overflow
assert e.args[1] == pyaudio.paInputOverflowed, e
print("OK: %s" % e.args[0])
else:
print("OK: got %d bytes of data" % len(data))
# close the stream; nothing should work with
# this stream afterwards
stream.close()
# query for properties
try:
stream.get_input_latency()
except IOError as e:
assert e.args[1] == pyaudio.paBadStreamPtr
print("OK: %s" % e.args[0])
else:
assert False, "closed stream"
# read some data again
try:
stream.read(10)
except IOError as e:
assert e.args[1] == pyaudio.paBadStreamPtr
print("OK: %s" % e.args[0])
else:
assert False, "closed stream"
# get invalid stream capabilities
try:
p.is_format_supported(8000, -1, 1, pyaudio.paInt16)
except ValueError as e:
assert e.args[1] == pyaudio.paInvalidDevice
print("OK: %s" % e.args[0])
else:
assert False, "invalid device"
# get invalid stream capabilities
try:
p.is_format_supported(8000, 0, -1, pyaudio.paInt16)
except ValueError as e:
assert e.args[1] == pyaudio.paInvalidChannelCount
print("OK: %s" % e.args[0])
else:
assert False, "invalid number of channels"
p.terminate()