develooper Front page | perl.midi | Postings from April 2002

noodling around with MIDI::Simple

Sean M. Burke
April 17, 2002 06:37
noodling around with MIDI::Simple
Message ID:
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
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) {
     sub {
       noop hn, c0, o5;
       n $nugget->[1]; n $nugget->[2];
     sub {
       noop en, c1, o4;
       n $_ for ( ($nugget->[0], $nugget->[1]) x 4 );
sleep(0), system("start.exe mplayer.exe /play $out")
  if $^O =~ m/MSWin32/;


Sean M. Burke Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About