annotate skaapsteker/sound.py @ 493:cb2060f8316f

Tweak gamestate to fail earlier on unknown items.
author Simon Cross <hodgestar@gmail.com>
date Sat, 09 Apr 2011 22:41:38 +0200
parents 9c9df17b98a7
children a89ab402b569
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
262
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
1 """Support for playing sounds and music"""
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
2
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
3 from pygame import mixer
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
4 import pygame
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
5 from . import data
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
6 from .constants import FREQ, BITSIZE, CHANNELS, BUFFER
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
7
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
8 class SoundSystem(object):
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
9
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
10 def __init__(self, want_sound):
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
11 if want_sound:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
12 # See if we can actually enabled sound
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
13 try:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
14 mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
15 test_sound = mixer.Sound(data.filepath('sounds/silence.ogg'))
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
16 test_sound.play()
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
17 self.sound_enabled = True
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
18 except pygame.error:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
19 print 'Unable to enable sound'
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
20 self.sound_enabled = False
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
21 else:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
22 self.sound_enabled = False
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
23
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
24 self._sounds = {}
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
25
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
26
453
9c9df17b98a7 Add support for choosing a volume. A music to temple.
Simon Cross <hodgestar@gmail.com>
parents: 263
diff changeset
27 def play_background_music(self, track_name, volume=1.0):
262
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
28 if self.sound_enabled:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
29 try:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
30 mixer.music.load(data.filepath(track_name))
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
31 mixer.music.play(-1) # Loop forever
453
9c9df17b98a7 Add support for choosing a volume. A music to temple.
Simon Cross <hodgestar@gmail.com>
parents: 263
diff changeset
32 mixer.music.set_volume(volume)
262
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
33 except pygame.error:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
34 print 'Unable to load track'
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
35
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
36 def stop_music(self):
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
37 if self.sound_enabled:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
38 mixer.music.stop()
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
39
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
40 def load_sound(self, key, track_name):
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
41 if key in self._sounds:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
42 # First caller wins on duplicate keys
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
43 return
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
44 if not self.sound_enabled:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
45 self._sounds[key] = None
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
46 else:
263
44cd7cfd2de3 Yelp when hit
Neil Muller <drnlmuller@gmail.com>
parents: 262
diff changeset
47 self._sounds[key] = pygame.mixer.Sound(data.filepath(track_name))
262
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
48
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
49 def play_sound(self, key):
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
50 sound = self._sounds.get(key, None)
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
51 if sound:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
52 sound.play()
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
53
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
54 def stop_all_sounds(self):
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
55 if self.sound_enabled:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
56 mixer.stop()
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
57