RE: My intro and what I am using MIDI Perl for
[email protected] (Dave Turner) Fri, 20 Jun 2003 20:24:17 +0000
| Newsgroups | perl.midi |
|---|---|
| Message-ID | <a05100303bb191a9fdf51@[81.174.192.245]> |
Charles,
Our ambitions are similar, but I want to convert MIDI files to Csound scores.
The code below works on my iMac and produces a score file (as defined
in the MIDI::Score module) from a MIDI file.
Note that it only works on Type 0 midi files - that's where all of
the midi info is in one track.
It took me a while to work that one out! For midi files of Type 1 (=
many tracks, up to 128 in the sequencer software I use) I have to
open up the file in my sequencer and then save it as a Type 0 file.
THEN the code below works properly!
The example of what the score looks like given in MIDI::Score is
exactly what comes out.
I have writtemn a useable midi to csound converter now that needs a
bit of human intervention to make it work, but not much. It is not
"clever" perl by any means. I tend to dip in and out of programming
as and when I need to so I write code that is open and easy for me to
understand.
I will post the code for that next.
#!perl
# midi2score this lists the midi track as a score and creates a text file
# using MacPerl 5.6.1r2 for Mac OS 9.2.2
use warnings;
use strict;
use MIDI;
my $DRIVE = 'Stuff:'; # the drive name
my $PATH = 'temptemp:'; # the full path name
my $MIDI = 'DancingQueen.mid'; # the midi file name
my $SCO = $MIDI.'.sco';
my $BUF;
my $IN_FILE = $DRIVE.$PATH.$MIDI;
#what follows is a direct lift from MIDI::Score and Seans's answers
to my posts.
my $opus = MIDI::Opus->new({ 'from_file' => $IN_FILE });
my $event_r = $opus->tracks_r->[0]->events_r;
my $score_r = MIDI::Score::events_r_to_score_r($event_r);
$BUF = dump_score_to_file( $score_r);
print "file $DRIVE$PATH$SCO created \n";
###########################################################################
=item dump_score_to_file( $score_r )
This dumps (via C<print>) a text representation of the contents of
the event structure you pass a reference to.
=cut
sub dump_score_to_file {
my $score_r = $_[0];
open(OUT,">>", $DRIVE.$PATH.$SCO); # create or append newfile
print OUT "\@notes = ( # ", scalar(@$score_r), " notes...\n";
foreach my $note_r (@$score_r) {
open(OUT,">>", $DRIVE.$PATH.$SCO); # create or append newfile
print OUT " [", &MIDI::_dump_quote(@$note_r), "],\n" if @$note_r;
}
print OUT ");\n";
close(OUT);
return;
}
###########################################################################
exit;