asiufy has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I need to convert MIDI files to a text-based musical notation (C#, A, B, etc). I have looked at some of the MIDI::* CPAN modules, but I am still quite confused, as I have no knowledge of the MIDI format itself...

So, before I get my hands "dirty" with the binary data in the MIDI files, I wonder if one of those modules would be able to take a (binary) MIDI file, and dump the individual notes (as text)...

Thanks!

Replies are listed 'Best First'.
(jeffa) Re: Dealing with MIDI
by jeffa (Bishop) on May 31, 2001 at 22:11 UTC
    There really is not support for MIDI in Perl that has warranted using Perl instead of a MIDI program. The truth is, if you are into MIDI, you are using tools such as Performer or Cakewalk, both of which are very capable of producing a nice display of the notes from a particular MIDI file.

    I have experimented with MIDI, but the results aren't a complete solution:

    use MIDI; use Data::Dumper; use strict; my $file = 'chimes.mid'; my $opus = MIDI::Opus->new({from_file=>$file}); # dump the contents of the events $opus->dump({flat=>1}); # example output (minus the header added by me) EVENT dtime channel note velocity ----------------------------------------------------------- text_event 0 (coded at Thu May 31 13:44:10 2001 ) patch_change 0 1 8 note_on 0 1 25 96 note_off 96 1 25 0 note_on 0 1 29 96 note_off 96 1 29 0 note_on 0 1 27 96 note_off 96 1 27 0 # print the notes as png file: 'experimental' according to docs # draw() returns a reference to a GD object my $im = $opus->draw; open(OUT, ">mid.png"); binmode(OUT); print OUT $im->png; close(OUT); # this produces an image like this +------------------------------------+ | | | ------------ | | ------------| |------------ | | | +------------------------------------+
    But this is sooooo lousy compared to what a MIDI software package offers (of course, I really shouldn't knock free software either). I think the best use of Perl for MIDI is to create patterns of notes that can be derived from a mathematical sense - like generating scales of notes based from trigonometric functions or other such methods.

    I am also anticipating that XML will help revolutionize the ease of displaying MIDI event information.

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    

      not to toot my own horn, but eventually 4ML will likely be a big help. there are other non-perl open-source MIDI things going on; check sourceforge.

(zdog) Re: Dealing with MIDI
by zdog (Priest) on May 31, 2001 at 21:31 UTC
    You might want to check out the MIDI module. It may not help you directly, but should keep you from having to deal with binary code.

    Zenon Zabinski | zdog | zdog7@hotmail.com

      MIDI has some of the most confusing module documentation I have ever seen. There's a possibly-helpful example at the start of MIDI::Event. It sets a callback function that gets called for each "event" in the file (where events include the start of notes, as well as other things too fierce to mention). The hard part is probably going to be converting the numerical pitch value to the text notation you want.