-
-
Save ology/f70dd307b9fc49b280c9aa22346b29aa to your computer and use it in GitHub Desktop.
Can't puzzle out how to play a note
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use v5.36; | |
use Data::Dumper::Compact qw(ddc); | |
use IO::Async::Timer::Periodic; | |
use IO::Async::Routine; | |
use IO::Async::Channel; | |
use IO::Async::Loop; | |
use Future::AsyncAwait; | |
use MIDI::RtMidi::FFI::Device; | |
use Time::HiRes qw/ gettimeofday tv_interval /; | |
my $port = shift || 'tempopad'; | |
my $loop = IO::Async::Loop->new; | |
my $midi_ch = IO::Async::Channel->new; | |
my $midi_rtn = IO::Async::Routine->new( | |
channels_out => [ $midi_ch ], | |
code => sub { | |
my $midi_in = MIDI::RtMidi::FFI::Device->new( type => 'in' ); | |
$midi_in->open_port_by_name(qr/\Q$port/i); | |
$midi_in->set_callback_decoded( | |
sub($ts, $msg, $data = undef) { | |
# my $t0 = [ gettimeofday ]; | |
$midi_ch->send($msg); | |
} | |
# sub { print join("\n", ($_[0], $_[2]->@*)) } | |
); | |
sleep; | |
} | |
); | |
$loop->add( $midi_rtn ); | |
my $midi_out = RtMidiOut->new; | |
$midi_out->open_virtual_port('foo'); | |
$midi_out->open_port_by_name(qr/fluid/i); | |
$SIG{TERM} = sub { $midi_rtn->kill('TERM') }; | |
async sub process_midi_events { | |
while ( my $event = await $midi_ch->recv ) { | |
say ddc $event; | |
# say "recv took " . tv_interval( $event->[0] ) . "s"; | |
# say "ts " . $event->[1] . "s"; | |
# say unpack 'H*', $event->[2]; | |
# $midi_out->note_on($message->[1], $note, $message->[3]); | |
} | |
} | |
my $tick = 0; | |
$loop->add( IO::Async::Timer::Periodic->new( | |
interval => 1, | |
on_tick => sub { say "Tick " . $tick++; }, | |
)->start ); | |
$loop->await( process_midi_events ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment