package MIDI::RtMidi::Util;
our $AUTHORITY = 'cpan:GENE';

# ABSTRACT: Handy Utilities for Real-time MIDI

our $VERSION = '0.0100';

use v5.36;
use feature 'try';

use Data::Dumper::Compact qw(ddc);
use MIDI::RtMidi::FFI::Device ();
use Exporter 'import';
our @EXPORT = qw(
    out_port
    halt
);

no warnings 'experimental::try';



sub out_port ($name) {
    my $midi_out = RtMidiOut->new;
    say ddc $midi_out;
    try { $midi_out->open_virtual_port('RtMidiOut') } # needed for mac
    catch ($e) {
        # warn 'Not a Mac';
    }
    try { $midi_out->open_port_by_name(qr/\Q$name/i) }
    catch ($e) { die "Can't open MIDI port: $name\n" }
    return $midi_out;
}


sub halt ($midi_out) {
    try {
        $midi_out->stop;
        $midi_out->panic;
    }
    catch ($e) {
        warn "Can't halt the MIDI out devices: $e\n";
    }
    exit;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

MIDI::RtMidi::Util - Handy Utilities for Real-time MIDI

=head1 VERSION

version 0.0100

=head1 SYNOPSIS

  use MIDI::RtMidi::Util qw(out_port);

  my $out_port = out_port('usb');

=head1 DESCRIPTION

C<MIDI::RtMidi::Util> is a junk drawer for Real-time MIDI utilities.

=head1 FUNCTIONS

=head2 out_port

  $out_port = out_port($name);

Open and return a L<MIDI::RtMidi::FFI::Device> C<RtMidiOut> device.

=head2 halt

  halt();

Stop and close an open C<RtMidiOut> device.

=head1 SEE ALSO

L<MIDI::RtMidi::FFI::Device>

=head1 AUTHOR

Gene Boggs <gene.boggs@gmail.com>

=head1 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.

=cut
