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
« FFI.pm ↑ Summary
1 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 6 ▶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  
8 6 ▶use MIDI::RtMidi::FFI ':all';
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use MIDI::Event;
10 use Carp;
11  
12 our $VERSION = '0.04';
13  
14 my $rtmidi_api_names = {
15 unspecified => [ "Unknown", RTMIDI_API_UNSPECIFIED ],
16 core => [ "CoreMidi", RTMIDI_API_MACOSX_CORE ],
17 alsa => [ "ALSA", RTMIDI_API_LINUX_ALSA ],
18 jack => [ "Jack", RTMIDI_API_UNIX_JACK ],
19 winmm => [ "Windows MultiMedia", RTMIDI_API_WINDOWS_MM ],
20 dummy => [ "Dummy", RTMIDI_API_RTMIDI_DUMMY ]
21 };
22  
23  
24 sub new {
25 my ( $class, @args ) = @_;
26 my $self = ( @args == 1 and ref $args[0] eq 'HASH' )
27 ? bless( $args[0], $class )
28 : bless( { @args }, $class );
29 $self->{type} //= 'out';
30 $self->{ignore_sysex} //= 1;
31 $self->{ignore_timing} //= 1;
32 $self->{ignore_sensing} //= 1;
33 croak "Unknown type : $self->{type}" unless $self->{type} eq 'in' || $self->{type} eq 'out';
34 $self->_create_device;
35 return $self;
36 }
37  
38  
39 sub ok { $_[0]->{device}->ok }
40 sub msg { $_[0]->{device}->msg }
41 sub data { $_[0]->{device}->data }
42 sub ptr { $_[0]->{device}->ptr }
43  
44  
45 sub open_virtual_port {
46 my ( $self, $port_name ) = @_;
47 rtmidi_open_virtual_port( $self->{device}, $port_name );
48 }
49  
50  
51 sub open_port {
52 my ( $self, $port_number, $port_name ) = @_;
53 $self->{port_name} = $port_name;
54 rtmidi_open_port( $self->{device}, $port_number, $port_name );
55 }
56  
57  
58 sub get_ports_by_name {
59 my ( $self, $name ) = @_;
60 my @ports = grep {
61 my $pn = $self->get_port_name( $_ );
62 ref $name eq 'Regexp'
63 ? $pn =~ $name
64 : $pn eq $name
65 } 0..($self->get_port_count-1);
66 return @ports;
67 }
68  
69  
70 sub open_port_by_name {
71 my ( $self, $name, $portname ) = @_;
72 $portname //= $self->{type} . '-' . time();
73 if ( ref $name eq 'ARRAY' ) {
74 for ( @{ $name } ) {
75 return if $self->open_port_by_name( $_ );
76 }
77 }
78 else {
79 my @ports = $self->get_ports_by_name( $name );
80 return 0 unless @ports;
81 return !$self->open_port( $ports[0], $portname );
82 }
83 }
84  
85  
86 sub close_port {
87 my ( $self ) = @_;
88 rtmidi_close_port( $self->{device} );
89 }
90  
91  
92 sub get_port_count {
93 my ( $self ) = @_;
94 rtmidi_get_port_count( $self->{device} );
95 }
96  
97  
98 sub get_port_name {
99 my ( $self, $port_number ) = @_;
100 rtmidi_get_port_name( $self->{device}, $port_number );
101 }
102  
103  
104 sub get_current_api {
105 my ( $self ) = @_;
106 my $api_dispatch = {
107 rtmidi_in_get_current_api => \&rtmidi_in_get_current_api,
108 rtmidi_out_get_current_api => \&rtmidi_out_get_current_api,
109 };
110 my $fn = "rtmidi_$self->{type}_get_current_api";
111 croak "Unknown device type : $self->{type}" unless $api_dispatch->{ $fn };
112 $api_dispatch->{ $fn }->( $self->{device} );
113 }
114  
115  
116 sub set_callback {
117 my ( $self, $cb, $data ) = @_;
118 croak "Unable to set_callback for device type : $self->{type}" unless $self->{type} eq 'in';
119 $self->{callback} = rtmidi_in_set_callback( $self->{device}, $cb, $data );
120 }
121  
122  
123 sub set_callback_decoded {
124 my ( $self, $cb, $data ) = @_;
125 my $event_cb = sub {
126 my ( $ts, $msg, $data ) = @_;
127 my $decoded = $self->decode_message( $msg );
128 $cb->( $ts, $msg, $decoded, $data );
129 };
130 $self->set_callback( $event_cb, $data );
131 }
132  
133 sub cancel_callback {
134 my ( $self ) = @_;
135 return unless $self->{callback};
136 croak "Unable to cancel_callback for device type : $self->{type}" unless $self->{type} eq 'in';
137 delete $self->{callback};
138 rtmidi_in_cancel_callback( $self->{device} );
139 }
140  
141  
142 sub ignore_types {
143 my ( $self, $sysex, $timing, $sensing ) = @_;
144 @{ $self }{ qw/ ignore_sysex ignore_timing ignore_sensing / } = ( $sysex, $timing, $sensing );
145 croak "Unable to ignore_types for device type : $self->{type}" unless $self->{type} eq 'in';
146 rtmidi_in_ignore_types( $self->{device}, $sysex, $timing, $sensing );
147 }
148  
149  
150 sub ignore_sysex {
151 my ( $self, $ignore_sysex ) = @_;
152 $self->{ignore_sysex} = $ignore_sysex;
153 $self->ignore_types( @{ $self }{ qw/ ignore_sysex ignore_timing ignore_sensing / } )
154 }
155  
156  
157 sub ignore_timing {
158 my ( $self, $ignore_timing ) = @_;
159 $self->{ignore_timing} = $ignore_timing;
160 $self->ignore_types( @{ $self }{ qw/ ignore_sysex ignore_timing ignore_sensing / } )
161 }
162  
163  
164 sub ignore_sensing {
165 my ( $self, $ignore_sensing ) = @_;
166 $self->{ignore_sensing} = $ignore_sensing;
167 $self->ignore_types( @{ $self }{ qw/ ignore_sysex ignore_timing ignore_sensing / } )
168 }
169  
170  
171 sub get_message {
172 my ( $self ) = @_;
173 croak "Unable to get_message for device type : $self->{type}" unless $self->{type} eq 'in';
174 rtmidi_in_get_message( $self->{device}, $self->{queue_size_limit} );
175 }
176  
177  
178 sub get_message_decoded {
179 my ( $self ) = @_;
180 $self->decode_message( $self->get_message );
181 }
182  
183  
184 *get_event = \&get_message_decoded;
185  
186  
187 sub decode_message {
188 my ( $self, $msg ) = @_;
189 return unless $msg;
190  
191 # Real-time messages don't have 'dtime', but MIDI::Event expects it:
192 $msg = chr(0) . $msg;
193  
194 my $decoded = MIDI::Event::decode( \$msg )->[0];
195  
196 if ( ref $decoded ne 'ARRAY' ) {
197 warn "Could not decode message " . unpack( 'H*', $msg );
198 return;
199 }
200  
201 # Delete dtime
202 splice( @{ $decoded }, 1, 1 );
203  
204 $decoded->[0] = 'note_off' if ( $decoded->[0] eq 'note_on' && $decoded->[-1] == 0 );
205 return wantarray
206 ? @{ $decoded }
207 : $decoded;
208 }
209  
210  
211 sub send_message {
212 my ( $self, $msg ) = @_;
213 croak "Unable to send_message for device type : $self->{type}" unless $self->{type} eq 'out';
214 rtmidi_out_send_message( $self->{device}, $msg );
215 }
216  
217  
218 sub encode_message {
219 my ( $self, @event ) = @_;
220  
221 $event[0] = 'sysex_f0' if $event[0] eq 'sysex';
222  
223 # Insert 0 dtime
224 splice @event, 1, 0, 0;
225  
226 my $msg = MIDI::Event::encode( [[@event]], { never_add_eot => 1 } );
227  
228 # Strip dtime before send
229 substr( $$msg, 0, 1 ) = '';
230  
231 # Terminate SysEx messages (hax hax hax, probably fragile...)
232 my $first = substr( $$msg, 0, 1 );
233 if ( ( $first eq chr( 0xf0 ) || $first eq chr( 0xf7 ) ) && substr( $$msg, -1, 1 ) ne chr( 0xf7 ) ) {
234 $$msg .= chr( 0xf7 );
235 }
236  
237 return $$msg;
238 }
239  
240  
241 sub send_message_encoded {
242 my ( $self, @event ) = @_;
243 $self->send_message( $self->encode_message( @event ) );
244 }
245  
246  
247 *send_event = \&send_message_encoded;
248  
249 sub port_name { $_[0]->{port_name}; }
250 sub name { $_[0]->{name}; }
251  
252 sub _create_device {
253 my ( $self ) = @_;
254 my $create_dispatch = {
255 rtmidi_out_create_default => \&rtmidi_out_create_default,
256 rtmidi_out_create => \&rtmidi_out_create,
257 rtmidi_in_create_default => \&rtmidi_in_create_default,
258 rtmidi_in_create => \&rtmidi_in_create,
259 };
260 my $fn = "rtmidi_$self->{type}_create";
261 $fn = "${fn}_default" if !$self->{api} && !$self->{name} && !$self->{queue_size_limit};
262 croak "Unknown type : $self->{type}" unless $create_dispatch->{ $fn };
263  
264 $self->{queue_size_limit} //= $self->{bufsize} //= 1024;
265 my $api_by_name;
266 $api_by_name = $rtmidi_api_names->{ $self->{api_str} } if $self->{api_str};
267 $self->{api} //= $api_by_name->[1] if $api_by_name;
268 $self->{api} //= $rtmidi_api_names->{ unspecified }->[1];
269 $self->{device} = $create_dispatch->{ $fn }->( $self->{api}, $self->{name}, $self->{queue_size_limit} );
270 $self->{type} eq 'in' && $self->ignore_types(
271 $self->{ignore_sysex},
272 $self->{ignore_timing},
273 $self->{ignore_sensing},
274 );
275 }
276  
277 my $free_dispatch = {
278 in => \&rtmidi_in_free,
279 out => \&rtmidi_out_free
280 };
281 sub DESTROY {
282 my ( $self ) = @_;
283 my $fn = $free_dispatch->{ $self->{type} };
284 # croak "Unable to free type : $self->{type}" unless $fn;
285 # There is an extant issue around the Perl object lifecycle and C++ object lifecycle.
286 # If we free the RtMidiPtr here, a double-free error may occur on process exit.
287 # For now, cancel the callback and close the port, then trust the process ...
288 $self->cancel_callback;
289 $self->close_port;
290 # $fn->( $self->{device} );
291 }
292  
293 {
294 package RtMidiIn;
295 use strict; use warnings;
296 sub new {
297 shift;
298 MIDI::RtMidi::FFI::Device->new( @_, type => 'in' );
299 }
300 }
301  
302 {
303 package RtMidiOut;
304 use strict; use warnings;
305 sub new {
306 shift;
307 MIDI::RtMidi::FFI::Device->new( @_, type => 'out' );
308 }
309 }
310  
311 1;
312  
313 __END__
« FFI.pm ↑ Summary