open-audiovisual
Search GitHub

oav-p5 reference · The OAVp5 object (advanced)

OAVp5.clock#

objectoav-p5
OAVp5.clock

What oav-p5 hears of an external MIDI clock: its state, tempo and where it is in the bar. Nothing until another program or device starts sending one.

Some programs keep the time and everyone else follows: Strudel (midicmd("clock*96, <start stop>/8")), a DAW over a virtual cable, a drum machine on a MIDI cable. They send 24 clock messages per beat, plus start, continue, stop and song position. oav-p5 counts them: OAVp5.clock.info says whether a clock is running, OAVp5.clock.bpm its tempo and OAVp5.clock.phase how far through the bar and the beat it is now.

When a clock is moving, drumPhase, beatPhase, drumBar, drumStep and drumBpm read it, and a drum machine that is up follows it. ?oav_clock=off turns all of that off. Clock messages are not delivered to midiMessage(): they belong to the clock.

Notes

  • The first clock message loads the counting code (@openav/midi's MidiClock) from next to oav-p5.js, which takes a few frames. Until then the members read none, null and null.
  • This is advanced: most sketches only read drumPhase and let it follow a clock.

Examples

jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  OAVp5.input([0xFA]);                          // MIDI start
  setInterval(() => OAVp5.input([0xF8]), 20);   // clock ticks: 24 a beat, here 50 a second (125 BPM)
}

function draw() {
  background(20);
  fill(255);
  text('clock: ' + OAVp5.clock.info.state, 20, 30);
  text('tempo: ' + (OAVp5.clock.bpm ? nf(OAVp5.clock.bpm, 1, 1) : '-') + ' BPM', 20, 60);
  const p = OAVp5.clock.phase;
  if (p) circle(40 + p.beat * (width - 80), 150, 30);   // sweeps across once a beat
}
The sketch sends itself a made-up MIDI clock at about 125 BPM. A circle sweeps across once per beat, and the state and tempo are printed.

Related

  • OAVp5.clock.info A snapshot of the external clock: state (none, running, stopped or lost), bpm, whether the drums follow it, where it came from, and counts.
  • OAVp5.clock.bpm The tempo of the external MIDI clock in beats a minute, or null while there is none.
  • OAVp5.clock.phase Where the external clock is right now: { pos, bar, beat, bpm, running }, or null while there is no tempo.
  • clock Whether an external MIDI clock (from a DAW, Strudel or a drum machine) moves drumPhase, drumBpm and the drum machine: follow or off.
  • drumPhase How far through the current bar the drum machine is, smoothly from 0 up to just below 1, as it is heard.
  • beatPhase How far through the current beat the drum machine is, smoothly from 0 up to just below 1, four times per bar.

p5/oav-p5.js, line 741

↑ Back to top · All names