set default device type per platform

This commit is contained in:
Anthony Samms
2025-11-12 00:33:23 -05:00
parent b33f43fa0f
commit 6f9d353d14
4 changed files with 32 additions and 12 deletions

View File

@@ -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

View File

@@ -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')

View File

@@ -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

View File

@@ -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"""