Graphic display of beats and ticks

This page is for programmers who want to read, maintain or extend the code that plays the ticks of a time base and draws the beats in the graphics of the Bol Processor (BP3). A time base is a -tb file named on top of a grammar or data project. Its ticks are percussion notes superposed on the performance of each item, in MIDI files, in real-time MIDI, and on the MIDI output of real-time Csound, SuperCollider and OSC. Ticks and beats share a single piece of code: the tempo map of the item, which tells at what date each beat of the item is played.

The code lives in the console (source/BP3/) and in the PHP interface (php/):

  • TimeBase.c contains everything specific to time bases: loading the file (LoadTimeBase()), the tempo map (TimeBaseSetStructure(), TbWalk(), TbApplyBreaths(), TbBeatToDate(), TbRealDate()), playing the ticks (TimeBaseStartItem(), TimeBaseSendUntil(), TimeBaseEndItem()) and the dates of the vertical lines of the graphics (TimeBaseGraphicTicks(), TimeBaseGraphicBeats());
  • PlayThings.c, MakeSound.c and MIDIstuff.c call these functions at the right moments of a performance;
  • Polymetric.c writes the tags of _tempo() read by the tempo map;
  • Graphic.c draws the lines in DrawItemBackground();
  • CsoundRealTime.c and MIDIdriver.c send ticks in real-time formats and route them to MIDI outputs;
  • on the PHP side, timebase.php edits the file, _basic_tasks.php reads and writes its JSON (tb_data_to_json(), tb_read_resource(), tb_metronome()), and produce.php passes it to the console.

Ticks were a feature of BP2, where they were sent to real-time MIDI only. The BP2 code is kept as documentation in source/not_used/Ticks.c. The BP3 implementation was written from scratch in November 2026.

Overview

Each item goes through the following steps. Steps 2 to 4 are done for every item, with or without a time base, because the beats of the graphics use the same tempo map.

  1. Loading (once): option -tb FILE makes main() call LoadTimeBase(), which reads the tracks of the file.
  2. Tempo map: PlayBuffer1() runs PolyMake() with the flag PolyTempoTags set, then calls TimeBaseSetStructure() on the work string, then TimeBaseRemoveTags().
  3. Time-setting: TimeSet() computes the dates of the columns of the phase diagram. PlayBuffer1() then calls TimeBaseSetItemEnd(tmax,maxseq), giving the end of the item and its last column.
  4. Graphics: DrawItemBackground() asks TimeBaseGraphicTicks() for the dates of the ticks, or, without ticks, TimeBaseGraphicBeats() for the dates of the beats.
  5. Performance: MakeSound() calls TimeBaseStartItem() at the start of each performance of the item. Before each MIDI event, SendToDriver() (MIDIstuff.c) calls TimeBaseSendUntil(time), which sends the ticks due by then. TimeBaseEndItem() sends the remaining ones, and TimeBaseCancel() stops them if the performance is interrupted.

All dates in TimeBase.c are in milliseconds from date 0 of the item, except in TimeBaseSendUntil(), which adds the origin of the performance (TBorigin).

The time base file

A -tb file is JSON, written by tb_data_to_json(). Like the settings files, each value is an object with a key and a value, which the console finds by key with TbValue() and TbNumber(), whatever the label in front of it. The file contains:

  • the metronome of the time base: Pclock ticks in Qclock beats, e.g. 3 ticks in 1 beat. Files saved before this field existed, or with an empty field, get 1 tick in 1 beat;
  • a list of tracks (Tracks), each with TickKey, TickChannel, TickVelocity, a cycle TickCycle of 1 to 40 beats, a speed ratio Ptick/Qtick, the duration of a tick TickDuration (ms), and the array Ticks telling which beats of the cycle have a tick (an odd value, as in BP2).

LoadTimeBase() keeps up to 32 tracks (TB_MAXTRACKS) in the static array TBtrack[], ignoring tracks with no tick in their cycle or a velocity of 0. The first track of the file, the upper one on timebase.php, is marked upper: its ticks are drawn differently. A file in the old text format, or invalid JSON, gives an error message and no ticks: the production goes on. produce.php converts old-format files to JSON before running the console (convert_timebase_to_json()).

Time base files always live in the top folder of the workspace (ctests in the distribution), even for a project in a subfolder such as Imported_MusicXML/. PHP code must find them with timebase_dir($dir). Only the last -tb line of a project counts, and saving a project with several shows a warning (timebase_declarations_warning()).

How far apart ticks are

Ticks are counted in beats of the item, not in seconds. A beat of the item is a symbol played at tempo 1: 1000 * Pclock / Qclock ms of the project's metronome (note that in the project's settings, Pclock/Qclock is a period in seconds), divided by the tempo of the item where the beat falls. Beat n of a track is at n × TbRatio() beats of the item, where:

TbRatio = (Qtick / Ptick) × (Qclock / Pclock of the time base)

So three things set the spacing of ticks: the speed ratio of the track, the metronome of the time base and the metronome of the project, and the tempo of the item follows. For example, a time base of 3 ticks in 1 beat gives a tick every 333 ms with a project at 60 beats per minute, every 222 ms at 90 beats per minute, and every 444 ms during a passage at _tempo(3/4) of the 60-beat project. timebase.php, which has no project, plays its tracks at 60 beats per minute.

A track whose beats would be shorter than 5 ms somewhere in the item (shortest beat TbMinBeat() × TbRatio()) is ignored, with a warning, because it would flood the MIDI output.

The tempo map

The tempo map converts a beat of the item, possibly fractional, into its date. It is a list of segments in the static arrays TBmapbeat[], TBmapdate[] and TBmapms[]: segment k starts at beat TBmapbeat[k] and date TBmapdate[k], and its beats last TBmapms[k] ms. The last segment goes on after the end of the item. TbMapAdd() appends a segment, or extends the last one if the tempo has not changed.

TbBeatToDate(beat) finds the last segment starting at or before beat by binary search (with a tolerance of 10-9 beat for rounding errors), computes the ideal date TBmapdate[k] + (beat − TBmapbeat[k]) × TBmapms[k], and passes it to TbRealDate(), which gives the date of the performance (see below). When two segments start at the same beat, which happens after a breath, the second one is taken: that beat falls at the end of the breath.

Without a map (smooth time, no metronome), beats are those of the metronome.

Making the tempo map from the work string

TimeBaseSetStructure() builds the map from the work string as PolyMake() leaves it, by two calls to TbWalk(): a probe, which returns the duration of the first beat of the item and becomes segment 0, then the walk proper. Token codes are described in docs-developer/BP3-info.txt.

Speed markers

After PolyMake(), every duration in the work string is set by absolute speed markers: * (T0 21) and ** (T0 24) set the scale, / (T0 11) and \ (T0 25) the speed, each followed by its value as T1 a T1 b (a × TOKBASE + b, read by TbMarkerValue()). A symbol then lasts metronome × scale / speed. PolyMake() has also replaced numbers with silences: 4 becomes - followed by three prolongations _ (T3 1 then T3 0).

The difficulty is that these markers mean two different things. Some come from a tempo (_tempo(2), or /2 at the top level), which changes the length of the beat. Others make a rhythm inside a polymetric expression ({3, A B}, /3, beat separators, fractional durations such as 9/2), which does not. The walk has to set the duration of a beat, called unit in the code, from the first kind only.

The path followed

The duration of a polymetric expression is that of its first field. The walk therefore follows the top level and, in each expression, its first field, at any depth. On a , it sets skip to the depth of that expression: the other fields don't change the map. They are still walked, with their own dates, to find breaths. A stack of TbLevel keeps, for each open {, the scale and speed before it, its date, and the end of its first field, so that each field starts at the date of the {, and the } resumes at the end of the first field with the tempo of before the expression.

Each symbol of the path (T25 simple note, T3 terminal or silence, T4 time pattern) adds a segment (beat, date, unit), then advances beat by ms / unit and date by ms. Other tokens (performance controls, out-time objects, synchronisation tags) take no time.

Where the tempo is read

unit is set in the following places, in the order the walk meets them:

  1. A tag of _tempo() (T0 26) on the path: unit = metronome × the scale and speed of the markers that follow the tag. This is the reliable source, see below.
  2. At the top level, a note or sound-object sets unit to its own duration. Silences don't, because a fractional silence such as 1/2 is written *a /2b -, which would halve the beat.
  3. In the first field of a top-level expression (firstfield), the first note sets unit, as do notes after markers inside that field, e.g. {E4 F4 _tempo(2.7) G4 F4}, so that ticks still follow the notes after the }.
  4. A first field that is only a duration, silences up to a comma, as in {9/2, …} (TbDurationField()): its markers are a rhythm, so unit is the tempo before the {. A rest followed by notes, as in {5/3 . C4 D4 . E4 B3}, is not a duration.
  5. An expression starting the first field of a top-level expression, the typical measure imported from MusicXML, {_tempo(13/15) _vel(64) {25/6, …}}: unit is the tempo at the inner {, unless the first field of that inner expression is a duration starting with markers (nested = 2). Then the first pair of markers gives the tempo: *15 /13 *5 /26 - _ _ … for 25/6 at tempo 13/15, where the second pair is the rhythm of the sixths. This handles measures such as {_vel(64) {4,_tempo(14/15) G4 …,_tempo(14/15) …}}, where the tempo is only written inside the voices and PolyMake() gives it to the duration 4.

Markers in deeper fields are never used as tempos: they are rhythms (e.g. -gr.Visser5).

Tags of _tempo()

Rules 2 to 5 are guesses, because a tempo and a fractional duration can produce exactly the same tokens. {9/2, C4 D4 E4} and {_tempo(2) 9, C4 D4 E4} both become { *1 /1 *1 /2 - _ _ _ _ _ _ _ _ , …}, and their notes are played at the same dates. Yet the first one has beats at the project's tempo and the second one beats twice as fast.

To tell them apart, PolyExpand() (Polymetric.c), where it converts a _tempo() (two tokens T43 p, T43 q) into markers, writes a tag T0 26 just before these markers when the global PolyTempoTags is set. The tag is followed by the scale and speed markers of the new tempo, or by nothing if the tempo does not change. PlayBuffer1() sets PolyTempoTags only around its own call of PolyMake(), and calls TimeBaseRemoveTags() right after TimeBaseSetStructure(). So time-setting, the phase diagram and all other code never see a tag. The other callers of PolyMake() (expansion, templates, analysis) don't get tags either.

In the walk, a tag on the path sets unit and clears firstfield and nested, so that the guesses above don't override it. Tags in fields that are not followed are ignored. Nested tempos are followed: in {_tempo(1.414) A3 {B3 C4 -, F3} {_tempo(1.414) A3 …}}, beats get shorter at each level.

Breaths

A breath is a short silence that delays the music without being part of a beat. The performance tool _breath (index 68 of PerformanceControl in php/console_strings.json, encoded T12 31 by Encode.c, displayed back by DisplayArg.c) marks the silence that follows it as a breath.

In every field, followed or not, the walk records the silence after _breath, with its prolongations, with TbBreathAdd(): its date, its duration, its measure (top-level expression) and its field. A { ends the breath, since the first field of an expression is often a duration written as a silence ({2,G5}). At the end of the walk, TbApplyBreaths() turns breaths into pauses of the map with TbPause(start,end): beats stop at start, the following segments lose the beats taken by the breath, and a segment starting at end with the same beat is inserted.

The MusicXML import writes a breath in every voice of a measure, but not always at the same date or with the same length. In measure 18 of Les Ombres Errantes, three voices have it after their first beat and the fourth after a rest of two beats: {25/6,{1/8,Eb4 D4} {7/8,Eb4} _breath 1/6 …, …, 2 _breath 1/6 {2,D4}}. A breath may also be added to a rest (_breath 19/6). So, for each measure, TbApplyBreaths() keeps as many breaths as the field with the most breaths, at the dates found in the most fields, each with the shortest duration found at its date, and ignores a breath starting during the previous one.

The import writes [TAG] _breath 1/6. For scores imported earlier, which have [TAG] 1/6, data.php inserts _breath when it prepares the items (breath_tag_of() reads the tag in the header line // Interpreting breaths as 1/6 beat with tag [🌱], convert_breath_tags() is called by create_parts()). run-ctests.sh does the same.

From ideal dates to the dates of the performance

The tempo map gives ideal dates. Notes are not played exactly there: TimeSet() places them on the columns of the phase diagram, whose dates (*p_T)[j] include the correction of the total duration (CorrectionFactor), the compression of the diagram (Kpress), and the delays of time-setting. TbRealDate() gives a tick the date of the column of its position, as a note at the same position would get (see Technical analysis of TimeSet):

  • the position is in = 1 + date × Ratio / metronome, in units of 1/Ratio beat, rounded when it is a whole number except for rounding errors (they put some ticks of -gr.Mozart 45 ms before their notes);
  • its column is Class(in). With compression, TimeSet() gives each column the date of the next one, up to about 10 ms later, even the first; taking the column's date keeps ticks with the notes (in _tempo(1.3) C4 D4 {E4 F4 _tempo(2.7) G4 F4}, the first note is at 10 ms);
  • a position between two positions of the grid is interpolated between their columns. Without this, in C4 _ _ _, whose columns are a beat apart, all ticks of a beat took its date (8 ticks per beat in -tb.tansen trance);
  • beyond the last column (TBmaxseq, set by TimeBaseSetItemEnd()), the date is extrapolated from it.

The break tempo property of sound-objects needs nothing more. When time-setting breaks the tempo after such an object (sol[1] in Possible_choices()), the delay is added to the dates of all following columns (“global drift” in SetTimeObjects()), so ticks follow it. In -gr.tryKeyMap, 210 of 216 objects start on a tick; the other six start 62 ms before their beat because time-setting moved the objects themselves, which doesn't move the beat.

MIDI files are shifted so that the first note is at date 0. This is why the dates of the columns could not be corrected instead: an attempt to keep T[0] at 0 made every note after the first one 10 ms late.

Playing the ticks

TimeBaseStartItem(origin,end) is called by MakeSound() for each performance of the item, except when writing Csound score files. It does nothing in smooth time, without a metronome, with option --mute-ticks (TimeBaseMute()), during capture or prototype playing, or when no MIDI output is open. It resets each track to beat 1: no tick is played at date 0, where it would only blur the attack of the first note.

In MIDI files and real-time MIDI, events must be sent in chronological order. So the ticks are not sent at once: SendToDriver() calls TimeBaseSendUntil(time) before each event of the item. This function repeatedly finds the earliest pending event among the tracks, a NoteOff (first, at equal dates) or the next tick, skipping beats without a tick, and sends it with TbSend() on instance TB_TICK_INSTANCE. TbBeatDate() computes each beat from the origin, so that rounding errors don't add up. A tick ends after its duration, at the next tick of its track, or at the end of the item, whichever comes first. A recursion guard (TBsending) prevents SendToDriver() from calling it again. TimeBaseEndItem() sends what remains before the end.

In real-time Csound, SuperCollider and OSC (CsoundRT), all ticks of the item are queued at once by TimeBaseSendUntil(end) through CsRTMidiTick(), in the MIDI queue of CsoundRealTime.c, and sent to the MIDI output (--rtmidi) at their dates plus --csoundrt-delay. With --rt-dump FILE, they are written as DATE MIDI NoteOn channel key velocity lines instead, which is the easiest way to test them.

In real-time MIDI, sendMIDIEvent() (MIDIdriver.c) sends events of TB_TICK_INSTANCE to the outputs whose channel filter accepts their channel, or to the first output if none does: projects often filter the channels of their music only (e.g. channel 1 in -da.Beethoven-fugue-b-flat-major), which silenced ticks on channel 10. The Windows MIDI loop doesn't apply channel filters.

Lines in the graphics

DrawItemBackground() draws thick blue vertical lines in the object graph and the piano roll, in one of three ways:

  1. Ticks, when a time base is played: TimeBaseGraphicTicks() returns their dates, with ticks of several tracks at the same date listed once, and upper set if one of them belongs to the first track. Those are drawn as solid lines with a short bar on top, like a “T”; the others as dashed lines (style "dotted" of draw_line(), setLineDash([16,10])). The tick at date 0 is drawn although it is not played, and so is a tick falling on the end of the item (within 2 ms), closing its last beat. Lines are not numbered.
  2. Beats, without a time base or when ticks are muted: TimeBaseGraphicBeats() returns the dates of beats 0, 1, 2… from the tempo map, as the ticks of -tb.onebeat would be, up to the right edge of the image. They are numbered from 0, or from 1 with the setting StartFromOne (“Number beats from 1”). A line closer than 20 pixels to the previous one is skipped.
  3. Beats of the phase diagram, as before November 2026, when there is no tempo map: in smooth time, or without a metronome.

TbPixel() converts a date into pixels the way objects are drawn: rounded on the piano roll, truncated on the object graph, otherwise lines and boxes could be one pixel apart. Thin lines of subdivisions are only drawn when they are far enough apart; the first one used to be drawn alone, next to the first tick.

Interface

  • grammar.php and data.php pass timebase= to produce.php, which gives the console -tb FILE. In real-time Csound, SuperCollider and OSC, it adds --rtmidi when the project names a time base, even without saved MIDI ports.
  • The checkbox “Play ticks of the time base” (play_ticks_checkbox()), shown except in smooth time, is saved per project in _settings.php. Unchecked, produce links carry mute_ticks=1 and the console gets --mute-ticks: no ticks are played and the graphics show the beats.
  • timebase.php edits tracks and the metronome of the time base (“N ticks in M beat(s)”, tb_metronome()), and plays them at 60 beats per minute. The page of a subfolder lists the time bases of the top folder (display_timebases()), and CREATE NEW TIMEBASE saves there.

Testing

Ticks can be compared with notes without listening, from a dump of a real-time OSC run, run from php/:

../bp play -da ITEM.bpda -se ../ctests_git/-se.trySpeeds -tb ../ctests_git/-tb.onebeat --seed 1 --osc 7400 --rt-dump OUT.txt

Notes are the /bp/note lines and ticks the MIDI NoteOn lines with a non-zero velocity, each preceded by its date. -tb.onebeat has a tick on every beat, so it shows the tempo map directly. In the graphics (--traceout), tick lines can be compared with the strokeRect() of the object boxes. run-ctests.sh passes the -tb file named by a project (names may contain spaces); its MIDI files include the ticks, so diff -rq between two builds detects any change.

Useful examples in ctests_git/: -da.trySpeeds (tempo changes inside fields), -gr.Mozart and -gr.Visser.Waves (time bases with several tracks), -gr.Ruwet (subdivisions), -gr.tryKeyMap (break tempo), and Imported_MusicXML/-da.Ombres_errantes (breaths and measures imported from MusicXML).

Known limits

  • No ticks or lines in smooth time, and no ticks in Csound score files.
  • Beats drawn after the end of an item, up to the right edge of the image, are extrapolated and may be irregularly spaced.
  • Without a _tempo(), markers at the start of a top-level expression whose first field is not a pure duration are still read by rules 2 and 3, which can mistake a rhythm for a tempo.
  • A breath inside an expression of fixed duration (e.g. {3, C4 _breath 1/2 D4 E4}) compresses the notes rather than delaying them, yet ticks are still delayed. Imported measures don't have this problem because their duration includes the breath (25/6 instead of 4).
  • An older console cannot compile a project containing _breath.

Note: This page was entirely composed by Claude Opus 5.5 on 26 November 2026.

Leave a Reply

Your email address will not be published. Required fields are marked *

« Translate