From 6f9d353d14674944b3384fde70d0023170163ca9 Mon Sep 17 00:00:00 2001 From: Anthony Samms Date: Wed, 12 Nov 2025 00:33:23 -0500 Subject: [PATCH] set default device type per platform --- config.toml | 12 +++++------- libs/audio.py | 4 ++-- libs/global_data.py | 3 +-- libs/utils.py | 25 ++++++++++++++++++++++++- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/config.toml b/config.toml index a88efe3..478d36d 100644 --- a/config.toml +++ b/config.toml @@ -57,15 +57,13 @@ right_don = [17] right_kat = [12] [audio] -# device_type: 0 = default, check console output from list_host_apis() for other options -# Windows users should generally pick 3 (WASAPI) and Linux users should pick 0 (ALSA) -device_type = 0 -# sample_rate: -1 = use device default (usually 44100 or 48000) -sample_rate = -1 +# device_type: -1 = default, this will be set on first launch to the recommended setting +# Windows users should generally pick 4 (WDM-KS) and Linux users should pick 0 (ALSA) +device_type = -1 +sample_rate = 44100 # buffer_size: Size in samples per audio buffer # - 0 = let driver choose (may result in very small buffers with ASIO, typically 64) -buffer_size = 0 -exclusive = false +buffer_size = 32 [volume] sound = 1.0 diff --git a/libs/audio.py b/libs/audio.py index 32b02d4..466bc7a 100644 --- a/libs/audio.py +++ b/libs/audio.py @@ -198,7 +198,7 @@ class AudioEngine: """Load a sound file and return sound ID""" try: if platform.system() == 'Windows': - # Use Windows ANSI codepage (e.g., cp932 for Japanese, cp1252 for Western) + # Use Windows ANSI codepage (cp932 for Japanese) file_path_str = str(file_path).encode('cp932', errors='replace') else: file_path_str = str(file_path).encode('utf-8') @@ -319,7 +319,7 @@ class AudioEngine: def load_music_stream(self, file_path: Path, name: str) -> str: """Load a music stream and return music ID""" if platform.system() == 'Windows': - # Use Windows ANSI codepage (e.g., cp932 for Japanese, cp1252 for Western) + # Use Windows ANSI codepage (cp932 for Japanese) file_path_str = str(file_path).encode('cp932', errors='replace') else: file_path_str = str(file_path).encode('utf-8') diff --git a/libs/global_data.py b/libs/global_data.py index 33351b4..7fd089c 100644 --- a/libs/global_data.py +++ b/libs/global_data.py @@ -1,6 +1,6 @@ from dataclasses import dataclass, field from pathlib import Path -from typing import Any, Optional, TypedDict +from typing import Any, TypedDict class GeneralConfig(TypedDict): fps_counter: bool @@ -56,7 +56,6 @@ class AudioConfig(TypedDict): device_type: int sample_rate: int buffer_size: int - exclusive: bool class VolumeConfig(TypedDict): sound: float diff --git a/libs/utils.py b/libs/utils.py index 8d2f637..2c1fae6 100644 --- a/libs/utils.py +++ b/libs/utils.py @@ -5,6 +5,7 @@ import sys import logging import time import json +import cffi from libs.global_data import Config, global_data from functools import lru_cache from pathlib import Path @@ -82,7 +83,29 @@ def get_config() -> Config: with open(config_path, "r", encoding="utf-8") as f: config_file = tomlkit.load(f) - return json.loads(json.dumps(config_file)) + config: Config = json.loads(json.dumps(config_file)) + if config["audio"]["device_type"] == -1: + if sys.platform == "win32": + ffi = cffi.FFI() + ffi.cdef(""" + const char* get_host_api_name(PaHostApiIndex hostApi); + """) + lib = ffi.dlopen("libaudio.dll") + for i in range(5): + result = lib.get_host_api_name(i) # type: ignore + result = ffi.string(result) + if isinstance(result, bytes): + result = result.decode('utf-8') + if "WDM" in result: + config["audio"]["device_type"] = i + break + else: + config["audio"]["device_type"] = 0 + else: + config["audio"]["device_type"] = 0 + save_config(config) + + return config def save_config(config: Config) -> None: """Save the configuration to the TOML file"""