[584] | 1 | #!/usr/bin/env python |
---|
[134] | 2 | |
---|
| 3 | # This file is part of PARPG. |
---|
| 4 | |
---|
| 5 | # PARPG is free software: you can redistribute it and/or modify |
---|
| 6 | # it under the terms of the GNU General Public License as published by |
---|
| 7 | # the Free Software Foundation, either version 3 of the License, or |
---|
| 8 | # (at your option) any later version. |
---|
| 9 | |
---|
| 10 | # PARPG is distributed in the hope that it will be useful, |
---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | # GNU General Public License for more details. |
---|
| 14 | |
---|
| 15 | # You should have received a copy of the GNU General Public License |
---|
| 16 | # along with PARPG. If not, see <http://www.gnu.org/licenses/>. |
---|
| 17 | |
---|
| 18 | # sounds.py holds the object code to play sounds and sound effects |
---|
| 19 | |
---|
| 20 | class SoundEngine: |
---|
| 21 | def __init__(self, fife_engine): |
---|
[141] | 22 | """Initialise the SoundEngine instance |
---|
| 23 | @type fife_engine: fine.Engine |
---|
| 24 | @param fife_engine: Instance of the Fife engine |
---|
| 25 | @return: None""" |
---|
[134] | 26 | self.engine = fife_engine |
---|
| 27 | self.sound_engine = self.engine.getSoundManager() |
---|
| 28 | self.sound_engine.init() |
---|
| 29 | # set up the sound |
---|
| 30 | self.music = self.sound_engine.createEmitter() |
---|
[135] | 31 | self.music_on = False |
---|
[140] | 32 | self.music_init = False |
---|
[134] | 33 | |
---|
[313] | 34 | def playMusic(self, sfile=None): |
---|
[141] | 35 | """Play music, with the given file if passed |
---|
| 36 | @type sfile: string |
---|
| 37 | @param sfile: Filename to play |
---|
| 38 | @return: None""" |
---|
[313] | 39 | if(sfile is not None): |
---|
[134] | 40 | # setup the new sound |
---|
| 41 | sound = self.engine.getSoundClipPool().addResourceFromFile(sfile) |
---|
| 42 | self.music.setSoundClip(sound) |
---|
| 43 | self.music.setLooping(True) |
---|
[140] | 44 | self.music_init = True |
---|
[134] | 45 | self.music.play() |
---|
[135] | 46 | self.music_on = True |
---|
[140] | 47 | |
---|
[134] | 48 | def pauseMusic(self): |
---|
[141] | 49 | """Stops current playback |
---|
| 50 | @return: None""" |
---|
[140] | 51 | if(self.music_init == True): |
---|
| 52 | self.music.pause() |
---|
| 53 | self.music_on = False |
---|
[134] | 54 | |
---|
[135] | 55 | def toggleMusic(self): |
---|
[141] | 56 | """Toggle status of music, either on or off |
---|
| 57 | @return: None""" |
---|
[140] | 58 | if((self.music_on == False)and(self.music_init == True)): |
---|
[135] | 59 | self.playMusic() |
---|
| 60 | else: |
---|
| 61 | self.pauseMusic() |
---|
| 62 | |
---|
[134] | 63 | def setVolume(self, volume): |
---|
[141] | 64 | """Set the volume of the music |
---|
| 65 | @type volume: integer |
---|
| 66 | @param volume: The volume wanted, 0 to 100 |
---|
| 67 | @return: None""" |
---|
[313] | 68 | self.sound_engine.setVolume(0.01 * volume) |
---|
[134] | 69 | |
---|