Skip to content

Commit

Permalink
refactor: rename slide to portamento
Browse files Browse the repository at this point in the history
  • Loading branch information
SoulMelody committed Aug 6, 2024
1 parent 28cd99a commit b3fd8d7
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 49 deletions.
26 changes: 13 additions & 13 deletions libresvip/model/pitch_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@

from libresvip.core.time_sync import TimeSynchronizer
from libresvip.model.base import Note
from libresvip.model.pitch_slide import PitchSlide
from libresvip.model.portamento import PortamentoPitch
from libresvip.utils.search import find_last_index


@dataclasses.dataclass
class PitchSimulator:
synchronizer: TimeSynchronizer
note_list: list[Note]
slide: PitchSlide
portamento: PortamentoPitch
pitch_tags: list[tuple[float, int]] = dataclasses.field(default_factory=list)

def __post_init__(self) -> None:
if len(self.note_list) == 0:
return
max_slide_time = self.slide.max_inter_time_in_secs
max_slide_percent = self.slide.max_inter_time_percent
max_portamento_time = self.portamento.max_inter_time_in_secs
max_portamento_percent = self.portamento.max_inter_time_percent

current_note = self.note_list[0]
current_head = self.synchronizer.get_actual_secs_from_ticks(current_note.start_pos)
current_dur = self.synchronizer.get_duration_secs_from_ticks(
current_note.start_pos, current_note.end_pos
)
current_slide = min(current_dur * max_slide_percent, max_slide_time)
current_portamento = min(current_dur * max_portamento_percent, max_portamento_time)

self.pitch_tags.append((current_head, current_note.key_number))
for i in range(len(self.note_list) - 1):
Expand All @@ -33,38 +33,38 @@ def __post_init__(self) -> None:
next_dur = self.synchronizer.get_duration_secs_from_ticks(
next_note.start_pos, next_note.end_pos
)
next_slide = min(next_dur * max_slide_percent, max_slide_time)
next_portamento = min(next_dur * max_portamento_percent, max_portamento_time)
interval = next_head - current_head - current_dur
if interval <= 2 * max_slide_time:
if interval <= 2 * max_portamento_time:
self.pitch_tags.append(
(
next_head - interval / 2 - current_slide,
next_head - interval / 2 - current_portamento,
current_note.key_number,
)
)
self.pitch_tags.append(
(
next_head - interval / 2 + next_slide,
next_head - interval / 2 + next_portamento,
next_note.key_number,
)
)
else:
self.pitch_tags.append(
(
next_head - interval / 2 - max_slide_time,
next_head - interval / 2 - max_portamento_time,
current_note.key_number,
)
)
self.pitch_tags.append(
(
next_head - interval / 2 + max_slide_time,
next_head - interval / 2 + max_portamento_time,
next_note.key_number,
)
)
current_note = next_note
current_head = next_head
current_dur = next_dur
current_slide = next_slide
current_portamento = next_portamento
self.pitch_tags.append((current_head + current_dur, current_note.key_number))

def pitch_at_ticks(self, ticks: int) -> float:
Expand All @@ -79,7 +79,7 @@ def pitch_at_secs(self, secs: float) -> float:
elif self.pitch_tags[index][1] == self.pitch_tags[index + 1][1]:
value = self.pitch_tags[index][1]
else:
value = self.slide.inter_func( # type: ignore[assignment]
value = self.portamento.inter_func( # type: ignore[assignment]
secs, self.pitch_tags[index], self.pitch_tags[index + 1]
)
return value * 100
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@


@dataclass
class PitchSlide:
class PortamentoPitch:
max_inter_time_in_secs: float
max_inter_time_percent: float
inter_func: Callable[[float, tuple[float, float], tuple[float, float]], float]

@classmethod
def cosine_slide(cls) -> PitchSlide:
def cosine_portamento(cls) -> PortamentoPitch:
return cls(0.05, 0.1, cosine_easing_in_out_interpolation)

@classmethod
def cubic_slide(cls) -> PitchSlide:
def cubic_portamento(cls) -> PortamentoPitch:
return cls(0.05, 0.1, cubic_interpolation)

@classmethod
def sigmoid_slide(cls) -> PitchSlide:
def sigmoid_portamento(cls) -> PortamentoPitch:
return cls(0.075, 0.48, partial(sigmoid_interpolation, k=5.5)) # type: ignore[call-arg]
4 changes: 2 additions & 2 deletions libresvip/plugins/svp/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

TICK_RATE: Final[int] = 1470000
DEFAULT_PITCH_TRANSITION: Final[float] = 0.0
DEFAULT_PITCH_SLIDE: Final[float] = 0.07
DEFAULT_PITCH_PORTAMENTO: Final[float] = 0.07
DEFAULT_PITCH_DEPTH: Final[float] = 0.15
DEFAULT_VIBRATO_START: Final[float] = 0.25
DEFAULT_VIBRATO_FADE: Final[float] = 0.2
DEFAULT_VIBRATO_DEPTH: Final[float] = 1.0
DEFAULT_VIBRATO_FREQUENCY: Final[float] = 5.5
DEFAULT_VIBRATO_PHASE: Final[float] = 0.0
DEFAULT_VIBRATO_JITTER: Final[float] = 1.0
SYSTEM_PITCH_SLIDE: Final[float] = 0.1
SYSTEM_PITCH_PORTAMENTO: Final[float] = 0.1
SYSTEM_PITCH_DEPTH: Final[float] = 0.0
MAX_BREAK: Final[float] = 0.01
DEFAULT_PHONE_RATIO: Final[float] = 1.8
Expand Down
30 changes: 15 additions & 15 deletions libresvip/plugins/svp/layer_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ class NoteStruct(NamedTuple):
key: int
start: float
end: float
slide_offset: float
slide_left: float
slide_right: float
portamento_offset: float
portamento_left: float
portamento_right: float
depth_left: float
depth_right: float
vibrato_start: float
Expand Down Expand Up @@ -135,21 +135,21 @@ def __post_init__(self, _note_list: list[NoteStruct]) -> None:
for current_note, next_note in more_itertools.pairwise(self.note_list):
if current_note.key == next_note.key:
continue
if (diameter := current_note.slide_right + next_note.slide_left) and (
if (diameter := current_note.portamento_right + next_note.portamento_left) and (
next_note.start - current_note.end <= MAX_BREAK
):
start = clamp(
current_note.end - current_note.slide_right + next_note.slide_offset,
current_note.end - current_note.portamento_right + next_note.portamento_offset,
current_note.start,
current_note.end,
)
end = clamp(
next_note.start + next_note.slide_left + next_note.slide_offset,
next_note.start + next_note.portamento_left + next_note.portamento_offset,
next_note.start,
next_note.end,
)
mid = clamp(
(current_note.end + next_note.start) / 2 + next_note.slide_offset,
(current_note.end + next_note.start) / 2 + next_note.portamento_offset,
start,
end,
)
Expand Down Expand Up @@ -315,7 +315,7 @@ def __post_init__(self, _note_list: list[NoteStruct]) -> None:
is_end_point=True,
_origin=current_note.start,
_depth=-current_note.depth_left,
_width=current_note.slide_left,
_width=current_note.portamento_left,
_length_l=math.nan,
_length_r=current_note.end - current_note.start,
)
Expand All @@ -327,7 +327,7 @@ def __post_init__(self, _note_list: list[NoteStruct]) -> None:
is_end_point=True,
_origin=current_note.end,
_depth=current_note.depth_right,
_width=current_note.slide_left,
_width=current_note.portamento_left,
_length_l=current_note.end - current_note.start,
_length_r=math.nan,
)
Expand All @@ -337,15 +337,15 @@ def __post_init__(self, _note_list: list[NoteStruct]) -> None:
is_end_point=True,
_origin=next_note.start,
_depth=-next_note.depth_left,
_width=next_note.slide_left,
_width=next_note.portamento_left,
_length_l=math.nan,
_length_r=next_note.end - next_note.start,
)
)
else:
middle = (current_note.end + next_note.start) / 2
origin = clamp(
middle + current_note.slide_offset,
middle + current_note.portamento_offset,
current_note.start,
current_note.end,
)
Expand All @@ -360,7 +360,7 @@ def __post_init__(self, _note_list: list[NoteStruct]) -> None:
is_end_point=False,
_origin=origin,
_depth=depth_l,
_width=-current_note.slide_right,
_width=-current_note.portamento_right,
_length_l=origin - current_note.start,
_length_r=next_note.end - origin,
)
Expand All @@ -376,7 +376,7 @@ def __post_init__(self, _note_list: list[NoteStruct]) -> None:
is_end_point=False,
_origin=origin,
_depth=depth_r,
_width=next_note.slide_left,
_width=next_note.portamento_left,
_length_l=origin - current_note.start,
_length_r=next_note.end - origin,
)
Expand All @@ -386,7 +386,7 @@ def __post_init__(self, _note_list: list[NoteStruct]) -> None:
is_end_point=True,
_origin=_note_list[-1].end,
_depth=-_note_list[-1].depth_right,
_width=_note_list[-1].slide_left,
_width=_note_list[-1].portamento_left,
_length_l=_note_list[-1].end - _note_list[-1].start,
_length_r=math.nan,
)
Expand Down Expand Up @@ -446,7 +446,7 @@ def __post_init__(self, _note_list: list[NoteStruct]) -> None:
continue
if i < len(_note_list) - 1 and _note_list[i + 1].start - end < MAX_BREAK:
end += min(
_note_list[i + 1].slide_offset,
_note_list[i + 1].portamento_offset,
min(
_note_list[i + 1].vibrato_start,
_note_list[i + 1].end - _note_list[i + 1].start,
Expand Down
20 changes: 10 additions & 10 deletions libresvip/plugins/svp/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,21 +230,21 @@ def _set_transition_offset(self, value: float) -> None:

transition_offset = property(_get_transition_offset, _set_transition_offset) # type: ignore [pydantic-field]

def _get_slide_left(self) -> float:
return constants.DEFAULT_PITCH_SLIDE if self.t_f0_left is None else self.t_f0_left
def _get_portamento_left(self) -> float:
return constants.DEFAULT_PITCH_PORTAMENTO if self.t_f0_left is None else self.t_f0_left

def _set_slide_left(self, value: float) -> None:
def _set_portamento_left(self, value: float) -> None:
self.t_f0_left = value

slide_left = property(_get_slide_left, _set_slide_left) # type: ignore [pydantic-field]
portamento_left = property(_get_portamento_left, _set_portamento_left) # type: ignore [pydantic-field]

def _get_slide_right(self) -> float:
return constants.DEFAULT_PITCH_SLIDE if self.t_f0_right is None else self.t_f0_right
def _get_portamento_right(self) -> float:
return constants.DEFAULT_PITCH_PORTAMENTO if self.t_f0_right is None else self.t_f0_right

def _set_slide_right(self, value: float) -> None:
def _set_portamento_right(self, value: float) -> None:
self.t_f0_right = value

slide_right = property(_get_slide_right, _set_slide_right) # type: ignore [pydantic-field]
portamento_right = property(_get_portamento_right, _set_portamento_right) # type: ignore [pydantic-field]

def _get_depth_left(self) -> float:
return constants.DEFAULT_PITCH_DEPTH if self.d_f0_left is None else self.d_f0_left
Expand Down Expand Up @@ -349,8 +349,8 @@ def pitch_edited(
transition_edited &= any(
x >= tolerance
for x in (
abs(self.slide_left - constants.DEFAULT_PITCH_SLIDE),
abs(self.slide_right - constants.DEFAULT_PITCH_SLIDE),
abs(self.portamento_left - constants.DEFAULT_PITCH_PORTAMENTO),
abs(self.portamento_right - constants.DEFAULT_PITCH_PORTAMENTO),
abs(self.depth_left - constants.DEFAULT_PITCH_DEPTH),
abs(self.depth_right - constants.DEFAULT_PITCH_DEPTH),
)
Expand Down
6 changes: 3 additions & 3 deletions libresvip/plugins/svp/param_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ def __post_init__(
end=_synchronizer.get_actual_secs_from_ticks(
position_to_ticks(sv_note.onset + sv_note.duration)
),
slide_offset=sv_note.attributes.transition_offset,
slide_left=sv_note.attributes.slide_left,
slide_right=sv_note.attributes.slide_right,
portamento_offset=sv_note.attributes.transition_offset,
portamento_left=sv_note.attributes.portamento_left,
portamento_right=sv_note.attributes.portamento_right,
depth_left=sv_note.attributes.depth_left,
depth_right=sv_note.attributes.depth_right,
vibrato_start=sv_note.attributes.vibrato_start,
Expand Down
4 changes: 2 additions & 2 deletions libresvip/plugins/svp/synthv_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Track,
)
from libresvip.model.pitch_simulator import PitchSimulator
from libresvip.model.pitch_slide import PitchSlide
from libresvip.model.portamento import PortamentoPitch
from libresvip.utils.audio import audio_track_info
from libresvip.utils.music_math import (
clamp,
Expand Down Expand Up @@ -123,7 +123,7 @@ def generate_track(self, track: Track) -> Optional[SVTrack]:
self.pitch_simulator = PitchSimulator(
synchronizer=self.synchronizer,
note_list=track.note_list,
slide=PitchSlide.sigmoid_slide(),
portamento=PortamentoPitch.sigmoid_portamento(),
)
sv_track.main_group.parameters = self.generate_params(track.edited_params)

Expand Down

0 comments on commit b3fd8d7

Please sign in to comment.