Skip to content

Instantly share code, notes, and snippets.

@cvzi
Created June 10, 2020 09:45
Show Gist options
  • Save cvzi/8ad0a0a34f93fe82426a240a992944f0 to your computer and use it in GitHub Desktop.
Save cvzi/8ad0a0a34f93fe82426a240a992944f0 to your computer and use it in GitHub Desktop.
Set the volume level of all channels to the same volume.
"""
Sets the volume level of all channels to the same volume.
It chooses the minimum of all volume levels.
Requirements
- Windows 10
- pip install pycaw
"""
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
# Calculate minimum
minVol = 1.0
for i in range(volume.GetChannelCount()):
minVol = min(minVol, volume.GetChannelVolumeLevel(i))
# Set all channels to the minimum
for i in range(volume.GetChannelCount()):
volume.SetChannelVolumeLevel(i, minVol, None)
# Print new volume percentage
scalar = int(volume.GetChannelVolumeLevelScalar(i) * 100)
print(f"{'🔈🔉🔊'[len(str(scalar)) - 1]} {scalar}% [{volume.GetChannelCount()} channels]")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment