Skip to content

Instantly share code, notes, and snippets.

@ology
Created March 26, 2026 17:42
Show Gist options
  • Select an option

  • Save ology/97da38f3777c356be8bb293e6864e587 to your computer and use it in GitHub Desktop.

Select an option

Save ology/97da38f3777c356be8bb293e6864e587 to your computer and use it in GitHub Desktop.
How to add an independent process (like sleep(2); say "Hello";) to this code?
#!/usr/bin/env perl
# Clock an external MIDI device, like a drum machine or sequencer.
# Example: perl clock-gen-async.pl usb 90
use v5.36;
use IO::Async::Loop ();
use IO::Async::Timer::Periodic ();
use MIDI::RtMidi::FFI::Device ();
my $name = shift || 'usb'; # MIDI sequencer device
my $bpm = shift || 120;
my $interval = 60 / $bpm / 24; # seconds / bpm / clocks-per-beat
my $midi_out = RtMidiOut->new;
$midi_out->open_virtual_port('RtMidiOut');
$midi_out->open_port_by_name(qr/\Q$name/i);
$midi_out->start;
$SIG{INT} = sub {
say "\nStop";
$midi_out->stop;
exit;
};
my $loop = IO::Async::Loop->new;
my $timer = IO::Async::Timer::Periodic->new(
interval => $interval,
on_tick => sub { $midi_out->clock },
);
$timer->start;
$loop->add($timer);
$loop->run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment