Difficult-Rocket/libs/pyglet/media/drivers/__init__.py

83 lines
2.6 KiB
Python
Raw Normal View History

2021-04-16 23:21:06 +08:00
"""Drivers for playing back media."""
import atexit
import pyglet
_debug = pyglet.options['debug_media']
def get_audio_driver():
"""Get the preferred audio driver for the current platform.
2023-03-01 13:35:55 +08:00
Currently pyglet supports DirectSound, PulseAudio and OpenAL drivers. If
the platform supports more than one of those audio drivers, the
application can give its preference with :data:`pyglet.options` ``audio``
keyword. See the Programming guide, section
:doc:`/programming_guide/media`.
2021-04-16 23:21:06 +08:00
Returns:
2023-03-01 13:35:55 +08:00
AbstractAudioDriver : The concrete implementation of the preferred
audio driver for this platform.
2021-04-16 23:21:06 +08:00
"""
global _audio_driver
if _audio_driver:
return _audio_driver
_audio_driver = None
for driver_name in pyglet.options['audio']:
try:
if driver_name == 'pulse':
from . import pulse
_audio_driver = pulse.create_audio_driver()
break
elif driver_name == 'xaudio2':
2023-01-27 20:28:43 +08:00
if pyglet.compat_platform in ('win32', 'cygwin'):
from pyglet.libs.win32.constants import WINDOWS_8_OR_GREATER
if WINDOWS_8_OR_GREATER:
from . import xaudio2
_audio_driver = xaudio2.create_audio_driver()
break
2021-04-16 23:21:06 +08:00
elif driver_name == 'directsound':
2023-01-27 20:28:43 +08:00
if pyglet.compat_platform in ('win32', 'cygwin'):
from . import directsound
_audio_driver = directsound.create_audio_driver()
break
2021-04-16 23:21:06 +08:00
elif driver_name == 'openal':
from . import openal
_audio_driver = openal.create_audio_driver()
break
elif driver_name == 'silent':
from . import silent
_audio_driver = silent.create_audio_driver()
break
except Exception:
if _debug:
2023-01-25 20:38:17 +08:00
print(f'Error importing driver {driver_name}:')
2021-04-16 23:21:06 +08:00
import traceback
traceback.print_exc()
2021-08-13 12:25:29 +08:00
else:
from . import silent
_audio_driver = silent.create_audio_driver()
2021-04-16 23:21:06 +08:00
return _audio_driver
def _delete_audio_driver():
# First cleanup any remaining spontaneous Player
from .. import Source
for p in Source._players:
# Remove the reference to _on_player_eos which had a closure on the player
p.on_player_eos = None
del p
del Source._players
global _audio_driver
_audio_driver = None
2023-03-01 13:35:55 +08:00
_audio_driver = None
2021-04-16 23:21:06 +08:00
atexit.register(_delete_audio_driver)