NAME

MIDI::ClockDaemon - Real-time MIDI clock client/server

VERSION

version 0.0101

SYNOPSIS

# daemon:
my $bpm = shift || 60; # beats per minute
my $c = MIDI::ClockDaemon->new(
  bpm     => $bpm,
  verbose => 1,
);
$c->invoke_daemon;

# worker:
use MIDI::ClockDaemon ();
my $port_name = shift || die "Usage: $0 midi_port_substring [channel] [udp_port]\n";
my $channel   = shift // 0;
my $udp_port  = shift // 9000;
my $c = MIDI::ClockDaemon->new(
    port_name => $port_name,
    port_num  => $udp_port,
    verbose   => 1,
);
my $cb = sub {
    my ($self, $tick) = @_;
    if ($tick % $self->ppqn == 0) {
        $self->midi_out->note_on($channel, 60, 100);
    }
    elsif ($tick % $self->ppqn == 12) {
        $self->midi_out->note_off($channel, 60, 0);
    }
};
$c->invoke_worker($cb);

DESCRIPTION

A MIDI::ClockDaemon is a real-time MIDI clock client/server.

ATTRIBUTES

bpm

$bpm = $c->bpm;

Beats per minute.

Default: 60

host

$host = $c->host;

Where are we?

Default: 127.0.0.1

loop

$loop = $c->loop;

An IO::Async::Loop object.

midi_out

$midi_out = $c->midi_out;

An MIDI::RtMidi::FFI::Device::Out object.

port_num

$port_num = $c->port_num;

Given worker port number.

Default: The first item in the port_range list.

port_name

$port_name = $c->port_name;

Full name or substring fragment of the MIDI output port.

Default: undef

port_range

$port_range = $c->port_range;

The range of 16 ports to use for broadcase/listening.

Default: 9000 .. 9015

send_clock

$send_clock = $c->send_clock;

Send MIDI clock messages to the output port. When several workers share one MIDI port, only one of them should do this. Otherwise the clock runs too fast.

Default: 1

ppqn

$ppqn = $c->ppqn;

Pulses per quarter-note.

Default: 24

tick_interval

$tick_interval = $c->tick_interval;

Computation: 60 / ($bpm * $ppqn)

Which is approximately 0.020833s at 120 BPM.

verbose

$verbose = $c->verbose;

Show progress.

METHODS

new

$c = MIDI::ClockDaemon->new(verbose => 1);

Create a new MIDI::ClockDaemon object.

invoke_daemon

$c->invoke_daemon;

Start the daemon!

invoke_worker

$c->invoke_worker($cb);

Start a worker with a callback code reference.

SEE ALSO

Moo

https://en.wikipedia.org/wiki/MIDI_beat_clock

https://en.wikipedia.org/wiki/MIDI_timecode

AUTHOR

Gene Boggs <gene.boggs@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Gene Boggs.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.