Last active
September 8, 2024 22:10
-
-
Save logixism/613337a02f6638d31a42053fdb42fd35 to your computer and use it in GitHub Desktop.
Audio control using python + AudioDeviceCmdlets.dll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from subprocess import run | |
| from re import split | |
| class PyAudioDevice: | |
| @staticmethod | |
| def _exec_powershell(cmd): | |
| result = run(["powershell", "-Command", cmd], capture_output=True, text=True) | |
| if result.stderr != "": | |
| raise ValueError(result.stderr) | |
| else: | |
| return result.stdout | |
| @staticmethod | |
| def _convert_value(value): | |
| value = value.strip() | |
| if value.isdigit(): | |
| return int(value) | |
| elif value.lower() == "true": | |
| return True | |
| elif value.lower() == "false": | |
| return False | |
| elif value.find("%") > 0: | |
| return float(value.replace("%", "")) | |
| else: | |
| return value | |
| @classmethod | |
| def _format_data(cls, data): | |
| entries = split(r"\n\n+", data.strip()) | |
| entry_dict = {} | |
| for entry in entries: | |
| lines = entry.split("\n") | |
| for line in lines: | |
| key, value = split(r"\s*:\s*", line) | |
| entry_dict[key.strip()] = cls._convert_value(value) | |
| return entry_dict | |
| """ | |
| Get the default playback device as <AudioDevice> | |
| """ | |
| @classmethod | |
| def get_default_device(cls): | |
| powershell_command = "Get-AudioDevice -Playback" | |
| data = cls._exec_powershell(f"{cls._import} {powershell_command}") | |
| return cls._format_data(data) | |
| """ | |
| Get the default playback device's mute state as <bool> | |
| """ | |
| @classmethod | |
| def get_is_mute(cls): | |
| powershell_command = "Get-AudioDevice -PlaybackMute" | |
| data = cls._exec_powershell(f"{cls._import} {powershell_command}") | |
| return cls._convert_value(data) | |
| """ | |
| Get the default playback device's volume level on 100 as <float> | |
| """ | |
| @classmethod | |
| def get_volume(cls): | |
| powershell_command = "Get-AudioDevice -PlaybackVolume" | |
| data = cls._exec_powershell(f"{cls._import} {powershell_command}") | |
| return cls._convert_value(data) | |
| """ | |
| Set the default playback device's mute state to the opposite of its current mute state | |
| """ | |
| @classmethod | |
| def toggle_mute(cls): | |
| powershell_command = "Set-AudioDevice -PlaybackMuteToggle" | |
| cls._exec_powershell(f"{cls._import} {powershell_command}") | |
| """ | |
| Set the default playback device's mute state to the given <bool> | |
| """ | |
| @classmethod | |
| def set_mute(cls, mute: bool): | |
| powershell_command = f"""Set-AudioDevice -PlaybackMute ${str(mute).lower()}""" | |
| cls._exec_powershell(f"{cls._import} {powershell_command}") | |
| """ | |
| Set the default playback device's volume level on 100 to the given <float> | |
| """ | |
| @classmethod | |
| def set_volume(cls, volume: float): | |
| powershell_command = f"Set-AudioDevice -PlaybackVolume {volume}" | |
| cls._exec_powershell(f"{cls._import} {powershell_command}") | |
| """ | |
| Get the active playback device as <AudioDevice> | |
| """ | |
| @classmethod | |
| def get_active_device(cls): | |
| powershell_command = "Get-AudioDevice -PlaybackCommunication" | |
| data = cls._exec_powershell(powershell_command) | |
| return cls._format_data(data) | |
| """ | |
| Get the device with the ID corresponding to the given <string> | |
| """ | |
| @classmethod | |
| def get_audio_device_by_id(cls, id): | |
| powershell_command = f"Get-AudioDevice -ID {id}" | |
| data = cls._exec_powershell(powershell_command) | |
| return cls._format_data(data) | |
| """ | |
| Get a list of all enabled devices as <AudioDevice> | |
| """ | |
| @classmethod | |
| def get_audio_device_list(cls): | |
| powershell_command = "Get-AudioDevice -List" | |
| data = cls._exec_powershell(powershell_command) | |
| entries = split(r"\n\n+", data.strip()) | |
| result_dict = {} | |
| for entry in entries: | |
| entry_dict = {} | |
| lines = entry.split("\n") | |
| for line in lines: | |
| key, value = split(r"\s*:\s*", line) | |
| entry_dict[key.strip()] = cls._convert_value(value) | |
| name = f"{entry_dict.get('Name')}:{entry_dict.get('Type')}" | |
| if name: | |
| result_dict[name] = entry_dict | |
| return result_dict | |
| """ | |
| Set the device with the ID corresponding to the given <string> as the default device and not the default communication device, for its type | |
| """ | |
| @classmethod | |
| def set_active_device_by_id(cls, device_id: str): | |
| powershell_command = f"""Set-AudioDevice -ID "{device_id}" -DefaultOnly""" | |
| cls._exec_powershell(powershell_command) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SEHR GUTT!!!