Skip to content

Commit 54fda80

Browse files
Improve variable names, documentation
Add more values to models.py to track the min and max overflow
1 parent 418485f commit 54fda80

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

amplipi/ctrl.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -847,18 +847,17 @@ def set_mute():
847847

848848
def set_vol():
849849
""" Update the zone's volume. Could be triggered by a change in
850-
vol, vol_f, vol_min, or vol_max.
850+
vol, vol_f, vol_f_delta, vol_min, or vol_max.
851851
"""
852852
# Field precedence: vol (db) > vol_delta > vol (float)
853-
# NOTE: checks happen in reverse precedence to cover default case of unchanged volume
853+
# vol (db) is last in the stack to cover the default case of a None volume change, but when it does have a value it takes overrides the others
854854
if update.vol_delta_f is not None and update.vol is None:
855855
true_vol_f = zone.vol_f + zone.vol_f_overflow
856-
totaled_delta = update.vol_delta_f + true_vol_f
857-
applied_delta = utils.clamp(totaled_delta, 0, 1)
856+
expected_vol_total = update.vol_delta_f + true_vol_f
857+
vol_f_new = utils.clamp(expected_vol_total, models.MIN_VOL_F, models.MAX_VOL_F)
858858

859-
vol_db = utils.vol_float_to_db(applied_delta, zone.vol_min, zone.vol_max)
860-
vol_f_new = applied_delta
861-
zone.vol_f_overflow = 0 if models.MIN_VOL_F < totaled_delta and totaled_delta < models.MAX_VOL_F else utils.clamp((totaled_delta - applied_delta), -1.0, 1.0)
859+
vol_db = utils.vol_float_to_db(vol_f_new, zone.vol_min, zone.vol_max)
860+
zone.vol_f_overflow = 0 if models.MIN_VOL_F < expected_vol_total and expected_vol_total < models.MAX_VOL_F else utils.clamp((expected_vol_total - vol_f_new), models.MIN_VOL_F_OVERFLOW, models.MAX_VOL_F_OVERFLOW) # Clamp the remaining delta to be between -1 and 1
862861

863862
elif update.vol_f is not None and update.vol is None:
864863
clamp_vol_f = utils.clamp(vol_f, 0, 1)
@@ -875,6 +874,8 @@ def set_vol():
875874
else:
876875
raise Exception('unable to update zone volume')
877876

877+
# If the change made vol f be between the min and max, delete the overflow
878+
# This is useful so that you can click wherever you want on the volume bar and expect it to end up there without rubberbanding back to whatever vol_f + vol_f_overflow value you'd otherwise be at
878879
zone.vol_f_overflow = 0 if vol_f_new != models.MIN_VOL_F or vol_f_new != models.MAX_VOL_F else zone.vol_f_overflow
879880

880881
# To avoid potential unwanted loud output:

amplipi/models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
MAX_VOL_F = 1.0
3939
""" Max volume for slider bar. Will be mapped to dB. """
4040

41+
MIN_VOL_F_OVERFLOW = MIN_VOL_F - MAX_VOL_F
42+
"""Min overflow for volume sliders, set to be the full range of vol_f below zero"""
43+
44+
MAX_VOL_F_OVERFLOW = MAX_VOL_F - MIN_VOL_F
45+
"""Max overflow for volume sliders, set to be the full range of vol_f above zero"""
46+
4147
MIN_VOL_DB = -80
4248
""" Min volume in dB. -80 is special and is actually -90 dB (mute). """
4349

@@ -111,7 +117,7 @@ class fields_w_default(SimpleNamespace):
111117
Volume = Field(default=MIN_VOL_DB, ge=MIN_VOL_DB, le=MAX_VOL_DB, description='Output volume in dB')
112118
VolumeF = Field(default=MIN_VOL_F, ge=MIN_VOL_F, le=MAX_VOL_F,
113119
description='Output volume as a floating-point scalar from 0.0 to 1.0 representing MIN_VOL_DB to MAX_VOL_DB')
114-
VolumeFOverflow = Field(default=0.0, ge=(MIN_VOL_F - MAX_VOL_F), le=(MAX_VOL_F - MIN_VOL_F),
120+
VolumeFOverflow = Field(default=0.0, ge=MIN_VOL_F_OVERFLOW, le=MAX_VOL_F_OVERFLOW,
115121
description='Output volume as a floating-point scalar that has a range equal to MIN_VOL_F - MAX_VOL_F in both directions from zero, and is used to keep track of the relative distance between two or more zone volumes when they would otherwise have to exceed their VOL_F range')
116122
VolumeMin = Field(default=MIN_VOL_DB, ge=MIN_VOL_DB, le=MAX_VOL_DB,
117123
description='Min output volume in dB')

0 commit comments

Comments
 (0)