-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting any device volume , a small discrepancy in volume.SetMasterVolumeLevelScalar() and the significance of volume.GetVolumeRange() #51
Comments
I was also having trouble with the # python -m pip install pycaw pytest
import pytest
from pytest import approx
def range_convertion(OldValue, OldMin, OldMax, NewMin, NewMax):
# https://stackoverflow.com/questions/929103/convert-a-number-range-to-another-range-maintaining-ratio
OldRange = OldMax - OldMin
if OldRange == 0:
NewValue = NewMin
else:
NewRange = (NewMax - NewMin)
NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin
return NewValue
def get_system_volume_percent(decibel):
minimum, maximum, step = volume.GetVolumeRange()
percent = range_convertion(decibel, minimum, maximum, 0, 100)
return percent
def get_system_volume_decibel(percent):
minimum, maximum, step = volume.GetVolumeRange()
decibel = range_convertion(percent, 0, 100, minimum, maximum)
return decibel
def test_percent_decibel():
minimum, maximum, step = volume.GetVolumeRange()
assert get_system_volume_percent(minimum) == 0
assert get_system_volume_percent((maximum + minimum) / 2) == 50
assert get_system_volume_percent(0) == 100
assert get_system_volume_decibel(get_system_volume_percent(10)) == approx(get_system_volume_percent(get_system_volume_decibel(10)))
assert get_system_volume_decibel(get_system_volume_percent(23)) == approx(get_system_volume_percent(get_system_volume_decibel(23)))
assert get_system_volume_decibel(0) == minimum
assert get_system_volume_decibel(50) == (maximum + minimum) / 2
assert get_system_volume_decibel(100) == maximum
pytest.main([sys.argv[0], '-vvv',])
jumpSize = -5
# defaultSystemVolumeDecibel = volume.GetMasterVolumeLevelScalar()
defaultSystemVolumeDecibel = volume.GetMasterVolumeLevel()
defaultSystemVolumePercent = get_system_volume_percent(defaultSystemVolumeDecibel)
newSystemVolumePercent = min(defaultSystemVolumePercent + jumpSize, 100)
newSystemVolumeDecibel = get_system_volume_decibel(newSystemVolumePercent)
print(f'jumpSize {jumpSize}.')
print(f'defaultSystemVolumeDecibel {defaultSystemVolumeDecibel:.2f}, defaultSystemVolumePercent {defaultSystemVolumePercent:.2f}.')
print(f'newSystemVolumeDecibel {newSystemVolumeDecibel:.2f}, newSystemVolumePercent {newSystemVolumePercent:.2f}.')
# volume.SetMasterVolumeLevel(newSystemVolumeDecibel, None) DB values are not linear, they are logarithmic, meaning they do not increase the sound proportionally as the percentages got from |
I adapted a program by akaufman1 at #8 to read and set any audio device volume. (Well done akaufman1)
This allows me to set microphone and other volumes on my PC (Windows 11, Python 3.9.1)
I have noticed slight discrepancies in setting the volume of the speakers to say, 22% using volume.SetMasterVolumeLevelScalar(0.22, None).
volume.GetMasterVolumeLevelScalar() Reports 22 after setting. However windows control bar shows 21.
Thanks to Joseph Argumido for pointing me in the right direction for SetMasterVolumeLevelScalar.
To date, I have been unable to fully understand the significance of the 3 values returned by volume.GetVolumeRange()
Any thoughts? If some or all of the values are in decibels, how can they be converted to more meaningful values?
Is there a GetVolumeRangeLevel equivalent?
The text was updated successfully, but these errors were encountered: