Created
June 27, 2025 06:02
-
-
Save EncodeTheCode/9f6704231f74bda6296eccaccd550795 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 | |
# === Audio Quality Presets === | |
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 | |
} | |
} | |
AUDIO_QUALITY_LEVELS = list(AUDIO_QUALITIES.keys()) | |
def init_audio_engine(quality_level): | |
if quality_level not in AUDIO_QUALITIES: | |
raise ValueError(f"Invalid quality level: {quality_level}") | |
settings = AUDIO_QUALITIES[quality_level] | |
mixer.init( | |
frequency=settings["frequency"], | |
size=settings["size"], | |
channels=settings["channels"], | |
buffer=settings["buffer"] | |
) | |
print(f"[Audio] Initialized with '{quality_level}' quality.") | |
def find_audio_file(base_name, extension): | |
filename = f"{base_name}.{extension}" | |
if os.path.exists(filename): | |
return filename | |
else: | |
raise FileNotFoundError(f"Audio file '{filename}' not found.") | |
# === MP3 Playback (Streaming) with Cleanup === | |
def play_mp3(base_name, loop=False): | |
try: | |
filename = find_audio_file(base_name, "mp3") | |
except FileNotFoundError as e: | |
print(e) | |
return | |
music = mixer.Music(filename) | |
music.play(loops=-1 if loop else 0) | |
print(f"[Audio] Playing MP3: {filename} {'(looped)' if loop else ''}") | |
# Wait until finished (if not looping), then clean | |
if not loop: | |
while music.get_busy(): | |
time.sleep(0.1) | |
music.stop() | |
del music # Free memory | |
print("[Audio] MP3 playback complete and memory cleaned.") | |
# === WAV Playback (Sound Effect) with Cleanup === | |
def play_wav(base_name, loop=False): | |
try: | |
filename = find_audio_file(base_name, "wav") | |
except FileNotFoundError as e: | |
print(e) | |
return | |
sound = mixer.Sound(filename) | |
channel = sound.play(loops=-1 if loop else 0) | |
print(f"[Audio] Playing WAV: {filename} {'(looped)' if loop else ''}") | |
if not loop: | |
# Wait for sound to finish | |
while channel.get_busy(): | |
time.sleep(0.1) | |
del sound # Free memory | |
print("[Audio] WAV playback complete and memory cleaned.") | |
# === Main Usage Example === | |
if __name__ == "__main__": | |
quality = "high" # "low", "medium", "high", "ultra" | |
init_audio_engine(quality) | |
try: | |
# Play a sound, automatically clean after playback | |
play_mp3("background_music", loop=False) # background_music.mp3 | |
play_wav("click", loop=False) # click.wav | |
except KeyboardInterrupt: | |
print("\n[Audio] Playback interrupted.") | |
finally: | |
mixer.quit() | |
print("[Audio] Mixer shutdown complete.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment