bear_hug.sound module

A sound system.

Currently it exports a single class called SoundListener. It’s a Listener wrapper around simpleaudio and wave libraries. While later the backend is likely to change (at least to support sound formats other than .wav), event API is probably gonna remain backwards-compatible.

class bear_hug.sound.SoundListener(sounds)

Bases: bear_hug.widgets.Listener

It doesn’t listen to sounds. It listens to the events and plays sounds.

This class is expected to be used as a singleton, ie there is no reason to have two SoundListeners active at the same time, and therefore no API for it.

Accepts a single kind of event:

BearEvent(event_type=’play_sound’, event_value=sound_name)

If sound_name is a known sound ID, this sound is (asynchronously) played. Otherwise, BearSoundException is raised. Sounds can be either supplied in a single arg during creation, or added later via register_sound. In either case, for a sound either a simpleaudio.WaveObject or a string is expected. In the latter case, a string is treated as a path to a .wav file.

Parameters:sounds – a dict of {'sound_id': simlpleaudio.WaveObject}
register_sound(sound, sound_name)

Register a new sound for this listener

Parameters:
  • sound – WaveObject or str. A sound to be registered. If str, this is treated as a path to a .wav file.
  • sound_name – name of this sound.
play_sound(sound_name)

Play a sound.

In case you need to play the sound without requesting it through the event.

Parameters:sound_name – A sound to play.