-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_manager.py
94 lines (82 loc) · 3.49 KB
/
audio_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
import pygame
import threading
class AudioManager:
def __init__(self, frequency=44100, size=-16, channels=8, buffer=4096, num_reserved=2):
pygame.mixer.init(frequency=frequency, size=size, channels=channels, buffer=buffer)
self.num_reserved = num_reserved
pygame.mixer.set_reserved(self.num_reserved)
self.sounds = {}
self.music = {}
self.current_music = None
self.music_volume = 1.0
self.sound_effects_volume = 1.0
self.music_muted = False
self.sound_effects_muted = False
self.music_lock = threading.Lock()
def initialize_sounds(self, sound_files):
for name, file_path in sound_files.items():
try:
self.sounds[name] = pygame.mixer.Sound(file_path)
except pygame.error as e:
print(f"Error loading sound {name}: {e}")
def initialize_music(self, music_files):
for name, file_path in music_files.items():
self.music[name] = file_path
def play_sound(self, name, priority=False):
if not self.sound_effects_muted and name in self.sounds:
if priority:
for i in range(self.num_reserved):
channel = pygame.mixer.Channel(i)
if not channel.get_busy():
channel.set_volume(self.sound_effects_volume)
channel.play(self.sounds[name])
return
else:
sound = self.sounds[name]
sound.set_volume(self.sound_effects_volume)
sound.play()
def play_music(self, track, fade_time=0):
with self.music_lock:
if not self.music_muted and track in self.music:
if self.current_music != track:
if fade_time > 0:
pygame.mixer.music.fadeout(fade_time)
while pygame.mixer.music.get_busy():
pygame.time.delay(100)
pygame.mixer.music.load(self.music[track])
pygame.mixer.music.set_volume(self.music_volume)
pygame.mixer.music.play(-1)
self.current_music = track
def mute_music(self):
if not self.music_muted:
self.music_muted = True
pygame.mixer.music.set_volume(0.0)
def unmute_music(self):
if self.music_muted:
self.music_muted = False
pygame.mixer.music.set_volume(self.music_volume)
def mute_sound_effects(self):
if not self.sound_effects_muted:
self.sound_effects_muted = True
for sound in self.sounds.values():
sound.set_volume(0.0)
def unmute_sound_effects(self):
if self.sound_effects_muted:
self.sound_effects_muted = False
for sound in self.sounds.values():
sound.set_volume(self.sound_effects_volume)
def set_music_volume(self, volume):
self.music_volume = volume
if not self.music_muted:
pygame.mixer.music.set_volume(volume)
def set_sound_effects_volume(self, volume):
self.sound_effects_volume = volume
if not self.sound_effects_muted:
for sound in self.sounds.values():
sound.set_volume(volume)
def stop_all(self):
for i in range(pygame.mixer.get_num_channels()):
channel = pygame.mixer.Channel(i)
channel.stop()
pygame.mixer.music.stop()
self.current_music = None