annotate gamelib/sound.py @ 101:41db9f3ba29e

Add missing sys import.
author Simon Cross <hodgestar@gmail.com>
date Wed, 02 Sep 2009 17:26:01 +0000
parents f5d56688943b
children e23637457aa4
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
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
38 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
39 # 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
40 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
41
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 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
43 """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
44 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
45 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
46 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
47 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
48 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
49 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
50 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
51 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
52 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
53 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
54 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
55 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
56 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
57