Skip to content

utils

Utility functions for the matching submodule.

remap

remap(
    x: float,
    o_min: float,
    o_max: float,
    n_min: float,
    n_max: float,
) -> float

Linearly map one range to another. For example, if the original range is 0 to 10 and the new range is 0 to 5, and x value of 5 will result in an output of 2.5.

This function can handle negative values and inverted ranges. If the input is -10 to 0 and the new range is 5 to 10, the output will still be valid.

PARAMETER DESCRIPTION

x

The value inside the old range to be remapped.

TYPE: float

o_min

Minimum value of the old range.

TYPE: float

o_max

Maximum value of the old range.

TYPE: float

n_min

Minimum value of the new range.

TYPE: float

n_max

Maximum value of the new range.

TYPE: float

RETURNS DESCRIPTION
result

x modified to be fit inside the new range.

TYPE: float

Source code in src/downmixer/matching/utils.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def remap(x: float, o_min: float, o_max: float, n_min: float, n_max: float) -> float:
    """Linearly map one range to another. For example, if the original range is 0 to 10 and the new range is 0 to 5,
    and `x` value of 5 will result in an output of 2.5.

    This function can handle negative values and inverted ranges. If the input is -10 to 0 and the new range is 5 to
    10, the output will still be valid.

    Args:
        x (float): The value inside the old range to be remapped.
        o_min (float): Minimum value of the old range.
        o_max (float): Maximum value of the old range.
        n_min (float): Minimum value of the new range.
        n_max (float): Maximum value of the new range.

    Returns:
        result (float): `x` modified to be fit inside the new range.
    """
    # check reversed input range
    reverse_input = False
    old_min = min(o_min, o_max)
    old_max = max(o_min, o_max)
    if not old_min == o_min:
        reverse_input = True

    # check reversed output range
    reverse_output = False
    new_min = min(n_min, n_max)
    new_max = max(n_min, n_max)
    if not new_min == n_min:
        reverse_output = True

    portion = (x - old_min) * (new_max - new_min) / (old_max - old_min)
    if reverse_input:
        portion = (old_max - x) * (new_max - new_min) / (old_max - old_min)

    result = portion + new_min
    if reverse_output:
        result = new_max - portion

    return result

ease

ease(x: float, falloff: float = 4.8) -> float

Returns \(y\) according to the equation \(y=1-(f*x^2)\), where \(f\) is an arbitrary "falloff" value.

PARAMETER DESCRIPTION

x

The value to be placed in the curve.

TYPE: float

falloff

An arbitrary value that determines how sharply the value for \(y\) decreases as \(x\) increases. Default is 4.8, picked simply because it gave fairly good results.

TYPE: float DEFAULT: 4.8

RETURNS DESCRIPTION
y

The value placed in the curve.

TYPE: float

Source code in src/downmixer/matching/utils.py
46
47
48
49
50
51
52
53
54
55
56
57
def ease(x: float, falloff: float = 4.8) -> float:
    """Returns $y$ according to the equation $y=1-(f*x^2)$, where $f$ is an arbitrary "falloff" value.

    Args:
        x (float): The value to be placed in the curve.
        falloff (float): An arbitrary value that determines how sharply the value for $y$ decreases as $x$ increases.
            Default is 4.8, picked simply because it gave fairly good results.

    Returns:
        y (float): The value placed in the curve.
    """
    return 1 - (falloff * x * x)