| 1 |
#! /usr/bin/perl |
| 2 |
######################################################################### |
| 3 |
# This Perl script is Copyright (c) 2006, 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 (very roughly) a tape-delay echo on a particular MIDI-channel |
| 10 |
# in a MIDI-file, or, since 2.0. on real-time-MIDI, |
| 11 |
# by issuing repeated note_on events with diminishing volume. YMMV! |
| 12 |
|
| 13 |
use Term::ReadKey; |
| 14 |
use bytes; |
| 15 |
|
| 16 |
my $Version = '4.5'; # -E specifies which controllers get echoed |
| 17 |
my $VersionDate = '13jun2016'; |
| 18 |
my %Channel = ('0',1); # MIDI channel on which the echoes will be added |
| 19 |
my %EchoNotes = (); # MIDI notes to which the echoes will be added |
| 20 |
my @Delays = (300); # incremental milliseconds of the various delays |
| 21 |
my @Echoes = (); # the channels that the echoes will be sent to |
| 22 |
my @PitchChanges = (0); # the pitch-changes of the various channels |
| 23 |
my %PitchWheel = (); # pitch_wheel, -1..+1 semitones, -4095..+4096 |
| 24 |
my @PitchDelta = (); # int(PitchChange/100) added to note-pitch |
| 25 |
my @Patches = (); # the Patches that the echo-channels will be set to |
| 26 |
my @Softenings = (25); # decremental velocites (loudness) of the echoes |
| 27 |
my %DoEchoCC = map {$_,1} (1,5,11,64,65,66,84); |
| 28 |
# do echo Modulation,Portamento,Expression and Pedals |
| 29 |
my $Debug = 0; |
| 30 |
my $AlsaName = "midiecho pid=$$"; # 4.3 for -N option |
| 31 |
# no display in real-time-mode; for use in background, and in scripts: |
| 32 |
my $Quiet = 0; |
| 33 |
my $RealTimeMode = 0; |
| 34 |
my $InputPort = q{}; |
| 35 |
my $OutputPort = q{}; |
| 36 |
my $E_option = 0; |
| 37 |
# vt100 globals |
| 38 |
my $CursorRow = 7; |
| 39 |
my $Irow = 1; |
| 40 |
my $Icol = 1; |
| 41 |
my $MidCol = 32; |
| 42 |
|
| 43 |
# use Data::Dumper; # to send the event array from parent to child |
| 44 |
use Time::HiRes; |
| 45 |
|
| 46 |
# check format of options args... |
| 47 |
while ($ARGV[$[] =~ /^-(\w)/) { |
| 48 |
if ($1 eq 'v') { shift; |
| 49 |
my $n = $0; $n =~ s{^.*/([^/]+)$}{$1}; |
| 50 |
print "$n version $Version $VersionDate\n"; |
| 51 |
exit 0; |
| 52 |
} elsif ($1 eq 'c') { shift; %Channel = (); |
| 53 |
my $a = shift; if ($a !~ /^\d[\d,]*$/) { die "bad -c arg: $a\n"; } |
| 54 |
foreach (split (',', $a)) { $Channel{$_} = 1; } |
| 55 |
} elsif ($1 eq 'd') { shift; |
| 56 |
my $a = shift; if ($a !~ /^\d[\d,]*$/) { die "bad -d arg: $a\n"; } |
| 57 |
@Delays = split (',', $a); # 4.2 don't sort ! |
| 58 |
} elsif ($1 eq 'e') { shift; |
| 59 |
my $a = shift; if ($a !~ /^\d[\d,]*$/) { die "bad -e arg: $a\n"; } |
| 60 |
@Echoes = split (',', $a); |
| 61 |
} elsif ($1 eq 'E') { shift; |
| 62 |
my $a = shift; if ($a !~ /^\d[\d,]*$/) { die "bad -E arg: $a\n"; } |
| 63 |
%DoEchoCC = map { $_, 1 } split (',', $a); |
| 64 |
$E_option = 1; |
| 65 |
} elsif ($1 eq 'p') { shift; |
| 66 |
my $a = shift; if ($a !~ /^[\d,]*$/) { die "bad -p arg: $a\n"; } |
| 67 |
@Patches = split (',', $a); |
| 68 |
} elsif ($1 eq 'w') { shift; # 3.1 |
| 69 |
my $a = shift; if ($a !~ /^[-\d,]*$/) { die "bad -w arg: $a\n"; } |
| 70 |
@PitchChanges = split (',', $a); |
| 71 |
} elsif ($1 eq 's' or $1 eq 'q') { shift; |
| 72 |
my $a = shift; if ($a !~ /^\d[\d,]*$/) { die "bad -s arg: $a\n"; } |
| 73 |
@Softenings = split (',', $a); |
| 74 |
} elsif ($1 eq 'n') { shift; |
| 75 |
my $a = shift; if ($a !~ /^\d[\d,]*$/) { die "bad -n arg: $a\n"; } |
| 76 |
shift; foreach (split (',', $a)) { $EchoNotes{$_} = 1; } |
| 77 |
} elsif ($1 eq 'i') { shift; $RealTimeMode = 1; $InputPort = shift; |
| 78 |
} elsif ($1 eq 'o') { shift; $RealTimeMode = 1; $OutputPort = shift; |
| 79 |
} elsif ($1 eq 'N') { shift; # 4.3 |
| 80 |
$AlsaName = shift; |
| 81 |
} elsif ($1 eq 'Q') { shift; $Quiet = 1; |
| 82 |
} elsif ($1 eq 'D') { shift; $Debug = 1; |
| 83 |
} else { |
| 84 |
my $n = $0; $n =~ s#^.*/([^/]+)$#$1#; |
| 85 |
print "usage:\n"; |
| 86 |
my $synopsis = 0; |
| 87 |
while (<DATA>) { |
| 88 |
if (/^=head1 SYNOPSIS/) { push @Synopsis,$_; $synopsis=1; next; } |
| 89 |
if ($synopsis && /^=head1/) { last; } |
| 90 |
if ($synopsis) { print $_; next; } |
| 91 |
} |
| 92 |
exit 1; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
# pre-extend @Softenings @Echoes and @PitchChanges to same length as @Delays |
| 97 |
my $i=$[; while (1) { |
| 98 |
last if ($i > $#Delays); |
| 99 |
if ($Delays[$i] < 1) { $Delays[$i] = 1; } # 1.6; delay=0 causes midi chaos |
| 100 |
$i++; |
| 101 |
} |
| 102 |
$i=$[; while (1) { last if ($i > $#Delays); |
| 103 |
if (!defined $Softenings[$i]) { $Softenings[$i] = $Softenings[$i-1]; } |
| 104 |
$i++; |
| 105 |
} |
| 106 |
$#Softenings = $#Delays; |
| 107 |
# if (@Echoes) { |
| 108 |
{ |
| 109 |
my $i=$[; while (1) { last if ($i > $#Delays); |
| 110 |
my @c = sort keys %Channel; |
| 111 |
if (!defined $Echoes[$i]) { $Echoes[$i] = $c[$[] or 0; } |
| 112 |
$i++; |
| 113 |
} |
| 114 |
} |
| 115 |
$#Echoes = $#Delays; |
| 116 |
@Echoes = map { 0+$_ } @Echoes; # 3.5 |
| 117 |
|
| 118 |
if (@PitchChanges) { |
| 119 |
my $i=$[; while (1) { |
| 120 |
last if ($i > $#PitchChanges); |
| 121 |
if (defined $PitchChanges[$i]) { # 3.1 |
| 122 |
$PitchDelta[$i] = int ($PitchChanges[$i]/100); # 4.0 |
| 123 |
$PitchWheel{$Echoes[$i]} |
| 124 |
= int (40.96 * ($PitchChanges[$i]-100*$PitchDelta[$i])); |
| 125 |
# %PitchWheel is not so clear, because it's a cc10= |
| 126 |
} |
| 127 |
$i++; |
| 128 |
} |
| 129 |
} |
| 130 |
$#PitchChanges = $#Delays; |
| 131 |
$#PitchDelta = $#Delays; |
| 132 |
|
| 133 |
if ($RealTimeMode) { |
| 134 |
eval 'require MIDI::ALSA'; if ($@) { |
| 135 |
die "you need to install the MIDI::ALSA module from www.cpan.org\n"; |
| 136 |
} |
| 137 |
|
| 138 |
if (! defined $OutputPort) { $OutputPort = $ENV{'ALSA_OUTPUT_PORTS'}; } |
| 139 |
if (! defined $OutputPort) { # 4.4 |
| 140 |
warn "OutputPort not specified and ALSA_OUTPUT_PORTS not set\n"; |
| 141 |
} |
| 142 |
if ($Quiet and !$InputPort) { # 3.1 |
| 143 |
die "in -Q Quiet-mode you must specify the -i InputPort\n"; |
| 144 |
} |
| 145 |
MIDI::ALSA::client( $AlsaName, 1, 1, 1 ); |
| 146 |
foreach my $cl_po (split /,/, $InputPort) { # 3.6 |
| 147 |
if ($cl_po ne '0' and ! MIDI::ALSA::connectfrom(0, $cl_po)) { # 4.4 |
| 148 |
die "can't connect from ALSA client $cl_po\n"; |
| 149 |
} |
| 150 |
} |
| 151 |
foreach my $cl_po (split /,/, $OutputPort) { # 3.6 |
| 152 |
if ($cl_po ne '0' and ! MIDI::ALSA::connectto(1, $cl_po)) { # 4.4 |
| 153 |
die "can't connect to ALSA client $cl_po\n"; |
| 154 |
} |
| 155 |
} |
| 156 |
if (! MIDI::ALSA::start()) { |
| 157 |
die "can't start the queue of the ALSA client\n"; |
| 158 |
} |
| 159 |
$CursorRow = default_cursor_row(); |
| 160 |
display_alsa(); display_channel(); display_keystrokes(); display_echoes(); |
| 161 |
|
| 162 |
# output the patch-change events on the channels that need them |
| 163 |
#warn "Patches=@Patches\n"; |
| 164 |
foreach my $i_echo ($[ .. $#Patches) { |
| 165 |
MIDI::ALSA::output(MIDI::ALSA::pgmchangeevent( |
| 166 |
$Echoes[$i_echo],$Patches[$i_echo],)); |
| 167 |
#warn "pgmchangeevent($Echoes[$i_echo],$Patches[$i_echo],)\n"; |
| 168 |
} |
| 169 |
# output the pitch-change events on the channels that need them |
| 170 |
foreach my $channel (keys %PitchWheel) { |
| 171 |
my $change = $PitchWheel{$channel}; |
| 172 |
MIDI::ALSA::output(MIDI::ALSA::pitchbendevent($channel,$change,)); |
| 173 |
# if abs(cents)>100 (e.g. cents==1200) then we keep track |
| 174 |
# of that in @PitchDelta and add it into the note events! |
| 175 |
} |
| 176 |
|
| 177 |
# How can we respond to keystrokes as well as to alsaevents? |
| 178 |
# defined ($key = ReadKey(-1)) tests if a char is waiting, |
| 179 |
# MIDI::ALSA::inputpending() tests if an alsaevent is waiting, |
| 180 |
# but how do we just sit there waiting for the next of either ? |
| 181 |
# I don't want to do an ugly 1ms-loop ... |
| 182 |
# The plan is to use Up/Down to select an Echo, then offering keystrokes |
| 183 |
# (all case-insensitive, for ergonomics _and_ comptibility with midikbd) |
| 184 |
# but how to respond to both keystrokes and alsaevents? |
| 185 |
# Could fork a ReadKey process which writes the char to its stdout, then |
| 186 |
# sends a signal to the parent process where a handler reads the char ? |
| 187 |
# Alternatively, the child could do all the user-interface and |
| 188 |
# the parent just run as an ALSA client. But no, the UI and the |
| 189 |
# resulting manipluations on the midi are tightly linked in the app; |
| 190 |
# so the parent should do both, and the child just getc and signal. |
| 191 |
# The child should have a 1-sec-timeout read, so the parent |
| 192 |
# can update its "Connected to|from" lines every second |
| 193 |
# or so; this again is an app-related functionality. |
| 194 |
# Attempts with no signalling (so each process updates the screen) |
| 195 |
# will interrupt each other's dialogues; except if you could set |
| 196 |
# up a "I'm in the middle of a dialogue" lock-flag on the UI. |
| 197 |
# AHA... The parent would like to run choose() and ask() as part |
| 198 |
# of its UI; but the parent _can't_, because it must keep the |
| 199 |
# midi-loop going. Therefore either the parent has to pass |
| 200 |
# sophisticated requests to the child (like choose and ask) |
| 201 |
# or the child has to run the whole UI and pass somewhat |
| 202 |
# sophisticated data back to the parent, like setting variables; |
| 203 |
# this is probably best because it's one-way: $Delay[3]=480; |
| 204 |
if (! $Quiet) { # 3.1 |
| 205 |
my $parent_pid = $$; |
| 206 |
my $child_pid = open(CHILD_STDOUT, "-|"); |
| 207 |
sub handle_child_output { |
| 208 |
my $cmd = <CHILD_STDOUT>; |
| 209 |
eval $cmd; if ($@) { warn "can't eval $cmd $@\n"; } |
| 210 |
} |
| 211 |
if (! $child_pid) { # The child does all the UI |
| 212 |
while (1) { |
| 213 |
ReadMode(4, STDIN); |
| 214 |
my $c = ReadKey(0, STDIN); |
| 215 |
if ($c =~ /^\e$/) { # reduce an escape sequence to just 1 char |
| 216 |
$c = ReadKey(0, STDIN); |
| 217 |
if ($c eq '[') { |
| 218 |
$c = ReadKey(0, STDIN); |
| 219 |
if ($c =~ /^\d$/) { # e.g. Delete; throw away the ~ |
| 220 |
my $tilde = ReadKey(0, STDIN); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
if ($c =~ /^q$/i) { |
| 225 |
$CursorRow = default_cursor_row(); |
| 226 |
gotoxy(1, $CursorRow); display_keystrokes('quit'); |
| 227 |
ReadMode(0, STDIN); |
| 228 |
print STDOUT "wait; exit;\n"; kill 'HUP', $parent_pid; |
| 229 |
exit; |
| 230 |
} |
| 231 |
if ($c eq 'A') { # Up |
| 232 |
if ($CursorRow > 2) { |
| 233 |
$CursorRow -= 1; gotoxy(1, $CursorRow); |
| 234 |
display_keystrokes(); |
| 235 |
} |
| 236 |
} elsif ($c eq 'B') { # Down |
| 237 |
if ($CursorRow < default_cursor_row()) { |
| 238 |
$CursorRow += 1; gotoxy(1, $CursorRow); |
| 239 |
display_keystrokes(); |
| 240 |
} |
| 241 |
} elsif ($c eq '3') { # Delete |
| 242 |
} elsif ($c eq 'c' and $CursorRow == 4) { # change dry-channel |
| 243 |
my $ch = get_int('apply echo to which channel'); |
| 244 |
if (defined $ch) { |
| 245 |
%Channel = ($ch, 1); |
| 246 |
print STDOUT "%Channel = ($ch, 1);\n"; |
| 247 |
kill 'HUP', $parent_pid; |
| 248 |
} |
| 249 |
display_channel(); |
| 250 |
} elsif ($c eq 'n' and $CursorRow == default_cursor_row()) { |
| 251 |
push @Delays,350; |
| 252 |
push @Softenings,25; |
| 253 |
push @Echoes,0; |
| 254 |
print STDOUT 'push @Delays,350; ' |
| 255 |
.'push @Softenings,25; push @Echoes,0;'."\n"; |
| 256 |
kill 'HUP', $parent_pid; |
| 257 |
display_echoes(); display_keystrokes(); |
| 258 |
} elsif ($CursorRow > 4 and $CursorRow < default_cursor_row()) { |
| 259 |
my $i_echo = $CursorRow-5+$[; |
| 260 |
if ($c eq 'c') { # change an echo-channel |
| 261 |
my $ch = get_int('send echo to which channel'); |
| 262 |
if (defined $ch) { |
| 263 |
$Echoes[$i_echo] = $ch; |
| 264 |
print STDOUT "\$Echoes[$i_echo] = $ch;\n"; |
| 265 |
kill 'HUP', $parent_pid; |
| 266 |
} |
| 267 |
display_echoes(); display_keystrokes(); |
| 268 |
} elsif ($c eq 'd') { # change an echo-delay |
| 269 |
my $d = get_int('delay in millisecs'); |
| 270 |
if (defined $d) { |
| 271 |
$Delays[$i_echo] = $d; |
| 272 |
print STDOUT "\$Delays[$i_echo] = $d;\n"; |
| 273 |
kill 'HUP', $parent_pid; |
| 274 |
} |
| 275 |
display_echoes(); display_keystrokes(); |
| 276 |
} elsif ($c eq 'n' and $CursorRow > 4) { # a New echo |
| 277 |
splice @Delays,$i_echo,0,350; |
| 278 |
splice @Softenings,$i_echo,0,25; |
| 279 |
splice @Echoes,$i_echo,0,0; |
| 280 |
splice @Patches,$i_echo,0,undef; |
| 281 |
print STDOUT "splice \@Delays,$i_echo,0,350; " |
| 282 |
. "splice \@Softenings,$i_echo,0,25; " |
| 283 |
. "splice \@Echoes,$i_echo,0,0;\n"; |
| 284 |
kill 'HUP', $parent_pid; |
| 285 |
display_echoes(); display_keystrokes(); |
| 286 |
} elsif ($c eq 's') { # softer by how much |
| 287 |
my $d = get_int('softer by how much'); |
| 288 |
if (defined $d) { |
| 289 |
$Softenings[$i_echo] = $d; |
| 290 |
print STDOUT "\$Softenings[$i_echo] = $d;\n"; |
| 291 |
kill 'HUP', $parent_pid; |
| 292 |
} |
| 293 |
display_echoes(); display_keystrokes(); |
| 294 |
} elsif ($c eq 'p' and defined $Echoes[$i_echo]) { |
| 295 |
my $d = get_int('Patch'); |
| 296 |
if (defined $d) { |
| 297 |
$Patches[$i_echo] = $d; |
| 298 |
print STDOUT "MIDI::ALSA::output(MIDI::ALSA::" |
| 299 |
."pgmchangeevent($Echoes[$i_echo],$d,));\n"; |
| 300 |
kill 'HUP', $parent_pid; |
| 301 |
} |
| 302 |
display_echoes(); display_keystrokes(); |
| 303 |
} elsif ($c eq 'w') { # MIDI-controller |
| 304 |
my $d = get_int('pitch-Wheel (cents)'); |
| 305 |
if (defined $d) { |
| 306 |
my $cha = $Echoes[$i_echo]; |
| 307 |
$PitchChanges[$i_echo] = $d; # for us, the child |
| 308 |
my $cmd = "\$PitchChanges[$i_echo] = $d; "; |
| 309 |
my $delta = int($d/100); |
| 310 |
$cmd .= "\$PitchDelta[$i_echo] = $delta; "; |
| 311 |
my $w = int (40.96 * ($d-100*$delta)); |
| 312 |
$PitchWheel{$cha} = $w; |
| 313 |
print STDOUT "$cmd MIDI::ALSA::output(MIDI::ALSA::" |
| 314 |
."pitchbendevent($cha,$w,));\n"; |
| 315 |
kill 'HUP', $parent_pid; |
| 316 |
} |
| 317 |
display_echoes(); display_keystrokes(); |
| 318 |
} |
| 319 |
} |
| 320 |
# every second or so, the child should display_alsa() |
| 321 |
# print STDOUT "$cmd\n"; kill 'HUP', $parent_pid; |
| 322 |
} |
| 323 |
} |
| 324 |
$SIG{'HUP'} = \&handle_child_output; |
| 325 |
close STDIN; # end of child |
| 326 |
} # end of if(!$Quiet) |
| 327 |
|
| 328 |
while (1) { |
| 329 |
my @alsaevent = MIDI::ALSA::input(); |
| 330 |
if ($alsaevent[0] == MIDI::ALSA::SND_SEQ_EVENT_PORT_UNSUBSCRIBED() |
| 331 |
or $alsaevent[0] == MIDI::ALSA::SND_SEQ_EVENT_PORT_SUBSCRIBED()) { |
| 332 |
display_alsa(); # shit. The parent shouldn't be doing this :-( |
| 333 |
# we could signal HUP the child. But even then, that only |
| 334 |
# detects connects and disconnects on the input-port... |
| 335 |
# Probably the child should do display_alsa every second |
| 336 |
# This will become a big problem in a more general case :-( |
| 337 |
next; |
| 338 |
} |
| 339 |
# could detect a 0-delay arg and change the volume accordingly... |
| 340 |
MIDI::ALSA::output(@alsaevent); # direct dry output |
| 341 |
my ($is_running,$now,$nevents) = MIDI::ALSA::status(); |
| 342 |
# now output it, at all the various delays, to the right channels |
| 343 |
# Don't echo patch-change, or start of sysex, or pitchbend |
| 344 |
# (why not pitchbend, if it's going to a different channel ?) |
| 345 |
if ($alsaevent[0] == MIDI::ALSA::SND_SEQ_EVENT_PGMCHANGE) { next; } |
| 346 |
if ($alsaevent[0] == MIDI::ALSA::SND_SEQ_EVENT_SYSEX) { next; } |
| 347 |
# noteon, noteoff, pitch_wheel, controller, pressure: |
| 348 |
my $cha = $alsaevent[$#alsaevent][0]; |
| 349 |
if (! $Channel{$cha}) { next; } |
| 350 |
if ($alsaevent[0] == MIDI::ALSA::SND_SEQ_EVENT_CONTROLLER()) { # 3.4 |
| 351 |
my $cc = $alsaevent[7][4]; |
| 352 |
if (!$DoEchoCC{$cc}) { next; } |
| 353 |
} |
| 354 |
if ($alsaevent[0] == MIDI::ALSA::SND_SEQ_EVENT_NOTEON() |
| 355 |
or $alsaevent[0] == MIDI::ALSA::SND_SEQ_EVENT_NOTEOFF()) { |
| 356 |
my $note = $alsaevent[$#alsaevent][1]; |
| 357 |
if (%EchoNotes and !$EchoNotes{"$note"}) { next; } |
| 358 |
} |
| 359 |
my %already = (0+$cha, 1); # 3.5 |
| 360 |
$alsaevent[3] = 0; # reset "queue" |
| 361 |
my $cumulative_delay = 0; |
| 362 |
my $dry_note = $alsaevent[$#alsaevent][1]; |
| 363 |
foreach my $j ($[ .. $#Delays) { |
| 364 |
$cumulative_delay += $Delays[$j]/1000; |
| 365 |
my $secs = $now + $cumulative_delay; |
| 366 |
$alsaevent[4] = $secs; |
| 367 |
if (defined $Echoes[$j]) { # set the -e output-channel |
| 368 |
$alsaevent[$#alsaevent][0] = $Echoes[$j]; |
| 369 |
if ($alsaevent[0] == MIDI::ALSA::SND_SEQ_EVENT_PITCHBEND() |
| 370 |
or $alsaevent[0] == MIDI::ALSA::SND_SEQ_EVENT_CONTROLLER()) { |
| 371 |
if (! $already{$Echoes[$j]}) { # 3.5 |
| 372 |
MIDI::ALSA::output(@alsaevent); |
| 373 |
$already{$Echoes[$j]} = 1; |
| 374 |
} |
| 375 |
next; |
| 376 |
} |
| 377 |
} |
| 378 |
if ($alsaevent[0] == MIDI::ALSA::SND_SEQ_EVENT_NOTEON() |
| 379 |
or $alsaevent[0] == MIDI::ALSA::SND_SEQ_EVENT_NOTEOFF()) { |
| 380 |
my $quietenedvol = $alsaevent[$#alsaevent][2]; |
| 381 |
if ($quietenedvol > 0) { |
| 382 |
$quietenedvol -= $Softenings[$j]; |
| 383 |
if ($quietenedvol < 1) { $quietenedvol = 1; } |
| 384 |
$alsaevent[$#alsaevent][2] = $quietenedvol; |
| 385 |
} |
| 386 |
$alsaevent[$#alsaevent][1] = $dry_note + $PitchDelta[$j]; #4.0 |
| 387 |
my $rc = MIDI::ALSA::output(@alsaevent); |
| 388 |
} |
| 389 |
} |
| 390 |
} |
| 391 |
exit 0; # end of RealTime mode |
| 392 |
} |
| 393 |
|
| 394 |
#--------- RealTime UI and infrastructure, recycled from midikbd --------- |
| 395 |
sub display_alsa { |
| 396 |
return if $Quiet; |
| 397 |
@ConnectedTo = (); |
| 398 |
my $id = MIDI::ALSA::id(); |
| 399 |
foreach (MIDI::ALSA::listconnectedto()) { |
| 400 |
my @cl = @$_; |
| 401 |
push @ConnectedTo, "$cl[1]:$cl[2]" |
| 402 |
} |
| 403 |
@ConnectedFrom = (); |
| 404 |
foreach (MIDI::ALSA::listconnectedfrom()) { |
| 405 |
my @cl = @$_; |
| 406 |
push @ConnectedFrom, "$cl[1]:$cl[2]" |
| 407 |
} |
| 408 |
gotoxy(1,1); puts_30c("ALSA client $id"); |
| 409 |
gotoxy($MidCol,1); puts_clr("midiecho pid=$$"); |
| 410 |
my $s = "Input port $id:0 is "; |
| 411 |
if (@ConnectedFrom) { $s .= "connected from ".join(',',@ConnectedFrom); |
| 412 |
} else { $s .= "not connected from anything"; |
| 413 |
} |
| 414 |
gotoxy(1,2); puts_clr($s); |
| 415 |
my $s = "Ouput port $id:1 is "; |
| 416 |
if (@ConnectedTo) { $s .= "connected to ".join(',',@ConnectedTo); |
| 417 |
} else { $s .= "not connected to anything"; |
| 418 |
} |
| 419 |
gotoxy(1,3); puts_clr($s); |
| 420 |
gotoxy(1,$CursorRow); |
| 421 |
} |
| 422 |
|
| 423 |
sub display_channel { |
| 424 |
# %Channel # MIDI channel on which the echoes will be added |
| 425 |
# %EchoNotes # MIDI notes to which the echoes will be added |
| 426 |
return if $Quiet; |
| 427 |
my @c = sort keys %Channel; |
| 428 |
gotoxy(1,4); |
| 429 |
if (1 == @c) { puts("Echo is being applied to input channel $c[$[]"); |
| 430 |
} else { puts("Echo is being applied to input channels @c"); |
| 431 |
} |
| 432 |
gotoxy(1,$CursorRow); |
| 433 |
} |
| 434 |
|
| 435 |
sub display_echoes { |
| 436 |
return if $Quiet; |
| 437 |
my $i = 0; while ($i <= $#Delays) { |
| 438 |
my $s = "Delay $Delays[$i] ms"; |
| 439 |
if (defined $Echoes[$i]) { $s .= ", to Channel $Echoes[$i]"; } |
| 440 |
if ($Softenings[$i]) { $s .= ", Softer by $Softenings[$i]"; } |
| 441 |
if ($Patches[$i]) { $s .= ", Patch=$Patches[$i]"; } |
| 442 |
if ($PitchChanges[$i]) {$s.=", pitchWheel $PitchChanges[$i] cents";} |
| 443 |
gotoxy(1,5+$i); puts_clr($s); |
| 444 |
$i += 1; |
| 445 |
} |
| 446 |
# $CursorRow = 5+$i; |
| 447 |
gotoxy(1,default_cursor_row()); puts_clr(""); |
| 448 |
gotoxy(1,$CursorRow); |
| 449 |
} |
| 450 |
|
| 451 |
sub default_cursor_row { # The default CursorRow, beneath the Echos |
| 452 |
return 5+@Delays; |
| 453 |
} |
| 454 |
|
| 455 |
sub display_keystrokes { |
| 456 |
if ($Quiet) { return; } |
| 457 |
# or on the "Connected to" line, offer keystrokes: Delete, n=New |
| 458 |
# or on the "Connected from" line, offer keystrokes: Delete, n=New |
| 459 |
if ($CursorRow == 2) { # Input port |
| 460 |
$s = "Down, Delete, n=New"; |
| 461 |
} elsif ($CursorRow == 3) { # Output port |
| 462 |
$s = "Up, Down, Delete, n=New"; |
| 463 |
} elsif ($CursorRow == 4) { # Echo is applied to channel |
| 464 |
$s ="Up, Down, c=Channel"; |
| 465 |
} elsif ($_[$[] eq 'quit') { |
| 466 |
gotoxy(1, default_cursor_row()+2); puts_clr(''); |
| 467 |
gotoxy(1, default_cursor_row()); display_equivalent_cmd(); |
| 468 |
gotoxy(1, default_cursor_row()+1); puts_clr(''); |
| 469 |
return; |
| 470 |
} elsif ($CursorRow == default_cursor_row()) { |
| 471 |
gotoxy(1, default_cursor_row()+1); |
| 472 |
puts_clr("Up, n=New echo, q=Quit"); |
| 473 |
gotoxy(1, default_cursor_row()+2); |
| 474 |
display_equivalent_cmd(); |
| 475 |
gotoxy(1,$CursorRow); |
| 476 |
return; |
| 477 |
} else { # an echo |
| 478 |
# should not offer p=Patch if there is no Channel set |
| 479 |
$s = "Up, Down, Delete, n=New, d=Delay, c=Channel, s=Softer, " |
| 480 |
. "p=Patch, w=pitchWheel"; |
| 481 |
gotoxy(1, default_cursor_row()+2); |
| 482 |
display_equivalent_cmd(); |
| 483 |
} |
| 484 |
gotoxy(1, default_cursor_row()+1); puts_clr($s); gotoxy(1,$CursorRow); |
| 485 |
} |
| 486 |
|
| 487 |
sub display_equivalent_cmd { |
| 488 |
my @c = sort keys %Channel; |
| 489 |
my $s = "midiecho -c $c[$[] -d ".join(",",@Delays); |
| 490 |
if (@Echoes) { $s .= " -e ".join(",",@Echoes); } |
| 491 |
if ($E_option) { $s .= " -E ".join(",",sort keys %DoEchoCC); } |
| 492 |
$s .= " -s ".join(",",@Softenings); |
| 493 |
if (@Patches) { $s .= " -p ".join(",",@Patches); } |
| 494 |
if (@PitchChanges) { $s .= " -w ".join(",",@PitchChanges); } |
| 495 |
puts_clr($s); |
| 496 |
} |
| 497 |
|
| 498 |
sub get_int { my $s = $_[$[]; # this runs in the child |
| 499 |
my $min_int = 0; |
| 500 |
my $max_int = 127; |
| 501 |
if ($s =~ /channel/i) { $max_int = 15; |
| 502 |
} elsif ($s =~ /quiet/i) { $max_int = 50; |
| 503 |
} elsif ($s =~ /delay/i) { $max_int = 10000; |
| 504 |
} elsif ($s =~ /wheel/i) { $min_int = -2400; $max_int = 2400; |
| 505 |
} |
| 506 |
ReadMode(0, STDIN); |
| 507 |
my $int; |
| 508 |
while (1) { |
| 509 |
puts_clr("$s ($min_int..$max_int) ? "); |
| 510 |
$int = <STDIN>; |
| 511 |
print STDERR "\e[A"; |
| 512 |
if ($int =~ /^-?[0-9]+$/ and $int >= $min_int and $int <= $max_int) { |
| 513 |
ReadMode(4, STDIN); |
| 514 |
return 0+$int; |
| 515 |
} |
| 516 |
if ($int =~ /^\s*$/) { |
| 517 |
ReadMode(4, STDIN); |
| 518 |
return undef; |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
# --------------- vt100 stuff, evolved from Term::Clui --------------- |
| 524 |
sub puts { my $s = join q{}, @_; |
| 525 |
$Irow += ($s =~ tr/\n/\n/); |
| 526 |
if ($s =~ /\r\n?$/) { $Icol = 0; |
| 527 |
} else { $Icol += length($s); # BUG, wrong on multiline strings! |
| 528 |
} |
| 529 |
# print STDERR "$s\e[K"; # and clear-to-eol |
| 530 |
# should be caller's responsibility ? or an option ? a different sub ? |
| 531 |
print STDERR $s; |
| 532 |
} |
| 533 |
sub puts_30c { my $s = $_[$[]; # assumes no newlines |
| 534 |
my $rest = 30-length($s); |
| 535 |
print STDERR $s, " "x$rest, "\e[D"x$rest; |
| 536 |
$Icol += length($s); |
| 537 |
} |
| 538 |
sub puts_clr { my $s = $_[$[]; # assumes no newlines |
| 539 |
my $rest = 30-length($s); |
| 540 |
print STDERR "$s\e[K"; |
| 541 |
$Icol += length($s); |
| 542 |
} |
| 543 |
sub clrtoeol { |
| 544 |
print STDERR "\e[K"; |
| 545 |
} |
| 546 |
sub up { |
| 547 |
# if ($_[$[] < 0) { down(0 - $_[$[]); return; } |
| 548 |
print STDERR "\e[A" x $_[$[]; $Irow -= $_[$[]; |
| 549 |
} |
| 550 |
sub down { |
| 551 |
# if ($_[$[] < 0) { up(0 - $_[$[]); return; } |
| 552 |
print STDERR "\n" x $_[$[]; $Irow += $_[$[]; |
| 553 |
} |
| 554 |
sub right { |
| 555 |
# if ($_[$[] < 0) { left(0 - $_[$[]); return; } |
| 556 |
print STDERR "\e[C" x $_[$[]; $Icol += $_[$[]; |
| 557 |
} |
| 558 |
sub left { |
| 559 |
# if ($_[$[] < 0) { right(0 - $_[$[]); return; } |
| 560 |
print STDERR "\e[D" x $_[$[]; $Icol -= $_[$[]; |
| 561 |
} |
| 562 |
sub gotoxy { my $newcol = shift; my $newrow = shift; |
| 563 |
if ($newcol == 0) { print STDERR "\r" ; $Icol = 0; |
| 564 |
} elsif ($newcol > $Icol) { right($newcol-$Icol); |
| 565 |
} elsif ($newcol < $Icol) { left($Icol-$newcol); |
| 566 |
} |
| 567 |
if ($newrow > $Irow) { down($newrow-$Irow); |
| 568 |
} elsif ($newrow < $Irow) { up($Irow-$newrow); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
# =================================================================== |
| 574 |
|
| 575 |
# we're in MIDI-file mode (not RealTime-mode) ... |
| 576 |
# 20120908 work in score form :-) |
| 577 |
eval 'require MIDI'; if ($@) { |
| 578 |
die "you'll need to install the MIDI::Perl module from www.cpan.org\n"; |
| 579 |
} |
| 580 |
import MIDI; |
| 581 |
my @Score = file2ms_score($ARGV[$[] || '-'); |
| 582 |
my @Track = @{$Score[$[+1]}; |
| 583 |
my @NewTrack = (); # |
| 584 |
my $Now = 1; |
| 585 |
|
| 586 |
foreach my $cha (keys %PitchWheel) { |
| 587 |
push @NewTrack, ['pitch_wheel_change',$Now,$cha,$PitchWheel{$cha}]; |
| 588 |
$Now = $Now + 1; |
| 589 |
} |
| 590 |
foreach my $i ($[..$#Patches) { # 3.9 |
| 591 |
push @NewTrack, ['patch_change',$Now,$Echoes[$i],$Patches[$i]]; |
| 592 |
$Now = $Now + 1; |
| 593 |
} |
| 594 |
foreach my $eventref (@Track) { |
| 595 |
push @NewTrack, $eventref; # straight-through |
| 596 |
my @event = @$eventref; |
| 597 |
if ($event[$[] eq 'note') { # this is a dry-note |
| 598 |
my ($evtype, $time, $duration, $cha, $note, $vol) = @event; |
| 599 |
if ($Channel{$cha} && (!%EchoNotes || $EchoNotes{$note})) { |
| 600 |
my $quietenedvol = $vol; |
| 601 |
my $cumulative_delay = 0; |
| 602 |
foreach my $i ($[ .. $#Delays) { |
| 603 |
my @new_event = ( @event ); # make a new copy |
| 604 |
$cumulative_delay += $Delays[$i]; |
| 605 |
$new_event[$[+1] = $time + $cumulative_delay; |
| 606 |
if (defined $Echoes[$i]) { $new_event[$[+3] = $Echoes[$i]; |
| 607 |
} else { $new_event[$[+3] = $cha; |
| 608 |
} |
| 609 |
$new_event[$[+4] = $note + $PitchDelta[$i]; |
| 610 |
$quietenedvol -= $Softenings[$i]; |
| 611 |
if ($quietenedvol < 1) { next; } |
| 612 |
$new_event[$[+5] = $quietenedvol; |
| 613 |
push @NewTrack, \@new_event; # time-order doesn't matter :-) |
| 614 |
} |
| 615 |
} |
| 616 |
} elsif ($event[$]] eq 'pitch_wheel_change') { |
| 617 |
my ($evtype, $time, $cha, $val) = @event; |
| 618 |
if ($Channel{$cha}) { |
| 619 |
my $cumulative_delay = 0; |
| 620 |
foreach my $i ($[ .. $#Delays) { |
| 621 |
my @new_event = ( @event ); # make a new copy |
| 622 |
$cumulative_delay += $Delays[$i]; |
| 623 |
$new_event[$[+1] = $time + $cumulative_delay; |
| 624 |
my $echocha = $cha; |
| 625 |
if (defined $Echoes[$i]) { $echocha = $Echoes[$i]; } |
| 626 |
$new_event[$[+2] = $echocha; |
| 627 |
if ($PitchWheel{$echocha}) { push @NewTrack, \@new_event; } |
| 628 |
} |
| 629 |
} |
| 630 |
} elsif ($event[$]] eq 'control_change') { |
| 631 |
my ($evtype, $time, $cha, $cc, $val) = @event; |
| 632 |
if ($Channel{$cha} and $DoEchoCC{$cc}) { |
| 633 |
my $cumulative_delay = 0; |
| 634 |
foreach my $i ($[ .. $#Delays) { |
| 635 |
my @new_event = ( @event ); # make a new copy |
| 636 |
$cumulative_delay += $Delays[$i]; |
| 637 |
$new_event[$[+1] = $time + $cumulative_delay; |
| 638 |
my $echocha = $cha; |
| 639 |
if (defined $Echoes[$i]) { $echocha = $Echoes[$i]; } |
| 640 |
$new_event[$[+2] = $echocha; |
| 641 |
push @NewTrack, \@new_event; |
| 642 |
} |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
score2file('-', 1000,\@NewTrack); |
| 647 |
|
| 648 |
#--------------------- Non-real-time infrastructure ------------------ |
| 649 |
|
| 650 |
# api_for_perl.txt - Peter Billam 2012 |
| 651 |
# |
| 652 |
# This bit of Perl code will wrap the CPAN MIDI module |
| 653 |
# http://search.cpan.org/perldoc?MIDI |
| 654 |
# so as to present a calling-interface compatible with the Lua module |
| 655 |
# http://www.pjb.com.au/comp/lua/MIDI.html |
| 656 |
# and the Python module |
| 657 |
# http://www.pjb.com.au/midi/MIDI.html |
| 658 |
# |
| 659 |
# This code is used in midisox_pl |
| 660 |
# http://www.pjb.com.au/midi/midisox.html |
| 661 |
# and in midiedit |
| 662 |
# http://www.pjb.com.au/midi/midiedit.html |
| 663 |
# |
| 664 |
# The original is at |
| 665 |
# http://www.pjb.com.au/midi/free/api_for_perl.txt |
| 666 |
|
| 667 |
#------------ MIDI infrastructure from midisox_pl ------------ |
| 668 |
|
| 669 |
sub round { my $x = $_[$[]; |
| 670 |
if ($x > 0.0) { return int ($x + 0.5); } |
| 671 |
if ($x < 0.0) { return int ($x - 0.5); } |
| 672 |
return 0; |
| 673 |
} |
| 674 |
sub deepcopy { |
| 675 |
use Storable; |
| 676 |
if (1 == @_ and ref($_[$[])) { return Storable::dclone($_[$[]); |
| 677 |
} else { my $b_ref = Storable::dclone(\@_); return @$b_ref; |
| 678 |
} |
| 679 |
} |
| 680 |
sub vol_mul { |
| 681 |
my $vol = $_[$[] || 100; |
| 682 |
my $mul = $_[$[+1] || 1.0; |
| 683 |
my $new_vol = round($vol*$mul); |
| 684 |
if ($new_vol < 0) { $new_vol = 0 - $new_vol; } |
| 685 |
if ($new_vol > 127) { $new_vol = 127; |
| 686 |
} elsif ($new_vol < 1) { $new_vol = 1; # some synths see vol=0 as default |
| 687 |
} |
| 688 |
return $new_vol; |
| 689 |
} |
| 690 |
|
| 691 |
#---------------------- Encoding stuff ----------------------- |
| 692 |
|
| 693 |
sub opus2file { |
| 694 |
my ($filename, @opus) = @_; |
| 695 |
my $format = 1; |
| 696 |
if (2 == @opus) { $format = 0; } |
| 697 |
my $cpan_opus = MIDI::Opus->new( |
| 698 |
{'format'=>$format, 'ticks' => 1000, 'tracks' => []}); |
| 699 |
my @list_of_tracks = (); |
| 700 |
my $itrack = $[+1; |
| 701 |
while ($itrack <= $#opus) { |
| 702 |
push @list_of_tracks, |
| 703 |
MIDI::Track->new({ 'type' => 'MTrk', 'events' => $opus[$itrack]}); |
| 704 |
$itrack += 1; |
| 705 |
} |
| 706 |
$cpan_opus->tracks(@list_of_tracks); |
| 707 |
if ($filename eq '-') { |
| 708 |
$cpan_opus->write_to_file( '>-' ); |
| 709 |
} elsif ($filename eq '-d') { |
| 710 |
$PID = fork; |
| 711 |
if ($PID) { |
| 712 |
eval "sub END { kill 'INT', $PID; wait;}"; |
| 713 |
$SIG{'HUP'} = sub { exit; }; |
| 714 |
} else { |
| 715 |
if (!open(P, '| aplaymidi -')) { die "can't run aplaymidi: $!\n"; } |
| 716 |
$cpan_opus->write_to_handle( *P{IO}, {} ); |
| 717 |
close P; |
| 718 |
exit 0; |
| 719 |
} |
| 720 |
} else { |
| 721 |
$cpan_opus->write_to_file($filename); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
sub score2opus { |
| 726 |
if (2 > @_) { return (1000, []); } |
| 727 |
my ($ticks, @tracks) = @_; |
| 728 |
my @opus = ($ticks,); |
| 729 |
my $itrack = $[; |
| 730 |
while ($itrack <= $#tracks) { |
| 731 |
my %time2events = (); |
| 732 |
foreach my $scoreevent_ref (@{$tracks[$itrack]}) { |
| 733 |
my @scoreevent = @{$scoreevent_ref}; |
| 734 |
if ($scoreevent[0] eq 'note') { |
| 735 |
my @note_on_event = ('note_on',$scoreevent[1], |
| 736 |
$scoreevent[3],$scoreevent[4],$scoreevent[5]); |
| 737 |
my @note_off_event = ('note_off',$scoreevent[1]+$scoreevent[2], |
| 738 |
$scoreevent[3],$scoreevent[4],$scoreevent[5]); |
| 739 |
if ($time2events{$note_on_event[1]}) { |
| 740 |
push @{$time2events{$note_on_event[1]}}, \@note_on_event; |
| 741 |
} else { |
| 742 |
@{$time2events{$note_on_event[1]}} = (\@note_on_event,); |
| 743 |
} |
| 744 |
if ($time2events{$note_off_event[1]}) { |
| 745 |
push @{$time2events{$note_off_event[1]}}, \@note_off_event; |
| 746 |
} else { |
| 747 |
@{$time2events{$note_off_event[1]}} = (\@note_off_event,); |
| 748 |
} |
| 749 |
} elsif ($time2events{$scoreevent[1]}) { |
| 750 |
push @{$time2events{$scoreevent[1]}}, \@scoreevent; |
| 751 |
} else { |
| 752 |
@{$time2events{$scoreevent[1]}} = (\@scoreevent,); |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
my @sorted_events = (); # list of event_refs sorted by time |
| 757 |
for my $time (sort {$a <=> $b} keys %time2events) { |
| 758 |
push @sorted_events, @{$time2events{$time}}; |
| 759 |
} |
| 760 |
|
| 761 |
my $abs_time = 0; |
| 762 |
for my $event_ref (@sorted_events) { # convert abstimes => deltatimes |
| 763 |
my $delta_time = ${$event_ref}[1] - $abs_time; |
| 764 |
$abs_time = ${$event_ref}[1]; |
| 765 |
${$event_ref}[1] = $delta_time; |
| 766 |
} |
| 767 |
push @opus, \@sorted_events; |
| 768 |
$itrack += 1; |
| 769 |
} |
| 770 |
return (@opus); |
| 771 |
} |
| 772 |
|
| 773 |
sub score2file { my ($filename, @score) = @_; |
| 774 |
my @opus = score2opus(@score); |
| 775 |
return opus2file($filename, @opus); |
| 776 |
} |
| 777 |
|
| 778 |
#--------------------------- Decoding stuff ------------------------ |
| 779 |
|
| 780 |
sub file2opus { |
| 781 |
my $opus_ref; |
| 782 |
if ($_[$[] eq '-') { |
| 783 |
$opus_ref = MIDI::Opus->new({'from_handle' => *STDIN{IO}}); |
| 784 |
} elsif ($_[$[] =~ /^[a-z]+:\//) { |
| 785 |
eval 'require LWP::Simple'; if ($@) { |
| 786 |
die "you need to install libwww-perl from www.cpan.org\n"; |
| 787 |
} |
| 788 |
$midi = LWP::Simple::get($_[$[]); |
| 789 |
if (! defined $midi) { die("can't fetch $_[$[]\n"); } |
| 790 |
open(P, '<', \$midi) or die("can't open FileHandle, need Perl5.8\n"); |
| 791 |
$opus_ref = MIDI::Opus->new({'from_handle' => *P{IO}}); |
| 792 |
close P; |
| 793 |
} else { |
| 794 |
$opus_ref = MIDI::Opus->new({'from_file' => $_[$[]}); |
| 795 |
} |
| 796 |
my @my_opus = (${$opus_ref}{'ticks'},); |
| 797 |
foreach my $track ($opus_ref->tracks) { |
| 798 |
push @my_opus, $track->events_r; |
| 799 |
} |
| 800 |
return (@my_opus); |
| 801 |
} |
| 802 |
|
| 803 |
sub opus2score { my ($ticks, @opus_tracks) = @_; |
| 804 |
if (!@opus_tracks) { return (1000,[],); } |
| 805 |
my @score = ($ticks,); |
| 806 |
my @tracks = deepcopy(@opus_tracks); # couple of slices probably quicker... |
| 807 |
foreach my $opus_track_ref (@tracks) { |
| 808 |
my $ticks_so_far = 0; |
| 809 |
my @score_track = (); |
| 810 |
my %chapitch2note_on_events = (); # 4.4 XXX!!! Must be by Channel !! |
| 811 |
foreach $opus_event_ref (@{$opus_track_ref}) { |
| 812 |
my @opus_event = @{$opus_event_ref}; |
| 813 |
$ticks_so_far += $opus_event[1]; |
| 814 |
if ($opus_event[0] eq 'note_off' |
| 815 |
or ($opus_event[0] eq 'note_on' and $opus_event[4]==0)) { # YY |
| 816 |
my $cha = $opus_event[2]; |
| 817 |
my $pitch = $opus_event[3]; |
| 818 |
my $key = $cha*128 + $pitch; |
| 819 |
if ($chapitch2note_on_events{$key}) { |
| 820 |
my $new_event_ref = shift @{$chapitch2note_on_events{$key}}; |
| 821 |
${$new_event_ref}[2] = $ticks_so_far - ${$new_event_ref}[1]; |
| 822 |
push @score_track, $new_event_ref; |
| 823 |
} else { |
| 824 |
warn("note_off without a note_on, cha=$cha pitch=$pitch\n"); |
| 825 |
} |
| 826 |
} elsif ($opus_event[0] eq 'note_on') { |
| 827 |
my $cha = $opus_event[2]; # 4.4 |
| 828 |
my $pitch = $opus_event[3]; |
| 829 |
my $new_event_ref = ['note', $ticks_so_far, 0, |
| 830 |
$cha, $pitch, $opus_event[4]]; |
| 831 |
my $key = $cha*128 + $pitch; |
| 832 |
push @{$chapitch2note_on_events{$key}}, $new_event_ref; |
| 833 |
} else { |
| 834 |
$opus_event[1] = $ticks_so_far; |
| 835 |
push @score_track, \@opus_event; |
| 836 |
} |
| 837 |
} |
| 838 |
# 4.7 check for unterminated notes, see: ~/lua/lib/MIDI.lua |
| 839 |
while (my ($k1,$v1) = each %chapitch2note_on_events) { |
| 840 |
foreach my $new_e_ref (@{$v1}) { |
| 841 |
${$new_e_ref}[2] = $ticks_so_far - ${$new_e_ref}[1]; |
| 842 |
push @score_track, $new_e_ref; |
| 843 |
warn("opus2score: note_on with no note_off cha=" |
| 844 |
. ${$new_e_ref}[3] . ' pitch=' |
| 845 |
. ${$new_e_ref}[4] . "; adding note_off at end\n"); |
| 846 |
} |
| 847 |
} |
| 848 |
push @score, \@score_track; |
| 849 |
} |
| 850 |
return @score; |
| 851 |
} |
| 852 |
|
| 853 |
sub file2score { |
| 854 |
return opus2score(file2opus($_[$[])); |
| 855 |
} |
| 856 |
|
| 857 |
sub file2ms_score { |
| 858 |
my @score = opus2score(to_millisecs(file2opus($_[$[]))); |
| 859 |
# must merge the tracks of a format-2 file; could perhaps even |
| 860 |
# extend the @event to indicate which Track it originated in... |
| 861 |
my $itrack = $#score; while ($itrack > ($[+1.5)) { |
| 862 |
foreach my $event_ref (@{$score[$itrack]}) { |
| 863 |
push @{$score[$[+1]}, $event_ref; # push them onto track 1 |
| 864 |
} |
| 865 |
$itrack -= 1; |
| 866 |
$#score = $itrack; # and jettison the last track |
| 867 |
} |
| 868 |
return @score; |
| 869 |
} |
| 870 |
|
| 871 |
#------------------------ Other Transformations --------------------- |
| 872 |
|
| 873 |
sub to_millisecs { |
| 874 |
my @old_opus = @_; |
| 875 |
if (!@old_opus) { |
| 876 |
return (1000,[],); |
| 877 |
} |
| 878 |
my $old_tpq = $_[$[]; |
| 879 |
my @new_opus = (1000,); |
| 880 |
my $millisec_per_old_tick = 1000.0 / $old_tpq; # float: will round later |
| 881 |
$itrack = $[+1; |
| 882 |
while ($itrack <= $#old_opus) { |
| 883 |
my $millisec_so_far = 0.0; |
| 884 |
my $previous_millisec_so_far = 0.0; |
| 885 |
my @new_track = (['set_tempo',0,1000000],); # new "crochet" is 1 sec |
| 886 |
foreach my $old_event_ref (@{$old_opus[$itrack]}) { |
| 887 |
my @old_event = @{$old_event_ref}; |
| 888 |
if ($old_event[0] eq 'note') { |
| 889 |
die "to_millisecs needs an opus, not a score\n"; |
| 890 |
} |
| 891 |
my @new_event = deepcopy(@old_event); # copy.deepcopy ? |
| 892 |
$millisec_so_far += ($millisec_per_old_tick * $old_event[1]); |
| 893 |
$new_event[1] = round($millisec_so_far-$previous_millisec_so_far); |
| 894 |
if ($old_event[0] eq 'set_tempo') { |
| 895 |
$millisec_per_old_tick = $old_event[2] / (1000.0 * $old_tpq); |
| 896 |
} else { |
| 897 |
$previous_millisec_so_far = $millisec_so_far; |
| 898 |
push @new_track, \@new_event; |
| 899 |
} |
| 900 |
} |
| 901 |
push @new_opus, \@new_track; |
| 902 |
$itrack += 1; |
| 903 |
} |
| 904 |
return @new_opus; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
sub usecs { |
| 909 |
my ($secs, $usecs) = Time::HiRes::gettimeofday(); |
| 910 |
return 1000000*$secs + $usecs; |
| 911 |
} |
| 912 |
|
| 913 |
__END__ |
| 914 |
|
| 915 |
=pod |
| 916 |
|
| 917 |
=head1 NAME |
| 918 |
|
| 919 |
midiecho - Simulates tape-delay echo, on MIDI files or on real-time MIDI |
| 920 |
|
| 921 |
=head1 SYNOPSIS |
| 922 |
|
| 923 |
# on midi-files (e.g. *.mid ) : |
| 924 |
midiecho -c 3 fn # echo will be added to midi channel 3 |
| 925 |
midiecho -c 3 -d 450,450,450 fn # three echoes at 450 mS gaps |
| 926 |
midiecho -c 3 -d 450,450 -s 30 fn # each echo is (MIDI) 30 softer |
| 927 |
midiecho -c 2 -d 450 -e 5 -s 30 fn # the echo appears on channel 5 |
| 928 |
midiecho -c 3 -d 40 -e 4 -w 10 -s 0 # Automatic-Double-Tracking |
| 929 |
midiecho filename # defaults: midiecho -c 0 -d 300 -s 30 |
| 930 |
muscript -midi f.txt | midiecho -c 1 -d 300 -s 25 -e 2 >f.mid |
| 931 |
|
| 932 |
# on real-time (raw) midi : |
| 933 |
~> xterm -g 80x16+1+1 -exec 'midiecho -i 32 -d 22 -c 3 -e 4' & |
| 934 |
~> midiecho -i 32:0 -o 128:0 -c 3 -d 450,400 -e 4,5 |
| 935 |
ALSA client 129 midiecho pid=2157 |
| 936 |
Input port 129:0 is connected from 32:0 |
| 937 |
Ouput port 129:1 is connected to 128:0 |
| 938 |
Echo is being applied to input channel 3 |
| 939 |
Delay 450 ms, to Channel 4, Softer by 25 |
| 940 |
Delay 400 ms, to Channel 5, Softer by 25 |
| 941 |
_ |
| 942 |
Up, n=New echo, q=Quit |
| 943 |
midiecho -c 3 -d 450,400 -e 3,3 -s 25,25 |
| 944 |
|
| 945 |
http://www.pjb.com.au/midi/midiecho.html |
| 946 |
|
| 947 |
=head1 DESCRIPTION |
| 948 |
|
| 949 |
Simulates a tape-delay echo on a particular MIDI-channel |
| 950 |
by issuing repeated note_on events with diminishing volume. |
| 951 |
Since version 2.0, the -i and -o options |
| 952 |
allow I<midiecho> to work on real-time (raw) midi inputs, |
| 953 |
as well as on midi files. |
| 954 |
|
| 955 |
Midiecho sounds best if the -e option is used, to assign the |
| 956 |
echoes to different MIDI-channels; this avoids notes being |
| 957 |
restarted before they have finished. |
| 958 |
Without -e, I<midiecho> works much better on transient sounds, |
| 959 |
e.g. banjo, or snare-drum. |
| 960 |
|
| 961 |
If the -e option is not being used, |
| 962 |
then the echo note is played on the same channel as the original note. |
| 963 |
If this leaves your synth chopping of lots of notes |
| 964 |
(when the original note is not finished by the time the echo note starts), |
| 965 |
then your synth is probably stateless, |
| 966 |
and you should try invoking midiecho with the -S option. |
| 967 |
|
| 968 |
Since version 4.5, the -E option specifies the list of CC controllers |
| 969 |
which get echoed to the echo-channels. |
| 970 |
This is different from previous versions, |
| 971 |
in which the -E option did not work and was undocumented. |
| 972 |
|
| 973 |
Since version 2.6, the delays are incremental (since the previous delay) |
| 974 |
not absolute (since the original note); |
| 975 |
this is a bug-fix, but it was a well-established bug. |
| 976 |
|
| 977 |
Version 3.0 brings major changes, |
| 978 |
involving some loss of backward-compatibility. |
| 979 |
Since version 3.0: |
| 980 |
|
| 981 |
=over 3 |
| 982 |
|
| 983 |
=item * |
| 984 |
|
| 985 |
In real-time mode, |
| 986 |
the MIDI::ALSA module |
| 987 |
is used to create a proper ALSA client, |
| 988 |
so Virtual-MIDI clients are no longer needed. |
| 989 |
|
| 990 |
=item * |
| 991 |
|
| 992 |
The real-time mode |
| 993 |
now has a keyboard interface, |
| 994 |
allowing real-time adjustment of the delay parameters. |
| 995 |
If you don't want the interface (e.g. in a Makefile), |
| 996 |
the -Q option sets Quiet-mode. |
| 997 |
|
| 998 |
=item * |
| 999 |
|
| 1000 |
The -d option specifies delays |
| 1001 |
I<incrementally> in milliseconds since the previous signal, |
| 1002 |
not in absolute milliseconds since the dry signal. |
| 1003 |
|
| 1004 |
=item * |
| 1005 |
|
| 1006 |
The -p option |
| 1007 |
specifies the Patches of the various echoes, |
| 1008 |
in the same order as they were given delays. |
| 1009 |
|
| 1010 |
=item * |
| 1011 |
|
| 1012 |
The former "Pitch" option is now called "Wheel" and is invoked by |
| 1013 |
B<-w>; |
| 1014 |
it allows the echo to be detuned (in 1/100's of a semitone) |
| 1015 |
which makes possible an "Automatic Double-Tracking" effect. |
| 1016 |
|
| 1017 |
=item * |
| 1018 |
|
| 1019 |
The B<-m> option |
| 1020 |
specifies |
| 1021 |
MIDI-Controller settings of the various Echoes; |
| 1022 |
|
| 1023 |
midiecho -c 0 -d 300,300 -e 1,2 -p 0,74 -m cc10=15,cc10=103 |
| 1024 |
|
| 1025 |
This option is currently unimplemented. |
| 1026 |
In this example, the echo on |
| 1027 |
channel 1 is panned (MIDI-controller number 10) over to the left (cc10=15), |
| 1028 |
and the echo on channel 2 is panned over to the right (cc10=103). |
| 1029 |
|
| 1030 |
=item * |
| 1031 |
|
| 1032 |
The B<-s> option replaces the -q option, |
| 1033 |
because in the real-time mode keyboard interface B<q> means quit. |
| 1034 |
|
| 1035 |
=back |
| 1036 |
|
| 1037 |
=head1 OPTIONS |
| 1038 |
|
| 1039 |
=over 3 |
| 1040 |
|
| 1041 |
=item I<-c 3> |
| 1042 |
|
| 1043 |
Echo will be added to midi B<C>hannel 3. The channels are numbered |
| 1044 |
from 0...15 If -c is not specified, the default channel is 0. |
| 1045 |
Currently, I<midiecho> can only add echoes to one channel at once; |
| 1046 |
the other channels pass through unaltered. |
| 1047 |
|
| 1048 |
=item I<-d 350,300,250> |
| 1049 |
|
| 1050 |
The echo notes will be B<D>elayed at gaps of 350, 300, and 250 mS, |
| 1051 |
which means at 350, 300 and 250 mS after the previous. |
| 1052 |
If -d is not specified, the default delay is just 300 mS |
| 1053 |
|
| 1054 |
=item I<-e 4,5,4> |
| 1055 |
|
| 1056 |
The B<E>choes are produced not on the original (-c) channel |
| 1057 |
but on the channels 4 then 5 then 4 again |
| 1058 |
(in this example there are three echoes). |
| 1059 |
This is a really useful option :-) |
| 1060 |
|
| 1061 |
As one example usage, you might have set up your synth's channel 4 and 5 |
| 1062 |
with the same patch (instrumental sound) as the original channel (e.g. 3), |
| 1063 |
but panned to different places in the stereo image. |
| 1064 |
This creates a very realistic echo-effect. |
| 1065 |
|
| 1066 |
Another example usage could be to set up the echo-channels with a |
| 1067 |
completely different sound, maybe something atmospheric or ethereal. |
| 1068 |
|
| 1069 |
Another example usage could be to set up the echo-channels |
| 1070 |
with a different patch, and use a 1ms delay, thus doubling |
| 1071 |
the original channel with a different sound. |
| 1072 |
|
| 1073 |
If the number of echo-channels (-e) is fewer than the number of delays |
| 1074 |
in the -d list, then the last echo-channel is repeated as necessary. |
| 1075 |
|
| 1076 |
=item I<-n 38,40> |
| 1077 |
|
| 1078 |
Echo will be added only to midi B<N>otes 38 and 40. |
| 1079 |
This option is mainly useful with General-MIDI channel 9, |
| 1080 |
which represents a drumkit, with each note representing a different drum, |
| 1081 |
see http://www.pjb.com.au/muscript/gm.html#perc |
| 1082 |
|
| 1083 |
In this I<-c 9 -n 38,40> example, echoes would only be added to |
| 1084 |
the Acoustic Snare and the Electric Snare sounds. |
| 1085 |
|
| 1086 |
=item I<-p 74,93> |
| 1087 |
|
| 1088 |
The channels specifed by the B<-e> option |
| 1089 |
will be preset to use MIDI-Patches |
| 1090 |
74 and 93 (in this example). |
| 1091 |
|
| 1092 |
=item I<-w 8> |
| 1093 |
|
| 1094 |
The echo will be changed by the pitch-B<W>heel up 8 cents |
| 1095 |
(hundredth's of a semitone). |
| 1096 |
|
| 1097 |
This can be used in conjunction with the B<-e>, B<-d> and B<-s> |
| 1098 |
options to produce the "Automatic-Double-Tracking" effect, e.g. |
| 1099 |
midiecho -c 3 -e 4 -d 40 -w -10 -s 0 |
| 1100 |
|
| 1101 |
which assumes that the original channel 3 is panned over to one extreme, |
| 1102 |
and the echo-channel 4 is set up with the same patch but panned over |
| 1103 |
the other way. It then produces an "echo" of the same volume and just |
| 1104 |
40mS late and just 10 cents lower. Because the two sounds are in |
| 1105 |
different speakers they don't beat with each other, and sound almost |
| 1106 |
like two instruments playing in unison. |
| 1107 |
|
| 1108 |
With larger parameters, it can be used to produce doubling at |
| 1109 |
a large interval; e.g. I<-d 10 -w 1200> causes the original |
| 1110 |
voice to be doubled (with a delay of 10ms) at the octave (1200 cents). |
| 1111 |
|
| 1112 |
=item I<-s 35,20> |
| 1113 |
|
| 1114 |
The first delayed note is 35 (MIDI) B<S>ofter than the original, |
| 1115 |
and the second is 20 softer still. |
| 1116 |
If the number of softenings (-e) is fewer than the number of delays |
| 1117 |
in the -d list, then the last softening is repeated as necessary. |
| 1118 |
If an echo ends up with zero volume or less, then it is suppressed. |
| 1119 |
|
| 1120 |
If -s is not specified, by default each echo is 30 softer |
| 1121 |
than the previous. |
| 1122 |
|
| 1123 |
=item I<-S> |
| 1124 |
|
| 1125 |
You'll need to use the -S option if you're not using -e, |
| 1126 |
and if the sythesiser you're going to be using is B<S>tateless. |
| 1127 |
In other words, if the sythesiser does not keep a count |
| 1128 |
of how many note_on's there have been on a given note, |
| 1129 |
and switches the note off if receives even just one note_off command. |
| 1130 |
So if your synth seems to be chopping off lots of notes, |
| 1131 |
you should try invoking midiecho with the -S option. |
| 1132 |
|
| 1133 |
=item I<-i 32:0> or I<-i ProKeys> |
| 1134 |
|
| 1135 |
This option puts I<midiecho> into raw-midi |
| 1136 |
(or real-time, or midi-on-the-wire) |
| 1137 |
mode, and takes the midi-data from the specified port. |
| 1138 |
|
| 1139 |
The port is specified as an ALSA-port; |
| 1140 |
you can check out the available ports with the command |
| 1141 |
I<arecordmidi -l> or I<aconnect -il>. |
| 1142 |
|
| 1143 |
Since Version 3.6, |
| 1144 |
you may supply a comma-separated list of ports, e.g. B<-i 28:0,32> |
| 1145 |
|
| 1146 |
=item I<-o 128:0> or I<-o TiMidity> |
| 1147 |
|
| 1148 |
This option puts I<midiecho> into raw-midi mode |
| 1149 |
and sets the ouput-port to which the midi output will be sent. |
| 1150 |
You can check out the available ports with the command |
| 1151 |
I<aplaymidi -l> or I<aconnect -ol>. |
| 1152 |
The default ouput-port |
| 1153 |
(if only B<-i> option is present) |
| 1154 |
is the environment variable $ALSA_OUTPUT_PORTS |
| 1155 |
|
| 1156 |
Since Version 3.6, |
| 1157 |
you may supply a comma-separated list of ports, e.g. B<-o Roland,128:1> |
| 1158 |
|
| 1159 |
=item I<-N my_echo_1> |
| 1160 |
|
| 1161 |
This option sets the ALSA Client-Name, |
| 1162 |
to I<my_echo_1> in this example, |
| 1163 |
that I<midiecho> will adopt |
| 1164 |
if an I<-i> or I<-o> option is used to put it into raw-midi mode, |
| 1165 |
This is useful if starting up I<midiecho>, or especially multiple I<midiecho>s, |
| 1166 |
from a script which will then need to connect them to other ALSA clients. |
| 1167 |
|
| 1168 |
The default ALSA-name is I<midiecho pid=456> or whatever the pid is |
| 1169 |
of the midiecho process. |
| 1170 |
|
| 1171 |
=back |
| 1172 |
|
| 1173 |
=head1 AUTHOR |
| 1174 |
|
| 1175 |
Peter J Billam http://www.pjb.com.au/comp/contact.html |
| 1176 |
|
| 1177 |
=head1 CREDITS |
| 1178 |
|
| 1179 |
Based on the MIDI::Perl CPAN module in midi-file mode, |
| 1180 |
and the MIDI::ALSA CPAN module in real-time mode. |
| 1181 |
|
| 1182 |
=head1 SEE ALSO |
| 1183 |
|
| 1184 |
http://search.cpan.org/perldoc?MIDI |
| 1185 |
http://search.cpan.org/perldoc?MIDI::ALSA |
| 1186 |
http://www.pjb.com.au/muscript |
| 1187 |
http://www.pjb.com.au/midi |
| 1188 |
http://www.pjb.com.au/midi/midiedit.html |
| 1189 |
http://www.pjb.com.au/midi/midithru.html |
| 1190 |
|
| 1191 |
=cut |