| 1 |
#! /usr/bin/perl |
| 2 |
######################################################################### |
| 3 |
# This Perl script is Copyright (c) 2011, Peter J Billam # |
| 4 |
# c/o P J B Computing, GPO Box 669, Hobart TAS 7001, Australia # |
| 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 |
# Simulates a metronome, on real-time MIDI, as an ALSA client |
| 10 |
|
| 11 |
use Term::ReadKey; |
| 12 |
use Time::HiRes; |
| 13 |
use bytes; |
| 14 |
|
| 15 |
my $Version = '1.4'; # use the new MIDI-ALSA 1.11 to handle portnames |
| 16 |
my $VersionDate = '03nov2011'; |
| 17 |
my $Tempo = 120; # beats per minute |
| 18 |
my $BarLength = 0; # beats per bar |
| 19 |
my $Volume = 100; # MIDI velocity 0..127 |
| 20 |
my $Debug = 0; |
| 21 |
my $Quiet = 0; # no display; for use in background, and in scripts |
| 22 |
my $Paused = 0; |
| 23 |
my $OutputPort = q{}; |
| 24 |
# vt100 globals |
| 25 |
my $CursorRow = 5; |
| 26 |
my $Irow = 1; |
| 27 |
my $Icol = 1; |
| 28 |
my $MidCol = 32; |
| 29 |
|
| 30 |
eval 'require MIDI::ALSA'; if ($@) { |
| 31 |
die "you'll need to install the MIDI-ALSA module from www.cpan.org\n"; |
| 32 |
} |
| 33 |
|
| 34 |
# check format of options args... |
| 35 |
while ($ARGV[$[] =~ /^-(\w)/) { |
| 36 |
if ($1 eq 'V') { shift; |
| 37 |
my $n = $0; $n =~ s{^.*/([^/]+)$}{$1}; |
| 38 |
print "$n version $Version $VersionDate\n"; |
| 39 |
exit 0; |
| 40 |
} elsif ($1 eq 't') { shift; |
| 41 |
my $a = shift; if ($a !~ /^\d[\d.]*$/) { die "bad -t arg: $a\n"; } |
| 42 |
$Tempo = 0 + $a; |
| 43 |
} elsif ($1 eq 'b') { shift; |
| 44 |
my $a = shift; if ($a !~ /^\d+$/) { die "bad -b arg: $a\n"; } |
| 45 |
$BarLength = 0 + $a; |
| 46 |
} elsif ($1 eq 'v') { shift; |
| 47 |
my $a = shift; if ($a !~ /^\d+$/) { die "bad -v arg: $a\n"; } |
| 48 |
$Volume = 0 + $a; |
| 49 |
if ($Volume < 1) { $Volume = 1; |
| 50 |
} elsif ($Volume > 127) { $Volume = 127; |
| 51 |
} |
| 52 |
} elsif ($1 eq 'o') { shift; $OutputPort = shift; |
| 53 |
} elsif ($1 eq 'p') { shift; $Paused = 1; |
| 54 |
} elsif ($1 eq 'Q') { shift; $Quiet = 1; |
| 55 |
} elsif ($1 eq 'D') { shift; $Debug = 1; |
| 56 |
} else { |
| 57 |
my $n = $0; $n =~ s#^.*/([^/]+)$#$1#; |
| 58 |
print "usage:\n"; |
| 59 |
my $synopsis = 0; |
| 60 |
while (<DATA>) { |
| 61 |
if (/^=head1 SYNOPSIS/) { push @Synopsis,$_; $synopsis=1; next; } |
| 62 |
if ($synopsis && /^=head1/) { last; } |
| 63 |
if ($synopsis) { print $_; next; } |
| 64 |
} |
| 65 |
exit 1; |
| 66 |
} |
| 67 |
} |
| 68 |
if ($Quiet) { $Paused = 0; } |
| 69 |
|
| 70 |
my $RealTimeMode = 1; # an anacronism from midiecho; we're always RealTime. |
| 71 |
if ($RealTimeMode) { |
| 72 |
eval 'require MIDI::ALSA'; if ($@) { |
| 73 |
die "you need to install the MIDI::ALSA module from www.cpan.org\n"; |
| 74 |
} |
| 75 |
|
| 76 |
#if ($OutputPort =~ /^$|^\d+(:\d)?(,\d+(:\d)?)*$/) { |
| 77 |
#} else { die "bad -o arg: $OutputPort\n"; |
| 78 |
#} |
| 79 |
if (! MIDI::ALSA::client( "midiclick pid=$$", 0, 1, 1 )) { |
| 80 |
die "can't create the MIDI::ALSA::client\n"; |
| 81 |
} |
| 82 |
|
| 83 |
if (!$OutputPort) { $OutputPort = $ENV{'ALSA_OUTPUT_PORTS'}; } |
| 84 |
if (!$OutputPort) { |
| 85 |
warn "OutputPort not specified and ALSA_OUTPUT_PORTS not set\n"; |
| 86 |
} |
| 87 |
foreach my $cl_po (split /,/, $OutputPort) { # 3.6 |
| 88 |
#$cl_po =~ /^(\d+):?(\d*)$/; |
| 89 |
#my $cl = $1; my $po = $2 or 0; |
| 90 |
#if ($cl == MIDI::ALSA::id()) { |
| 91 |
# die "can't connect to $cl_po, which is myself\n"; |
| 92 |
#} |
| 93 |
if (! MIDI::ALSA::connectto( 1, $cl_po )) { # 1.4 |
| 94 |
die "can't connect to ALSA client $cl_po\n"; |
| 95 |
} |
| 96 |
} |
| 97 |
if (! MIDI::ALSA::start()) { |
| 98 |
die "can't start the queue of the ALSA client\n"; |
| 99 |
} |
| 100 |
# system "aconnect -oil"; |
| 101 |
display_alsa(); display_keystrokes(); display_tempo(); |
| 102 |
display_paused(); display_volume(); |
| 103 |
|
| 104 |
# The child responds to keystrokes and the parent emits the alsaevents |
| 105 |
if (! $Quiet) { # 3.1 |
| 106 |
my $parent_pid = $$; |
| 107 |
my $child_pid = open(CHILD_STDOUT, "-|"); |
| 108 |
sub handle_child_output { |
| 109 |
my $cmd = <CHILD_STDOUT>; |
| 110 |
eval $cmd; if ($@) { warn "can't eval $cmd $@\n"; } |
| 111 |
} |
| 112 |
if (! $child_pid) { # The child does all the UI |
| 113 |
while (1) { |
| 114 |
ReadMode(4, STDIN); |
| 115 |
my $c = ReadKey(0, STDIN); |
| 116 |
if ($c =~ /^\e$/) { # reduce an escape sequence to just 1 char |
| 117 |
$c = ReadKey(0, STDIN); |
| 118 |
if ($c eq '[') { |
| 119 |
$c = ReadKey(0, STDIN); |
| 120 |
if ($c =~ /^\d$/) { # e.g. Delete; throw away the ~ |
| 121 |
my $tilde = ReadKey(0, STDIN); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
if ($c =~ /^q$/i) { |
| 126 |
gotoxy(1, $CursorRow); display_keystrokes('quit'); |
| 127 |
ReadMode(0, STDIN); |
| 128 |
print STDOUT "wait; exit;\n"; kill 'HUP', $parent_pid; |
| 129 |
exit; |
| 130 |
} |
| 131 |
if ($c eq 't') { |
| 132 |
my $ch = get_int('tempo (beats per minute) ?'); |
| 133 |
if (defined $ch) { |
| 134 |
$Tempo = $ch; |
| 135 |
print STDOUT "\$Tempo = $ch;\n"; |
| 136 |
kill 'HUP', $parent_pid; |
| 137 |
} |
| 138 |
display_tempo(); display_keystrokes(); |
| 139 |
} elsif ($c eq 'b') { |
| 140 |
my $d = get_int('bar length (in beats) ?'); |
| 141 |
if (defined $d) { |
| 142 |
$BarLength = $d; |
| 143 |
print STDOUT "\$BarLength = $d;\n"; |
| 144 |
kill 'HUP', $parent_pid; |
| 145 |
} |
| 146 |
display_tempo(); display_keystrokes(); |
| 147 |
} elsif ($c eq 'v') { |
| 148 |
my $d = get_int('volume ?'); |
| 149 |
if (defined $d) { |
| 150 |
$Volume = $d; |
| 151 |
print STDOUT "\$Volume = $d;\n"; |
| 152 |
kill 'HUP', $parent_pid; |
| 153 |
} |
| 154 |
display_volume(); display_keystrokes(); |
| 155 |
} elsif ($c eq ' ') { |
| 156 |
if ($Paused) { $Paused=0; } else { $Paused = 1; } |
| 157 |
print STDOUT "\$Paused = $Paused;\n"; |
| 158 |
kill 'HUP', $parent_pid; |
| 159 |
display_paused(); display_keystrokes(); |
| 160 |
} |
| 161 |
# every second or so, the child should display_alsa() |
| 162 |
# print STDOUT "$cmd\n"; kill 'HUP', $parent_pid; |
| 163 |
} |
| 164 |
} |
| 165 |
$SIG{'HUP'} = \&handle_child_output; |
| 166 |
close STDIN; # end of child |
| 167 |
} # end of if(!$Quiet) |
| 168 |
|
| 169 |
my $beat = 0; # 0 .. ($BarLength-1) |
| 170 |
my $next_click = 0; |
| 171 |
while (1) { # the parent... |
| 172 |
# on my system, sleep is interrupted by HUP... |
| 173 |
if ($Paused) { $beat = 0; Time::HiRes::sleep(10.0); next; } |
| 174 |
MIDI::ALSA::syncoutput(); |
| 175 |
my ($is_running,$now,$nevents) = MIDI::ALSA::status(); |
| 176 |
my $pitch = 33; |
| 177 |
if ($beat == 0 and $BarLength > 0) { $pitch = 34; } |
| 178 |
$next_click += 60/$Tempo; |
| 179 |
my @alsaevent=MIDI::ALSA::noteevent(9,$pitch,$Volume,$next_click,0.3); |
| 180 |
my $rc = MIDI::ALSA::output(@alsaevent); |
| 181 |
# HUP aborts this sleep, therefore must syncoutput as well; |
| 182 |
Time::HiRes::sleep(60/$Tempo); |
| 183 |
# but on its own, syncoutput lurches on BarLength change, not sure why |
| 184 |
$beat += 1; |
| 185 |
if ($beat >= $BarLength) { $beat = 0; } |
| 186 |
} |
| 187 |
exit 0; # end of RealTime mode |
| 188 |
} |
| 189 |
|
| 190 |
#--------- RealTime UI and infrastructure, recycled from midikbd --------- |
| 191 |
sub display_alsa { |
| 192 |
return if $Quiet; |
| 193 |
@ConnectedTo = (); |
| 194 |
my $id = MIDI::ALSA::id(); |
| 195 |
foreach (MIDI::ALSA::listconnectedto()) { |
| 196 |
my @cl = @$_; |
| 197 |
push @ConnectedTo, "$cl[1]:$cl[2]" |
| 198 |
} |
| 199 |
@ConnectedFrom = (); |
| 200 |
foreach (MIDI::ALSA::listconnectedfrom()) { |
| 201 |
my @cl = @$_; |
| 202 |
push @ConnectedFrom, "$cl[1]:$cl[2]" |
| 203 |
} |
| 204 |
gotoxy(1,1); puts_30c("ALSA client $id"); |
| 205 |
gotoxy($MidCol,1); puts_clr("midiclick pid=$$"); |
| 206 |
gotoxy(1,2); puts_clr($s); |
| 207 |
my $s = "Ouput port $id:0 is "; |
| 208 |
if (@ConnectedTo) { $s .= "connected to ".join(',',@ConnectedTo); |
| 209 |
} else { $s .= "not connected to anything"; |
| 210 |
} |
| 211 |
gotoxy(1,2); puts_clr($s); |
| 212 |
gotoxy(1,$CursorRow); |
| 213 |
} |
| 214 |
|
| 215 |
sub display_tempo { |
| 216 |
return if $Quiet; |
| 217 |
gotoxy(1,3); puts_30c("Tempo $Tempo beats/min"); |
| 218 |
gotoxy($MidCol,3); puts_clr("BarLength $BarLength beats"); |
| 219 |
gotoxy(1,$CursorRow); |
| 220 |
} |
| 221 |
|
| 222 |
sub display_paused { |
| 223 |
return if $Quiet; |
| 224 |
gotoxy(1,4); |
| 225 |
if ($Paused) { |
| 226 |
puts_30c("PAUSED"); |
| 227 |
} else { |
| 228 |
puts_30c("Playing"); |
| 229 |
} |
| 230 |
gotoxy(1,$CursorRow); |
| 231 |
} |
| 232 |
|
| 233 |
sub display_volume { |
| 234 |
return if $Quiet; |
| 235 |
gotoxy($MidCol,4); puts_clr("Volume $Volume"); |
| 236 |
gotoxy(1,$CursorRow); |
| 237 |
} |
| 238 |
|
| 239 |
sub display_keystrokes { my $arg = $_[$[]; |
| 240 |
if ($Quiet) { return; } |
| 241 |
my $s = " t=Tempo b=Barlength <spacebar>="; |
| 242 |
if ($Paused) { $s .= "Play"; } else { $s .= "Pause"; } |
| 243 |
$s .= " v=Volume q=Quit" ; |
| 244 |
if ($arg eq 'quit') { $s = ''; gotoxy(1,4); puts_30c("Quit"); } |
| 245 |
gotoxy(1, $CursorRow+1); puts_clr($s); gotoxy(1,$CursorRow); |
| 246 |
} |
| 247 |
|
| 248 |
sub get_int { my $s = $_[$[]; # this runs in the child |
| 249 |
my $min_int = 0; |
| 250 |
my $max_int = 127; |
| 251 |
if ($s =~ /bar/i) { $max_int = 50; |
| 252 |
} elsif ($s =~ /tempo/i) { $min_int = 30; $max_int = 300; |
| 253 |
} elsif ($s =~ /output/i) { $min_int = 10; $max_int = 200; |
| 254 |
} |
| 255 |
ReadMode(0, STDIN); |
| 256 |
my $int; |
| 257 |
while (1) { |
| 258 |
puts_clr("$s ($min_int..$max_int) ? "); |
| 259 |
$int = <STDIN>; |
| 260 |
print STDERR "\e[A"; $Icol = 1; |
| 261 |
if ($int =~ /^-?[0-9]+$/ and $int >= $min_int and $int <= $max_int) { |
| 262 |
ReadMode(4, STDIN); |
| 263 |
puts_clr(''); |
| 264 |
return 0+$int; |
| 265 |
} |
| 266 |
if ($int =~ /^\s*$/) { |
| 267 |
ReadMode(4, STDIN); |
| 268 |
puts_clr(''); |
| 269 |
return undef; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
# --------------- vt100 stuff, evolved from Term::Clui --------------- |
| 275 |
sub puts { my $s = join q{}, @_; |
| 276 |
$Irow += ($s =~ tr/\n/\n/); |
| 277 |
if ($s =~ /\r\n?$/) { $Icol = 0; |
| 278 |
} else { $Icol += length($s); # BUG, wrong on multiline strings! |
| 279 |
} |
| 280 |
# print STDERR "$s\e[K"; # and clear-to-eol |
| 281 |
# should be caller's responsibility ? or an option ? a different sub ? |
| 282 |
print STDERR $s; |
| 283 |
} |
| 284 |
sub puts_30c { my $s = $_[$[]; # assumes no newlines |
| 285 |
my $rest = 30-length($s); |
| 286 |
print STDERR $s, " "x$rest, "\e[D"x$rest; |
| 287 |
$Icol += length($s); |
| 288 |
} |
| 289 |
sub puts_clr { my $s = $_[$[]; # assumes no newlines |
| 290 |
my $rest = 30-length($s); |
| 291 |
print STDERR "$s\e[K"; |
| 292 |
$Icol += length($s); |
| 293 |
} |
| 294 |
sub clrtoeol { |
| 295 |
print STDERR "\e[K"; |
| 296 |
} |
| 297 |
sub up { |
| 298 |
# if ($_[$[] < 0) { down(0 - $_[$[]); return; } |
| 299 |
print STDERR "\e[A" x $_[$[]; $Irow -= $_[$[]; |
| 300 |
} |
| 301 |
sub down { |
| 302 |
# if ($_[$[] < 0) { up(0 - $_[$[]); return; } |
| 303 |
print STDERR "\n" x $_[$[]; $Irow += $_[$[]; |
| 304 |
} |
| 305 |
sub right { |
| 306 |
# if ($_[$[] < 0) { left(0 - $_[$[]); return; } |
| 307 |
print STDERR "\e[C" x $_[$[]; $Icol += $_[$[]; |
| 308 |
} |
| 309 |
sub left { |
| 310 |
# if ($_[$[] < 0) { right(0 - $_[$[]); return; } |
| 311 |
print STDERR "\e[D" x $_[$[]; $Icol -= $_[$[]; |
| 312 |
} |
| 313 |
sub gotoxy { my $newcol = shift; my $newrow = shift; |
| 314 |
if ($newcol == 0) { print STDERR "\r" ; $Icol = 0; |
| 315 |
} elsif ($newcol > $Icol) { right($newcol-$Icol); |
| 316 |
} elsif ($newcol < $Icol) { left($Icol-$newcol); |
| 317 |
} |
| 318 |
if ($newrow > $Irow) { down($newrow-$Irow); |
| 319 |
} elsif ($newrow < $Irow) { up($Irow-$newrow); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
#sub usecs { |
| 324 |
# my ($secs, $usecs) = Time::HiRes::gettimeofday(); |
| 325 |
# return 1000000*$secs + $usecs; |
| 326 |
#} |
| 327 |
|
| 328 |
__END__ |
| 329 |
|
| 330 |
=pod |
| 331 |
|
| 332 |
=head1 NAME |
| 333 |
|
| 334 |
midiclick - generates a metronome click-track on MIDI channel 9 |
| 335 |
|
| 336 |
=head1 SYNOPSIS |
| 337 |
|
| 338 |
midiclick -t 108 # tempo is (starts at) 108 beats/min |
| 339 |
midiclick -t 108 -b 4 # Four beats in a bar |
| 340 |
midiclick -Q -t 108 -b 4 # Quiet mode; no User-Interface |
| 341 |
midiclick -o 14:1 -t 108 # Output to ALSA-port 14:1 |
| 342 |
midiclick -p # starts up in Paused mode |
| 343 |
xterm -geometry 80x7-1+1 -exec 'midiclick -t 165 -b 5' & |
| 344 |
|
| 345 |
~> midiclick -o TiMidity -t 144 -b 5 -p |
| 346 |
ALSA client 129 midiclick pid=2157 |
| 347 |
Output port 129:1 is connected to 128:0 |
| 348 |
Tempo 144 beats/min BarLength 5 beats |
| 349 |
PAUSED Volume 100 |
| 350 |
_ |
| 351 |
t=Tempo b=Barlength <spacebar>=Play v=Volume q=Quit |
| 352 |
|
| 353 |
http://www.pjb.com.au/midi/midiclick.html |
| 354 |
|
| 355 |
=head1 DESCRIPTION |
| 356 |
|
| 357 |
The MIDI::ALSA module is used to create an ALSA client. |
| 358 |
|
| 359 |
The keyboard interface, |
| 360 |
allows real-time adjustment of the delay parameters. |
| 361 |
If you don't want the interface (e.g. in a Makefile), |
| 362 |
the -Q option sets Quiet-mode. |
| 363 |
|
| 364 |
=head1 OPTIONS |
| 365 |
|
| 366 |
=over 3 |
| 367 |
|
| 368 |
=item I<-t 72> |
| 369 |
|
| 370 |
The Tempo will be set; to 72 beats per minute in this example. |
| 371 |
The default is 120. |
| 372 |
|
| 373 |
=item I<-b 3> |
| 374 |
|
| 375 |
The Bar length will be set; |
| 376 |
the bell will sound every 3 beats in this example |
| 377 |
The default is 0, which means every beat clicks and there are no bells. |
| 378 |
|
| 379 |
=item I<-v 80> |
| 380 |
|
| 381 |
This option sets the Volume (or Velocity) of the midi output. |
| 382 |
The default is 100. |
| 383 |
|
| 384 |
=item I<-p> |
| 385 |
|
| 386 |
This option starts I<midiclick> in Paused state; |
| 387 |
you can then start it Playing with spacebar. |
| 388 |
This option is ignored in -Q Quiet mode. |
| 389 |
|
| 390 |
=item I<-o 128:0> or I<-o TiMidity> |
| 391 |
|
| 392 |
This option sets the ouput-port to which the midi output will be sent. |
| 393 |
You can check out the available ports with the command |
| 394 |
I<aplaymidi -l> or I<aconnect -ol>. |
| 395 |
The default ouput-port |
| 396 |
is the environment variable $ALSA_OUTPUT_PORTS |
| 397 |
|
| 398 |
=item I<-Q> |
| 399 |
|
| 400 |
This option runs I<midiclick> in Quiet mode; |
| 401 |
there is no user-interface, and the metronome just runs |
| 402 |
with its Tempo and BarLength as given on the command-line, |
| 403 |
until it is interrupted. It can be useful in scripts. |
| 404 |
|
| 405 |
=item I<-V> |
| 406 |
|
| 407 |
This option displays the Version number. |
| 408 |
|
| 409 |
=back |
| 410 |
|
| 411 |
=head1 AUTHOR |
| 412 |
|
| 413 |
Peter J Billam http://www.pjb.com.au/comp/contact.html |
| 414 |
|
| 415 |
=head1 CREDITS |
| 416 |
|
| 417 |
Based on the MIDI::Perl CPAN module in midi-file mode, |
| 418 |
and the MIDI::ALSA CPAN module in real-time mode. |
| 419 |
|
| 420 |
=head1 SEE ALSO |
| 421 |
|
| 422 |
http://search.cpan.org/perldoc?MIDI |
| 423 |
http://search.cpan.org/perldoc?MIDI::ALSA |
| 424 |
http://www.pjb.com.au/muscript |
| 425 |
http://www.pjb.com.au/midi |
| 426 |
|
| 427 |
=cut |