Mercurial > rinkhals
annotate gamelib/sound.py @ 99:f5d56688943b
Added background music support, and daytime and night time music (really animal sounds, not music)
author | David Fraser <davidf@sjsoft.com> |
---|---|
date | Wed, 02 Sep 2009 12:50:28 +0000 |
parents | 529a4d41c67a |
children | 41db9f3ba29e |
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 |
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
|
32 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
|
33 |
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
|
34 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
|
35 """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
|
36 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
|
37 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
|
38 # 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
|
39 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
|
40 |
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 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
|
42 """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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 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
|
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 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
|
50 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
|
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 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
|
53 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
|
54 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
|
55 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
|
56 |