Re: Transforming midi note numbers
[email protected] Mon, 9 Oct 2006 14:16:03 +1100 (EST)
| Newsgroups | perl.midi |
|---|---|
| Message-ID | <[email protected]> |
Marcus wrote:
> I need a perl script that given an input midi file writes a new file
> with notes transformed (for example, transposed by +3 note numbers).
One possibilty (not much tested) follows after .sig, usage:
miditranspose -3 fn.mid > fn_low.mid # transposes down 3 semtiones
miditranspose +5 fn.mid > fn_high.mid # transposes up 3 semtiones
Regards, Peter
AUS/TAS/DPIW/CIT/Servers hbt/lnd/l8 6233 3061 http://www.pjb.com.au
Pasaré, pasarémos dice el agua y canta la verdad contra la piedra
-- Pablo Neruda
------------------------------------------------------------------
#! /usr/bin/perl
eval 'require MIDI'; if ($@) {
die "you'll need to install the MIDI::Perl module from www.cpan.org\n";
}
import MIDI;
my $Version = '1.0';
# check format of options args...
$amount = shift;
if ($amount !~ /[+-]?\d+/ || $amount < -25 || $amount > +25) {
die "improbable transposition by $amount semitones\n";
}
$amount = 0 + $amount;
my $opus = MIDI::Opus->new({ 'from_file' => $ARGV[$[] || '-'});
foreach my $track ($opus->tracks()) { # there will usually be only one
my $events_r = $track->events_r();
foreach my $event (@{$events_r}) {
# varnames are only accurate for note_on and note_off events:
my ($evtype, $dticks, $cha, $note, $vol) = @$event;
if ($evtype eq 'note_on' | $evtype eq 'note_off') {
if ($cha != 9) { ${$event}[$[+3] += $amount; }
}
}
}
$opus->write_to_file( '>-' );
__END__
=pod
=head1 NAME
miditranspose - Simulates the old tape-loop delay transpose on MIDI files
=head1 SYNOPSIS
miditranspose -3 fn.mid > fn_low.mid # transposes down 3 semtiones
miditranspose +5 fn.mid > fn_high.mid # transposes up 3 semtiones
=head1 DESCRIPTION
By default, MIDI channel 9 (or 10, if you're counting 1..16)
is left unchanged as, in General MIDI, it's the percussion channel.
=head1 OPTIONS
These options are currently unimplemented :-)
=over 3
=item I<-a>
All channels are transposed, i.e. including channel 9.
=item I<-n 3,4,6,9,12>
Channels 3,4,6,9 and 12 will Not be transposed.
The default is I<-n 9>
=back
=head1 AUTHOR
Peter J Billam http://www.pjb.com.au/comp/contact.html
=head1 CREDITS
Based on Sean Burke's MIDI::Perl CPAN module.
=head1 SEE ALSO
http://search.cpan.org/~sburke
http://www.pjb.com.au/muscript
http://www.pjb.com.au/midi
=cut