FFI/Device.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary FFI.pm »
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package MIDI::RtMidi::FFI::Device;
4  
5 # ABSTRACT: OO interface for MIDI::RtMidi::FFI
6  
7 =encoding UTF-8
8  
9 =head1 NAME
10  
11 MIDI::RtMidi::FFI::Device - OO interface for L<MIDI::RtMidi::FFI>
12  
13 =head1 VERSION
14  
15 version 0.03
16  
17 =head1 SYNOPSIS
18  
19 use MIDI::RtMidi::FFI::Device;
20
21 my $device = MIDI::RtMidi::FFI::Device->new;
22 $device->open_virtual_port( 'perl-rtmidi' );
23 $device->send_event( note_on => 0x40, 0x5a );
24 sleep 1;
25 $device->send_event( note_off => 0x40, 0x5a );
26  
27 =head1 DESCRIPTION
28  
29 MIDI::RtMidi::FFI::Device is an OO interface for L<MIDI::RtMidi::FFI> to help
30 you manage devices, ports and MIDI events.
31  
32 =cut
33  
34 4 ▶use MIDI::RtMidi::FFI ':all';
Subroutine BEGIN: called CC=1 SCAR=2.6
35 use MIDI::Event;
36 use Carp;
37  
38 our $VERSION = $MIDI::RtMidi::FFI::VERSION;
39  
40 my $rtmidi_api_names = {
41 unspecified => [ "Unknown", RTMIDI_API_UNSPECIFIED ],
42 core => [ "CoreMidi", RTMIDI_API_MACOSX_CORE ],
43 alsa => [ "ALSA", RTMIDI_API_LINUX_ALSA ],
44 jack => [ "Jack", RTMIDI_API_UNIX_JACK ],
45 winmm => [ "Windows MultiMedia", RTMIDI_API_WINDOWS_MM ],
46 dummy => [ "Dummy", RTMIDI_API_RTMIDI_DUMMY ]
47 };
48  
49 my $music_events = +{ map { $_ => 1 } qw/
50 note_off note_on key_after_touch
51 control_change patch_change
52 channel_after_touch pitch_wheel_change
53 / };
54  
55 =head1 METHODS
56  
57 =head2 new
58  
59 my $device = MIDI::RtMidi::FFI::Device->new( %attributes );
60  
61 Returns a new MIDI::RtMidi::FFI::Device object. Valid attributes:
62  
63 =over 4
64  
65 =item *
66  
67 B<type> -
68 Device type : 'in' or 'out' (defaults to 'out')
69  
70 =item *
71  
72 B<api> -
73 MIDI API to use. This should be a L<RtMidiApi constant|MIDI::RtMidi::FFI/"RtMidiApi">.
74 By default the device should use the first compiled API available. See search
75 order notes in
76 L<Using Simultaneous Multiple APIs|https://www.music.mcgill.ca/~gary/rtmidi/index.html#multi>
77 on the RtMidi website.
78  
79 =item *
80  
81 B<api_name> -
82 MIDI API to use by name. One of 'alsa', 'jack', 'core', 'winmm' or 'dummy'.
83  
84 =item *
85  
86 B<name> -
87 Device name
88  
89 =item *
90  
91 B<queue_size_limit> -
92 (Type 'in' only) The buffer size to allocate for queueing incoming messages
93 (defaults to 1024)
94  
95 =item *
96  
97 B<bufsize> -
98 (Type 'in' only) An alias for B<queue_size_limit>.
99  
100 =item *
101  
102 B<ignore_sysex> -
103 (Type 'in' only) Ignore incoming SYSEX messages (defaults to true)
104  
105 =item *
106  
107 B<ignore_timing> -
108 (Type 'in' only) Ignore incoming timing messages (defaults to true)
109  
110 =item *
111  
112 B<ignore_sensing> -
113 (Type 'in' only) Ignore incoming active sensing messages (defaults to true)
114  
115 =item *
116  
117 =back
118  
119 =cut
120  
121 sub new {
122 my ( $class, @args ) = @_;
123 my $self = ( @args == 1 and ref $args[0] eq 'HASH' )
124 ? bless( $args[0], $class )
125 : bless( { @args }, $class );
126 $self->{type} //= 'out';
127 $self->{ignore_sysex} //= 1;
128 $self->{ignore_timing} //= 1;
129 $self->{ignore_sensing} //= 1;
130 croak "Unknown type : $self->{type}" unless $self->{type} eq 'in' || $self->{type} eq 'out';
131 $self->_create_device;
132 return $self;
133 }
134  
135 =head2 ok, msg, data, ptr
136  
137 warn $device->msg unless $device->ok;
138  
139 Getters for RtMidiWrapper device struct members
140  
141 =cut
142  
143 sub ok { $_[0]->{device}->ok }
144 sub msg { $_[0]->{device}->msg }
145 sub data { $_[0]->{device}->data }
146 sub ptr { $_[0]->{device}->ptr }
147  
148 =head2 open_virtual_port
149  
150 $device->open_virtual_port( $name );
151  
152 Open a virtual device port.
153  
154 This method will not work on Windows.
155  
156 =cut
157  
158 sub open_virtual_port {
159 my ( $self, $port_name ) = @_;
160 rtmidi_open_virtual_port( $self->{device}, $port_name );
161 }
162  
163 =head2 open_port
164  
165 $device->open_port( $port, $name );
166  
167 Open a port.
168  
169 =cut
170  
171 sub open_port {
172 my ( $self, $port_number, $port_name ) = @_;
173 $self->{port_name} = $port_name;
174 rtmidi_open_port( $self->{device}, $port_number, $port_name );
175 }
176  
177 =head2 get_ports_by_name
178  
179 $device->get_ports_by_name( $name );
180 $device->get_ports_by_name( qr/name/ );
181  
182 Returns a list of ports matching the supplied name criteria.
183  
184 =cut
185  
186 sub get_ports_by_name {
187 my ( $self, $name ) = @_;
188 my @ports = grep {
189 my $pn = $self->get_port_name( $_ );
190 ref $name eq 'Regexp'
191 ? $pn =~ $name
192 : $pn eq $name
193 } 0..($self->get_port_count-1);
194 return @ports;
195 }
196  
197 =head2 open_port_by_name
198  
199 $device->open_port_by_name( $name );
200 $device->open_port_by_name( qr/name/ );
201 $device->open_port_by_name( [ $name, $othername, qr/anothername/ ] );
202  
203 Opens the first port found matching the supplied name criteria.
204  
205 =cut
206  
207 sub open_port_by_name {
208 my ( $self, $name, $portname ) = @_;
209 $portname //= $self->{type} . '-' . time();
210 if ( ref $name eq 'ARRAY' ) {
211 for ( @{ $name } ) {
212 return if $self->open_port_by_name( $_ );
213 }
214 }
215 else {
216 my @ports = $self->get_ports_by_name( $name );
217 return 0 unless @ports;
218 return !$self->open_port( $ports[0], $portname );
219 }
220 }
221  
222 =head2 close_port
223  
224 $device->close_port();
225  
226 Closes the currently open port
227  
228 =cut
229  
230 sub close_port {
231 my ( $self ) = @_;
232 rtmidi_close_port( $self->{device} );
233 }
234  
235 =head2 get_port_count
236  
237 $device->get_port_count();
238  
239 Return the number of available MIDI ports to connect to.
240  
241 =cut
242  
243 sub get_port_count {
244 my ( $self ) = @_;
245 rtmidi_get_port_count( $self->{device} );
246 }
247  
248 =head2 get_port_name
249  
250 $self->get_port_name( $port );
251  
252 Returns the name of the supplied port number.
253  
254 =cut
255  
256 sub get_port_name {
257 my ( $self, $port_number ) = @_;
258 rtmidi_get_port_name( $self->{device}, $port_number );
259 }
260  
261 =head2 get_current_api
262  
263 $self->get_current_api();
264  
265 Returns the MIDI API in use for the device.
266  
267 This is a L<RtMidiApi constant|MIDI::RtMidi::FFI/"RtMidiApi">.
268  
269 =cut
270  
271 sub get_current_api {
272 my ( $self ) = @_;
273 my $api_dispatch = {
274 rtmidi_in_get_current_api => \&rtmidi_in_get_current_api,
275 rtmidi_out_get_current_api => \&rtmidi_out_get_current_api,
276 };
277 my $fn = "rtmidi_$self->{type}_get_current_api";
278 croak "Unknown device type : $self->{type}" unless $api_dispatch->{ $fn };
279 $api_dispatch->{ $fn }->( $self->{device} );
280 }
281  
282 =head2 set_callback 🐉
283  
284 Here be dragons.
285  
286 $device->set_callback( sub {
287 my ( $ts, $msg, $data ) = @_;
288 # handle $msg here
289 }, $data );
290  
291 Type 'in' only. Sets a callback to be executed when an incoming message is
292 received. Your callback receives the timestamp of the event, the message, and
293 optionally some data you set while defining the callback. This data should
294 be a simple scalar string, not a reference or other data structure.
295  
296 In my experience, receiving a message on your device while a callback is in
297 progress results in a crash.
298  
299 Depending on the message rate your application expects, this may be OK.
300  
301 =cut
302  
303 sub set_callback {
304 my ( $self, $cb, $data ) = @_;
305 croak "Unable to set_callback for device type : $self->{type}" unless $self->{type} eq 'in';
306 $self->{callback} = rtmidi_in_set_callback( $self->{device}, $cb, $data );
307 }
308  
309 =head2 cancel_callback
310  
311 $device->cancel_callback();
312  
313 Type 'in' only. Removes the callback from your device.
314  
315 =cut
316  
317 sub cancel_callback {
318 my ( $self ) = @_;
319 croak "Unable to cancel_callback for device type : $self->{type}" unless $self->{type} eq 'in';
320 rtmidi_in_cancel_callback( $self->{device} );
321 }
322  
323 =head2 ignore_types
324  
325 $device->ignore_types( $ignore_sysex, $ignore_timing, $ignore_sensing );
326 $device->ignore_types( (1)x3 );
327  
328 Type 'in' only. Set message types to ignore.
329  
330 =cut
331  
332 sub ignore_types {
333 my ( $self, $sysex, $time, $sense ) = @_;
334 croak "Unable to ignore_types for device type : $self->{type}" unless $self->{type} eq 'in';
335 rtmidi_in_ignore_types( $self->{device}, $sysex, $time, $sense );
336 }
337  
338 =head2 get_message
339  
340 $device->get_message();
341  
342 Type 'in' only. Gets the next message from the queue, if available.
343  
344 =cut
345  
346 sub get_message {
347 my ( $self ) = @_;
348 croak "Unable to get_message for device type : $self->{type}" unless $self->{type} eq 'in';
349 rtmidi_in_get_message( $self->{device}, $self->{queue_size_limit} );
350 }
351  
352 =head2 get_event
353  
354 $device->get_event();
355  
356 Type 'in' only. Gets the next message from the queue, if available, as a decoded L<MIDI::Event>.
357  
358 =cut
359  
360 sub get_event {
361 my ( $self ) = @_;
362 my $msg = $self->get_message;
363 return unless $msg;
364 $msg = "0$msg"; # restore dtime
365 my $decoded = MIDI::Event::decode( \$msg )->[0];
366 if ( ref $decoded ne 'ARRAY' ) {
367 my $hmsg = join '', map { sprintf "%02x", ord $_ } split '', $msg;
368 warn "Could not decode message $hmsg";
369 }
370  
371 my @event = @{ $decoded };
372 my $is_music_event = $music_events->{ $event[0] };
373 splice( @event, 1, 1 ); # dtime
374 splice( @event, 1, 1 ) if $is_music_event; # channel
375  
376 $event[0] = 'note_off' if ( $event[0] eq 'note_on' && $event[-1] == 0 );
377 return wantarray
378 ? @event
379 : \@event;
380 }
381  
382 =head2 send_message
383  
384 $device->send_message( $msg );
385  
386 Type 'out' only. Sends a message to the open port.
387  
388 =cut
389  
390 sub send_message {
391 my ( $self, $msg ) = @_;
392 croak "Unable to send_message for device type : $self->{type}" unless $self->{type} eq 'out';
393 rtmidi_out_send_message( $self->{device}, $msg );
394 }
395  
396 =head2 send_event
397  
398 $device->send_event( @event );
399 $device->send_event( note_on => 0x40, 0x5a );
400  
401 Type 'out' only. Sends a L<MIDI::Event> encoded message to the open port.
402  
403 NOTE: The dtime and channel values should be omitted from the message.
404  
405 =cut
406  
407 sub send_event {
408 my ( $self, @event ) = @_;
409 my $is_music_event = $music_events->{ $event[0] };
410 splice @event, 1, 0, 0; # dtime
411 splice @event, 1, 0, 0 if $is_music_event; # channel
412 my $msg = MIDI::Event::encode( [[@event]], { never_add_eot => 1 } );
413 substr( $$msg, 0, 1 ) = ''; # snip dtime
414 $self->send_message( $$msg );
415 }
416  
417 sub port_name { $_[0]->{port_name}; }
418 sub name { $_[0]->{name}; }
419  
420 sub _create_device {
421 my ( $self ) = @_;
422 my $create_dispatch = {
423 rtmidi_out_create_default => \&rtmidi_out_create_default,
424 rtmidi_out_create => \&rtmidi_out_create,
425 rtmidi_in_create_default => \&rtmidi_in_create_default,
426 rtmidi_in_create => \&rtmidi_in_create,
427 };
428 my $fn = "rtmidi_$self->{type}_create";
429 $fn = "${fn}_default" if !$self->{api} && !$self->{name} && !$self->{queue_size_limit};
430 croak "Unknown type : $self->{type}" unless $create_dispatch->{ $fn };
431  
432 $self->{queue_size_limit} //= $self->{bufsize} //= 1024;
433 my $api_by_name;
434 $api_by_name = $rtmidi_api_names->{ $self->{api_str} } if $self->{api_str};
435 $self->{api} //= $api_by_name->[1] if $api_by_name;
436 $self->{api} //= $rtmidi_api_names->{ unspecified }->[1];
437 $self->{device} = $create_dispatch->{ $fn }->( $self->{api}, $self->{name}, $self->{queue_size_limit} );
438 $self->{type} eq 'in' && $self->ignore_types(
439 $self->{ignore_sysex},
440 $self->{ignore_timing},
441 $self->{ignore_sensing},
442 );
443 }
444  
445 my $free_dispatch = {
446 in => \&rtmidi_in_free,
447 out => \&rtmidi_out_free
448 };
449 sub DESTROY {
450 my ( $self ) = @_;
451 my $fn = $free_dispatch->{ $self->{type} };
452 croak "Unable to free type : $self->{type}" unless $fn;
453 $self->close_port;
454 delete $self->{callback};
455 $fn->( delete $self->{device} );
456 }
457  
458 1;
459  
460 __END__
↑ Summary FFI.pm »