Skip to content

EZ Web Audio / musicalTimeToBeats

Function: musicalTimeToBeats()

musicalTimeToBeats(notation, beatsPerBar, ticksPerBeat): number

Defined in: packages/core/src/utils/musical-time.ts:50

Convert musical time notation to beats (BPM-independent).

Supports:

  • Note values: '1n' (whole=4), '2n' (half=2), '4n' (quarter=1), '8n', '16n', '32n'
  • Triplets: '4t', '8t', '16t' (2/3 of the base note value)
  • Dotted notes: '4n.', '8n.' (1.5x the base note value)
  • Measures: '1m', '2m', '4m' (beatsPerBar * count)
  • Bar:beat:tick: '2:1:0' (positional, 1-indexed bar/beat, 0-indexed tick)
  • Numeric passthrough: number values returned as-is

Parameters

notation

MusicalTimeNotation

Musical time notation string or numeric beat value

beatsPerBar

number = 4

Beats per bar for measure/position calculations (default: 4)

ticksPerBeat

number = 4

Ticks per beat for position calculations (default: 4)

Returns

number

Number of beats

Throws

if notation is invalid

Example

typescript
musicalTimeToBeats('4n')        // 1.0 (quarter note = 1 beat)
musicalTimeToBeats('8t')        // 0.333... (eighth triplet)
musicalTimeToBeats('4n.')       // 1.5 (dotted quarter)
musicalTimeToBeats('2m')        // 8 (two bars in 4/4)
musicalTimeToBeats('2:1:0')     // 4 (bar 2, beat 1 in 4/4)
musicalTimeToBeats('1m', 3)     // 3 (one bar in 3/4)