mirror of
https://github.com/Yonokid/PyTaiko.git
synced 2026-02-04 19:50:12 +01:00
Update audio.py
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import cffi
|
import cffi
|
||||||
|
import ctypes
|
||||||
import platform
|
import platform
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -110,6 +111,20 @@ except OSError as e:
|
|||||||
print(f"Failed to load shared library: {e}")
|
print(f"Failed to load shared library: {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
def get_short_path_name(long_path: str) -> str:
|
||||||
|
"""Convert long path to Windows short path (8.3 format)"""
|
||||||
|
if platform.system() != 'Windows':
|
||||||
|
return long_path
|
||||||
|
|
||||||
|
# Get short path name
|
||||||
|
buffer = ctypes.create_unicode_buffer(512)
|
||||||
|
get_short_path = ctypes.windll.kernel32.GetShortPathNameW
|
||||||
|
ret = get_short_path(long_path, buffer, 512)
|
||||||
|
|
||||||
|
if ret:
|
||||||
|
return buffer.value
|
||||||
|
return long_path
|
||||||
|
|
||||||
class AudioEngine:
|
class AudioEngine:
|
||||||
"""Initialize an audio engine for playing sounds and music."""
|
"""Initialize an audio engine for playing sounds and music."""
|
||||||
def __init__(self, device_type: int, sample_rate: float, buffer_size: int, volume_presets: dict[str, float]):
|
def __init__(self, device_type: int, sample_rate: float, buffer_size: int, volume_presets: dict[str, float]):
|
||||||
@@ -180,7 +195,8 @@ class AudioEngine:
|
|||||||
"""Load a sound file and return sound ID"""
|
"""Load a sound file and return sound ID"""
|
||||||
try:
|
try:
|
||||||
if platform.system() == 'Windows':
|
if platform.system() == 'Windows':
|
||||||
file_path_str = str(file_path).encode('utf-16')
|
# Use Windows ANSI codepage (e.g., cp932 for Japanese, cp1252 for Western)
|
||||||
|
file_path_str = str(file_path).encode('cp932', errors='replace')
|
||||||
else:
|
else:
|
||||||
file_path_str = str(file_path).encode('utf-8')
|
file_path_str = str(file_path).encode('utf-8')
|
||||||
sound = lib.load_sound(file_path_str) # type: ignore
|
sound = lib.load_sound(file_path_str) # type: ignore
|
||||||
@@ -297,7 +313,8 @@ class AudioEngine:
|
|||||||
def load_music_stream(self, file_path: Path, name: str) -> str:
|
def load_music_stream(self, file_path: Path, name: str) -> str:
|
||||||
"""Load a music stream and return music ID"""
|
"""Load a music stream and return music ID"""
|
||||||
if platform.system() == 'Windows':
|
if platform.system() == 'Windows':
|
||||||
file_path_str = str(file_path).encode('utf-16')
|
# Use Windows ANSI codepage (e.g., cp932 for Japanese, cp1252 for Western)
|
||||||
|
file_path_str = str(file_path).encode('cp932', errors='replace')
|
||||||
else:
|
else:
|
||||||
file_path_str = str(file_path).encode('utf-8')
|
file_path_str = str(file_path).encode('utf-8')
|
||||||
music = lib.load_music_stream(file_path_str) # type: ignore
|
music = lib.load_music_stream(file_path_str) # type: ignore
|
||||||
|
|||||||
Reference in New Issue
Block a user