39 lines
1.6 KiB
Python
39 lines
1.6 KiB
Python
|
|
from numbers import Real
|
||
|
|
|
||
|
|
type _RealNumber = int | float | Real
|
||
|
|
type _TimeSignature = tuple[int, int]
|
||
|
|
|
||
|
|
def tick2second(tick: _RealNumber, ticks_per_beat: _RealNumber, tempo: _RealNumber) -> float:
|
||
|
|
"""Convert absolute time in ticks to seconds.
|
||
|
|
|
||
|
|
Returns absolute time in seconds for a chosen MIDI file time resolution
|
||
|
|
(ticks/pulses per quarter note, also called PPQN) and tempo (microseconds
|
||
|
|
per quarter note).
|
||
|
|
"""
|
||
|
|
|
||
|
|
def second2tick(second: _RealNumber, ticks_per_beat: _RealNumber, tempo: _RealNumber) -> int:
|
||
|
|
"""Convert absolute time in seconds to ticks.
|
||
|
|
|
||
|
|
Returns absolute time in ticks for a chosen MIDI file time resolution
|
||
|
|
(ticks/pulses per quarter note, also called PPQN) and tempo (microseconds
|
||
|
|
per quarter note). Normal rounding applies.
|
||
|
|
"""
|
||
|
|
|
||
|
|
def bpm2tempo(bpm: _RealNumber, time_signature: _TimeSignature = (4, 4)) -> int:
|
||
|
|
"""Convert BPM (beats per minute) to MIDI file tempo (microseconds per
|
||
|
|
quarter note).
|
||
|
|
|
||
|
|
Depending on the chosen time signature a bar contains a different number of
|
||
|
|
beats. These beats are multiples/fractions of a quarter note, thus the
|
||
|
|
returned BPM depend on the time signature. Normal rounding applies.
|
||
|
|
"""
|
||
|
|
|
||
|
|
def tempo2bpm(tempo: _RealNumber, time_signature: _TimeSignature = (4, 4)) -> float:
|
||
|
|
"""Convert MIDI file tempo (microseconds per quarter note) to BPM (beats
|
||
|
|
per minute).
|
||
|
|
|
||
|
|
Depending on the chosen time signature a bar contains a different number of
|
||
|
|
beats. The beats are multiples/fractions of a quarter note, thus the
|
||
|
|
returned tempo depends on the time signature denominator.
|
||
|
|
"""
|