From 467b769ea44b8e090af0df21baad786507fc13e9 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Thu, 8 Feb 2024 13:43:49 +0000 Subject: [PATCH] from_db, to_db decorator functions added to util. _get_{fader,level}_val, _set_{fader,level}_val removed --- xair_api/util.py | 94 +++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 53 deletions(-) diff --git a/xair_api/util.py b/xair_api/util.py index cfc69a9..3946bad 100644 --- a/xair_api/util.py +++ b/xair_api/util.py @@ -1,3 +1,4 @@ +import functools from math import exp, log @@ -17,61 +18,48 @@ def log_set(min, max, val): return log(val / min) / log(max / min) -def _get_fader_val(retval): - if retval >= 1: - return 10 - elif retval >= 0.5: - return round((40 * retval) - 30, 1) - elif retval >= 0.25: - return round((80 * retval) - 50, 1) - elif retval >= 0.0625: - return round((160 * retval) - 70, 1) - elif retval >= 0: - return round((480 * retval) - 90, 1) - else: - return -90 +def from_db(func): + """fader|level converter for getters""" + + @functools.wraps(func) + def wrapper(*args, **kwargs): + retval = func(*args, **kwargs) + + if retval >= 1: + return 10 + elif retval >= 0.5: + return round((40 * retval) - 30, 1) + elif retval >= 0.25: + return round((80 * retval) - 50, 1) + elif retval >= 0.0625: + return round((160 * retval) - 70, 1) + elif retval >= 0: + return round((480 * retval) - 90, 1) + else: + return -90 + + return wrapper -def _set_fader_val(self, val): - if val >= 10: - self.setter("fader", 1) - elif val >= -10: - self.setter("fader", (val + 30) / 40) - elif val >= -30: - self.setter("fader", (val + 50) / 80) - elif val >= -60: - self.setter("fader", (val + 70) / 160) - elif val >= -90: - self.setter("fader", (val + 90) / 480) - else: - self.setter("fader", 0) +def to_db(func): + """fader|level converter for setters""" + @functools.wraps(func) + def wrapper(*args, **kwargs): + param, val = args + if val >= 10: + val = 1 + elif val >= -10: + val = (val + 30) / 40 + elif val >= -30: + val = (val + 50) / 80 + elif val >= -60: + val = (val + 70) / 160 + elif val >= -90: + val = (val + 90) / 480 + else: + val = 0 -def _get_level_val(retval): - if retval >= 1: - return 10 - elif retval >= 0.5: - return round((40 * retval) - 30, 1) - elif retval >= 0.25: - return round((80 * retval) - 50, 1) - elif retval >= 0.0625: - return round((160 * retval) - 70, 1) - elif retval >= 0: - return round((480 * retval) - 90, 1) - else: - return -90 + func(param, val, **kwargs) - -def _set_level_val(self, val): - if val >= 10: - self.setter("level", 1) - elif val >= -10: - self.setter("level", (val + 30) / 40) - elif val >= -30: - self.setter("level", (val + 50) / 80) - elif val >= -60: - self.setter("level", (val + 70) / 160) - elif val >= -90: - self.setter("level", (val + 90) / 480) - else: - self.setter("level", 0) + return wrapper