annotate skaapsteker/sound.py @ 632:0675f390653c

Initial port to Python 3 and Pygame 2.
author Simon Cross <hodgestar@gmail.com>
date Fri, 20 Jan 2023 20:01:06 +0100
parents 62569f486ede
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
262
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
1 """Support for playing sounds and music"""
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
2
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
3 from pygame import mixer
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
4 import pygame
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
5 from . import data
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
6 from .constants import FREQ, BITSIZE, CHANNELS, BUFFER
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
7
529
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
8 _GLOBAL_SOUND = None
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
9
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
10
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
11 def load_sound(key, track_name, volume=None):
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
12 if _GLOBAL_SOUND is not None:
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
13 _GLOBAL_SOUND.load_sound(key, track_name, volume)
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
14
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
15 def play_sound(key):
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
16 if _GLOBAL_SOUND is not None:
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
17 _GLOBAL_SOUND.play_sound(key)
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
18
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
19
262
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
20 class SoundSystem(object):
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
21
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
22 def __init__(self, want_sound):
529
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
23 global _GLOBAL_SOUND
262
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
24 if want_sound:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
25 # See if we can actually enabled sound
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
26 try:
553
62569f486ede Debug print cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 529
diff changeset
27 mixer.init(FREQ, BITSIZE, CHANNELS, BUFFER)
62569f486ede Debug print cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 529
diff changeset
28 test_sound = mixer.Sound(data.filepath('sounds/silence.ogg'))
62569f486ede Debug print cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 529
diff changeset
29 test_sound.play()
62569f486ede Debug print cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 529
diff changeset
30 self.sound_enabled = True
262
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
31 except pygame.error:
632
0675f390653c Initial port to Python 3 and Pygame 2.
Simon Cross <hodgestar@gmail.com>
parents: 553
diff changeset
32 print('Unable to enable sound')
553
62569f486ede Debug print cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 529
diff changeset
33 self.sound_enabled = False
262
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
34 else:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
35 self.sound_enabled = False
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
36
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
37 self._sounds = {}
529
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
38 _GLOBAL_SOUND = self
262
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
39
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
40
453
9c9df17b98a7 Add support for choosing a volume. A music to temple.
Simon Cross <hodgestar@gmail.com>
parents: 263
diff changeset
41 def play_background_music(self, track_name, volume=1.0):
262
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
42 if self.sound_enabled:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
43 try:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
44 mixer.music.load(data.filepath(track_name))
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
45 mixer.music.play(-1) # Loop forever
453
9c9df17b98a7 Add support for choosing a volume. A music to temple.
Simon Cross <hodgestar@gmail.com>
parents: 263
diff changeset
46 mixer.music.set_volume(volume)
262
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
47 except pygame.error:
632
0675f390653c Initial port to Python 3 and Pygame 2.
Simon Cross <hodgestar@gmail.com>
parents: 553
diff changeset
48 print('Unable to load track')
262
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
49
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
50 def stop_music(self):
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
51 if self.sound_enabled:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
52 mixer.music.stop()
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
53
529
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
54 def load_sound(self, key, track_name, volume=None):
262
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
55 if key in self._sounds:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
56 # First caller wins on duplicate keys
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
57 return
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
58 if not self.sound_enabled:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
59 self._sounds[key] = None
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
60 else:
263
44cd7cfd2de3 Yelp when hit
Neil Muller <drnlmuller@gmail.com>
parents: 262
diff changeset
61 self._sounds[key] = pygame.mixer.Sound(data.filepath(track_name))
529
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
62 if volume is not None:
0ffb493a6fa4 Add support for sounds for breaking objects.
Simon Cross <hodgestar@gmail.com>
parents: 505
diff changeset
63 self._sounds[key].set_volume(volume)
262
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
64
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
65 def play_sound(self, key):
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
66 sound = self._sounds.get(key, None)
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
67 if sound:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
68 sound.play()
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
69
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
70 def stop_all_sounds(self):
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
71 if self.sound_enabled:
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
72 mixer.stop()
de60329cfc9f Factor out sound stuff
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
73
505
a89ab402b569 Pause music when pressing pause in level.
Simon Cross <hodgestar@gmail.com>
parents: 453
diff changeset
74 def pause(self):
a89ab402b569 Pause music when pressing pause in level.
Simon Cross <hodgestar@gmail.com>
parents: 453
diff changeset
75 if self.sound_enabled:
a89ab402b569 Pause music when pressing pause in level.
Simon Cross <hodgestar@gmail.com>
parents: 453
diff changeset
76 mixer.music.pause()
a89ab402b569 Pause music when pressing pause in level.
Simon Cross <hodgestar@gmail.com>
parents: 453
diff changeset
77
a89ab402b569 Pause music when pressing pause in level.
Simon Cross <hodgestar@gmail.com>
parents: 453
diff changeset
78 def unpause(self):
a89ab402b569 Pause music when pressing pause in level.
Simon Cross <hodgestar@gmail.com>
parents: 453
diff changeset
79 if self.sound_enabled:
a89ab402b569 Pause music when pressing pause in level.
Simon Cross <hodgestar@gmail.com>
parents: 453
diff changeset
80 mixer.music.unpause()