multi-track things with MIDI::Simple

[email protected] ("Sean M. Burke") Sat, 21 Sep 2002 17:34:49 -0600
Newsgroups perl.midi
Message-ID <[email protected]>
Someone wrote to me with a little problem: for various complex reasons, he 
wanted to compose music with MIDI::Simple, but needed its output to be a 
four-track file.  So I cobbled together the following, which might interest 
folks on this list:


use MIDI::Simple;
use MIDI;

my @Opusses;
sub save_track {
   push @Opusses, Self()->make_opus;
   new_score();
   return;
}

sub save_multitrack {
   my $filename = $_[0];
   my $first = shift @Opusses;
   $first->format(1); # multitrack
   my $tracks = $first->tracks_r;
   foreach my $o (splice @Opusses) { push @$tracks, $o->tracks }
   $first->write_to_file($filename);
   return;
}



And here's how to use it: call save_track().  That'll turn the contents of 
the current score into an opus and store it in @Opusses, then reset the 
score.  Then once you've called save_track() for the last time, call 
save_multitrack($some_filename), which'll write those all into the named file.
Trivial example:

text_event("Stuff!");
save_track();

text_event("Stuff 2!");
save_track();

text_event("Stuff 3!");
save_track();

save_multitrack("stuff.mid");
exit;
__END__

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