noodling around with MIDI::Simple

[email protected] ("Sean M. Burke") Wed, 17 Apr 2002 07:37:16 -0600
Newsgroups perl.midi
Message-ID <[email protected]>
Someone wanted an example MIDI::Simple program that was a little more 
advanced than the Westminster Chimes example in the Synopsis example of 
MIDI::Simple.  So I made up the following program off the top of my head.
Output at http://members.spinn.net/~sburke/hubahuba.mid
I dare say It's not half bad, considering I'm not a musician, have never 
really written a piece of music in my life, and I more or less just made up 
the note values spontaneously.

Anyhoo, parts of the program are interesting from a Perl point of 
view.  Win32 people should like the start.exe trick at the end.


use MIDI::Simple;
use strict 'vars', 'refs';

my $out;
{ # Get a good output name
   my $counter = 1001;
   do {$out = "hubahuba$counter.mid"; ++$counter; } while -e $out;
}

patch_change 0, 73; # recorder
patch_change 1, 42; # cello
set_tempo 400_000;

my @plan = (
  [C,E,F], [C,E,F],
  [C,D,C], [C,D,C],

  [C,E,F], [C,E,F],
  [D,Fs,F], [C,G_u1,G_u1],

  [C,E_u1,E_u1],  [C,D_u1,E_u1],
  [D,F_u1,G_u1],  [D,F_u1,E_u1],

  [E,E_u1,E_u1],  [E,D_u1,E_u1],
  [C,E_u1,E_u1],  [C,D_u1,C_u1],
);
push @plan, @plan;

# Change last measure
pop @plan;
push @plan, [E,C,C];

foreach my $nugget (@plan) {
   synch
     sub {
       noop hn, c0, o5;
       n $nugget->[1]; n $nugget->[2];
     }
     ,
     sub {
       noop en, c1, o4;
       n $_ for ( ($nugget->[0], $nugget->[1]) x 4 );
     }
   ;
}
write_score($out);
sleep(0), system("start.exe mplayer.exe /play $out")
  if $^O =~ m/MSWin32/;
exit;

__END__

--
Sean M. Burke    http://www.spinn.net/~sburke/