MIDI clock
Table of Contents
:ID: 24E1468A-279A-4B44-8AB8-A8A1C5D8D42D
See also MIDI messages
# Clock Message
- 24 messages = 1 quarter note.
- When 24 clock messages are received, the receiving MIDI device knows that 1 quarter note has passed.
- To generate a MIDI clock signal, calculate the pulse rate based on the BPM and send a simple message at that rate.
# Formula
# Pulse per ms rate
1 minute = 60 seconds = 60,000 ms
60,000 ms / ppq * bpm
Example using 120 BPM:
ppq = 24.0 bpm = 120.0 ppms = 60_000 / (ppq * bpm) "One pulse every #{ppms} ms"
One pulse every 20.833333333333332 ms
# Message Contents
# Status
0xF8
There is no channel number.
# Data
No data is required for this message, just the status byte
# Sequencing
# MIDI Start
# MIDI Stop
# MIDI Continue
# Sending MIDI Clock from Mixxx
# Existing Limitation
Mixxx only supports a 20ms resolution timer. This will work for BPMs under 100 so it’s not practical. See ControllerScriptInterfaceLegacy::beginTimer. It uses QObject startTimer which only supports using MS. This need microsecond precision.
# Possible solution
See https://stackoverflow.com/a/21856299.
This would require adding a high resolution timer. Maybe an API like
engine.beginHighResTimer
.
- Other references
- https://github.com/99x/timercpp/blob/master/timercpp.h
- This is not thread safe.
- Maybe it could be adapted to use
std::chrono::microseconds
- See also https://en.cppreference.com/w/cpp/thread/sleep_for
- https://blat-blatnik.github.io/computerBear/making-accurate-sleep-function/
- The naive approach of using
std::this_thread::sleep_for
is grossly inaccurate. This nice post explains why and some alternative approaches.
- The naive approach of using
- https://github.com/99x/timercpp/blob/master/timercpp.h
# RtMidi
https://www.music.mcgill.ca/~gary/rtmidi/
C++ Library for sending/receiving MIDI messages.
# Needs jack2 development files
In order to develop stuff depending on RtMidi and compiling for JACK, you’ll need the jackd2 development files:
sudo apt-get install libjack-jackd2-dev
# JACK audio connection kit
https://jackaudio.org/api/index.html
This can be used to send/receive MIDI.
# Examples
- https://github.com/x42/jack_midi_clock/blob/master/jack_midi_clock.c
- https://github.com/DarkPoubelle/behringer-x-touch-mini-mapper/blob/080e8abe2fce00e84678c000afefed8b63fe6731/main.cpp
- https://github.com/Ardour/ardour/blob/83225886a16a29100b88825e083da267e38e26e1/libs/backends/jack/jack_portengine.cc