NAME

MIDI::RtController - Control your MIDI controller

VERSION

version 0.0701

SYNOPSIS

use MIDI::RtController ();

my $rtc = MIDI::RtController->new(
  input  => 'input device 1',
  output => 'output device',
);

sub filter_notes {
  my ($note) = @_;
  return $note, $note + 7, $note + 12;
}
sub filter_tone {
  my ($midi_port, $delta_time, $event) = @_; # 3 required filter arguments
  my ($ev, $channel, $note, $vel) = $event->@*;
  my @notes = filter_notes($note);
  $rtc->send_it([ $ev, $channel, $_, $vel ]) for @notes;
  return 0;
}

# respond to specific events:
$rtc->add_filter('filter_tone', $_, \&filter_tone)
  for qw(note_on note_off);
# Or:
$rtc->add_filter('filter_tone', [qw(note_on note_off)], \&filter_tone);

# respond to all events:
$rtc->add_filter(
  'echo',
  all => sub {
    my ($port, $dt, $event) = @_;
    print "port: $port, delta-time: $dt, ev: ", join(', ', @$event), "\n"
      unless $event->[0] eq 'clock';
    return 0;
  }
);

# add stuff to the $rtc->loop...
$rtc->run;

# you can also use multiple input sources simultaneously:
my $rtc2 = MIDI::RtController->new(
  input    => 'input device 2',
  loop     => $rtc->loop,
  midi_out => $rtc->midi_out,
);
$rtc2->run;

DESCRIPTION

MIDI::RtController allows you to control your MIDI controller using plug-in filters.

ATTRIBUTES

verbose

$verbose = $rtc->verbose;

Show progress.

input

$input = $rtc->input;

Return the MIDI input port.

output

$output = $rtc->output;

Return the MIDI output port.

loop

$loop = $rtc->loop;

Return the IO::Async::Loop.

filters

$filters = $rtc->filters;

Return or set the filters.

midi_out

$midi_out = $rtc->midi_out;

Return the midi_out port.

METHODS

new

$rtc = MIDI::RtController->new(%attributes);

Create a new MIDI::RtController object given the above attributes.

add_filter

$rtc->add_filter($name, $event_type, $action);

Add a named filter, defined by the CODE reference action for an event_type like note_on or note_off. An ARRAY reference of event types like: [qw(note_on note_off)] may also be given.

The special event type all may also be used to refer to any controller event (e.g. note_on, control_change, pitch_wheel_change, etc.).

send_it

$rtc->send_it($event);

Send a MIDI event to the output port, where the MIDI event is an ARRAY reference like, ['note_on', 0, 40, 107] or ['control_change', 0, 1, 24], etc.

delay_send

$rtc->delay_send($delay_time, $event);

Send a MIDI event to the output port when the delay_time (in seconds) expires.

run

$rtc->run;

Run the asynchronous loop!

UTILITIES

open_controllers

$controllers = MIDI::RtController::open_controllers(
  $input_names, $output_name, $verbose
);

Return a hash reference of MIDI::RtController instances, keyed by each input (given by a comma-separated string of MIDI input_names controller devices).

The output_name is used for the MIDI output device for each instance. The verbose Boolean flag is passed to the instances.

THANK YOU

This code would not exist without the help of CPAN's JBARRETT (John Barrett AKA fuzzix).

SEE ALSO

The eg/*.pl example programs!

Future::AsyncAwait

IO::Async::Channel

IO::Async::Loop

IO::Async::Routine

IO::Async::Timer::Countdown

MIDI::RtMidi::FFI::Device

Moo

AUTHOR

Gene Boggs <gene.boggs@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2025 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.