Created
June 27, 2025 06:10
-
-
Save EncodeTheCode/678c7e447e3be1c85ce7b059aa8bb853 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
import pygame._sdl2.mixer as mixer | |
class AudioEngine: | |
AUDIO_QUALITIES = { | |
"low": { | |
"frequency": 11025, | |
"size": -8, | |
"channels": 1, | |
"buffer": 512 | |
}, | |
"medium": { | |
"frequency": 22050, | |
"size": -16, | |
"channels": 1, | |
"buffer": 1024 | |
}, | |
"high": { | |
"frequency": 44100, | |
"size": -16, | |
"channels": 2, | |
"buffer": 2048 | |
}, | |
"ultra": { | |
"frequency": 48000, | |
"size": -16, | |
"channels": 2, | |
"buffer": 4096 | |
} | |
} | |
def __init__(self, quality="high"): # Default quality set here | |
if quality not in self.AUDIO_QUALITIES: | |
raise ValueError(f"Invalid quality level: {quality}") | |
settings = self.AUDIO_QUALITIES[quality] | |
mixer.init( | |
frequency=settings["frequency"], | |
size=settings["size"], | |
channels=settings["channels"], | |
buffer=settings["buffer"] | |
) | |
print(f"[AudioEngine] Initialized with '{quality}' quality.") | |
def find_audio_file(self, base_name, extensions): | |
if isinstance(extensions, str): | |
extensions = [extensions] | |
for ext in extensions: | |
filename = f"{base_name}.{ext}" | |
if os.path.exists(filename): | |
return filename | |
raise FileNotFoundError(f"Audio file '{base_name}' with extensions {extensions} not found.") | |
def play_mp3(self, base_name, loop=False, volume=1.0): | |
filename = self.find_audio_file(base_name, ["mp3"]) | |
try: | |
sound = mixer.Sound(filename) | |
except Exception as e: | |
print(f"[AudioEngine] Error loading MP3 as sound: {e}") | |
return None | |
sound.set_volume(volume) | |
channel = sound.play(loops=-1 if loop else 0) | |
print(f"[AudioEngine] Playing MP3 as Sound: {filename} {'(looped)' if loop else ''}") | |
return channel | |
def play_wav(self, base_name, loop=False, volume=1.0): | |
filename = self.find_audio_file(base_name, ["wav"]) | |
sound = mixer.Sound(filename) | |
sound.set_volume(volume) | |
channel = sound.play(loops=-1 if loop else 0) | |
print(f"[AudioEngine] Playing WAV: {filename} {'(looped)' if loop else ''}") | |
return channel | |
def stop(self, channel): | |
if channel and isinstance(channel, mixer.Channel): | |
channel.stop() | |
print(f"[AudioEngine] Stopped channel {channel}") | |
def stop_all(self): | |
mixer.stop() | |
print("[AudioEngine] All playback stopped.") | |
def shutdown(self): | |
mixer.quit() | |
print("[AudioEngine] Audio engine shut down.") | |
# === Example Usage === | |
if __name__ == "__main__": | |
AE = AudioEngine() # Default quality 'high' used here | |
try: | |
music1 = AE.play_mp3("background_music", loop=True, volume=0.6) | |
music2 = AE.play_mp3("ambience", loop=True, volume=0.4) | |
click = AE.play_wav("click", volume=1.0) | |
explosion = AE.play_wav("explosion", volume=1.0) | |
time.sleep(3) | |
AE.stop(click) | |
print("[Main] Click sound stopped.") | |
time.sleep(5) | |
AE.stop(music2) | |
print("[Main] Ambience stopped.") | |
time.sleep(5) | |
except KeyboardInterrupt: | |
print("\n[Main] Interrupted by user.") | |
finally: | |
AE.shutdown() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment