hub.sound – Sound

The sound module lets you control the hub speaker to play sound files and beeps.

hub.sound.volume(volume: int)None

Sets the volume of the speaker.


Volume – Volume between 0 (no sound) and 10 (maximum volume).


If no argument is given, this returns the current volume.

hub.sound.beep(freq=1000, time=1000, waveform=0)None

Starts beeping with a given frequency, duration, and wave form.

Keyword Arguments
  • freq – Frequency of the beep in Hz (100 - 10000).

  • time – Duration of the beep in milliseconds (0 - 32767).

  • waveform – Wave form used for the beep. See constants for all possible values. str, rate=16000)None

Starts playing a sound file.

The sound file must be raw 16 bit data at 16 kHz.


filename – Absolute path to the sound file.

Keyword Arguments

rate – Playback speed in Hz.


OSError (ENOENT) – If the file does not exist.

hub.sound.callback(self, function: Callable[[int], None])None

Sets the callback function that is called when a sound finished playing or when it is interrupted.

The function must accept one argument, whose value indicates why the callback was called:

  • If the value is 0, the sound completed successfully.

  • If the value is 1, the sound was interrupted.


function – Callable function that takes one argument. Choose None to disable the callback.


These values are used by the hub.sound.beep() function.

hub.sound.SOUND_SIN = 0

The beep is a smooth sine wave.

hub.sound.SOUND_SQUARE = 1

The beep is a loud and raw square wave.

hub.sound.SOUND_TRIANGLE = 2

The beep has a triangular wave form.

hub.sound.SOUND_SAWTOOTH = 3

The beep has a sawtooth-shaped wave form.