Mercurial > rinkhals
annotate gamelib/sound.py @ 90:23a8b2e49e9f
Added ability to initialize sound and play sounds, and handle sound not working / file being missing etc
author | David Fraser <davidf@sjsoft.com> |
---|---|
date | Wed, 02 Sep 2009 10:34:22 +0000 |
parents | |
children | 529a4d41c67a |
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 |
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 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
|
19 """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
|
20 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
|
21 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
|
22 file_path = data.filepath("sounds", 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
|
23 if not os.path.exists(file_path): |
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 pygame.mixer.music.load(file_path) |
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 pygame.mixer.music.play() |
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
|
27 |
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 |