Skip to content

Commit

Permalink
refactor: add wrapper for interpolation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SoulMelody committed Aug 6, 2024
1 parent 8fa7545 commit 28cd99a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 46 deletions.
2 changes: 1 addition & 1 deletion libresvip/model/pitch_slide.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ def cubic_slide(cls) -> PitchSlide:

@classmethod
def sigmoid_slide(cls) -> PitchSlide:
return cls(0.075, 0.48, partial(sigmoid_interpolation, k=5.5))
return cls(0.075, 0.48, partial(sigmoid_interpolation, k=5.5)) # type: ignore[call-arg]
6 changes: 3 additions & 3 deletions libresvip/plugins/acep/base_pitch_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ def __post_init__(
if note.vibrato.release_ratio:
self.vibrato_coef_interval_dict[portion.openclosed(release_time, note_end)] = (
functools.partial(
linear_interpolation,
linear_interpolation, # type: ignore[call-arg]
start=(release_time, note.vibrato.release_level),
end=(note_end, 0),
)
)
self.vibrato_coef_interval_dict[portion.closed(attack_time, release_time)] = (
functools.partial(
linear_interpolation,
linear_interpolation, # type: ignore[call-arg]
start=(attack_time, note.vibrato.attack_level),
end=(release_time, note.vibrato.release_level),
)
Expand All @@ -111,7 +111,7 @@ def __post_init__(
self.vibrato_coef_interval_dict[
portion.closedopen(vibrato_start, attack_time)
] = functools.partial(
linear_interpolation,
linear_interpolation, # type: ignore[call-arg]
start=(vibrato_start, 0),
end=(attack_time, note.vibrato.attack_level),
)
Expand Down
2 changes: 1 addition & 1 deletion libresvip/plugins/ppsf/ppsf_interval_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def ppsf_key_interval_dict(
next_note.pos + next_event.portamento_offset + next_event.portamento_length,
)
] = functools.partial(
cosine_easing_in_out_interpolation,
cosine_easing_in_out_interpolation, # type: ignore[call-arg]
start=Point(
x=next_note.pos + next_event.portamento_offset,
y=prev_note.note_number,
Expand Down
4 changes: 2 additions & 2 deletions libresvip/plugins/s5p/synthv_editor_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def parse_notes(
self.vibrato_coef_interval_dict[
portion.closedopen(vibrato_start, vibrato_attack_time)
] = functools.partial(
linear_interpolation,
linear_interpolation, # type: ignore[call-arg]
start=(vibrato_start, 0),
end=(vibrato_attack_time, vibrato_depth),
)
Expand All @@ -195,7 +195,7 @@ def parse_notes(
self.vibrato_coef_interval_dict[
portion.closedopen(vibrato_release_time, vibrato_end)
] = functools.partial(
linear_interpolation,
linear_interpolation, # type: ignore[call-arg]
start=(vibrato_release_time, vibrato_depth),
end=(vibrato_end, 0),
)
Expand Down
75 changes: 36 additions & 39 deletions libresvip/utils/music_math.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import functools
import math
import re
from typing import Optional
from collections.abc import Callable
from typing import Any, Optional

from libresvip.core.constants import KEY_IN_OCTAVE

Expand Down Expand Up @@ -76,54 +78,49 @@ def clamp(
return min(max(x, lower), upper)


def linear_interpolation(x: float, start: tuple[float, float], end: tuple[float, float]) -> float:
x0, y0 = start
x1, y1 = end
r = (x - x0) / (x1 - x0)
return y0 + (y1 - y0) * r
def _transform_interpolation_args(
func: Callable[[float], float],
) -> Callable[[float, tuple[float, float], tuple[float, float]], float]:
@functools.wraps(func)
def inner(
x: float, start: tuple[float, float], end: tuple[float, float], *args: Any, **kwargs: Any
) -> float:
x0, y0 = start
x1, y1 = end
r = (x - x0) / (x1 - x0)
return y0 + (y1 - y0) * func(r, *args, **kwargs)

return inner

def cosine_easing_in_interpolation(
x: float, start: tuple[float, float], end: tuple[float, float]
) -> float:
x0, y0 = start
x1, y1 = end
r = (x - x0) / (x1 - x0)
return y0 + (y1 - y0) * (1 - math.cos(r * math.pi / 2))

@_transform_interpolation_args
def linear_interpolation(r: float) -> float:
return r

def cosine_easing_out_interpolation(
x: float, start: tuple[float, float], end: tuple[float, float]
) -> float:
x0, y0 = start
x1, y1 = end
r = (x - x0) / (x1 - x0)
return y0 + (y1 - y0) * math.cos(r * math.pi / 2)

@_transform_interpolation_args
def cosine_easing_in_interpolation(r: float) -> float:
return 1 - math.cos(r * math.pi / 2)

def cosine_easing_in_out_interpolation(
x: float, start: tuple[float, float], end: tuple[float, float]
) -> float:
x0, y0 = start
x1, y1 = end
r = (x - x0) / (x1 - x0)
return y0 + (y1 - y0) * (1 - math.cos(r * math.pi)) / 2

@_transform_interpolation_args
def cosine_easing_out_interpolation(r: float) -> float:
return math.cos(r * math.pi / 2)

def cubic_interpolation(x: float, start: tuple[float, float], end: tuple[float, float]) -> float:
x0, y0 = start
x1, y1 = end
r = (x - x0) / (x1 - x0)
return y0 + (y1 - y0) * ((3 - 2 * r) * r**2)

@_transform_interpolation_args
def cosine_easing_in_out_interpolation(r: float) -> float:
return (1 - math.cos(r * math.pi)) / 2

def sigmoid_interpolation(
x: float, start: tuple[float, float], end: tuple[float, float], k: float
) -> float:
x0, y0 = start
x1, y1 = end
r = (x - x0) / (x1 - x0)
return y0 + (y1 - y0) / (1 + math.exp(k * (-2 * r + 1)))

@_transform_interpolation_args
def cubic_interpolation(r: float) -> float:
return (3 - 2 * r) * r**2


@_transform_interpolation_args # type: ignore[arg-type]
def sigmoid_interpolation(r: float, k: float) -> float:
return 1 / (1 + math.exp(k * (-2 * r + 1)))


def db_to_float(db: float, using_amplitude: bool = True) -> float:
Expand Down

0 comments on commit 28cd99a

Please sign in to comment.