Clock

MIDI Clock — source and sink, platform-agnostic.

Uses only sequencer.event_write(event, tick=True) for output. Works identically on CoreMIDI (macOS) and ALSA (Linux).

class midi.clock.ClockSource(bpm=120.0, sequencer=None)

Bases: object

Clock source: schedules clock pulses via OS sequencer timestamps.

Call schedule_ahead() from your run loop to keep the buffer full. The OS delivers each pulse at the exact kernel-scheduled time.

Parameters:
__init__(bpm=120.0, sequencer=None)
Parameters:
Return type:

None

tick_for_pulse(pulse)

Return the sequencer tick value for a given pulse number.

Return type:

int

Parameters:

pulse (int)

start()

Send Start event and begin scheduling clock pulses.

Return type:

None

stop()

Send Stop event.

Return type:

None

cont()

Send Continue event and resume scheduling.

Return type:

None

schedule_ahead(pulses=48)

Pre-schedule clock pulses into the future via event_write(tick=True).

Only schedules pulses beyond what’s already been scheduled. Call this periodically from your run loop.

Return type:

None

Parameters:

pulses (int)

class midi.clock.ClockSink(sequencer_resolution)

Bases: object

Clock sink: processes incoming clock events.

Uses wall-clock time between ClockEvents for BPM estimation (exponential moving average). Feed events from event_read() into process().

Parameters:

sequencer_resolution (int)

__init__(sequencer_resolution)
Parameters:

sequencer_resolution (int)

Return type:

None

property bpm: float

Estimated BPM from wall-clock inter-pulse intervals.

tick_for_next_pulse(offset=0)

Predict tick for the next clock pulse (or +offset pulses ahead).

Return type:

int

Parameters:

offset (int)

process(event)

Feed events from event_read(). Recognizes Clock/Start/Stop/Continue/SPP.

Return type:

None

Parameters:

event (AbstractEvent)