#
###
# Application: Pycadia
# File:        sound.py
# Description: pycadia Sound class
# Copyright (c) 2002-2003   Andy Balcombe <http://www.anti-particle.com>
###
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import os
import pygame
from pygame.locals import *

class PycadiaSound:
    """make some noise"""
    def __init__(self, file):
        self.sound = None
        if not pygame.mixer: return
        file = os.path.join('sounds', file)
        try:
            self.sound = pygame.mixer.Sound(file)
        except pygame.error:
            #print 'Warning, unable to load: ', file
            pass
        #set playing 0=not playing, 1=already playing
        self.playing = 0
        
    def play(self, loop=0, volume=1.0):
        """plays a sound"""
        if self.sound:
            if not self.playing:
                if loop != 0: self.playing = 1 #only set this for looping samples
                self.sound.set_volume(volume)
                self.sound.play(loop)
        
    def stop(self):
        """stops playing a sound"""
        if self.sound:
            if self.playing:
                self.sound.stop()
                self.playing = 0

