Adding sounds

A sound engine can be created using the Sound class. The sound engine can be customized by setting a sound file to several sounds defined by a type.

Example:

import pygame_menu
from pygame_menu import sound

engine = sound.Sound()
engine.set_sound(sound.SOUND_TYPE_CLICK_MOUSE, '/home/me/click.ogg')
engine.set_sound(sound.SOUND_TYPE_OPEN_MENU, '/home/me/open.ogg')

menu = pygame_menu.Menu(...)
menu.set_sound(engine, recursive=True)  # Apply on menu and all sub-menus

Sound types are the following:

Type

Description

pygame_menu.sound.SOUND_TYPE_CLICK_MOUSE

Mouse click

pygame_menu.sound.SOUND_TYPE_CLOSE_MENU

A menu is closed

pygame_menu.sound.SOUND_TYPE_ERROR

Generic error

pygame_menu.sound.SOUND_TYPE_EVENT

Generic event

pygame_menu.sound.SOUND_TYPE_EVENT_ERROR

Error generated by user

pygame_menu.sound.SOUND_TYPE_KEY_ADDITION

User type a key

pygame_menu.sound.SOUND_TYPE_KEY_DELETION

User deletes with a key

pygame_menu.sound.SOUND_TYPE_OPEN_MENU

A menu is opened

pygame_menu.sound.SOUND_TYPE_WIDGET_SELECTION

A widget is selected

class pygame_menu.sound.Sound(allowedchanges=5, buffer=4096, channels=2, devicename='', force_init=False, frequency=22050, size=- 16, sound_id='', uniquechannel=True)[source]

Sound engine class.

Parameters
  • allowedchanges (int) – Convert the samples at runtime, only in pygame>=2.0.0

  • buffer (int) – Buffer size

  • channels (int) – Number of channels

  • devicename (str) – Device name

  • force_init (bool) – Force mixer init with new parameters

  • frequency (int) – Frequency of sounds

  • size (int) – Size of sample

  • sound_id (str) – Sound ID

  • uniquechannel (bool) – Force the channel to be unique, this is set at the object creation moment

copy()[source]

Return a copy of the object.

Return type

Sound

Returns

Sound copied

get_channel()[source]

Return the channel of the sound engine.

Return type

Channel

Returns

Sound engine channel

get_channel_info()[source]

Return the channel information.

Return type

Dict[str, Any]

Returns

Information dict e.g.: {'busy': 0, 'endevent': 0, 'queue': None, 'sound': None, 'volume': 1.0}

load_example_sounds(volume=0.5)[source]

Load the example sounds provided by the package.

Parameters

volume (float) – Volume of the sound, from 0 to 1

Return type

Sound

Returns

Self reference

pause()[source]

Pause the channel.

Return type

Sound

Returns

Self reference

play_click_mouse()[source]

Play click mouse sound.

Return type

Sound

Returns

Self reference

play_close_menu()[source]

Play close Menu sound.

Return type

Sound

Returns

Self reference

play_error()[source]

Play error sound.

Return type

Sound

Returns

Self reference

play_event()[source]

Play event sound.

Return type

Sound

Returns

Self reference

play_event_error()[source]

Play event error sound.

Return type

Sound

Returns

Self reference

play_key_add()[source]

Play key addition sound.

Return type

Sound

Returns

Self reference

play_key_del()[source]

Play key deletion sound.

Return type

Sound

Returns

Self reference

play_open_menu()[source]

Play open Menu sound.

Return type

Sound

Returns

Self reference

play_widget_selection()[source]

Play widget selection sound.

Return type

Sound

Returns

Self reference

set_sound(sound_type, sound_file, volume=0.5, loops=0, maxtime=0, fade_ms=0)[source]

Link a sound file to a sound type.

Parameters
  • sound_type (str) – Sound type

  • sound_file (Union[str, Path, None]) – Sound file. If None disable the given sound type

  • volume (float) – Volume of the sound, from 0.0 to 1.0

  • loops (int) – Loops of the sound

  • maxtime (Union[int, float]) – Max playing time of the sound

  • fade_ms (Union[int, float]) – Fading ms

Return type

bool

Returns

The status of the sound load, True if the sound was loaded

stop()[source]

Stop the channel.

Return type

Sound

Returns

Self reference

unpause()[source]

Unpause channel.

Return type

Sound

Returns

Self reference