0

I have a certain program which changes the volume of a computer using the pulsectl module in python. The following is the code:

if(distance > self.threshold):
    self.pulse.volume_change_all_chans(self.sink, 0.02)
else:
    self.pulse.volume_change_all_chans(self.sink, -0.02)

Using this code leads to a minimum value of 0 and a maximum value can go up to infinity. What I would like to do is that the maximum value of volume should be set at one. It shouldn't increase even a bit beyond one.

For ex, if my current volume is 0.99 and the condition in if statement is True, then, my volume changes to 1.1. This shouldn't happen. It should increase maximum to 1.0. Never beyond it.

Also, the current value of value of volume can be obtained by self.pulse.volume_get_all_chans(self.sink)

How can i do this without writing too many if else conditions in python.

Shawn Brar
  • 1,346
  • 3
  • 17
  • if(volume>1) {volume=1;} – john-jones Jan 29 '23 at 11:27
  • Hint: can you think of a mathematical rule that tells you how much to increase the volume, if it's currently at 0.99? Then, you have two candidates for how much to increase the volume: the one calculated by that rule (even when inappropriate), and the constant `0.02`. Can you think of a mathematical rule that tells you which one to use (that works for all cases)? – Karl Knechtel Jan 29 '23 at 11:39
  • Another approach: can you think of a rule that tells you what the volume should be after the change, given two candidates (the original plus the increment, vs. the limit value)? – Karl Knechtel Jan 29 '23 at 11:40

1 Answers1

-1

How can i do this without writing too many if else conditions in python.

You might harness max and min for keeping value in certain range without any if at all, consider following simple example

RANGE_LOW = 0.0
RANGE_HIGH = 1.0
value = float(input("Provide some value: "))
value = min(value,RANGE_HIGH)
value = max(value,RANGE_LOW)
print("value was set to",value)
Daweo
  • 31,313
  • 3
  • 12
  • 25