Sunday, March 28, 2010

Pyglet: Audio Support

music technology articleI promised I would cover audio support in Pyglet, even though, when all is said and done, there is little to say. Support is quite rudimentary, though what is there works well. I was able to write a simple application on Windows 7, test it on LINUX and then install it for OS X without any bugs, code changes or even configuration issues. That is cross-platform programming as it should be. (Though I would accept the need for config changes to address hardware-specific issues.)

Audio and video playback is handled by the same package, pyglet.media, which automatically detects the media type and acts appropriately. As I mentioned in Pyglet: Helpful Tips, you will want to install the "optional" AVbin to support anything other than plain uncompressed formats.

Though you might want to refer to the API docs for all the details, I'll explain here how to set up sound playback. In brief, you use a Source instance to decode an audio file and then queue this on a Player for playback. By default this decoding is done on-the-fly as a StreamingSource. If you want a sound to be decoded in advance, you can designate it as a StaticSource. This is useful for short sound effects and the like. There's not much you can do with a source except read its duration.

A Player can be used to play, pause and restart a sound. You can get the current playback point and seek to a position in the source. There is support for pitch shifting and attenuation, but I did not try these. There is no time stretching, however, and even basic filtering or EQ is outside the realm of what this simple library is designed to do. I would like to see these features, as well as the ability to cross-fade sources, as this would provide commonly needed functionality.

Here is the basic code I used:

import pyglet

wavfile = 'some/path/to/file.wav'
sound = pyglet.media.load(wavfile)

core = pyglet.media.Player()
core.queue(sound)
core.play()


There is only one event provided for responding to audio, on_eos(), which triggers when the player has reached the end of the current source. It would be great to be able to set up triggers at other times (so one could have advanced notice of the approaching end of a file) and on other events (for example, pausing).

This library for sufficient for my simple case, but I would have to look at something more complete for more than the rudiments... a search that still continues!

RELATED POSTS

No comments:

Post a Comment