view pyntnclick/sound.py @ 548:ded4324b236e pyntnclick

Moved stuff around, broke everything.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 11 Feb 2012 13:10:18 +0200
parents gamelib/sound.py@6df527142e69
children 2f7aa3cad77c
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