# Processing midifiles with Ruby

**URL:** <https://www.ruby-forum.com/t/processing-midifiles-with-ruby/127998>\
**Category:** Ruby\
**Created:** [February 4, 2008, 3:45am UTC](https://www.ruby-forum.com/t/processing-midifiles-with-ruby/127998 "2008-02-04T03:45:55Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [February 4, 2008, 3:45am UTC](https://www.ruby-forum.com/t/processing-midifiles-with-ruby/127998/1 "2008-02-04T03:45:55Z")

</div>

If anyone else has a need to handle standard midifiles with Ruby  
they might be interested in a little package I’ve written for that  
purpose.

A little while back I found I was doing a lot of analysis of some  
particular midifiles. Typically I was using one of the midifile-\>text  
packages and then running a ruby script on the result, but those  
extra text files became an annoyance, and I figured it would be better  
to read the midifiles directly with Ruby.

I found Jim M.'s ‘midilib’ quickly, but it seemed both more and  
less than I needed. (‘Less’ in particular because it doesn’t handle  
Format 0 files, and those were what I was looking at then.) So [of  
course!] I ended up writing a library to suit myself. I’ve finally  
cleaned it up and added a bit of documentation, so I’m making it  
available in case it’s useful to others.

It provides facilities for both reading and writing all types of  
midifiles.  
Reading is particularly simple – you simply create a Midifile object  
with  
the opened file as a parameter, and use its ‘each’ method to extract  
every element of the file in sequence. Building a new file for writing  
is also straightforward. The package includes a few demo scripts to  
illustrate its usage.

I’ve put the package up on the web at:

```
http://jwgibbs.cchem.berkeley.edu/pete/midifile_rb/

```

Cheers,  
– Pete –
