#!/usr/local/bin/perl
# Time-stamp: "2000-10-31 21:04:03 MST"
# Sean M. Burke, sburke@cpan.org
# See what instruments (patches) are used in a given MIDI file.
#
# Note that nothing in the standard stops a track from
# using more than one instrument.
use strict;
use MIDI;
my $o = MIDI::Opus->new(
  { "from_file" => $ARGV[0] || die }
);
my @tracks = $o->tracks;
print "$ARGV[0] has ", scalar(@tracks), " tracks\n";

for(my $i = 0; $i < @tracks; ++$i) {
  foreach my $e ($tracks[$i]->events) {
    if($e->[0] eq 'patch_change') {
      printf "track %02d channel %02d%s uses patch %03d (%s)\n",
        $i, $e->[2], $e->[2] == 9 ? '!' : ' ',
         # channel 9 is apparently special, for percussion
        $e->[3], $MIDI::number2patch{$e->[3]} || '?'
    }
  }
}

# Actually it'd be most efficient to just set up a callback
# for the parser, and catch just patch_change events.
# But the above is a simpler and more general approach.
