comparison 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
comparison
equal deleted inserted replaced
96:a851461d345e 97:529a4d41c67a
13 pygame.mixer.init(constants.FREQ, constants.BITSIZE, constants.CHANNELS, constants.BUFFER) 13 pygame.mixer.init(constants.FREQ, constants.BITSIZE, constants.CHANNELS, constants.BUFFER)
14 SOUND_INITIALIZED = True 14 SOUND_INITIALIZED = True
15 except pygame.error, exc: 15 except pygame.error, exc:
16 print >>sys.stderr, "Could not initialize sound system: %s" % exc 16 print >>sys.stderr, "Could not initialize sound system: %s" % exc
17 17
18 SOUND_CACHE = {}
19
18 def play_sound(filename): 20 def play_sound(filename):
19 """plays the sound with the given filename from the data sounds directory""" 21 """plays the sound with the given filename from the data sounds directory"""
20 if not SOUND_INITIALIZED: 22 if not SOUND_INITIALIZED:
21 return 23 return
22 file_path = data.filepath("sounds", filename) 24 file_path = data.filepath("sounds", filename)
23 if not os.path.exists(file_path): 25 sound = SOUND_CACHE.get(file_path, None)
24 return 26 if not sound:
25 pygame.mixer.music.load(file_path) 27 if not os.path.exists(file_path):
26 pygame.mixer.music.play() 28 return
29 SOUND_CACHE[file_path] = sound = pygame.mixer.Sound(file_path)
30 sound.play()
27 31
28 32