view gamelib/sound.py @ 315:7b07ffc37ec0

Suspend background sound for bridge computer detail
author Neil Muller <neil@dip.sun.ac.za>
date Sat, 28 Aug 2010 12:11:42 +0200
parents aebd4fd5b561
children 6df527142e69
line wrap: on
line source

# Sound management for Suspended Sentence

# This re-implements some of the albow.resource code to
# a) work around an annoying bugs
# b) add some missing functionality (disable_sound)

import os

import pygame
from pygame.mixer import music
from albow.resource import _resource_path, dummy_sound
import albow.music

sound_cache = {}

def get_sound(*names):
    if sound_cache is None:
        return dummy_sound
    path = _resource_path("sounds", names)
    sound = sound_cache.get(path)
    if not sound:
        if not os.path.isfile(path):
            missing_sound("File does not exist", path)
            return dummy_sound
        try:
            from pygame.mixer import Sound
        except ImportError, e:
            no_sound(e)
            return dummy_sound
        try:
            sound = Sound(path)
        except pygame.error, e:
            missing_sound(e, path)
            return dummy_sound
        sound_cache[path] = sound
    return sound


def no_sound(e):
    global sound_cache
    print "get_sound: %s" % e
    print "get_sound: Sound not available, continuing without it"
    sound_cache = None
    albow.music.music_enabled = False

def disable_sound():
    global sound_cache
    sound_cache = None
    albow.music.music_enabled = False

def missing_sound(e, name):
    print "albow.resource.get_sound: %s: %s" % (name, e)


def start_next_music():
    """Start playing the next item from the current playlist immediately."""
    if albow.music.music_enabled and albow.music.current_playlist:
        next_music = albow.music.current_playlist.next()
        if next_music:
            #print "albow.music: loading", repr(next_music)
            music.load(next_music)
            music.play()
            albow.music.next_change_delay = albow.music.change_delay
        albow.music.current_music = next_music

def get_current_playlist():
    if albow.music.music_enabled and albow.music.current_playlist:
        return albow.music.current_playlist

# Monkey patch
albow.music.start_next_music = start_next_music