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.

For example, buttons or keys…

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(uniquechannel=True, frequency=22050, size=-16, channels=2, buffer=4096, devicename='', allowedchanges=5, force_init=False)[source]

Sound engine class.

Parameters:
  • uniquechannel (bool) – Force the channel to be unique, this is set at the moment of creation of the object
  • frequency (int) – Frequency of sounds
  • size (int) – Size of sample
  • channels (int) – Number of channels by default
  • buffer (int) – Buffer size
  • devicename (str) – Device name
  • allowedchanges (bool) – Convert the samples at runtime, only in pygame>=2.0.0
  • force_init (bool) – Force mixer init with new parameters
get_channel()[source]

Return the channel of the sound engine.

Returns:Channel
Return type:pygame.mixer.Channel
get_channel_info()[source]

Return the current channel information of the sound engine.

Returns:An info dict e.g.: {‘busy’: 0, ‘endevent’: 0, ‘queue’: None, ‘sound’: None, ‘volume’: 1.0}
Return type:dict
load_example_sounds(volume=0.5)[source]

Load the example sounds provided by the package.

Parameters:volume (float) – Volume of the sound, (0-1)
Returns:None
pause()[source]

Pause the current channel.

Returns:None
play_click_mouse()[source]

Play click mouse sound.

Returns:None
play_close_menu()[source]

Play close menu sound.

Returns:None
play_error()[source]

Play error sound.

Returns:None
play_event()[source]

Play event sound.

Returns:None
play_event_error()[source]

Play event error sound.

Returns:None
play_key_add()[source]

Play key addition sound.

Returns:None
play_key_del()[source]

Play key deletion sound.

Returns:None
play_open_menu()[source]

Play open menu sound.

Returns:None
play_widget_selection()[source]

Play widget selection sound.

Returns:None
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 (str, None) – Sound file
  • volume (float) – Volume of the sound, from 0.0 to 1.0
  • loops (int) – Loops of the sound
  • maxtime (int, float) – Max playing time of the sound
  • fade_ms (int, float) – Fading ms
Returns:

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

Return type:

bool

stop()[source]

Stop the current the channel.

Returns:None
unpause()[source]

Unpause channel.

Returns:None