Debian Codegraph @

examples/armid

raw
1 #! /usr/bin/perl
2 #########################################################################
3 # This Perl script is Copyright (c) 2011, Peter J Billam #
4 # www.pjb.com.au #
5 # #
6 # This script is free software; you can redistribute it and/or #
7 # modify it under the same terms as Perl itself. #
8 #########################################################################
9 my $Version = '1.2';
10 my $VersionDate = '03nov2011';
11 use open ':locale';
12
13 my $InputPort = '';
14 while ($ARGV[$[] =~ /^-([a-z])/) {
15 if ($1 eq 'v') { shift;
16 my $n = $0; $n =~ s{^.*/([^/]+)$}{$1};
17 print "$n version $Version $VersionDate\n";
18 exit 0;
19 } elsif ($1 eq 'p' or $1 eq 'i') { shift; $InputPort = shift;
20 } else {
21 print "usage:\n"; my $synopsis = 0;
22 while (<DATA>) {
23 if (/^=head1 SYNOPSIS/) { $synopsis = 1; next; }
24 if ($synopsis && /^=head1/) { last; }
25 if ($synopsis && /\S/) { s/^\s*/ /; print $_; next; }
26 }
27 exit 0;
28 }
29 }
30 if (!$InputPort) { $InputPort = $ENV{'ALSA_INPUT_PORTS'}; }
31 if (!$InputPort) { die "-p not specified and ALSA_INPUT_PORTS not set\n"; }
32
33 use Data::Dumper; $Data::Dumper::Indent = 0; $Data::Dumper::Sortkeys = 1;
34 eval 'require MIDI'; if ($@) {
35 die "you'll need to install the MIDI-Perl module from www.cpan.org\n";
36 }
37 eval 'require MIDI::ALSA'; if ($@) {
38 die "you'll need to install the MIDI::ALSA module from www.cpan.org\n";
39 }
40
41 MIDI::ALSA::client("$0 MIDI::ALSA client", 1, 0, 1) or die "client failed";
42 foreach my $cl_po (split /,/, $InputPort) {
43 if (! MIDI::ALSA::connectfrom( 0, $cl_po )) {
44 die "can't connect from ALSA client $cl_po\n";
45 }
46 }
47 MIDI::ALSA::start() or die "start failed";
48
49 my @score = (1000, [['set_tempo',0,1000000],]);
50 sub discon {
51 warn " Writing to file $ARGV[$[]\n";
52 score2file($ARGV[$[], @score);
53 exit 0;
54 };
55 $SIG{INT} = \&discon;
56 $SIG{QUIT} = \&discon;
57
58 while (1) {
59 # must exit the loop on SIGINT ...
60 @alsaevent = MIDI::ALSA::input();
61 if (!@alsaevent) { warn "interrupted\n"; last; }
62 if ($alsaevent[0]==MIDI::ALSA::SND_SEQ_EVENT_PORT_UNSUBSCRIBED()) {
63 warn "unsubscribed\n"; last;
64 }
65 my @scoreevent = MIDI::ALSA::alsa2scoreevent(@alsaevent);
66 if (@scoreevent) { push @{$score[1]}, \@scoreevent; }
67 }
68 warn " Writing to file $ARGV[$[]\n";
69 score2file($ARGV[$[], @score);
70 exit 0;
71
72 #--------------------- Encoding stuff from midisox_pl -------------------
73
74 sub opus2file {
75 my ($filename, @opus) = @_;
76 # print "opus2file: filename=$filename opus = ", Dumper(@opus);
77 my $format = 1;
78 if (2 == @opus) { $format = 0; }
79 my $cpan_opus = MIDI::Opus->new(
80 {'format'=>$format, 'ticks' => 1000, 'tracks' => []});
81 # my $tracks_r = $cpan_opus->tracks_r();
82 my @list_of_tracks = ();
83 my $itrack = $[+1;
84 while ($itrack <= $#opus) {
85 push @list_of_tracks,
86 MIDI::Track->new({ 'type' => 'MTrk', 'events' => $opus[$itrack]});
87 $itrack += 1;
88 }
89 # print "opus2file: list_of_tracks = ", Dumper(@list_of_tracks);
90 $cpan_opus->tracks(@list_of_tracks);
91 # $cpan_opus->dump({'dump_tracks'=>1});
92 if ($filename eq '-') {
93 $cpan_opus->write_to_file( '>-' );
94 # $cpan_opus->write_to_handle({'to_handle' => *STDOUT{IO}});
95 } elsif ($filename eq '-d') {
96 $PID = fork;
97 if (! $PID) {
98 if (!open(P, '| aplaymidi -')) { die "can't run aplaymidi: $!\n"; }
99 $cpan_opus->write_to_handle( *P{IO}, {} );
100 close P;
101 exit 0;
102 }
103 } else {
104 $cpan_opus->write_to_file($filename);
105 }
106 }
107
108 sub score2opus {
109 if (2 > @_) { return (1000, []); }
110 my ($ticks, @tracks) = @_;
111 # print "score2opus: tracks is ", Dumper(@tracks);
112 my @opus = ($ticks,);
113 my $itrack = $[;
114 while ($itrack <= $#tracks) {
115 # MIDI::Score::dump_score( $_[$itrack] );
116 # push @opus, MIDI::Score::score_r_to_events_r($_[$itrack]);
117 my %time2events = ();
118 foreach my $scoreevent_ref (@{$tracks[$itrack]}) {
119 my @scoreevent = @{$scoreevent_ref};
120 # print "score2opus: scoreevent = @scoreevent\n";
121 if ($scoreevent[0] eq 'note') {
122 my @note_on_event = ('note_on',$scoreevent[1],
123 $scoreevent[3],$scoreevent[4],$scoreevent[5]);
124 my @note_off_event = ('note_off',$scoreevent[1]+$scoreevent[2],
125 $scoreevent[3],$scoreevent[4],$scoreevent[5]);
126 if ($time2events{$note_on_event[1]}) {
127 push @{$time2events{$note_on_event[1]}}, \@note_on_event;
128 } else {
129 @{$time2events{$note_on_event[1]}} = (\@note_on_event,);
130 }
131 if ($time2events{$note_off_event[1]}) {
132 push @{$time2events{$note_off_event[1]}}, \@note_off_event;
133 } else {
134 @{$time2events{$note_off_event[1]}} = (\@note_off_event,);
135 }
136 } elsif ($time2events{$scoreevent[1]}) {
137 push @{$time2events{$scoreevent[1]}}, \@scoreevent;
138 } else {
139 @{$time2events{$scoreevent[1]}} = (\@scoreevent,);
140 }
141 }
142
143 my @sorted_events = (); # list of event_refs sorted by time
144 for my $time (sort {$a <=> $b} keys %time2events) {
145 push @sorted_events, @{$time2events{$time}};
146 }
147
148 my $abs_time = 0;
149 for my $event_ref (@sorted_events) { # convert abs times => delta times
150 my $delta_time = ${$event_ref}[1] - $abs_time;
151 $abs_time = ${$event_ref}[1];
152 ${$event_ref}[1] = $delta_time;
153 }
154 push @opus, \@sorted_events;
155 $itrack += 1;
156 }
157 return (@opus);
158 }
159
160 sub score2file { my ($filename, @score) = @_;
161 my @opus = score2opus(@score);
162 return opus2file($filename, @opus);
163 }
164
165
166 __END__
167
168 =pod
169
170 =head1 NAME
171
172 armid - rough arecordmidi work-alike, to demonstrate MIDI::ALSA
173
174 =head1 SYNOPSIS
175
176 armid -p 28 out.mid
177
178 =head1 DESCRIPTION
179
180 This script is a rough arecordmidi work-alike, to demonstrate MIDI::ALSA
181
182 =head1 OPTIONS
183
184 =over 3
185
186 =item I<-p 28:0,32:0>
187
188 Records from the ALSA clients 28 and 32;
189 the default is the envronment variable ALSA_INPUT_PORTS
190
191 =item I<-v>
192
193 Prints version number.
194
195 =back
196
197 =head1 CHANGES
198
199 20111103 1.2 use the new MIDI-ALSA 1.11 to handle portnames
200 20111031 1.1 connects from multiple (comma-separated) ports
201 20110310 1.0 first working version
202
203 =head1 AUTHOR
204
205 Peter J Billam http://www.pjb.com.au/comp/contact.html
206
207 =head1 CREDITS
208
209 Based on
210
211 =head1 SEE ALSO
212
213 http://www.pjb.com.au/
214 perl(1).
215
216 =cut
217