Debian Codegraph @

examples/midifade

raw
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 #
10 # see 20111002 midislide ?
11 # 20140306 seconded ! : for pitch-adjusting the midi
12 # when there are midi files and audio-files
13 # so that the synth can be tuned by hand to an arbitrary wav file
14 # could simply offer pitchbend as a alternative to cc=?
15 # perhaps just the letter "p" instead of the cc number !
16 # Or, should always apply to all channels ? This will be the common case...
17 #
18 # Also: an audiofile-mixer option, which starts up a suitable ecasound
19 # and offers vol,pan,filter on each audiofile !
20 # midifade -audio v,p gtr.wav v,f,q str.wav v brass.wav backing.wav
21
22 # and could record its fadermovements in a midi file
23 # which (or several of which) can be replayed behind subsequent passes
24 # should contain a comment on which vpfq and which audiofile,
25 # so the ecasound commands can be reconstructed.
26 # midifade -audio -replay mix0.mid,mix1.mid v,p,f,q vocal.wav final.wav
27 # those midifiles should automatically replay their audio-file.
28 # However the whole mechanism only redoes what can be done by producing
29 # a mixed-down .wav, and then using that as subsequent audio input.
30 # Therefore forget the -replay option and the custom midi-comments.
31
32
33 # Curses interface from midiedit, midi stuff from midikbd.
34 # On exit, could print the equivalent muscript, or Perl,Python,or Lua ?
35 # 20130814: midifade could conceiveably take an a=auto for the value,
36 # asking somehow for min, max, period, perhaps sin/tri/sawup/sawdown, and
37 # then behave like panfarm on MIDI::ALSA. Probably need a child process...
38 # 20130815: Also, then what would the Up/Down/PgUp/PgDown/Home/End keys do ?
39 # or, instead of n=new, perhaps w=wahwah and p=pan ? 74 wants also 71
40 # should this be in our hypothetical midihub? Only really applies to 11,74
41
42 my $Version = '2.0'; # a new fader with val=0 does not get given val=64
43 my $VersionDate = '09aug2015';
44 my $OutputPort = $ENV{'ALSA_OUTPUT_PORTS'};
45 my $ColsPerFader = 6;
46 my $AlsaName = "midifade";
47
48 eval 'require Curses'; if ($@) {
49 die "you'll need to install the Curses module from www.cpan.org\n";
50 }
51 import Curses;
52 eval 'require MIDI::ALSA'; if ($@) {
53 die "you'll need to install the MIDI::ALSA module from www.cpan.org\n";
54 }
55 import MIDI::ALSA;
56
57 # use Data::Dumper;
58
59 while ($ARGV[$[] =~ /^-(\w)/) {
60 if ($1 eq 'd') { $UseCurses = 0; shift;
61 } elsif ($1 eq 'o') { shift; $OutputPort = shift;
62 } elsif ($1 eq 'N') { shift; $AlsaName = shift;
63 } else {
64 print <<EOT; exit 0;
65 Usage:
66 midifade c13m71v120 c2m11v80 # 2 faders: cha2 cc11=80, cha13 cc71=120
67 midifade -o 128:0 # outputs to port 0 of client 128
68 midifade -N fad9 # sets AlsaClientName to 'fad9'
69 midifade -v # prints the Version number
70 perldoc midifade # read the manual :-)
71 Version $Version $VersionDate http://www.pjb.com.au/midi
72 EOT
73 }
74 }
75 my @Faders = (); # LoL {$c,$m,$v}
76 my $IFader = undef;
77
78 my @note2letter=split / /,'C C D E E F F G G A B B c c d e e f f g g a b b';
79 my @note2acc = ('','#','','b','','','#','','#','','b','');
80
81 if (! MIDI::ALSA::client( $AlsaName, 0, 1, 1 )) {
82 die "can't start up the ALSA client\n";
83 }
84 foreach my $cl_po (split /,/, $OutputPort) { # 1.3
85 if ($cl_po ne '0') {
86 if (! MIDI::ALSA::connectto( 1, $cl_po )) { # 1.4
87 die "can't connect to ALSA client $cl_po\n";
88 }
89 }
90 }
91 if (! MIDI::ALSA::start()) { die "can't start the ALSA client queue\n"; }
92 my $ID = MIDI::ALSA::id();
93
94 while ($ARGV[$[] =~ /^c(\d+)m(\d+)(v(\d+))?$/) { # must start client first!
95 add_new_fader($1,$2,$4); shift;
96 }
97
98 # eval 'sub END {all_sounds_off();}';
99
100 # ----- the Curses app...
101 initscr(); cbreak(); noecho(); nonl(); clear();
102 # start_color(); # can't seem to get it not to enforce white on black
103 # init_pair(1, COLOR_WHITE(), COLOR_BLACK());
104 # init_pair(2, COLOR_RED(), COLOR_RED());
105 # attrset(COLOR_PAIR(1));
106 # http://docstore.mik.ua/orelly/perl/cookbook/ch15_13.htm BUT:
107 keypad(stdscr(),1);
108 $SIG{'INT'} = sub {exit 0;}; $SIG{'TERM'} = sub {exit 0;};
109 eval 'sub END {endwin();}'; # mustn't create call to endwin in nonCurses mode
110 display_clientstr(); # 1.6
111 display_screen();
112
113 while (1) { # the loop
114 my $c = getch();
115 if ($c == ERR()) {
116 # see man ncurses ==> man inopts
117 timeout(-1); # Shouldn't happen. Anyway, block next read
118 # but could use this for a Message which vanishes after 2 sec
119 } elsif ($c eq 'q') { quit();
120 } elsif ($c eq 'Q') { exit 0;
121 } elsif ($c eq 'D') { # or $c==KEY_DL() or $c==KEY_DC()) { too close to End
122 if (@Faders) {
123 splice @Faders, $IFader, 1;
124 if ($IFader > $#Faders) { $IFader = $#Faders; }
125 }
126 display_screen();
127 } elsif ($c eq 'n') {
128 my ($c,$m,$v) = new_fader_dialogue();
129 if (defined $m) { add_new_fader($c,$m,$v); }
130 display_screen();
131 } elsif ($c == KEY_UP() or $c eq 'k') {
132 my ($c,$m,$v) = @{$Faders[$IFader]};
133 $v += 1; if ($v > 127) { $v = 127; }
134 $Faders[$IFader] = [$c,$m,$v]; output_fader($IFader); display_screen();
135 } elsif ($c == KEY_DOWN() or $c eq 'j') {
136 my ($c,$m,$v) = @{$Faders[$IFader]};
137 $v -= 1; if ($v < 0) { $v = 0; }
138 $Faders[$IFader] = [$c,$m,$v]; output_fader($IFader); display_screen();
139 } elsif ($c == KEY_LEFT() or $c eq 'h') {
140 if ($IFader>$[) { $IFader -= 1; } display_screen();
141 } elsif ($c == KEY_RIGHT() or $c eq 'l') {
142 if ($IFader<$#Faders) { $IFader += 1; } display_screen();
143 } elsif ($c eq "\t") {
144 if ($IFader<$#Faders) { $IFader += 1; } else { $IFader = $[; }
145 display_screen();
146 } elsif ($c == KEY_PPAGE() or $c eq 'K') {
147 my ($c,$m,$v) = @{$Faders[$IFader]};
148 $v += 10; if ($v > 127) { $v = 127; }
149 $Faders[$IFader] = [$c,$m,$v];
150 output_fader($IFader); display_screen();
151 } elsif ($c == KEY_NPAGE() or $c eq 'J') {
152 my ($c,$m,$v) = @{$Faders[$IFader]};
153 $v -= 10; if ($v < 0) { $v = 0; }
154 $Faders[$IFader] = [$c,$m,$v];
155 output_fader($IFader); display_screen();
156 } elsif ($c == KEY_HOME()) {
157 my ($c,$m,$v) = @{$Faders[$IFader]};
158 $Faders[$IFader] = [$c,$m,127];
159 output_fader($IFader); display_screen();
160 } elsif ($c == KEY_END()) { # all_sounds_off ? or v=0?
161 my ($c,$m,$v) = @{$Faders[$IFader]};
162 $Faders[$IFader] = [$c,$m,0];
163 output_fader($IFader); display_screen();
164 }
165 }
166
167 #-------------- Infrastructure for the Curses version -------------
168 sub addl { my ($lin,$col,$str) = @_;
169 move($lin,$col); addstr($str); clrtoeol();
170 }
171
172 sub all_sounds_off {
173 foreach my $c (0..15) {
174 MIDI::ALSA::output(MIDI::ALSA::controllerevent($c,120,0));
175 }
176 MIDI::ALSA::stop();
177 }
178
179 sub cc2str { my $m = $_[$[];
180 if (! %c2s) { %c2s = (
181 0, 'Bank Select (MSB)',
182 32, 'Bank Select (LSB)',
183 64, 'Sustain Pedal',
184 96, 'Data Increment',
185 1, 'Modulation (MSB)',
186 33, 'Modulation (LSB)',
187 65, 'Portamento on/off',
188 97, 'Data Decrement',
189 2, 'Breath Control (MSB)',
190 34, 'Breath Control (LSB)',
191 66, 'Sostenuto Pedal',
192 98, 'non-reg param lsb',
193 67, 'Soft Pedal',
194 99, 'non-reg param msb',
195 4, 'Foot Control (MSB)',
196 36, 'Foot Control (LSB)',
197 68, 'Legato Pedal',
198 100, 'Reg Param (LSB)',
199 5, 'Portamento Time (MSB)',
200 37, 'Portamento Time (LSB)',
201 69, 'Hold 2',
202 101, 'Reg Param (MSB)',
203 6, 'Data Entry (MSB)',
204 38, 'Data Entry (LSB)',
205 70, 'Sound Variation',
206 7, 'Channel Volume (MSB)',
207 39, 'Channel Volume (LSB)',
208 71, 'Resonance, Q',
209 8, 'Balance (MSB)',
210 40, 'Balance (LSB)',
211 72, 'Release Time',
212 73, 'Attack Time',
213 10, 'Pan (MSB)',
214 42, 'Pan (LSB)',
215 74, 'Cut-off Frequency',
216 11, 'Expression (MSB)',
217 43, 'Expression (LSB)',
218 75, 'Decay Time',
219 12, 'Effects Controller 1',
220 76, 'Vibrato Rate',
221 13, 'Effects Controller 2',
222 77, 'Vibrato Depth',
223 78, 'Vibrato Delay',
224 84, 'Portamento Control',
225 120, 'All Sound Off',
226 121, 'Reset All Controllers',
227 122, 'Local Control',
228 91, 'Reverb Depth',
229 123, 'All Notes Off',
230 92, 'Tremolo Depth',
231 124, 'Omni Off',
232 93, 'Chorus Depth',
233 125, 'Omni On',
234 94, 'Celeste (De-tune)',
235 126, 'Mono On (Poly off)',
236 95, 'Phaser Depth',
237 127, 'Poly On (Mono off)',
238 );
239 }
240 return $c2s{$_[$[]} || '';
241 }
242
243 sub debug {
244 open (T, '>>', '/tmp/debug');
245 print T $_[$[],"\n";
246 close T;
247 }
248
249 sub add_new_fader { my ($c,$m,$v) = @_;
250 if (! defined $v) { $v = 64; }
251 $c = 0+$c; $m = 0+$m; $v = 0+$v;
252 my $i = $[; while ($i <= $#Faders) {
253 my ($this_c,$this_m,$this_v) = @{$Faders[$i]};
254 if ($this_c == $c and $this_m == $m) { # a duplicate; update $v
255 $Faders[$i] = [$c,$m,$v]; $IFader = $i;
256 output_fader($IFader); return 1;
257 } elsif ($this_c > $c or ($this_c == $c and $this_m > $m)) {
258 splice @Faders, $i, 0, [$c,$m,$v]; $IFader = $i;
259 output_fader($IFader); return 1;
260 }
261 $i += 1;
262 }
263 push @Faders, [$c,$m,$v]; $IFader = $#Faders;
264 output_fader($IFader); return 1;
265 }
266
267 sub display_faders {
268 foreach my $irow (1..$LINES-4) { move($irow, 0); clrtoeol(); } # XXX
269 foreach my $i ($[ .. $#Faders) { display_fader($i); }
270 refresh();
271 }
272
273 sub output_fader { my $i = $_[$[];
274 if ($i < $[ or $i > $#Faders) { die "output_fader: i=$i\n"; }
275 my ($c,$m,$v) = @{$Faders[$i]};
276 my ($status,$time,$events ) = MIDI::ALSA::status();
277 MIDI::ALSA::output(MIDI::ALSA::controllerevent($c,$m,$v,$time));
278 MIDI::ALSA::syncoutput();
279 }
280 sub display_fader { my $i = $_[$[];
281 if ($i < $[ or $i > $#Faders) { die "display_fader: i=$i\n"; }
282 my $icol = 2 + $ColsPerFader * ($i-$[);
283 my ($c,$m,$v) = @{$Faders[$i]};
284 move($LINES-6,$icol); addstr(substr " c=$c ",$[,6);
285 move($LINES-5,$icol); addstr(substr " m=$m ",$[,6);
286 move($LINES-4,$icol); addstr(substr " v=$v ",$[,6);
287 my $top_of_fader = 0 + round(($LINES-7) * (128-$v) / 128);
288 my $irow = 1; while ($irow < $top_of_fader) {
289 move($irow, $icol); attrset(A_NORMAL()); addstr(q{ } x $ColsPerFader);
290 $irow += 1;
291 }
292 while ($irow < ($LINES-6)) {
293 move($irow, $icol+2);
294 if ($i == $IFader) {
295 attrset(A_REVERSE()); addstr(' '); attrset(A_NORMAL());
296 } else {
297 # attrset(A_REVERSE()); attrset(COLOR_PAIR(2));
298 addstr('XX');
299 # addstr("\e[41m \e[0m"); # addstr escapes the escapes :-(
300 # refresh(); print STDERR "\e[41m \e[0m\e[D\e[D"; leaves bg wrong
301 # attrset(A_NORMAL()); attrset(COLOR_PAIR(1));
302 }
303 $irow += 1;
304 }
305 if ($i == $IFader) {
306 move(0,0); clrtoeol();
307 my $s1 = cc2str($m);
308 my $x = $icol + 4 - round(0.5 * length($s1));
309 if ($x < 0) { $x = 0; } elsif ($x > $COLS) { $x = $COLS - length $s1; }
310 display_clientstr();
311 move(0,$x); addstr("$s1 "); # cc-str overwrites client-str if conflict
312 }
313 if ($i == $#Faders) {
314 foreach my $irow (1..$LINES-4) { move($irow, $icol+6); clrtoeol(); }
315 }
316 move($LINES-4, ($IFader-$[) * $ColsPerFader + 4);
317 }
318
319 sub display_clientstr { # 1.6
320 my $s2 = "client $ID, $AlsaName";
321 if (($icol+4) > 0.5*$COLS) { move(0,0);
322 } else { move(0, $COLS-length($s2)-1);
323 }
324 addstr($s2);
325 refresh();
326 }
327
328 sub display_keystrokes {
329 $TopKeystrokesLine = $LINES-4;
330 if ($Message) {
331 move($LINES-2,2); clrtoeol();
332 addl($LINES-2, round(0.4*($COLS - length $Message)) ,$Message);
333 # move($LINES-3,2); clrtoeol();
334 $Message = '';
335 } else {
336 addl($LINES-2,2,
337 'Left,Right,Tab=move between faders n=new D=Delete q=quit');
338 }
339 addl($LINES-1,2,
340 'k/Up/j/Down=+-1, K/PageUp/J/PageDown=+-10, Home=127, End=0');
341 refresh();
342 }
343
344 sub display_screen {
345 move($LINES-3,1); hline($ACS_HLINE,$COLS-2);
346 display_keystrokes();
347 display_faders();
348 refresh();
349 }
350
351 sub display_message {
352 my ($y,$x); getyx($y,$x);
353 $Message = $_[$[]; display_keystrokes();
354 move($y,$x);
355 refresh();
356 }
357
358 sub new_fader_dialogue {
359 # Could introduce value=A meaning Auto like panfarm
360 # and then ask for min, max, period, perhaps sin/tri/sawup/sawdown
361 addl($LINES-3,2,' Channel (0..15) ?');
362 addl($LINES-2,2,'MIDI-Controller (0..127) ?');
363 addl($LINES-1,2,' Value (0..127) ?');
364 refresh();
365 my @newfader = ();
366 my $iline = 3;
367 while ($iline > 0) {
368 move($LINES-$iline,29);
369 my $str; my $n;
370 echo();
371 if ($iline == 3) { $n = getnstr($str,2); # 1.2
372 } else { $n = getnstr($str,3);
373 }
374 noecho();
375 if (length $str) { $newfader[$[+3-$iline] = 0 + $str; } # 2.0 length
376 $iline -= 1;
377 }
378 return @newfader;
379 }
380
381 sub quit {
382 move($LINES-2,2); clrtobot();
383 addl($LINES-2,round(0.4*($COLS-24)),"OK to quit (y/n) ? ");
384 my $c = getch();
385 if ($c eq 'y') { exit 0; }
386 display_keystrokes();
387 move($LINES-4, ($IFader-$[) * $ColsPerFader + 4);
388 refresh();
389 }
390
391 # ----------------------- infrastructure --------------------
392 sub round { my $x = $_[$[];
393 if ($x > 0.0) { return int ($x + 0.5); }
394 if ($x < 0.0) { return int ($x - 0.5); }
395 return 0;
396 }
397 sub deepcopy {
398 use Storable;
399 if (1 == @_ and ref($_[$[])) { return Storable::dclone($_[$[]);
400 } else { my $b_ref = Storable::dclone(\@_); return @$b_ref;
401 }
402 }
403
404 =pod
405
406 =head1 NAME
407
408 midifade - Provides faders generating midi-controller events
409
410 =head1 SYNOPSIS
411
412 midifade c13m71v120 c2m11v80 # 2 faders: cha2 cc11=80, cha13 cc71=120
413 midifade -o 128:0 # outputs to port 0 of client 128
414 midifade -o TiMidity # outputs to TiMidity
415 midifade -N fad9 # sets AlsaClientName to 'fad9'
416 midifade -v # prints the Version number
417 perldoc midifade # read the manual :-)
418
419 =head1 DESCRIPTION
420
421 B<Midifade> is a Curses and ALSA application which provides on-screen faders,
422 to control various midi-controllers on various midi-channels.
423
424 It uses a simple user-interface:
425 The Left and Right arrow keys move from one fader to the next,
426 the Up and Down arrow keys adjust the value of the current fader by 1,
427 the PageUp and PageDown keys adjust the value by 10,
428 and the Home and End keys set it to maximum (127) or minimum (0).
429
430 The faders are always displayed sorted by channel-number
431 then by midi-controller-number.
432
433 The available keystrokes are displayed in the bottom three lines of the screen.
434
435 It uses the B<Curses> CPAN module for the user-interface,
436 and the B<MIDI::ALSA> CPAN module to set up an ALSA client
437 which can communicate with your synth.
438
439 =head1 OPTIONS
440
441 =over 3
442
443 =item I<-o 128:0>
444
445 This example plays into the ALSA port 128:0.
446 This option allows I<midifade> to use the same port-specification
447 as the other alsa-utils, e.g. I<aplaymidi> and I<aconnect>.
448 For port 0 of a client, the ":0" part of the port specification
449 can be omitted.
450 The port specification is taken from the ALSA_OUTPUT_PORTS
451 environment variable if none is given on the command line.
452 Since Version 1.3, you may supply a comma-separated list of ports,
453 e.g. B<-o 20,128:1>
454
455 If the ALSA port is specified as B<0> then I<midifade> will start
456 up without connecting to anything. This allows you, for example,
457 to use I<midifade> (assumed here to be starting up as ALSA-client
458 129 ; check with I<aconnect -ol>)
459 to control I<ecasound>:
460
461 midifade -o 0 c0m9v102 c1m9v105 c2m9v96 c3m9v64
462
463 ecasound -c -r -Md:alsaseq,midifade:0 \
464 -a:1 -i drums.wav -ea:200 -km:1,0,250,9,1 \
465 -a:2 -i synth-chords.wav -epp:30 -ea:120 -km:1,0,150,9,2 \
466 -a:3 -i bass-guitar_take-2.ewf -ea:75 -km:1,0,100,9,3 \
467 -a:4 -i brass-lead.wav -epp:70 -ea:50 -km:1,0,100,9,4 \
468 -a:1,2,3,4 -o loop,1 \
469 -a:5,6 -i loop,1 \
470 -a:5 -o alsa \
471 -a:6 -o current-mix.wav
472
473 Here I chose midi-controller 9 because it isn't defined in General-MIDI,
474 and therefore General-MIDI-labels, useless in this context,
475 do not appear in the I<midifade> screen.
476 See I<ecasound_manpage.html> and I<examples.html> in the
477 I<ecasound> documentation for details of the B<-ea> and B<-km> options.
478
479 =item I<-N my_fade>
480
481 Sets the Alsa-Client name, to I<my_name> in this example.
482
483 This is useful in scripts,
484 to be able to connect from a specific I<midifade> by a known name.
485
486 The default Alsa-Client name is "I<midifade>"
487
488 =item I<-v>
489
490 Prints version number.
491
492 =back
493
494 =head1 ARGUMENTS
495
496 =over 3
497
498 =item I<c14m74v123>
499
500 This example starts I<midifade> up with a fader on channel 14 (0..15),
501 midi-controller 74 (0..127), set initially to a value of 123 (0..127).
502 ( In I<muscript>, that would be expressed I<cha14 cc74=123> )
503 Multiple arguments can be specified.
504 The B<c> and B<m> and B<v> bits must be in that order,
505 all in one word with no spaces.
506 The B<v> bit is optional; its default value is 64.
507
508 =back
509
510 =head1 CHANGES
511
512 2.0, 20150809, a new fader with val=0 does not get given val=64
513 1.9, 20140907, -N my_name sets the AlsaClientName
514 1.8, 20130323, D can now delete the only fader
515 1.7, 20120831, -o "string" works
516 1.6, 20120820, display_clientstr called at start
517 1.5, 20111216, if -o 0 then midifade starts up but connects to nothing
518 1.4, 20111103, use the new MIDI-ALSA 1.11 to handle portnames
519 1.3, 20111028, OutputPort can be a comma-separated list
520 1.2, 20111027, add-new-fader dialogue allows 3-digit controller-nums
521 1.1, 20111023, much irrelevant code eliminated; q asks y/n first
522 1.0, 20111022, first working version
523
524 =head1 AUTHOR
525
526 Peter J Billam http://www.pjb.com.au/comp/contact.html
527
528 =head1 CREDITS
529
530 Based on the I<Curses> and I<MIDI::ALSA> CPAN modules.
531
532 =head1 SEE ALSO
533
534 aconnect -oil
535 http://www.pjb.com.au/muscript/index.html#midi_in_a_stave
536 http://www.pjb.com.au/muscript/gm.html#cc
537 http://ecasound.sourceforge.net/ecasound/Documentation/examples.html
538 http://search.cpan.org/perldoc?Curses
539 http://search.cpan.org/perldoc?MIDI::ALSA
540 http://www.pjb.com.au/midi
541
542 =cut