Mercurial > rinkhals
view gamelib/sound.py @ 122:d2b19131d537
Don't continue the night if we're not doing anything anymore
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Wed, 02 Sep 2009 20:54:47 +0000 |
parents | 41db9f3ba29e |
children | e23637457aa4 |
line wrap: on
line source
import os import pygame import sys import data import constants SOUND_INITIALIZED = False def init_sound(): """initialize the sound system""" global SOUND_INITIALIZED try: pygame.mixer.init(constants.FREQ, constants.BITSIZE, constants.CHANNELS, constants.BUFFER) SOUND_INITIALIZED = True except pygame.error, exc: print >>sys.stderr, "Could not initialize sound system: %s" % exc SOUND_CACHE = {} def play_sound(filename): """plays the sound with the given filename from the data sounds directory""" if not SOUND_INITIALIZED: return file_path = data.filepath("sounds", filename) sound = SOUND_CACHE.get(file_path, None) if not sound: if not os.path.exists(file_path): return SOUND_CACHE[file_path] = sound = pygame.mixer.Sound(file_path) sound.play() CURRENT_MUSIC_FILE = None def stop_background_music(): """stops any playing background music""" global CURRENT_MUSIC_FILE CURRENT_MUSIC_FILE = None # TODO: fadeout in a background thread pygame.mixer.music.stop() def background_music(filename): """plays the background music with the given filename from the data sounds directory""" global CURRENT_MUSIC_FILE if not SOUND_INITIALIZED: return file_path = data.filepath("sounds", filename) if CURRENT_MUSIC_FILE == file_path: return stop_background_music() if not os.path.exists(file_path): return CURRENT_MUSIC_FILE = file_path pygame.mixer.music.load(file_path) pygame.mixer.music.set_volume(0.3) pygame.mixer.music.play(-1)