Skip to content

Commit

Permalink
vxf: add VxPitchData model
Browse files Browse the repository at this point in the history
  • Loading branch information
SoulMelody committed Jan 11, 2025
1 parent 1418c04 commit 910f4c3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
4 changes: 2 additions & 2 deletions libresvip/plugins/ppsf/legacy_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
def ppsf_int_encoder(value: int, ctx: Context) -> int:
low = value & 0x7F
high = (value >> 7) << 8
width = math.floor(math.log(high, 256))
width = math.floor(high.bit_length() / 8)
base = 0x80 << (width * 8)
high += base
return high + low
Expand All @@ -39,7 +39,7 @@ def ppsf_int_encoder(value: int, ctx: Context) -> int:
def ppsf_int_decoder(value: int, ctx: Context) -> int:
high = value >> 8
low = value & 0xFF
width = math.floor(math.log(high, 256))
width = math.floor(high.bit_length() / 8)
base = 0x80 << (width * 8)
high -= base
return (high << 7) + low
Expand Down
2 changes: 1 addition & 1 deletion libresvip/plugins/tssln/value_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def _build(self, obj: int, stream: BinaryIO, context: Context, path: CSPath) ->
if obj < 0:
msg = "Negative numbers not supported"
raise ValueError(msg)
width = math.ceil(math.log(obj + 1, 256))
width = max(math.ceil(obj.bit_length() / 8), 1)
try:
content = obj.to_bytes(width, "little", signed=False)
stream.write(struct.pack("<B", width))
Expand Down
39 changes: 33 additions & 6 deletions libresvip/plugins/vxf/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
Struct,
obj_,
)
from pydantic import Field

from libresvip.model.base import BaseModel

# Reference: https://github.com/jazz-soft/JZZ/blob/master/javascript/JZZ.js
BASE_DCTPQ = 0x30
Expand Down Expand Up @@ -69,17 +72,26 @@
UmpTempo = Struct(
"delta_ticks" / DeltaTicks,
"magic" / Const(b"\xd0\x10\x00\x00"),
"tempo" / Int32ub,
"tempo"
/ ExprAdapter(
Int32ub,
encoder=lambda obj, context: int(1705032600 * obj),
decoder=lambda obj, context: 1705032600 / obj,
),
"padding" / Const(b"\x00" * 8),
)

UmpTimeSignature = Struct(
"delta_ticks" / DeltaTicks,
"magic" / Const(b"\xd0\x10\x00\x01"),
"numerator" / Int8ub,
"cc" / Int8ub,
"dd" / Int8ub,
"padding" / Const(b"\x00" * 9),
"denominator"
/ ExprAdapter(
Int8ub,
encoder=lambda obj, context: obj.bit_length() - 1,
decoder=lambda obj, context: 1 << obj,
),
"padding" / Const(b"\x08" + b"\x00" * 9),
)

UmpLyric = Struct(
Expand All @@ -96,8 +108,7 @@
"header" / Const(b"\xd0"),
"seq_stat" / Int8ub,
"magic" / Const(b"\x01\x51"),
"seq_num" / Int16ub,
"text" / PaddedString(10, "utf-8"),
"text" / PaddedString(12, "utf-8"),
)

UmpNoteOn = Struct(
Expand Down Expand Up @@ -129,3 +140,19 @@
"ppqn" / PPQN,
"tracks" / GreedyRange(VxTrack),
)


class VxPitchPoint(BaseModel):
pitch: float
applicable: bool
position: int


class VxTimeBasedPitchSequence(BaseModel):
time_frame_period_seconds: float = Field(alias="timeFramePeriodSeconds")
num_frames_overall_sequence: int = Field(alias="numFramesOverallSequence")
pitch_sequence: list[VxPitchPoint] = Field(alias="pitchSequence")


class VxPitchData(BaseModel):
time_based_pitch_sequence: VxTimeBasedPitchSequence = Field(alias="TimeBasedPitchSequence")

0 comments on commit 910f4c3

Please sign in to comment.