Update audio.py

Add docstrings
This commit is contained in:
Anthony Samms
2025-10-17 09:38:22 -04:00
committed by GitHub
parent 8a0f8f83de
commit 80ebe6a90a

View File

@@ -4,10 +4,8 @@ from pathlib import Path
from libs.utils import get_config from libs.utils import get_config
# Initialize CFFI
ffi = cffi.FFI() ffi = cffi.FFI()
# Define the C interface
ffi.cdef(""" ffi.cdef("""
typedef int PaHostApiIndex; typedef int PaHostApiIndex;
// Forward declarations // Forward declarations
@@ -101,8 +99,6 @@ ffi.cdef("""
void free(void *ptr); void free(void *ptr);
""") """)
# Load the compiled C library
# gcc -shared -fPIC -o libaudio.so audio.c -lportaudio -lsndfile -lpthread
try: try:
if platform.system() == "Windows": if platform.system() == "Windows":
lib = ffi.dlopen("libaudio.dll") lib = ffi.dlopen("libaudio.dll")
@@ -112,17 +108,6 @@ try:
lib = ffi.dlopen("./libaudio.so") lib = ffi.dlopen("./libaudio.so")
except OSError as e: except OSError as e:
print(f"Failed to load shared library: {e}") print(f"Failed to load shared library: {e}")
print("Make sure to compile your C code first.")
if platform.system() == "Linux":
print("Example:")
print("gcc -shared -fPIC -o libaudio.so audio.c -lportaudio -lsndfile -lpthread")
elif platform.system() == "Windows":
print("On Windows, make sure you've built a DLL with MinGW or MSVC:")
print("Example with MinGW:")
print("gcc -shared -o libaudio.dll audio.c -lportaudio -lsndfile -lpthread")
elif platform.system() == "Darwin":
print("On macOS:")
print("gcc -dynamiclib -o libaudio.dylib audio.c -lportaudio -lsndfile -lpthread")
raise raise
class AudioEngine: class AudioEngine:
@@ -140,6 +125,7 @@ class AudioEngine:
self.sounds_path = Path("Sounds") self.sounds_path = Path("Sounds")
def list_host_apis(self): def list_host_apis(self):
"""Prints a list of available host APIs to the console"""
lib.list_host_apis() # type: ignore lib.list_host_apis() # type: ignore
def init_audio_device(self) -> bool: def init_audio_device(self) -> bool:
@@ -268,6 +254,7 @@ class AudioEngine:
print(f"Sound {name} not found") print(f"Sound {name} not found")
def is_sound_playing(self, name: str) -> bool: def is_sound_playing(self, name: str) -> bool:
"""Check if a sound is playing"""
if name == 'don': if name == 'don':
return lib.is_sound_playing(self.don) # type: ignore return lib.is_sound_playing(self.don) # type: ignore
elif name == 'kat': elif name == 'kat':
@@ -280,6 +267,7 @@ class AudioEngine:
return False return False
def set_sound_volume(self, name: str, volume: float) -> None: def set_sound_volume(self, name: str, volume: float) -> None:
"""Set the volume of a specific sound"""
if name == 'don': if name == 'don':
lib.set_sound_volume(self.don, volume) # type: ignore lib.set_sound_volume(self.don, volume) # type: ignore
elif name == 'kat': elif name == 'kat':