-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsounds_manager.py
130 lines (105 loc) · 3.98 KB
/
sounds_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import pygame
from pygame import mixer
import os
import glob
import sys
import random
AUDIO_FILE_PATH = os.path.join('assets', 'audio')
class SoundManager:
sounds = {}
ambient_sounds = {}
ambient_small_sounds = {}
current_ambient = None
@staticmethod
def load_all():
pygame.mixer.init()
#print(mixer.get_num_channels())
#
mixer.set_num_channels(8)
all_audios = (glob.glob(os.path.join(AUDIO_FILE_PATH, "**/*.wav"), recursive=True))
n = len(all_audios)
for i, audio_path in enumerate(all_audios):
perc = i / n
#Get the name of the audio file without the .wav
name = audio_path.split('/')[-1].split('.')[0]
print(f'[SOUND-MANAGER] Loading sound {name[:10]:>10} ({100*perc:.0f}%)', end='\r')
ambient = 'ambient' in audio_path
random = 'small' in audio_path
SoundManager.load_sound(name, audio_path, ambient=ambient, random=random)
print(f'[SOUND-MANAGER] {n} sound loaded ') # space to remove previous
@staticmethod
def load_sound(name, filepath, ambient=False, random=False):
"""Load a sound effect or ambient sound."""
sound = mixer.Sound(filepath)
if ambient:
if random:
SoundManager.ambient_small_sounds[name] = sound
else:
SoundManager.ambient_sounds[name] = sound
else:
SoundManager.sounds[name] = sound
@staticmethod
def play_sound(name, volume=0.5):
"""Play a sound effect."""
if name in SoundManager.sounds:
SoundManager.sounds[name].play(0)
SoundManager.set_volume(name, volume)
else:
print(f"Sound {name} not found.")
@staticmethod
def stop_sound(name):
"""Stops a sound effect."""
if name in SoundManager.sounds:
SoundManager.sounds[name].stop()
else:
print(f"Sound {name} not found.")
@staticmethod
def play_ambient_small(volume=0.5):
name = random.choice(list(SoundManager.ambient_small_sounds.keys()))
SoundManager.ambient_small_sounds[name].play(1)
SoundManager.ambient_small_sounds[name].set_volume(volume)
@staticmethod
def play_ambient(name, volume=0.5):
"""Play an ambient sound, does not stop the previous one."""
if name in SoundManager.ambient_sounds:
current_ambient = SoundManager.ambient_sounds[name]
current_ambient.set_volume(volume)
mixer.set_reserved(1)
current_ambient.play(loops=-1) # -1 means the sound will loop indefinitely
else:
print(f"Ambient sound {name} not found.")
@staticmethod
def stop_ambient():
"""Stop the current ambient sound."""
if current_ambient:
current_ambient.stop()
current_ambient = None
@staticmethod
def set_volume(name, volume):
"""Set the volume for a specific sound."""
if name in SoundManager.sounds:
SoundManager.sounds[name].set_volume(volume)
elif name in SoundManager.ambient_sounds:
SoundManager.ambient_sounds[name].set_volume(volume)
else:
print(f"Sound {name} not found.")
@staticmethod
def stop_all_sounds():
"""Stop all sounds."""
for sound in SoundManager.sounds.values():
sound.stop()
SoundManager.stop_ambient()
# Example usage
if __name__ == "__main__":
SoundManager.load_all()
SoundManager.play_sound("door_1")
pygame.time.wait(300)
SoundManager.play_sound("beep_only")
# Play ambient sound
SoundManager.play_ambient("morning_highway_birds", volume=1)
# Change ambient sound after some action
pygame.time.wait(5000) # Wait for 5 seconds
SoundManager.play_ambient("empty_room_background")
# Stop all sounds
pygame.time.wait(5000) # Wait for another 5 seconds
SoundManager.stop_all_sounds()