79 lines
2.6 KiB
Python
79 lines
2.6 KiB
Python
from subprocess import CompletedProcess
|
|
import sounddevice as sd
|
|
|
|
from .core.Expando import Expando
|
|
from .core.utils import run_ps_command, run_ps_command_live, ProcessResult
|
|
|
|
|
|
def _is_audio_device_cmdlets_installed():
|
|
# PowerShell command to check if module is available
|
|
ps_command = "Get-Module -ListAvailable -Name AudioDeviceCmdlets"
|
|
|
|
result = run_ps_command(ps_command)
|
|
|
|
# If something is returned in stdout, the module is installed
|
|
return bool(result.stdout.strip())
|
|
|
|
|
|
def install_audio_cmdlets():
|
|
"""Install AudioDevice cmdlet for better audio device management"""
|
|
if _is_audio_device_cmdlets_installed():
|
|
return CompletedProcess(
|
|
args=["is_audio_device_cmdlets_installed()"],
|
|
returncode=0,
|
|
stdout="AudioDevice cmdlet is already installed",
|
|
stderr="")
|
|
|
|
ps_script = r"""
|
|
# 1) Trust PSGallery
|
|
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted -ErrorAction SilentlyContinue
|
|
|
|
# 2) Ensure NuGet provider is available
|
|
if (-not (Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue)) {
|
|
Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
|
|
}
|
|
|
|
# 3) Install AudioDeviceCmdlets if missing
|
|
if (-not (Get-Module -ListAvailable -Name AudioDeviceCmdlets)) {
|
|
Install-Module -Name AudioDeviceCmdlets -Scope CurrentUser -Force -AllowClobber
|
|
}
|
|
|
|
# 4) Import the module
|
|
Import-Module AudioDeviceCmdlets -ErrorAction SilentlyContinue
|
|
"""
|
|
return run_ps_command_live(ps_script)
|
|
|
|
|
|
def get_audio_devices_windows():
|
|
"""Get all audio devices using AudioDevice cmdlet"""
|
|
ps_script = "Get-AudioDevice -List | ConvertTo-Json"
|
|
return run_ps_command(ps_script)
|
|
|
|
|
|
def get_audio_devices_windows_from_pnp_devices():
|
|
ps_script = "Get-PnpDevice -Class AudioEndpoint | Select-Object FriendlyName, Status, InstanceId | ConvertTo-Json"
|
|
return run_ps_command(ps_script)
|
|
|
|
|
|
def get_audio_devices_linux():
|
|
"""Get all audio devices using sounddevice module"""
|
|
try:
|
|
devices = sd.query_devices()
|
|
result = []
|
|
|
|
for i, device in enumerate(devices):
|
|
hostapi_name = sd.query_hostapis(device['hostapi'])['name']
|
|
|
|
result.append(Expando({
|
|
"device_id": i,
|
|
"name": device['name'],
|
|
"max_input_channels": device['max_input_channels'],
|
|
"max_output_channels": device['max_output_channels'],
|
|
"default_samplerate": device['default_samplerate'],
|
|
"hostapi_name": hostapi_name}
|
|
))
|
|
|
|
return ProcessResult(result)
|
|
except Exception as e:
|
|
ProcessResult(None, f"Error querying Linux audio devices: {e}")
|