-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_pysoundio.py
352 lines (282 loc) · 11.7 KB
/
test_pysoundio.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""
test_pysoundio.py
PySoundIo Test Suite
"""
import unittest
import pysoundio
import _soundiox
class TestPySoundIo(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.sio = pysoundio.PySoundIo(
backend=pysoundio.SoundIoBackendDummy)
@classmethod
def tearDownClass(cls):
cls.sio.close()
def setUp(self):
self.sio = pysoundio.PySoundIo(
backend=pysoundio.SoundIoBackendDummy)
self.sio.channels = 2
self.sio.sample_rate = 44100
self.sio.format = pysoundio.SoundIoFormatFloat32LE
self.sio.block_size = None
self.sio.testing = True
self.callback_called = False
self.error_callback_called = False
# - Device API
def test_backend_count(self):
self.assertIsInstance(self.sio.backend_count, int)
def test_get_default_input_device(self):
self.assertIsNotNone(self.sio.get_default_input_device())
def test_get_input_device(self):
self.assertIsNotNone(self.sio.get_input_device(0))
def test_invalid_input_device(self):
with self.assertRaises(pysoundio.PySoundIoError):
self.sio.get_input_device(100)
def test_get_default_output_device(self):
self.assertIsNotNone(self.sio.get_default_output_device())
def test_get_output_device(self):
self.assertIsNotNone(self.sio.get_output_device(0))
def test_invalid_output_device(self):
with self.assertRaises(pysoundio.PySoundIoError):
self.sio.get_output_device(100)
def test_list_devices(self):
input_devices, output_devices = self.sio.list_devices()
self.assertIsInstance(input_devices, list)
self.assertIsInstance(output_devices, list)
self.assertTrue(len(input_devices) > 0)
self.assertTrue(len(output_devices) > 0)
self.assertIsInstance(input_devices[0], dict)
self.assertIsInstance(output_devices[0], dict)
self.assertIn('id', input_devices[0])
self.assertIn('name', input_devices[0])
self.assertIn('is_raw', input_devices[0])
self.assertIn('id', output_devices[0])
self.assertIn('name', output_devices[0])
self.assertIn('is_raw', output_devices[0])
def test_supports_sample_rate(self):
device = self.sio.get_input_device(0)
self.assertTrue(self.sio.supports_sample_rate(device, 44100))
def test_get_default_sample_rate(self):
device = self.sio.get_input_device(0)
self.assertIsInstance(self.sio.get_default_sample_rate(device), int)
def test_supports_format(self):
device = self.sio.get_input_device(0)
self.assertTrue(self.sio.supports_format(device, pysoundio.SoundIoFormatFloat32LE))
def test_get_default_format(self):
device = self.sio.get_input_device(0)
self.assertIsInstance(self.sio.get_default_format(device), int)
def test_get_default_layout(self):
self.assertIsNotNone(self.sio._get_default_layout(2))
def test_bytes_per_frame(self):
self.assertEqual(self.sio.get_bytes_per_frame(
pysoundio.SoundIoFormatFloat32LE, 2), 8)
def test_bytes_per_sample(self):
self.assertEqual(self.sio.get_bytes_per_sample(
pysoundio.SoundIoFormatFloat32LE), 4)
def test_bytes_per_second(self):
self.assertEqual(self.sio.get_bytes_per_second(
pysoundio.SoundIoFormatFloat32LE, 1, 44100), 176400)
# - Input Stream API
def fill_input_buffer(self):
data = bytearray(b'\x00' * 4096 * 8)
_soundiox.ring_buffer_write_ptr(self.sio.input_buffer, data, len(data))
_soundiox.ring_buffer_advance_write_ptr(self.sio.input_buffer, len(data))
def test__create_input_ring_buffer(self):
capacity = 44100 * 8
self.assertIsNotNone(self.sio._create_input_ring_buffer(capacity))
def test__create_input_stream(self):
self.sio.get_default_input_device()
stream = self.sio._create_input_stream()
self.assertIsNotNone(stream)
def test__create_input_stream_blocksize(self):
self.sio.block_size = 4096
self.sio.get_default_input_device()
stream = self.sio._create_input_stream()
self.assertIsNotNone(stream)
def test_get_input_latency(self):
self.sio.start_input_stream(
sample_rate=44100,
dtype=pysoundio.SoundIoFormatFloat32LE,
channels=2)
self.fill_input_buffer()
self.assertIsInstance(self.sio.get_input_latency(0.2), int)
def overflow_callback(self, stream):
self.callback_called = True
def error_callback(self, stream, err):
self.error_callback_called = True
def test_overflow_callback(self):
self.sio.overflow_callback = self.overflow_callback
self.sio._overflow_callback(None)
self.assertTrue(self.callback_called)
def test_error_callback(self):
self.sio.error_callback = self.error_callback
self.sio._error_callback(None, None)
self.assertTrue(self.error_callback_called)
def test_start_input_stream(self):
self.sio.start_input_stream(
sample_rate=44100,
dtype=pysoundio.SoundIoFormatFloat32LE,
channels=2)
self.fill_input_buffer()
self.assertIsNotNone(self.sio.input_stream)
def test_pause_input_stream(self):
self.sio.start_input_stream(
sample_rate=44100,
dtype=pysoundio.SoundIoFormatFloat32LE,
channels=2)
self.fill_input_buffer()
self.sio.pause_input_stream(True)
def test_start_input_stream_device(self):
self.sio.start_input_stream(
device_id=0,
sample_rate=44100,
dtype=pysoundio.SoundIoFormatFloat32LE,
channels=2)
self.fill_input_buffer()
self.assertIsNotNone(self.sio.input_stream)
def test_start_input_invalid_rate(self):
with self.assertRaises(pysoundio.PySoundIoError):
self.sio.start_input_stream(
sample_rate=10000000,
dtype=pysoundio.SoundIoFormatFloat32LE,
channels=2)
def test_start_input_default_rate(self):
self.sio.start_input_stream(
dtype=pysoundio.SoundIoFormatFloat32LE,
channels=2)
self.fill_input_buffer()
self.assertIsNotNone(self.sio.input_stream)
self.assertIsInstance(self.sio.sample_rate, int)
def test_start_input_invalid_format(self):
with self.assertRaises(pysoundio.PySoundIoError):
self.sio.start_input_stream(
sample_rate=44100,
dtype=50,
channels=2)
def test_start_input_default_format(self):
self.sio.start_input_stream(
sample_rate=44100,
channels=2)
self.fill_input_buffer()
self.assertIsNotNone(self.sio.input_stream)
self.assertIsInstance(self.sio.format, int)
# -- Output Stream API
def test__create_output_ring_buffer(self):
capacity = 44100 * 8
self.assertIsNotNone(self.sio._create_output_ring_buffer(capacity))
def test__create_output_stream(self):
self.sio.get_default_output_device()
stream = self.sio._create_output_stream()
self.assertIsNotNone(stream)
def test__create_output_stream_blocksize(self):
self.sio.block_size = 4096
self.sio.get_default_output_device()
stream = self.sio._create_output_stream()
self.assertIsNotNone(stream)
def test__open_output_stream(self):
self.sio.get_default_output_device()
self.sio._create_output_stream()
self.sio._open_output_stream()
self.assertIsNotNone(self.sio.block_size)
def test_clear_output_buffer(self):
capacity = 44100 * 8
self.assertIsNotNone(self.sio._create_output_ring_buffer(capacity))
self.sio._clear_output_buffer()
def underflow_callback(self, stream):
self.callback_called = True
def test_underflow_callback(self):
self.sio.underflow_callback = self.underflow_callback
self.sio._underflow_callback(None)
self.assertTrue(self.callback_called)
def test_get_output_latency(self):
self.sio.start_output_stream(
sample_rate=44100,
dtype=pysoundio.SoundIoFormatFloat32LE,
channels=2)
self.assertIsInstance(self.sio.get_output_latency(0.2), int)
def test_pause_output_stream(self):
self.sio.start_output_stream(
sample_rate=44100,
dtype=pysoundio.SoundIoFormatFloat32LE,
channels=2)
self.sio.pause_output_stream(True)
def test_start_output_stream(self):
self.sio.start_output_stream(
sample_rate=44100,
dtype=pysoundio.SoundIoFormatFloat32LE,
channels=2)
self.assertIsNotNone(self.sio.output_stream)
def test_start_output_stream_device(self):
self.sio.start_output_stream(
device_id=0,
sample_rate=44100,
dtype=pysoundio.SoundIoFormatFloat32LE,
channels=2)
self.assertIsNotNone(self.sio.output_stream)
def test_start_output_invalid_rate(self):
with self.assertRaises(pysoundio.PySoundIoError):
self.sio.start_output_stream(
sample_rate=10000000,
dtype=pysoundio.SoundIoFormatFloat32LE,
channels=2)
def test_start_output_default_rate(self):
self.sio.start_output_stream(
dtype=pysoundio.SoundIoFormatFloat32LE,
channels=2)
self.assertIsNotNone(self.sio.output_stream)
self.assertIsInstance(self.sio.sample_rate, int)
def test_start_output_invalid_format(self):
with self.assertRaises(pysoundio.PySoundIoError):
self.sio.start_output_stream(
sample_rate=44100,
dtype=50,
channels=2)
def test_start_output_default_format(self):
self.sio.start_output_stream(
sample_rate=44100,
channels=2)
self.assertIsNotNone(self.sio.output_stream)
self.assertIsInstance(self.sio.format, int)
class TestInputProcessing(unittest.TestCase):
def setUp(self):
self.sio = pysoundio.PySoundIo(
backend=pysoundio.SoundIoBackendDummy)
self.callback_called = False
def tearDown(self):
self.sio.close()
def callback(self, data, length):
self.callback_called = True
def test_read_callback(self):
self.sio.start_input_stream(
sample_rate=44100,
dtype=pysoundio.SoundIoFormatFloat32LE,
channels=2,
block_size=4096,
read_callback=self.callback)
self.assertIsNotNone(self.sio.input_stream)
data = bytearray(b'\x00' * 4096 * 8)
_soundiox.ring_buffer_write_ptr(self.sio.input_buffer, data, len(data))
_soundiox.ring_buffer_advance_write_ptr(self.sio.input_buffer, len(data))
thread = pysoundio.pysoundio._InputProcessingThread(parent=self.sio)
thread.run()
self.assertTrue(self.callback_called)
class TestOutputProcessing(unittest.TestCase):
def setUp(self):
self.sio = pysoundio.PySoundIo(
backend=pysoundio.SoundIoBackendDummy)
self.sio.testing = True
self.callback_called = False
def callback(self, data, length):
self.callback_called = True
def test_write_callback(self):
self.sio.start_output_stream(
sample_rate=44100,
dtype=pysoundio.SoundIoFormatFloat32LE,
channels=2,
block_size=4096,
write_callback=self.callback)
self.assertIsNotNone(self.sio.output_stream)
thread = pysoundio.pysoundio._OutputProcessingThread(parent=self.sio, block_size=4096)
thread.run()
self.assertTrue(self.callback_called)