annotate gamelib/sound.py @ 97:529a4d41c67a

Rather use sound playing (we don't need to stream sound effects from disk), and cache sounds
author David Fraser <davidf@sjsoft.com>
date Wed, 02 Sep 2009 11:52:02 +0000
parents 23a8b2e49e9f
children f5d56688943b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
90
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
1 import os
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
2 import pygame
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
3
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
4 import data
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
5 import constants
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
6
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
7 SOUND_INITIALIZED = False
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
8
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
9 def init_sound():
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
10 """initialize the sound system"""
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
11 global SOUND_INITIALIZED
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
12 try:
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
13 pygame.mixer.init(constants.FREQ, constants.BITSIZE, constants.CHANNELS, constants.BUFFER)
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
14 SOUND_INITIALIZED = True
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
15 except pygame.error, exc:
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
16 print >>sys.stderr, "Could not initialize sound system: %s" % exc
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
17
97
529a4d41c67a Rather use sound playing (we don't need to stream sound effects from disk), and cache sounds
David Fraser <davidf@sjsoft.com>
parents: 90
diff changeset
18 SOUND_CACHE = {}
529a4d41c67a Rather use sound playing (we don't need to stream sound effects from disk), and cache sounds
David Fraser <davidf@sjsoft.com>
parents: 90
diff changeset
19
90
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
20 def play_sound(filename):
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
21 """plays the sound with the given filename from the data sounds directory"""
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
22 if not SOUND_INITIALIZED:
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
23 return
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
24 file_path = data.filepath("sounds", filename)
97
529a4d41c67a Rather use sound playing (we don't need to stream sound effects from disk), and cache sounds
David Fraser <davidf@sjsoft.com>
parents: 90
diff changeset
25 sound = SOUND_CACHE.get(file_path, None)
529a4d41c67a Rather use sound playing (we don't need to stream sound effects from disk), and cache sounds
David Fraser <davidf@sjsoft.com>
parents: 90
diff changeset
26 if not sound:
529a4d41c67a Rather use sound playing (we don't need to stream sound effects from disk), and cache sounds
David Fraser <davidf@sjsoft.com>
parents: 90
diff changeset
27 if not os.path.exists(file_path):
529a4d41c67a Rather use sound playing (we don't need to stream sound effects from disk), and cache sounds
David Fraser <davidf@sjsoft.com>
parents: 90
diff changeset
28 return
529a4d41c67a Rather use sound playing (we don't need to stream sound effects from disk), and cache sounds
David Fraser <davidf@sjsoft.com>
parents: 90
diff changeset
29 SOUND_CACHE[file_path] = sound = pygame.mixer.Sound(file_path)
529a4d41c67a Rather use sound playing (we don't need to stream sound effects from disk), and cache sounds
David Fraser <davidf@sjsoft.com>
parents: 90
diff changeset
30 sound.play()
90
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
31
23a8b2e49e9f Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
David Fraser <davidf@sjsoft.com>
parents:
diff changeset
32