Skip to content

Commit

Permalink
refactor: move interpolation functions to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
SoulMelody committed Sep 30, 2024
1 parent 51e3df3 commit 77f861d
Show file tree
Hide file tree
Showing 5 changed files with 520 additions and 504 deletions.
40 changes: 1 addition & 39 deletions libresvip/model/point.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
from __future__ import annotations

import abc
from functools import partial
from typing import TYPE_CHECKING, Any, Generic, NamedTuple, TypeVar, Union

from more_itertools import pairwise

from libresvip.utils.music_math import (
cosine_easing_in_interpolation,
cosine_easing_in_out_interpolation,
cosine_easing_out_interpolation,
linear_interpolation,
)

if TYPE_CHECKING:
from collections.abc import Callable, Iterable
from collections.abc import Iterable


PointType = TypeVar("PointType")
Expand All @@ -33,34 +23,6 @@ def end_point(cls, value: int = -100) -> Point:
return cls(1073741823, value)


def _inner_interpolate(
data: list[Point],
sampling_interval_tick: int,
mapping: Callable[[int, Point, Point], float],
) -> list[Point]:
return (
(
[data[0]]
+ [
Point(x=x, y=round(mapping(x, start, end)))
for start, end in pairwise(data)
for x in range(start.x + 1, end.x, sampling_interval_tick)
]
+ [data[-1]]
)
if data
else data
)


interpolate_linear = partial(_inner_interpolate, mapping=linear_interpolation)
interpolate_cosine_ease_in_out = partial(
_inner_interpolate, mapping=cosine_easing_in_out_interpolation
)
interpolate_cosine_ease_in = partial(_inner_interpolate, mapping=cosine_easing_in_interpolation)
interpolate_cosine_ease_out = partial(_inner_interpolate, mapping=cosine_easing_out_interpolation)


class PointList(abc.ABC, Generic[PointType]):
root: list[PointType]

Expand Down
4 changes: 2 additions & 2 deletions libresvip/plugins/dv/deepvocal_pitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

from libresvip.core.time_sync import TimeSynchronizer
from libresvip.model.base import Note, ParamCurve, SongTempo
from libresvip.model.point import (
Point,
from libresvip.model.point import Point
from libresvip.utils.music_math import (
interpolate_cosine_ease_in_out,
interpolate_linear,
)
Expand Down
13 changes: 6 additions & 7 deletions libresvip/plugins/ust/interpolation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from libresvip.model.point import (
Point,
from libresvip.model.point import Point
from libresvip.utils.music_math import (
interpolate_cosine_ease_in,
interpolate_cosine_ease_in_out,
interpolate_cosine_ease_out,
Expand All @@ -17,11 +17,10 @@ def interpolate(
) -> list[Point]:
input_data = [last_point, this_point]
if curve_type == "s":
output = interpolate_linear(input_data, sampling_interval_tick)
return interpolate_linear(input_data, sampling_interval_tick)
elif curve_type == "j":
output = interpolate_cosine_ease_in(input_data, sampling_interval_tick)
return interpolate_cosine_ease_in(input_data, sampling_interval_tick)
elif curve_type == "r":
output = interpolate_cosine_ease_out(input_data, sampling_interval_tick)
return interpolate_cosine_ease_out(input_data, sampling_interval_tick)
else:
output = interpolate_cosine_ease_in_out(input_data, sampling_interval_tick)
return output
return interpolate_cosine_ease_in_out(input_data, sampling_interval_tick)
35 changes: 35 additions & 0 deletions libresvip/utils/music_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from collections.abc import Callable
from typing import Any, Optional

from more_itertools import pairwise

from libresvip.core.constants import KEY_IN_OCTAVE
from libresvip.model.point import Point


def midi2note(midi: float) -> str:
Expand Down Expand Up @@ -128,6 +131,38 @@ def sigmoid_interpolation(r: float, k: float) -> float:
return 1 / (1 + math.exp(k * (-2 * r + 1)))


def _inner_interpolate(
data: list[Point],
sampling_interval_tick: int,
mapping: Callable[[int, Point, Point], float],
) -> list[Point]:
return (
(
[data[0]]
+ [
Point(x=x, y=round(mapping(x, start, end)))
for start, end in pairwise(data)
for x in range(start.x + 1, end.x, sampling_interval_tick)
]
+ [data[-1]]
)
if data
else data
)


interpolate_linear = functools.partial(_inner_interpolate, mapping=linear_interpolation)
interpolate_cosine_ease_in_out = functools.partial(
_inner_interpolate, mapping=cosine_easing_in_out_interpolation
)
interpolate_cosine_ease_in = functools.partial(
_inner_interpolate, mapping=cosine_easing_in_interpolation
)
interpolate_cosine_ease_out = functools.partial(
_inner_interpolate, mapping=cosine_easing_out_interpolation
)


def db_to_float(db: float, using_amplitude: bool = True) -> float:
"""
Converts the input db to a float, which represents the equivalent
Expand Down
Loading

0 comments on commit 77f861d

Please sign in to comment.