Skip to content

Instantly share code, notes, and snippets.

@theapi
Created November 13, 2013 13:46
Show Gist options
  • Save theapi/7449353 to your computer and use it in GitHub Desktop.
Save theapi/7449353 to your computer and use it in GitHub Desktop.
Play sounds with pygame without a GUI with the sounds being chosen by key presses.
import os
os.environ['SDL_VIDEODRIVER'] = 'dummy'
import pygame
import curses
# -----------------------------------------------------------------------------
class curses_screen:
def __enter__(self):
self.stdscr = curses.initscr()
curses.cbreak()
curses.noecho()
curses.curs_set(0)
self.stdscr.keypad(1)
return self.stdscr
def __exit__(self,a,b,c):
curses.nocbreak()
self.stdscr.keypad(0)
curses.echo()
curses.endwin()
# -----------------------------------------------------------------------------
pygame.init()
pygame.display.set_mode((1,1))
pygame.mixer.init()
noise = pygame.mixer.Sound("/usr/share/sounds/alsa/Noise.wav")
snd_a = pygame.mixer.Sound("/usr/share/sounds/alsa/Front_Center.wav")
snd_b = pygame.mixer.Sound("/usr/share/sounds/alsa/Rear_Center.wav")
snd_c = pygame.mixer.Sound("/usr/share/sounds/alsa/Side_Left.wav")
snd_d = pygame.mixer.Sound("/usr/share/sounds/alsa/Side_Right.wav")
noise.play()
with curses_screen() as stdscr:
key = 'X'
while key != ord('q'):
key = stdscr.getch()
if key == ord('a'):
noise.play()
stdscr.addstr(0,0, 'NOISE')
stdscr.refresh()
if key == curses.KEY_UP:
snd_b.play()
stdscr.addstr(0,0, 'UP ')
stdscr.refresh()
if key == curses.KEY_DOWN:
snd_a.play()
stdscr.addstr(0,0, 'DOWN ')
stdscr.refresh()
if key == curses.KEY_LEFT:
snd_c.play()
stdscr.addstr(0,0, 'LEFT ')
stdscr.refresh()
if key == curses.KEY_RIGHT:
snd_d.play()
stdscr.addstr(0,0, 'RIGHT')
stdscr.refresh()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment