annotate gamelib/sound.py @ 520:3e19a7f5333e

Tweak controls dialog
author Neil Muller <drnlmuller@gmail.com>
date Fri, 27 Nov 2009 14:01:29 +0000
parents 03d5cb669298
children
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
101
41db9f3ba29e Add missing sys import.
Simon Cross <hodgestar@gmail.com>
parents: 99
diff changeset
2 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
3
411
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents: 363
diff changeset
4 import pygame
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents: 363
diff changeset
5
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
6 import data
411
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents: 363
diff changeset
7 from config import config
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
8 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
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 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
11
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 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
13 """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
14 global SOUND_INITIALIZED
411
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents: 363
diff changeset
15 if not config.sound:
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents: 363
diff changeset
16 return
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
17 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
18 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
19 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
20 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
21 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
22
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
23 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
24
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
25 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
26 """plays the sound with the given filename from the data sounds directory"""
411
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents: 363
diff changeset
27 if not (SOUND_INITIALIZED and config.sound):
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
28 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
29 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
30 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
31 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
32 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
33 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
34 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
35 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
36
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
37 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
38
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
39 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
40 """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
41 global CURRENT_MUSIC_FILE
411
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents: 363
diff changeset
42 if not (SOUND_INITIALIZED and config.sound):
363
e23637457aa4 Prevent error stopping music when sound not initialized
David Fraser <davidf@sjsoft.com>
parents: 101
diff changeset
43 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
44 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
45 # 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
46 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
47
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 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
49 """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
50 global CURRENT_MUSIC_FILE
411
03d5cb669298 Add config file and command line parameters.
Jeremy Thurgood <firxen@gmail.com>
parents: 363
diff changeset
51 if not (SOUND_INITIALIZED and config.sound):
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
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 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
54 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
55 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
56 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
57 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
58 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
59 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
60 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
61 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
62 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
63