A metrical timeline whose native coordinate axis is quarter notes.
BeatGrid delegates measure-boundary lookup to its attached MetricMap, beat-in-measure lookup to its attached BeatInMeasureMap, and combined measure/beat lookup to its attached MetricalPositionMap. Its public beat queries also apply beat_unit so that a musical beat need not equal a quarter note.
BeatGrid and MetricMap are converging representations of meter. BeatGrid provides the Timeline interface; MetricMap provides an increasingly rich conversion model. Irregular measures, anacrusis, and meter changes modeled by MetricMap.from_boundaries() are intended to become available through BeatGrid as that model develops.
Attributes
Name
Type
Description
beats_per_measure
int
Number of beats per measure.
beat_unit
Fraction
The note value of one beat (e.g., Fraction(1, 4) for quarter note).
start_measure
int
The number of the first measure (default 1).
quarters_per_measure
Fraction
Derived: quarters per measure.
C-Maps (automatically created): - quarters -> mc (MetricMap): Integer measure count. - quarters -> beat (BeatInMeasureMap): Beat position as Fraction. - quarters -> {mc, beat} (MetricalPositionMap): Combined output.
Examples
>>>from fractions import Fraction>>>from timetoalign import ContinuousPhysicalTimeline, TimeUnit>>>>>># Create a beat grid for 4/4 time, 222 measures>>> grid = BeatGrid(... length=Fraction(888, 1), # 888 quarter notes... beats_per_measure=4,... )>>>>>># Query measure and beat at quarter 100>>> grid.measure_at(100) # -> 26 (integer!)>>> grid.beat_at(100) # -> Fraction(1, 1) (proper Fraction!)>>> grid.metrical_position(100) # -> {"mc": 26, "beat": Fraction(1, 1)}>>>>>># Attach to audio timeline>>> audio = ContinuousPhysicalTimeline(length=300.0, unit=TimeUnit.seconds)>>> audio.add_child(grid, offset=1.3) # First beat at 1.3 seconds>>>>>># Create from tempo>>> grid2 = BeatGrid.from_tempo(... tempo_bpm=120.0,... beats_per_measure=4,... length_quarters=Fraction(888, 1),... )
Extends the base Timeline.export_to_csv() with special formats for audio annotation tools.
Parameters
Name
Type
Description
Default
filepath
str
Output CSV file path.
required
format
str
Output format. Options: - “default”: Standard timestamp table (inherited behavior). - “sonic_visualiser”: Sonic Visualiser / Audacity label track. Two fields (TIME, LABEL) with header row. - “tilia”: Tilia beat track format. Four fields (time, measure, beat, is_first_in_measure).
'default'
labels
str
What to export when using “sonic_visualiser” format: - “beats”: All beat positions with labels like “M1B1”, “M1B2”. - “measures”: Measure start positions with labels like “M1”, “M2”. - “both”: Both beats and measures.
'beats'
**kwargs
Any
Additional arguments passed to base export_to_csv() when using “default” format.
>>># Export for Sonic Visualiser>>> grid.export_to_csv("beats.csv", format="sonic_visualiser")120
>>># Export for Tilia>>> grid.export_to_csv("beats.csv", format="tilia")120
>>># Standard timestamp table>>> grid.export_to_csv("data.csv", format="default")120
from_dict
timelines.BeatGrid.from_dict(data)
Create a BeatGrid from a serialized dictionary.
The constructor recreates the meter-map family; the serialized conversion_maps list contains only the maps attached beyond that family (for example, a from_tempo tempo map or a user-attached map), so every entry is restored via ConversionMap.from_dict.
You must provide either length_seconds or length_quarters.
Parameters
Name
Type
Description
Default
tempo_bpm
float
Tempo in beats per minute.
required
beats_per_measure
int
Number of beats per measure. Default 4.
4
beat_unit
Fraction
Note value of one beat. Default 1/4 (quarter note).
Fraction(1, 4)
length_seconds
float | None
Duration in seconds (converted using tempo). If start_seconds > 0, this should be the TOTAL audio duration; the grid will span from start_seconds to length_seconds.
None
length_quarters
Fraction | int | None
Duration in quarter notes.
None
start_seconds
float
Offset in seconds where the first beat occurs. Default 0.0. Used by beat_seconds() and measure_seconds().
0.0
start_measure
int
MC of the first measure. Default 1.
1
start_mn
str | None
MN label of the first measure. Default: same as start_measure.
Convert the grid and its construction parameters to a dictionary.
The three metrical maps created by __init__ (meter, beat-in- measure, and metrical-position maps) are excluded from the serialized conversion_maps list, since from_dict rebuilds them from the construction parameters instead. Any other attached conversion map (for example, a tempo map from from_tempo or a user-attached map) is serialized normally.
Parameters
Name
Type
Description
Default
events
bool
If True, include the "events" key (beat events and any other events added to the grid).
False
external_references
bool
If True, include the "external_references" key, even when empty.
False
Returns
Name
Type
Description
dict[str, Any]
A dictionary representation that reconstructs the attached meter maps.