| 1 |
.. _guide-media: |
| 2 |
|
| 3 |
Playing Sound and Video |
| 4 |
======================= |
| 5 |
|
| 6 |
pyglet can load and play many audio and video formats, often with |
| 7 |
support for surround sound and video effects. |
| 8 |
|
| 9 |
WAV and MP3 files are the most commonly supported across platforms. The |
| 10 |
formats a specific computer can play are determined by which of the |
| 11 |
following are available: |
| 12 |
|
| 13 |
#. The built-in pyglet WAV file decoder (always available) |
| 14 |
#. Platform-specific APIs and libraries |
| 15 |
#. PyOgg |
| 16 |
#. :ref:`guide-supportedmedia-ffmpeg` version 4, 5, 6, or 7. |
| 17 |
|
| 18 |
Video is played into OpenGL textures, allowing real-time manipulation |
| 19 |
by applications. Examples include use in 3D environments or shader-based |
| 20 |
effects. To play video, :ref:`guide-supportedmedia-ffmpeg` must be |
| 21 |
installed. |
| 22 |
|
| 23 |
Audio is played back with one of the following: OpenAL, XAudio2, |
| 24 |
DirectSound, or PulseAudio. Hardware-accelerated mixing is available |
| 25 |
on all of them. 3D positional audio and surround sound features are |
| 26 |
available on all back-ends other than PulseAudio. |
| 27 |
|
| 28 |
.. _FFmpeg: https://www.ffmpeg.org/download.html |
| 29 |
.. _openal.org: https://www.openal.org/downloads |
| 30 |
|
| 31 |
Audio drivers |
| 32 |
------------- |
| 33 |
|
| 34 |
pyglet can use OpenAL, XAudio2, DirectSound, or PulseAudio to play |
| 35 |
sound. Only one driver can be used at a time, but the selection can |
| 36 |
be changed by altering the configuration and restarting the program. |
| 37 |
|
| 38 |
The default driver preference order works well for most users. However, |
| 39 |
you may override it by setting a different preference sequence before |
| 40 |
the :py:mod:`pyglet.media` module is loaded. See |
| 41 |
:ref:`guide-audio-driver-order` to learn more. |
| 42 |
|
| 43 |
The available drivers depend on your operating system: |
| 44 |
|
| 45 |
.. list-table:: |
| 46 |
:header-rows: 1 |
| 47 |
|
| 48 |
* - Windows |
| 49 |
- Mac OS X |
| 50 |
- Linux |
| 51 |
* - OpenAL [#openalf]_ |
| 52 |
- OpenAL |
| 53 |
- OpenAL [#openalf]_ |
| 54 |
* - DirectSound |
| 55 |
- |
| 56 |
- |
| 57 |
* - XAudio2 |
| 58 |
- |
| 59 |
- PulseAudio [#pulseaudiof]_ |
| 60 |
|
| 61 |
.. [#pulseaudiof] The :ref:`guide-audio-driver-pulseaudio` driver has |
| 62 |
limitations. For audio-intensive programs, consider using |
| 63 |
:ref:`guide-audio-driver-openal`. |
| 64 |
|
| 65 |
.. [#openalf] OpenAL does not come preinstalled on Windows and some |
| 66 |
Linux distributions. |
| 67 |
|
| 68 |
.. _guide-audio-driver-order: |
| 69 |
|
| 70 |
Choosing the audio driver |
| 71 |
^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 72 |
|
| 73 |
The ``'audio'`` key of the :py:data:`pyglet.options` dictionary |
| 74 |
specifies the audio driver preference order. |
| 75 |
|
| 76 |
On import, the :mod:`pyglet.media` will try each entry from first to |
| 77 |
last until it either finds a working driver or runs out of entries. For |
| 78 |
example, the default is equivalent to setting the following value:: |
| 79 |
|
| 80 |
pyglet.options['audio'] = ('xaudio2', 'directsound', 'openal', 'pulse', 'silent') |
| 81 |
|
| 82 |
You can also set a custom preference order. For example, we could add |
| 83 |
this line before importing the media module:: |
| 84 |
|
| 85 |
pyglet.options['audio'] = ('openal', 'pulse', 'xaudio2', 'directsound', 'silent') |
| 86 |
|
| 87 |
It tells pyglet to try using the OpenAL driver first. If is not |
| 88 |
available, try Pulseaudio, XAudio2, and DirectSound in that order. |
| 89 |
If all else fails, no driver will be instantiated and the game will |
| 90 |
run silently. |
| 91 |
|
| 92 |
The value for the ``'audio'`` key can be a list or tuple which contains |
| 93 |
one or more of the following strings: |
| 94 |
|
| 95 |
.. list-table:: |
| 96 |
:header-rows: 1 |
| 97 |
|
| 98 |
* - String |
| 99 |
- Audio driver |
| 100 |
* - ``'openal'`` |
| 101 |
- OpenAL |
| 102 |
* - ``'directsound'`` |
| 103 |
- DirectSound |
| 104 |
* - ``'xaudio2'`` |
| 105 |
- XAudio2 |
| 106 |
* - ``'pulse'`` |
| 107 |
- PulseAudio |
| 108 |
* - ``'silent'`` |
| 109 |
- No audio output |
| 110 |
|
| 111 |
You must set any custom ``'audio'`` preference order before importing |
| 112 |
:mod:`pyglet.media`. This can also be set through an environment variable; |
| 113 |
see :ref:`guide_environment-settings`. |
| 114 |
|
| 115 |
The following sections describe the requirements and limitations of each audio |
| 116 |
driver. |
| 117 |
|
| 118 |
.. _guide-audio-driver-xaudio2: |
| 119 |
|
| 120 |
XAudio2 |
| 121 |
^^^^^^^ |
| 122 |
XAudio2 is only available on Windows Vista and above and is the replacement of |
| 123 |
DirectSound. This provides hardware accelerated audio support for newer operating |
| 124 |
systems. |
| 125 |
|
| 126 |
Note that in some stripped down versions of Windows 10, XAudio2 may not be available |
| 127 |
until the required DLL's are installed. |
| 128 |
|
| 129 |
.. _guide-audio-driver-directsound: |
| 130 |
|
| 131 |
DirectSound |
| 132 |
^^^^^^^^^^^ |
| 133 |
|
| 134 |
DirectSound is available only on Windows, and is installed by default. |
| 135 |
pyglet uses only DirectX 7 features. On Windows Vista, DirectSound does not |
| 136 |
support hardware audio mixing or surround sound. |
| 137 |
|
| 138 |
.. _guide-audio-driver-openal: |
| 139 |
|
| 140 |
OpenAL |
| 141 |
^^^^^^ |
| 142 |
|
| 143 |
The favored driver for Mac OS X, but also available on other systems. |
| 144 |
|
| 145 |
This driver has the following advantages: |
| 146 |
|
| 147 |
* Either preinstalled or easy to install on supported platforms. |
| 148 |
* Implements features which may be absent from other drivers or |
| 149 |
OS-specific versions of their backing APIs. |
| 150 |
|
| 151 |
Its main downsides are: |
| 152 |
|
| 153 |
* Not guaranteed to be installed on platforms other than Mac OS X |
| 154 |
* On recent Windows versions, the :ref:`guide-audio-driver-xaudio2` and |
| 155 |
:ref:`guide-audio-driver-directsound` backends may support more |
| 156 |
features. |
| 157 |
|
| 158 |
Windows users can download an OpenAL implementation from `openal.org`_ |
| 159 |
or their sound device's manufacturer. |
| 160 |
|
| 161 |
On Linux, the following apply: |
| 162 |
|
| 163 |
* It can usually be installed through your distro's package manager. |
| 164 |
* It may already be installed as a dependency of other packages. |
| 165 |
* It lacks the limitations of the :ref:`guide-audio-driver-pulseaudio` |
| 166 |
driver. |
| 167 |
|
| 168 |
The commands below should install OpenAL on the most common Linux |
| 169 |
distros: |
| 170 |
|
| 171 |
.. list-table:: |
| 172 |
:header-rows: 1 |
| 173 |
|
| 174 |
* - Common Linux Distros |
| 175 |
- Install Command |
| 176 |
|
| 177 |
* - Ubuntu, Pop!_OS, Debian |
| 178 |
- ``apt install libopenal1`` |
| 179 |
|
| 180 |
* - Arch, Manjaro |
| 181 |
- ``pacman -S openal`` |
| 182 |
|
| 183 |
* - Fedora, Nobara |
| 184 |
- ``dnf install openal-soft`` |
| 185 |
|
| 186 |
You may need to prefix these commands with either ``sudo`` or another |
| 187 |
command. Consult your distro's documentation for more information. |
| 188 |
|
| 189 |
.. _guide-audio-driver-pulseaudio: |
| 190 |
|
| 191 |
PulseAudio |
| 192 |
^^^^^^^^^^ |
| 193 |
|
| 194 |
This backend is almost always supported, but it has limited features. |
| 195 |
|
| 196 |
If it fails to initialize, consult your distro's documentation to learn |
| 197 |
which supported audio back-ends you can install. |
| 198 |
|
| 199 |
Missing features |
| 200 |
"""""""""""""""" |
| 201 |
|
| 202 |
Although PulseAudio can theoretically support advanced multi-channel |
| 203 |
audio, the pyglet driver does not. The following features will not |
| 204 |
work properly: |
| 205 |
|
| 206 |
#. Positional audio: automatically changing the volume for individual |
| 207 |
audio channels based on the position of the sound source |
| 208 |
#. Integration with surround sound |
| 209 |
|
| 210 |
Switching to :ref:`guide-audio-driver-openal` should automatically enable them. |
| 211 |
|
| 212 |
.. _guide-supportedmedia: |
| 213 |
|
| 214 |
Supported media types |
| 215 |
--------------------- |
| 216 |
|
| 217 |
pyglet has included support for loading Wave (.wav) files, which are therefore |
| 218 |
guaranteed to work on all platforms. pyglet will also use various platform libraries |
| 219 |
and frameworks to support a limited amount of compressed audio types, without the need |
| 220 |
for FFmpeg. While FFmpeg supports a large array of formats and codecs, it may be an |
| 221 |
unnecessarily large dependency when only simple audio playback is needed. |
| 222 |
|
| 223 |
These formats are supported natively under the following systems and codecs: |
| 224 |
|
| 225 |
.. _guide-supportedmedia-wmf: |
| 226 |
|
| 227 |
Windows Media Foundation |
| 228 |
^^^^^^^^^^^^^^^^^^^^^^^^ |
| 229 |
Supported on Windows operating systems. |
| 230 |
|
| 231 |
The following are supported on **Windows Vista and above**: |
| 232 |
|
| 233 |
* MP3 |
| 234 |
* WMA |
| 235 |
* ASF |
| 236 |
* SAMI/SMI |
| 237 |
|
| 238 |
The following are also supported on **Windows 7 and above**: |
| 239 |
|
| 240 |
* AAC/ADTS |
| 241 |
|
| 242 |
The following is undocumented but known to work on **Windows 10**: |
| 243 |
|
| 244 |
* FLAC |
| 245 |
|
| 246 |
.. _guide-supportedmedia-gstreamer: |
| 247 |
|
| 248 |
GStreamer |
| 249 |
^^^^^^^^^ |
| 250 |
Supported on Linux operating systems that have the GStreamer installed. Please note that the |
| 251 |
associated Python packages for gobject & gst are also required. This varies by distribution, |
| 252 |
but will often already be installed along with GStreamer. |
| 253 |
|
| 254 |
* MP3 |
| 255 |
* FLAC |
| 256 |
* OGG |
| 257 |
* M4A |
| 258 |
|
| 259 |
.. _guide-supportedmedia-coreaudio: |
| 260 |
|
| 261 |
CoreAudio |
| 262 |
^^^^^^^^^ |
| 263 |
Supported on Mac operating systems. |
| 264 |
|
| 265 |
* AAC |
| 266 |
* AC3 |
| 267 |
* AIF |
| 268 |
* AU |
| 269 |
* CAF |
| 270 |
* MP3 |
| 271 |
* M4A |
| 272 |
* SND |
| 273 |
* SD2 |
| 274 |
|
| 275 |
.. _guide-supportedmedia-pyogg: |
| 276 |
|
| 277 |
PyOgg |
| 278 |
^^^^^ |
| 279 |
|
| 280 |
.. _pyogg_install: https://pyogg.readthedocs.io/en/latest/installation.html |
| 281 |
|
| 282 |
Supported on Windows, Linux, and Mac operating systems. |
| 283 |
|
| 284 |
PyOgg is a lightweight Python library that provides Python bindings for Opus, Vorbis, |
| 285 |
and FLAC codecs. |
| 286 |
|
| 287 |
If the PyOgg module is installed in your site packages, pyglet will optionally detect |
| 288 |
and use it. Since not all operating systems can decode the same audio formats natively, |
| 289 |
it can often be a hassle to choose an audio format that is truly cross platform with |
| 290 |
a small footprint. This wrapper was created to help with that issue. |
| 291 |
|
| 292 |
Supports the following formats: |
| 293 |
|
| 294 |
* OGG |
| 295 |
* FLAC |
| 296 |
* OPUS |
| 297 |
|
| 298 |
To install PyOgg, please see their `installation guide on readthedocs.io |
| 299 |
<pyogg_install_>`_. |
| 300 |
|
| 301 |
.. _guide-supportedmedia-ffmpeg: |
| 302 |
|
| 303 |
FFmpeg |
| 304 |
^^^^^^ |
| 305 |
.. _FFmpeg's license overview: https://www.ffmpeg.org/legal.html |
| 306 |
|
| 307 |
.. note:: The most recent pyglet release can use FFmpeg versions 4.X, 5.X, 6.X, and 7.X |
| 308 |
|
| 309 |
See :ref:`guide-media-ffmpeginstall` to learn more. |
| 310 |
|
| 311 |
FFmpeg is best when you need to support the maximum number of formats |
| 312 |
and encodings. It is also worth considering the following: |
| 313 |
|
| 314 |
* Support for many formats and container types means large download size |
| 315 |
* FFmpeg's compile options allow it to be built and used under :ref:`either |
| 316 |
the LGPL or GPL license <guide-ffmpeg-licenses>` |
| 317 |
|
| 318 |
See the following sections to learn more. |
| 319 |
|
| 320 |
See :ref:`guide-ffmpeg-licenses` to learn more. |
| 321 |
|
| 322 |
Supported Formats |
| 323 |
""""""""""""""""" |
| 324 |
|
| 325 |
.. _the FFmpeg documentation: https://ffmpeg.org/ffmpeg.html |
| 326 |
|
| 327 |
It is difficult to provide a complete list of FFmpeg's features due to |
| 328 |
the large number of audio and video codecs, options, and container |
| 329 |
formats it supports. Refer to `the FFmpeg documentation`_ for |
| 330 |
more information. |
| 331 |
|
| 332 |
Known supported audio formats include: |
| 333 |
|
| 334 |
* AU |
| 335 |
* MP2 |
| 336 |
* MP3 |
| 337 |
* OGG/Vorbis |
| 338 |
* WAV |
| 339 |
* WMA |
| 340 |
|
| 341 |
Known supported video formats include: |
| 342 |
|
| 343 |
* AVI |
| 344 |
* DivX |
| 345 |
* H.263 |
| 346 |
* H.264 |
| 347 |
* MPEG |
| 348 |
* MPEG-2 |
| 349 |
* OGG/Theora |
| 350 |
* Xvid |
| 351 |
* WMV |
| 352 |
* Webm |
| 353 |
|
| 354 |
The easiest way to check whether a file will load through FFmpeg is to |
| 355 |
try playing it through the ``media_player.py`` example. New releases of |
| 356 |
FFmpeg may fix bugs and add support for new formats. |
| 357 |
|
| 358 |
.. _guide-ffmpeg-licenses: |
| 359 |
|
| 360 |
FFmpeg & licenses |
| 361 |
""""""""""""""""" |
| 362 |
|
| 363 |
FFmpeg's code uses different licenses for different parts. |
| 364 |
|
| 365 |
The core of the project uses a modified LGPL license. However, the GPL |
| 366 |
is used for certain optional parts. Using these components, as well as |
| 367 |
bundling FFmpeg binaries which include them, may require full GPL |
| 368 |
compliance. As a result, some organizations may restrict some or all |
| 369 |
use of FFmpeg. |
| 370 |
|
| 371 |
pyglet's FFmpeg bindings do not rely on the optional GPL-licensed parts. |
| 372 |
Therefore, most projects should be free to use any license they choose |
| 373 |
for their own code as long as they use one of the following approaches: |
| 374 |
|
| 375 |
* Require users to install FFmpeg themselves using either: |
| 376 |
|
| 377 |
* The :ref:`guide-media-ffmpeginstall` section on this page |
| 378 |
* Custom instructions for a specific FFmpeg version |
| 379 |
|
| 380 |
* Make FFmpeg optional as described at the end of the |
| 381 |
:ref:`guide-media-ffmpeginstall` instructions |
| 382 |
* Bundle an LGPL-only build of FFmpeg |
| 383 |
|
| 384 |
See the following to learn more: |
| 385 |
|
| 386 |
* `FFmpeg's license overview`_ |
| 387 |
* The license documentation for your specific FFmpeg version: |
| 388 |
|
| 389 |
* `The FFmpeg 4.4 license breakdown <https://ffmpeg.org/doxygen/4.4/md_LICENSE.html>`_ |
| 390 |
* `The FFmpeg 5.1 license breakdown <https://ffmpeg.org/doxygen/5.1/md_LICENSE.html>`_ |
| 391 |
* `The FFmpeg 6.0 license breakdown <https://ffmpeg.org/doxygen/6.0/md_LICENSE.html>`_ |
| 392 |
* `The FFmpeg 7.0 license breakdown <https://ffmpeg.org/doxygen/7.0/md_LICENSE.html>`_ |
| 393 |
|
| 394 |
.. _guide-media-ffmpeginstall: |
| 395 |
|
| 396 |
FFmpeg installation |
| 397 |
------------------- |
| 398 |
|
| 399 |
You can install FFmpeg for your platform by following the instructions found |
| 400 |
in the `FFmpeg download <https://www.ffmpeg.org/download.html>`_ page. You must |
| 401 |
choose the shared build for the targeted OS with the architecture similar to |
| 402 |
the Python interpreter. |
| 403 |
|
| 404 |
All recent pyglet versions support FFmpeg 4.x. |
| 405 |
|
| 406 |
* Support for version 5.X requires at least: 1.5.28. |
| 407 |
* Support for version 6.X requires at least: 2.0.8. |
| 408 |
* Support for version 7.X requires at least: 2.0.20. |
| 409 |
|
| 410 |
Choose the correct architecture depending on the targeted |
| 411 |
**Python interpreter**. If you're shipping your project with a 32 bits |
| 412 |
interpreter, you must download the 32 bits shared binaries. |
| 413 |
|
| 414 |
On Windows, the usual error message when the wrong architecture was downloaded |
| 415 |
is:: |
| 416 |
|
| 417 |
WindowsError: [Error 193] %1 is not a valid Win32 application |
| 418 |
|
| 419 |
Finally make sure you download the **shared** builds, not the static or the |
| 420 |
dev builds. |
| 421 |
|
| 422 |
For Mac OS and Linux, the library is usually already installed system-wide. |
| 423 |
It may be easiest to list FFmpeg as a requirement for your project, |
| 424 |
and leave it up to the user to ensure that it is installed. |
| 425 |
For Windows users, it's not recommended to install the library in one of the |
| 426 |
windows sub-folders. |
| 427 |
|
| 428 |
Instead we recommend to use the :py:data:`pyglet.options` |
| 429 |
``search_local_libs``:: |
| 430 |
|
| 431 |
import pyglet |
| 432 |
pyglet.options['search_local_libs'] = True |
| 433 |
|
| 434 |
This will allow pyglet to find the FFmpeg binaries in the ``lib`` sub-folder |
| 435 |
located in your running script folder. |
| 436 |
|
| 437 |
Another solution is to manipulate the environment variable. On Windows you can |
| 438 |
add the dll location to the PATH:: |
| 439 |
|
| 440 |
os.environ["PATH"] += "path/to/ffmpeg" |
| 441 |
|
| 442 |
For Linux and Mac OS:: |
| 443 |
|
| 444 |
os.environ["LD_LIBRARY_PATH"] += ":" + "path/to/ffmpeg" |
| 445 |
|
| 446 |
.. tip:: Prevent crashes by checking for FFmpeg before loading media! |
| 447 |
|
| 448 |
Call :py:func:`pyglet.media.have_ffmpeg` to check whether |
| 449 |
FFmpeg was detected correctly. If it returns ``False``, you can |
| 450 |
take an appropriate action instead of crashing. Examples |
| 451 |
include: |
| 452 |
|
| 453 |
* Showing a helpful error in the GUI or console output |
| 454 |
* Exiting gracefully after the the user clicks OK on a dialog |
| 455 |
* Limiting the formats your project will attempt to load |
| 456 |
|
| 457 |
If you still have issues with the FFmpeg not being recognized, try enabling the debug flags to see if any relevant |
| 458 |
information is output: ``pyglet.options.debug_lib`` and/or ``pyglet.options.debug_media``. |
| 459 |
|
| 460 |
.. _guide-media-loading: |
| 461 |
|
| 462 |
Loading media |
| 463 |
------------- |
| 464 |
|
| 465 |
Audio and video files are loaded in the same way, using the |
| 466 |
:py:func:`pyglet.media.load` function, providing a filename:: |
| 467 |
|
| 468 |
source = pyglet.media.load('explosion.wav') |
| 469 |
|
| 470 |
If the media file is bundled with the application, consider using the |
| 471 |
:py:mod:`~pyglet.resource` module (see :ref:`guide_resources`). |
| 472 |
|
| 473 |
The result of loading a media file is a |
| 474 |
:py:class:`~pyglet.media.Source` object. This object provides useful |
| 475 |
information about the type of media encoded in the file, and serves as an |
| 476 |
opaque object used for playing back the file (described in the next section). |
| 477 |
|
| 478 |
The :py:func:`~pyglet.media.load` function will raise a |
| 479 |
:py:class:`~pyglet.media.exceptions.MediaException` if the format is unknown. |
| 480 |
``IOError`` may also be raised if the file could not be read from disk. |
| 481 |
Future versions of pyglet will also support reading from arbitrary file-like |
| 482 |
objects, however a valid filename must currently be given. |
| 483 |
|
| 484 |
The length of the media file is given by the |
| 485 |
:py:class:`~pyglet.media.Source.duration` property, which returns the media's |
| 486 |
length in seconds. |
| 487 |
|
| 488 |
Audio metadata is provided in the source's |
| 489 |
:py:attr:`~pyglet.media.Source.audio_format` attribute, which is ``None`` for |
| 490 |
silent videos. This metadata is not generally useful to applications. See |
| 491 |
the :py:class:`~pyglet.media.AudioFormat` class documentation for details. |
| 492 |
|
| 493 |
Video metadata is provided in the source's |
| 494 |
:py:attr:`~pyglet.media.Source.video_format` attribute, which is ``None`` for |
| 495 |
audio files. It is recommended that this attribute is checked before |
| 496 |
attempting play back a video file -- if a movie file has a readable audio |
| 497 |
track but unknown video format it will appear as an audio file. |
| 498 |
|
| 499 |
You can use the video metadata, described in a |
| 500 |
:py:class:`~pyglet.media.VideoFormat` object, to set up display of the video |
| 501 |
before beginning playback. The attributes are as follows: |
| 502 |
|
| 503 |
.. list-table:: |
| 504 |
:header-rows: 1 |
| 505 |
|
| 506 |
* - Attribute |
| 507 |
- Description |
| 508 |
* - ``width``, ``height`` |
| 509 |
- Width and height of the video image, in pixels. |
| 510 |
* - ``sample_aspect`` |
| 511 |
- The aspect ratio of each video pixel. |
| 512 |
|
| 513 |
You must take care to apply the sample aspect ratio to the video image size |
| 514 |
for display purposes. The following code determines the display size for a |
| 515 |
given video format:: |
| 516 |
|
| 517 |
def get_video_size(width, height, sample_aspect): |
| 518 |
if sample_aspect > 1.: |
| 519 |
return width * sample_aspect, height |
| 520 |
elif sample_aspect < 1.: |
| 521 |
return width, height / sample_aspect |
| 522 |
else: |
| 523 |
return width, height |
| 524 |
|
| 525 |
Media files are not normally read entirely from disk; instead, they are |
| 526 |
streamed into the decoder, and then into the audio buffers and video memory |
| 527 |
only when needed. This reduces the startup time of loading a file and reduces |
| 528 |
the memory requirements of the application. |
| 529 |
|
| 530 |
However, there are times when it is desirable to completely decode an audio |
| 531 |
file in memory first. For example, a sound that will be played many times |
| 532 |
(such as a bullet or explosion) should only be decoded once. You can instruct |
| 533 |
pyglet to completely decode an audio file into memory at load time:: |
| 534 |
|
| 535 |
explosion = pyglet.media.load('explosion.wav', streaming=False) |
| 536 |
|
| 537 |
The resulting source is an instance of :class:`~pyglet.media.StaticSource`, |
| 538 |
which provides the same interface as a :class:`~pyglet.media.StreamingSource`. |
| 539 |
You can also construct a :class:`~pyglet.media.StaticSource` directly from an |
| 540 |
already- loaded :class:`~pyglet.media.Source`:: |
| 541 |
|
| 542 |
explosion = pyglet.media.StaticSource(pyglet.media.load('explosion.wav')) |
| 543 |
|
| 544 |
|
| 545 |
.. _guide-media-audiosynthesis: |
| 546 |
|
| 547 |
Audio Synthesis |
| 548 |
--------------- |
| 549 |
|
| 550 |
In addition to loading audio files, the :py:mod:`pyglet.media.synthesis` |
| 551 |
module is available for simple audio synthesis. There are several basic |
| 552 |
waveforms available, including: |
| 553 |
|
| 554 |
* :py:class:`~pyglet.media.synthesis.Sine` |
| 555 |
* :py:class:`~pyglet.media.synthesis.Square` |
| 556 |
* :py:class:`~pyglet.media.synthesis.Sawtooth` |
| 557 |
* :py:class:`~pyglet.media.synthesis.Triangle` |
| 558 |
* :py:class:`~pyglet.media.synthesis.WhiteNoise` |
| 559 |
* :py:class:`~pyglet.media.synthesis.Silence` |
| 560 |
|
| 561 |
These waveforms can be constructed by specifying a duration, frequency, |
| 562 |
and sample rate. At a minimum, a duration is required. For example:: |
| 563 |
|
| 564 |
sine = pyglet.media.synthesis.Sine(3.0, frequency=440, sample_rate=44800) |
| 565 |
|
| 566 |
For shaping the waveforms, several simple envelopes are available. |
| 567 |
These envelopes affect the amplitude (volume), and can make for more |
| 568 |
natural sounding tones. You first create an envelope instance, |
| 569 |
and then pass it into the constructor of any of the above waveforms. |
| 570 |
The same envelope instance can be passed to any number of waveforms, |
| 571 |
reducing duplicate code when creating multiple sounds. |
| 572 |
If no envelope is used, all waveforms will default to the FlatEnvelope |
| 573 |
of maximum amplitude, which essentially has no effect on the sound. |
| 574 |
Check the module documentation of each Envelope to see which parameters |
| 575 |
are available. |
| 576 |
|
| 577 |
* :py:class:`~pyglet.media.synthesis.FlatEnvelope` |
| 578 |
* :py:class:`~pyglet.media.synthesis.LinearDecayEnvelope` |
| 579 |
* :py:class:`~pyglet.media.synthesis.ADSREnvelope` |
| 580 |
* :py:class:`~pyglet.media.synthesis.TremoloEnvelope` |
| 581 |
|
| 582 |
An example of creating an envelope and waveforms:: |
| 583 |
|
| 584 |
adsr = pyglet.media.synthesis.ADSREnvelope(attack=0.05, decay=0.2, release=0.1) |
| 585 |
saw = pyglet.media.synthesis.Sawtooth(duration=1.0, frequency=220, envelope=adsr) |
| 586 |
|
| 587 |
The waveforms you create with the synthesis module can be played like any |
| 588 |
other loaded sound. See the next sections for more detail on playback. |
| 589 |
|
| 590 |
.. _guide-media-simpleaudioplayback: |
| 591 |
|
| 592 |
Simple audio playback |
| 593 |
--------------------- |
| 594 |
|
| 595 |
Many applications, especially games, need to play sounds in their entirety |
| 596 |
without needing to keep track of them. For example, a sound needs to be |
| 597 |
played when the player's space ship explodes, but this sound never needs to |
| 598 |
have its volume adjusted, or be rewound, or interrupted. |
| 599 |
|
| 600 |
pyglet provides a simple interface for this kind of use-case. Call the |
| 601 |
:meth:`~pyglet.media.Source.play` method of any :class:`~pyglet.media.Source` |
| 602 |
to play it immediately and completely:: |
| 603 |
|
| 604 |
explosion = pyglet.media.load('explosion.wav', streaming=False) |
| 605 |
explosion.play() |
| 606 |
|
| 607 |
You can call :py:meth:`~pyglet.media.Source.play` on any |
| 608 |
:py:class:`~pyglet.media.Source`, not just |
| 609 |
:py:class:`~pyglet.media.StaticSource`. |
| 610 |
|
| 611 |
The return value of :py:meth:`~pyglet.media.Source.play` is a |
| 612 |
:py:class:`~pyglet.media.player.Player`, which can either be |
| 613 |
discarded, or retained to maintain control over the sound's playback. |
| 614 |
|
| 615 |
.. _guide-media-controllingplayback: |
| 616 |
|
| 617 |
Controlling playback |
| 618 |
-------------------- |
| 619 |
|
| 620 |
You can implement many functions common to a media player using the |
| 621 |
:py:class:`~pyglet.media.player.Player` |
| 622 |
class. Use of this class is also necessary for video playback. There are no |
| 623 |
parameters to its construction:: |
| 624 |
|
| 625 |
player = pyglet.media.Player() |
| 626 |
|
| 627 |
A player will play any source that is *queued* on it. Any number of sources |
| 628 |
can be queued on a single player, but once queued, a source can never be |
| 629 |
dequeued (until it is removed automatically once complete). The main use of |
| 630 |
this queueing mechanism is to facilitate "gapless" transitions between |
| 631 |
playback of media files. |
| 632 |
|
| 633 |
The :py:meth:`~pyglet.media.player.Player.queue` method is used to queue |
| 634 |
a media on the player - a :py:class:`~pyglet.media.StreamingSource` or a |
| 635 |
:py:class:`~pyglet.media.StaticSource`. Either you pass one instance, or you |
| 636 |
can also pass an iterable of sources. This provides great flexibility. For |
| 637 |
instance, you could create a generator which takes care of the logic about |
| 638 |
what music to play:: |
| 639 |
|
| 640 |
def my_playlist(): |
| 641 |
yield intro |
| 642 |
while game_is_running(): |
| 643 |
yield main_theme |
| 644 |
yield ending |
| 645 |
|
| 646 |
player.queue(my_playlist()) |
| 647 |
|
| 648 |
When the game ends, you will still need to call on the player:: |
| 649 |
|
| 650 |
player.next_source() |
| 651 |
|
| 652 |
The generator will pass the ``ending`` media to the player. |
| 653 |
|
| 654 |
A :py:class:`~pyglet.media.StreamingSource` can only ever be queued on one |
| 655 |
player, and only once on that player. :py:class:`~pyglet.media.StaticSource` |
| 656 |
objects can be queued any number of times on any number of players. Recall |
| 657 |
that a :py:class:`~pyglet.media.StaticSource` can be created by passing |
| 658 |
``streaming=False`` to the :py:func:`pyglet.media.load` method. |
| 659 |
|
| 660 |
In the following example, two sounds are queued onto a player:: |
| 661 |
|
| 662 |
player.queue(source1) |
| 663 |
player.queue(source2) |
| 664 |
|
| 665 |
Playback begins with the player's :py:meth:`~pyglet.media.Player.play` method |
| 666 |
is called:: |
| 667 |
|
| 668 |
player.play() |
| 669 |
|
| 670 |
Standard controls for controlling playback are provided by these methods: |
| 671 |
|
| 672 |
.. list-table:: |
| 673 |
:header-rows: 1 |
| 674 |
|
| 675 |
* - Method |
| 676 |
- Description |
| 677 |
* - :py:meth:`~pyglet.media.Player.play` |
| 678 |
- Begin or resume playback of the current source. |
| 679 |
* - :py:meth:`~pyglet.media.Player.pause` |
| 680 |
- Pause playback of the current source. |
| 681 |
* - :py:meth:`~pyglet.media.Player.next_source` |
| 682 |
- Dequeue the current source and move to the next one immediately. |
| 683 |
* - :py:meth:`~pyglet.media.Player.seek` |
| 684 |
- Seek to a specific time within the current source. |
| 685 |
|
| 686 |
Note that there is no `stop` method. If you do not need to resume playback, |
| 687 |
simply pause playback and discard the player and source objects. Using the |
| 688 |
:meth:`~pyglet.media.Player.next_source` method does not guarantee gapless |
| 689 |
playback. |
| 690 |
|
| 691 |
There are several properties that describe the player's current state: |
| 692 |
|
| 693 |
.. list-table:: |
| 694 |
:header-rows: 1 |
| 695 |
|
| 696 |
* - Property |
| 697 |
- Description |
| 698 |
* - :py:attr:`~pyglet.media.Player.time` |
| 699 |
- The current playback position within the current source, in |
| 700 |
seconds. This is read-only (but see the :py:meth:`~pyglet.media.Player.seek` method). |
| 701 |
* - :py:attr:`~pyglet.media.Player.playing` |
| 702 |
- True if the player is currently playing, False if there are no |
| 703 |
sources queued or the player is paused. This is read-only (but |
| 704 |
see the :py:meth:`~pyglet.media.Player.pause` and :py:meth:`~pyglet.media.Player.play` methods). |
| 705 |
* - :py:attr:`~pyglet.media.Player.source` |
| 706 |
- A reference to the current source being played. This is |
| 707 |
read-only (but see the :py:meth:`~pyglet.media.Player.queue` method). |
| 708 |
* - :py:attr:`~pyglet.media.Player.volume` |
| 709 |
- The audio level, expressed as a float from 0 (mute) to 1 (normal |
| 710 |
volume). This can be set at any time. |
| 711 |
* - :py:attr:`~pyglet.media.player.Player.loop` |
| 712 |
- ``True`` if the current source should be repeated when reaching |
| 713 |
the end. If set to ``False``, playback will continue to the next |
| 714 |
queued source. |
| 715 |
|
| 716 |
|
| 717 |
.. _guide-media-playbackevents: |
| 718 |
|
| 719 |
Handling playback events |
| 720 |
------------------------ |
| 721 |
|
| 722 |
When a player reaches the end of the current source, an :py:meth:`~pyglet.media.Player.on_eos` |
| 723 |
(on end-of-source) event is dispatched. Players have a default handler for this event, |
| 724 |
which will either repeat the current source (if the :py:attr:`~pyglet.media.player.Player.loop` |
| 725 |
attribute has been set to ``True``), or move to the next queued source immediately. |
| 726 |
When there are no more queued sources, the :py:meth:`~pyglet.media.Player.on_player_eos` |
| 727 |
event is dispatched, and playback stops until another source is queued. |
| 728 |
|
| 729 |
For loop control you can change the :py:attr:`~pyglet.media.player.Player.loop` attribute |
| 730 |
at any time, but be aware that unless sufficient time is given for the future |
| 731 |
data to be decoded and buffered there may be a stutter or gap in playback. |
| 732 |
If set well in advance of the end of the source (say, several seconds), there |
| 733 |
will be no disruption. |
| 734 |
|
| 735 |
The end-of-source behavior can be further customized by setting your own event handlers; |
| 736 |
see :ref:`guide_events`. You can either replace the default event handlers directly, |
| 737 |
or add an additional event as described in the reference. For example:: |
| 738 |
|
| 739 |
my_player.on_eos = my_player.pause |
| 740 |
|
| 741 |
|
| 742 |
.. _guide-media-gaplessplayback: |
| 743 |
|
| 744 |
Gapless playback |
| 745 |
---------------- |
| 746 |
|
| 747 |
To play back multiple similar sources without any audible gaps, |
| 748 |
:py:class:`~pyglet.media.SourceGroup` is provided. |
| 749 |
A :py:class:`~pyglet.media.SourceGroup` can only contain media sources |
| 750 |
with identical audio or video format. First create an instance of |
| 751 |
:py:class:`~pyglet.media.SourceGroup`, and then add all desired additional |
| 752 |
sources with the :func:`~pyglet.media.SourceGroup.add` method. |
| 753 |
Afterwards, you can queue the :py:class:`~pyglet.media.SourceGroup` |
| 754 |
on a Player as if it was a single source. |
| 755 |
|
| 756 |
.. _guide-media-incorporating_video: |
| 757 |
|
| 758 |
Incorporating video |
| 759 |
------------------- |
| 760 |
|
| 761 |
When a :py:class:`~pyglet.media.player.Player` is playing back a source with |
| 762 |
video, use the :attr:`~pyglet.media.Player.texture` property to obtain the |
| 763 |
video frame image. This can be used to display the current video image |
| 764 |
synchronised with the audio track, for example:: |
| 765 |
|
| 766 |
@window.event |
| 767 |
def on_draw(): |
| 768 |
player.texture.blit(0, 0) |
| 769 |
|
| 770 |
The texture is an instance of :class:`pyglet.image.Texture`, with an internal |
| 771 |
format of either ``GL_TEXTURE_2D`` or ``GL_TEXTURE_RECTANGLE_ARB``. While the |
| 772 |
texture will typically be created only once and subsequentally updated each |
| 773 |
frame, you should make no such assumption in your application -- future |
| 774 |
versions of pyglet may use multiple texture objects. |
| 775 |
|
| 776 |
.. _guide-media-positionalaudio: |
| 777 |
|
| 778 |
Positional audio |
| 779 |
---------------- |
| 780 |
|
| 781 |
pyglet includes features for positioning sound within a 3D space. This is |
| 782 |
particularly effective with a surround-sound setup, but is also applicable to |
| 783 |
stereo systems. |
| 784 |
|
| 785 |
A :py:class:`~pyglet.media.player.Player` in pyglet has an associated position |
| 786 |
in 3D space -- that is, it is equivalent to an OpenAL "source". The properties |
| 787 |
for setting these parameters are described in more detail in the API |
| 788 |
documentation; see for example :py:attr:`~pyglet.media.Player.position` and |
| 789 |
:py:attr:`~pyglet.media.Player.pitch`. |
| 790 |
|
| 791 |
A "listener" object is provided by the audio driver. To obtain the listener |
| 792 |
for the current audio driver:: |
| 793 |
|
| 794 |
pyglet.media.get_audio_driver().get_listener() |
| 795 |
|
| 796 |
This provides similar properties such as |
| 797 |
:py:attr:`~pyglet.media.listener.AbstractListener.position`, |
| 798 |
:py:attr:`~pyglet.media.listener.AbstractListener.forward_orientation` and |
| 799 |
:py:attr:`~pyglet.media.listener.AbstractListener.up_orientation` that |
| 800 |
describe the position of the user in 3D space. |
| 801 |
|
| 802 |
Note that only mono sounds can be positioned. Stereo sounds will play back as |
| 803 |
normal, and only their volume and pitch properties will affect the sound. |
| 804 |
|
| 805 |
.. _guide-media-tickingtheclock: |
| 806 |
|
| 807 |
Ticking the clock |
| 808 |
----------------- |
| 809 |
|
| 810 |
If you are using pyglet's media libraries outside of a pyglet app (not using |
| 811 |
``pyglet.app.run``) you will need to use some kind of loop to tick the pyglet clock |
| 812 |
periodically (perhaps every 200ms or so), otherwise you will have unintended side |
| 813 |
effects. Depending on the backend in use, this could mean only the first small sample |
| 814 |
of media will be played, or crashes due to internal resource exhaustion. At a minimum |
| 815 |
you will need to call:: |
| 816 |
|
| 817 |
pyglet.clock.tick() |
| 818 |
|
| 819 |
If you wish to have a media source loop continuously (``player.loop = True``) you will |
| 820 |
also need to ensure Pyglet's events are dispatched inside your loop:: |
| 821 |
|
| 822 |
pyglet.app.platform_event_loop.dispatch_posted_events() |
| 823 |
|
| 824 |
If you are inside a pyglet app then calling ``pyglet.app.run()`` takes care of |
| 825 |
all this for you. |