|
midifile.rb is a small library script for Ruby that provides facilities for reading and writing standard midifiles. It lets you use very simple code to read a midifile, breaking it out into elements (headers and events) in convenient form for further processing. It reads directly from the file, without storing it all in memory first. New files can be created as well, usually by building an object in memory, adding elements as desired, and writing the complete sequence to a file at the end. 'Generator' methods are supplied for easy creation of most kinds of events. Simple alterations (such as transposing all the notes) can actually be done without building a new object — elements can be written out immediately they are read from the input. This release fixes the rather lamebrained sorting of output events done by previous versions. 'Simultaneous' events no longer get shuffled, and the occasional mistiming of the last note is fixed. New 'channel' accessor methods havce been added to the MidiItem class that work with 'user' numbering (1..16) rather than the internal 0..15 range of the 'chan' attribute, which should make it easier to avoid confusion. There is a Ruby bug (versions 1.9.1 and 1.9.2) that will prevent midifile.rb working as supplied. See below for details and the fix. The code places few restrictions on the user. It will read and write both Format 0 and Format 1 (even Format 2...) files. It is intended to be minimal yet flexible. (Which means probably that you could generate an invalid file fairly easily, but it is straightforward enough that you should be able to avoid this.) As a taster, here is an absolutely trivial lister for "myfile.mid" (using the basic 'to_s' methods provided):
require 'midifile.rb'
open("myfile.mid", "r") {|f|
mf = Midifile.new(f)
mf.each {|elem|
puts elem
}
}
|
|
The downloadable archives below contain the following:
|
Downloads:You can download the package here:midifile_rb.tgz — gzipped tar, 21 KBmidifile_rb.zip — zip archive, 31 KB |
BUG -- Ruby 1.9.x(!):I've been notified (thanks Thomas!) that midifile.rb fails in Ruby 1.9.1 and 1.9.2. This is actually caused by a bug in the getc method in those versions of Ruby:http://redmine.ruby-lang.org/issues/show/3179The problem is that getc is returning a (single-character) string instead of the byte-value it should. If this is happening to you, you can replace the two occurrences of 'getc()' (lines 303 and 498) in midifile.rb with 'getc()[0]', to have the correct value returned. (According to the bug report, the issue has been fixed, so later versions of Ruby should work with midifile.rb as supplied.) |
Pete Goodeve
Berkeley, California
e-mail: pete@GoodeveCa.NET
pete.goodeve@computer.org