annotate gamelib/sound.py @ 372:90abd39e6c3c 1.0.x

Merge in post-pyweek fixes (r359:371).
author Simon Cross <hodgestar@gmail.com>
date Tue, 13 Oct 2009 20:28:48 +0000
parents 41db9f3ba29e
children 03d5cb669298
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
101
41db9f3ba29e Add missing sys import.
Simon Cross <hodgestar@gmail.com>
parents: 99
diff changeset
3 import sys
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
4
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 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
6 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
7
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 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
9
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 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
11 """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
12 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
13 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
14 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
15 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
16 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
17 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
18
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
19 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
20
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
21 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
22 """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
23 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
24 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
25 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
26 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
27 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
28 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
29 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
30 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
31 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
32
99
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
33 CURRENT_MUSIC_FILE = None
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
34
99
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
35 def stop_background_music():
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
36 """stops any playing background music"""
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
37 global CURRENT_MUSIC_FILE
372
90abd39e6c3c Merge in post-pyweek fixes (r359:371).
Simon Cross <hodgestar@gmail.com>
parents: 101
diff changeset
38 if not SOUND_INITIALIZED:
90abd39e6c3c Merge in post-pyweek fixes (r359:371).
Simon Cross <hodgestar@gmail.com>
parents: 101
diff changeset
39 return
99
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
40 CURRENT_MUSIC_FILE = None
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
41 # TODO: fadeout in a background thread
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
42 pygame.mixer.music.stop()
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
43
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
44 def background_music(filename):
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
45 """plays the background music with the given filename from the data sounds directory"""
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
46 global CURRENT_MUSIC_FILE
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
47 if not SOUND_INITIALIZED:
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
48 return
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
49 file_path = data.filepath("sounds", filename)
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
50 if CURRENT_MUSIC_FILE == file_path:
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
51 return
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
52 stop_background_music()
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
53 if not os.path.exists(file_path):
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
54 return
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
55 CURRENT_MUSIC_FILE = file_path
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
56 pygame.mixer.music.load(file_path)
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
57 pygame.mixer.music.set_volume(0.3)
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
58 pygame.mixer.music.play(-1)
f5d56688943b Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents: 97
diff changeset
59