Debian Codegraph @

tests/integration/media/test_openal.py

raw symbols SCIP
1 import pytest
2 import time
3 import weakref
4
5 import pyglet
6 _debug = False
7 pyglet.options['debug_media'] = _debug
8 from pyglet.media.synthesis import Silence
9
10 try:
11 from pyglet.media.drivers import openal
12 import pyglet.media.drivers.openal.adaptation
13 except ImportError:
14 openal = None
15
16
17 pytestmark = pytest.mark.skipif(openal is None, reason='No OpenAL available.')
18
19
20 def almost_equal(f1, f2, eps=0.0001):
21 return abs(f1 - f2) < eps
22
23
24 def almost_equal_coords(c1, c2, eps=0.0001):
25 return all(almost_equal(f1, f2, eps) for f1, f2 in zip(c1, c2))
26
27
28 @pytest.fixture
29 def device():
30 device = openal.interface.OpenALDevice()
31 yield device
32 device.close()
33
34
35 def test_device_create_close(device):
36 assert device.is_ready
37 device.close()
38 assert not device.is_ready
39
40
41 def test_device_version(device):
42 major, minor = device.get_version()
43 assert major > 0
44 assert minor > 0
45
46
47 def test_device_extensions(device):
48 extensions = device.get_extensions()
49 assert len(extensions) > 0
50 for ext in extensions:
51 assert isinstance(ext, str)
52
53
54 def test_context_create_delete(device):
55 context = device.create_context()
56 assert context is not None
57 context.delete()
58
59
60 @pytest.fixture
61 def context(device):
62 context = device.create_context()
63 yield context
64 context.delete()
65
66
67 def test_context_make_current(context):
68 context.make_current()
69
70
71 @pytest.fixture
72 def buffer_pool(context):
73 return context.device.buffer_pool
74
75
76 @pytest.fixture
77 def buf(buffer_pool):
78 buf = buffer_pool.get_buffer()
79 yield buf
80 buf.delete()
81
82
83 def test_buffer_create_delete(buf):
84 assert buf.is_valid
85 assert buf.al_name is not None
86 assert buf.name > 0
87 buf.delete()
88 assert not buf.is_valid
89
90
91 def test_buffer_data(buf):
92 assert buf.is_valid
93 audio_source = Silence(1.)
94 buf.data(audio_source.get_audio_data(audio_source.audio_format.bytes_per_second),
95 audio_source.audio_format)
96 assert buf.is_valid
97 buf.delete()
98 assert not buf.is_valid
99
100
101 def test_bufferpool_get_single_buffer(buffer_pool):
102 assert len(buffer_pool) == 0
103
104 buf = buffer_pool.get_buffer()
105 assert buf is not None
106 assert buf.is_valid
107 assert len(buffer_pool) == 0
108
109
110 def test_bufferpool_return_valid_buffer(buffer_pool):
111 buf = buffer_pool.get_buffer()
112 assert buf is not None
113 assert buf.is_valid
114 assert len(buffer_pool) == 0
115
116 buffer_pool.return_buffers((buf,))
117 assert len(buffer_pool) == 1
118
119 buf = buffer_pool.get_buffer()
120 assert buf is not None
121 assert buf.is_valid
122 assert len(buffer_pool) == 0
123
124
125 def test_bufferpool_get_multiple_buffers(buffer_pool):
126 bufs = buffer_pool.get_buffers(3)
127 assert bufs is not None
128 assert len(bufs) == 3
129 for buf in bufs:
130 assert buf.is_valid
131 assert len(buffer_pool) == 0
132
133
134 def test_bufferpool_return_multiple_valid_buffers(buffer_pool):
135 bufs = buffer_pool.get_buffers(3)
136 assert bufs is not None
137 assert len(bufs) == 3
138 for buf in bufs:
139 assert buf.is_valid
140 assert len(buffer_pool) == 0
141
142 return_count = 0
143 for buf in bufs:
144 buffer_pool.return_buffers((buf,))
145 return_count += 1
146 assert len(buffer_pool) == return_count
147
148 buf = buffer_pool.get_buffer()
149 assert buf is not None
150 assert buf.is_valid
151 assert len(buffer_pool) == 2
152
153
154 def test_bufferpool_return_invalid_buffer(buffer_pool):
155 buf = buffer_pool.get_buffer()
156 assert buf is not None
157 assert buf.is_valid
158 assert len(buffer_pool) == 0
159
160 buf.delete()
161 assert not buf.is_valid
162 buffer_pool.return_buffers((buf,))
163 assert len(buffer_pool) == 0
164
165 buf = buffer_pool.get_buffer()
166 assert buf is not None
167 assert buf.is_valid
168 assert len(buffer_pool) == 0
169
170
171 def test_bufferpool_invalidate_buffer_in_pool(buffer_pool):
172 buf = buffer_pool.get_buffer()
173 assert buf is not None
174 assert buf.is_valid
175 assert len(buffer_pool) == 0
176
177 buffer_pool.return_buffers((buf,))
178 assert len(buffer_pool) == 1
179
180 buf.delete()
181 assert not buf.is_valid
182
183 buf = buffer_pool.get_buffer()
184 assert buf is not None
185 assert buf.is_valid
186 assert len(buffer_pool) == 0
187
188
189 def test_source_create_delete(context):
190 source = context.create_source()
191 assert source.is_initial
192 assert not source.is_playing
193 assert not source.is_paused
194 assert not source.is_stopped
195 assert source.buffers_processed == 0
196 assert source.byte_offset == 0
197 source.delete()
198
199
200 @pytest.fixture
201 def source(context):
202 source = context.create_source()
203 yield source
204 source.delete()
205
206
207 @pytest.fixture
208 def filled_buffer(source):
209 buf = source.get_buffer()
210 assert buf.is_valid
211 audio_source = Silence(1.)
212 buf.data(audio_source.get_audio_data(audio_source.audio_format.bytes_per_second),
213 audio_source.audio_format)
214 yield buf
215 source.buffer_pool.return_buffers((buf,))
216
217
218 def test_source_queue_play_unqueue(context, filled_buffer):
219 source = context.create_source()
220
221 source.queue_buffer(filled_buffer)
222 assert source.is_initial
223 assert not source.is_playing
224 assert not source.is_paused
225 assert not source.is_stopped
226 assert source.buffers_processed == 0
227 assert source.buffers_queued == 1
228 assert source.byte_offset == 0
229
230 source.play()
231 assert not source.is_initial
232 assert source.is_playing
233 assert not source.is_paused
234 assert not source.is_stopped
235 assert source.byte_offset == 0
236
237 end_time = time.time() + 1.5
238 while time.time() < end_time:
239 if source.byte_offset > 0:
240 break
241 time.sleep(.1)
242 assert source.byte_offset > 0
243
244 end_time = time.time() + 1.5
245 while time.time() < end_time:
246 if source.buffers_processed > 0:
247 break
248 time.sleep(.1)
249 assert source.buffers_processed == 1
250
251 processed = source.unqueue_buffers()
252 assert processed == 1
253 assert source.buffers_processed == 0
254 assert source.buffers_queued == 0
255
256 assert not source.is_initial
257 assert not source.is_playing
258 assert not source.is_paused
259 assert source.is_stopped
260
261
262 @pytest.fixture
263 def filled_source(source, filled_buffer):
264 source.queue_buffer(filled_buffer)
265 return source
266
267
268 def test_source_pause_stop(filled_source):
269 assert filled_source.is_initial
270 assert not filled_source.is_playing
271 assert not filled_source.is_paused
272 assert not filled_source.is_stopped
273
274 filled_source.play()
275 assert not filled_source.is_initial
276 assert filled_source.is_playing
277 assert not filled_source.is_paused
278 assert not filled_source.is_stopped
279
280 filled_source.pause()
281 assert not filled_source.is_initial
282 assert not filled_source.is_playing
283 assert filled_source.is_paused
284 assert not filled_source.is_stopped
285
286 filled_source.play()
287 assert not filled_source.is_initial
288 assert filled_source.is_playing
289 assert not filled_source.is_paused
290 assert not filled_source.is_stopped
291
292 filled_source.stop()
293 assert not filled_source.is_initial
294 assert not filled_source.is_playing
295 assert not filled_source.is_paused
296 assert filled_source.is_stopped
297
298
299 def test_source_prop_position(filled_source):
300 assert almost_equal_coords(filled_source.position, (0., 0., 0.))
301 filled_source.position = 1., 2., 3.
302 assert almost_equal_coords(filled_source.position, (1., 2., 3.))
303
304
305 def test_source_prop_velocity(filled_source):
306 assert almost_equal_coords(filled_source.velocity, (0., 0., 0.))
307 filled_source.velocity = 1., 2., 3.
308 assert almost_equal_coords(filled_source.velocity, (1., 2., 3.))
309
310
311 def test_source_prop_gain(filled_source):
312 assert almost_equal(filled_source.gain, 1.)
313 filled_source.gain = 8.5
314 assert almost_equal(filled_source.gain, 8.5)
315
316
317 def test_source_prop_min_gain(filled_source):
318 assert almost_equal(filled_source.min_gain, 0.)
319 filled_source.min_gain = .5
320 assert almost_equal(filled_source.min_gain, .5)
321
322
323 def test_source_prop_max_gain(filled_source):
324 assert almost_equal(filled_source.max_gain, 1.)
325 filled_source.max_gain = .8
326 assert almost_equal(filled_source.max_gain, .8)
327
328
329 def test_source_prop_reference_distance(filled_source):
330 assert almost_equal(filled_source.reference_distance, 1.)
331 filled_source.reference_distance = 10.3
332 assert almost_equal(filled_source.reference_distance, 10.3)
333
334
335 def test_source_prop_rolloff_factor(filled_source):
336 assert almost_equal(filled_source.rolloff_factor, 1.)
337 filled_source.rolloff_factor = 4.5
338 assert almost_equal(filled_source.rolloff_factor, 4.5)
339
340
341 def test_source_prop_max_distance(filled_source):
342 assert filled_source.max_distance > 500.0 # No definition of MAX_FLOAT available, 1000.0 on OSX
343 filled_source.max_distance = 500.
344 assert almost_equal(filled_source.max_distance, 500.)
345
346
347 def test_source_prop_pitch(filled_source):
348 assert almost_equal(filled_source.pitch, 1.)
349 filled_source.pitch = 3.14
350 assert almost_equal(filled_source.pitch, 3.14)
351
352
353 def test_source_prop_direction(filled_source):
354 assert almost_equal_coords(filled_source.direction, (0., 0., 0.))
355 filled_source.direction = 1., 2., 3.
356 assert almost_equal_coords(filled_source.direction, (1., 2., 3.))
357
358
359 def test_source_prop_cone_inner_angle(filled_source):
360 assert almost_equal(filled_source.cone_inner_angle, 360.)
361 filled_source.cone_inner_angle = 180.
362 assert almost_equal(filled_source.cone_inner_angle, 180.)
363
364
365 def test_source_prop_cone_outer_angle(filled_source):
366 assert almost_equal(filled_source.cone_outer_angle, 360.)
367 filled_source.cone_outer_angle = 90.
368 assert almost_equal(filled_source.cone_outer_angle, 90.)
369
370
371 def test_source_prop_cone_outer_gain(filled_source):
372 assert almost_equal(filled_source.cone_outer_gain, 0.)
373 filled_source.cone_outer_gain = .6
374 assert almost_equal(filled_source.cone_outer_gain, .6)
375
376
377 def test_source_prop_sec_offset(filled_source):
378 assert almost_equal(filled_source.sec_offset, 0.)
379 filled_source.play()
380 filled_source.pause()
381 filled_source.sec_offset = .1
382 # Not stable: assert almost_equal(filled_source.sec_offset, .1)
383
384
385 def test_source_prop_sample_offset(filled_source):
386 assert almost_equal(filled_source.sample_offset, 0.)
387 filled_source.play()
388 filled_source.pause()
389 filled_source.sample_offset = 5.
390 # Not stable: assert almost_equal(filled_source.sample_offset, 5.)
391
392
393 def test_source_prop_byte_offset(filled_source):
394 assert almost_equal(filled_source.byte_offset, 0.)
395 filled_source.play()
396 filled_source.pause()
397 filled_source.byte_offset = 8.
398 # Not stable: assert almost_equal(filled_source.byte_offset, 8.)
399
400
401 def test_listener_prop_position(context):
402 listener = openal.interface.OpenALListener()
403 assert almost_equal_coords(listener.position, (0., 0., 0.))
404 filled_source.position = 1., 2., 3.
405 #TODO assert almost_equal_coords(listener.position, (1., 2., 3.))
406
407
408 def test_listener_prop_velocity(context):
409 listener = openal.interface.OpenALListener()
410 assert almost_equal_coords(listener.velocity, (0., 0., 0.))
411 filled_source.velocity = 1., 2., 3.
412 #TODO assert almost_equal_coords(listener.velocity, (1., 2., 3.))
413
414
415 def test_listener_prop_gain(context):
416 listener = openal.interface.OpenALListener()
417 assert almost_equal(listener.gain, 1.)
418 filled_source.gain = 8.5
419 #TODO assert almost_equal(listener.gain, 8.5)
420
421
422 def test_listener_prop_orientation(context):
423 listener = openal.interface.OpenALListener()
424 orientation = listener.orientation
425 assert almost_equal_coords(orientation.at, (0., 0., -1.))
426 assert almost_equal_coords(orientation.up, (0., 1., 0.))
427 listener.orientation = ((1., 2., 3.), (4., 5., 6.))
428 orientation = listener.orientation
429 assert almost_equal_coords(orientation.at, (1., 2., 3.))
430 assert almost_equal_coords(orientation.up, (4., 5., 6.))
431
432