blib/lib/MIDI/ClockDaemon.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.5% 29 / 31 CC 1 SCAR 0.4
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package MIDI::ClockDaemon;
2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Real-time MIDI clock client/server
5  
6 our $VERSION = '0.0200';
7  
8 2 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use IO::Async::Timer::Periodic;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use IO::Async::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 0 ▶use MIDI::RtMidi::Util qw(out_port);
Subroutine BEGIN: called CC=1 SCAR=2.6
16 use Moo;
17 use Socket qw(pack_sockaddr_in inet_aton);
18 use Types::Standard qw(ArrayRef Bool InstanceOf Str);
19 use Types::Common::Numeric qw(IntRange PositiveInt);
20 use Data::Validate::IP qw(is_ip);
21 use namespace::clean;
22  
23  
24 has bpm => (
25 is => 'ro',
26 isa => PositiveInt,
27 default => 60,
28 );
29  
30  
31 has host => (
32 is => 'ro',
33 isa => Str->where(sub { is_ip($_) }),
34 default => '127.0.0.1',
35 );
36  
37  
38 has loop => (
39 is => 'ro',
40 isa => InstanceOf['IO::Async::Loop'],
41 default => sub { IO::Async::Loop->new },
42 );
43  
44  
45 has midi_out => (
46 is => 'lazy',
47 isa => InstanceOf['MIDI::RtMidi::FFI::Device::Out'],
48 );
49 sub _build_midi_out ($self) {
50 my $midi_out = out_port($self->port_name);
51 return $midi_out;
52 }
53  
54  
55 has port_num => (
56 is => 'lazy',
57 isa => PositiveInt,
58 );
59 sub _build_port_num ($self) {
60 return $self->port_range->[0];
61 }
62  
63  
64 has port_name => (
65 is => 'ro',
66 isa => Str,
67 );
68  
69  
70 has port_range => (
71 is => 'ro',
72 isa => ArrayRef[IntRange[9000, 9015]],
73 default => sub { [9000 .. 9015] },
74 );
75  
76  
77 has send_clock => (
78 is => 'ro',
79 isa => Bool,
80 default => 1,
81 );
82  
83  
84 has ppqn => (
85 is => 'ro',
86 isa => PositiveInt,
87 default => 24,
88 );
89  
90  
91 has tick_interval => (
92 is => 'lazy',
93 );
94 sub _build_tick_interval {
95 my ($self) = @_;
96 return 60 / ($self->bpm * $self->ppqn);
97 }
98  
99  
100 has verbose => (
101 is => 'ro',
102 isa => Bool,
103 default => sub { 0 },
104 );
105  
106  
107 sub BUILD ($self, $args) {
108 my $port = $self->port_num;
109 croak "port_num $port is not in port_range"
110 unless grep { $_ == $port } $self->port_range->@*;
111 }
112  
113  
114 sub invoke_daemon ($self) {
115 my $socket = IO::Socket::INET->new(Proto => 'udp')
116 or die "Cannot create UDP socket: $!\n";
117  
118 my @dests = map { pack_sockaddr_in($_, inet_aton($self->host)) } $self->port_range->@*;
119  
120 my $tick = 0;
121  
122 # graceful shutdown
123 $self->loop->watch_signal(INT => sub {
124 say "\nStopping MIDI clock";
125 $self->loop->stop;
126 });
127  
128 my $timer = IO::Async::Timer::Periodic->new(
129 interval => $self->tick_interval,
130 reschedule => 'hard',
131 on_tick => sub {
132 my $msg = pack('N', $tick);
133 for my $dest (@dests) {
134 $socket->send($msg, 0, $dest) // warn "send failed: $!\n";
135 }
136 $tick++;
137 },
138 );
139  
140 $self->loop->add($timer);
141 $timer->start;
142  
143 say 'Async Clock daemon running at ',
144 $self->bpm, ' BPM, sending to ', $self->host,
145 ' on ports ', $self->port_range->[0], '-', $self->port_range->[-1]
146 if $self->verbose;
147  
148 $self->loop->run;
149 }
150  
151  
152 sub invoke_worker ($self, $cb) {
153 croak 'port_name is required for a worker' unless defined $self->port_name;
154  
155 # graceful shutdown
156 $self->loop->watch_signal(INT => sub {
157 say "\nStopping worker for '", $self->port_name, "'...";
158 $self->midi_out->panic;
159 $self->loop->stop;
160 });
161  
162 my $raw_sock = IO::Socket::INET->new(
163 LocalAddr => $self->host,
164 LocalPort => $self->port_num,
165 Proto => 'udp',
166 ) or die "Can't bind UDP port ", $self->port_num, ": $!\n";
167  
168 my $async_sock = IO::Async::Socket->new(
169 handle => $raw_sock,
170 on_recv => sub {
171 my ($obj, $dgram, $addr) = @_;
172 my $tick = unpack('N', $dgram);
173  
174 $self->midi_out->clock if $self->send_clock; # keep time!
175  
176 $cb->($self, $tick); # generic callback to do something cool
177 },
178 );
179  
180 $self->loop->add($async_sock);
181  
182 say 'Worker listening on UDP ', $self->port_num,
183 " -> MIDI port matching '", $self->port_name, "'"
184 if $self->verbose;
185  
186 $self->loop->run;
187 }
188  
189 1;
190  
191 __END__
↑ Summary