Source

Fat x Fast / FatxFast / audio.py

Full commit
# This file is part of FatxFast.
#
#    FatxFast is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    FatxFast is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with FatxFast.  If not, see <http://www.gnu.org/licenses/>.

# Versioning based on: 
# http://en.wikipedia.org/wiki/Versioning#Designating_development_stage
__author__ = "dryatu (c) 2013"
__version__ = "1.2.5"

import pygame
from FatxFast.config import Config
from FatxFast.resource import resource_manager

class Audio(object):

    def __init__(self, audio_enabled=True, config=Config()):
        self.sounds = {}
        self.config = config
        self.enabled = audio_enabled
        self.music = pygame.mixer.music

    def load_music(self, name, volume=None):
        if self.enabled:
            self.music.load(resource_manager.get_resource_path(name))
            if volume is None:
                volume = self.config.volume
            self.music.set_volume(volume)

    def load_sound(self, name):
        if self.enabled:
            self.sounds[name] = pygame.mixer.Sound(file=
                resource_manager.get_resource_path(name))

    def play_sound(self, name):
        if name in self.sounds and self.enabled and self.config.music:
            self.sounds[name].play()

    def play_music(self, loops=0):
        if self.enabled and self.config.music:
            self.music.play(loops)

    def stop_music(self):
        if self.enabled:
            self.music.stop()

    def update(self, dt):
        if self.enabled:
            if self.config.volume != pygame.mixer.music.get_volume():
                pygame.mixer.music.set_volume(self.config.volume)