Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Create a MIDI file from scratch in Ruby (See related posts)

Source: Index of /cheats/ruby/projects/mc/utils/midilib-0.8.5/examples [u2me.com]

<snip>
#! /usr/bin/env ruby
#
# usage: from_scratch.rb
#
# This script shows you how to create a new sequence from scratch and save it
# to a MIDI file. It creates a file called 'from_scratch.mid'.

# Start looking for MIDI module classes in the directory above this one.
# This forces us to use the local copy, even if there is a previously
# installed version out there somewhere.
$LOAD_PATH[0, 0] = File.join(File.dirname(__FILE__), '..', 'lib')

require 'midilib/sequence'
require 'midilib/consts'
include MIDI

seq = Sequence.new()

# Create a first track for the sequence. This holds tempo events and stuff
# like that.
track = Track.new(seq)
seq.tracks << track
track.events << Tempo.new(Tempo.bpm_to_mpq(120))
track.events << MetaEvent.new(META_SEQ_NAME, 'Sequence Name')

# Create a track to hold the notes. Add it to the sequence.
track = Track.new(seq)
seq.tracks << track

# Give the track a name and an instrument name (optional).
track.name = 'My New Track'
track.instrument = GM_PATCH_NAMES[0]

# Add a volume controller event (optional).
track.events << Controller.new(0, CC_VOLUME, 127)

# Add events to the track: a major scale. Arguments for note on and note off
# constructors are channel, note, velocity, and delta_time. Channel numbers
# start at zero. We use the new Sequence#note_to_delta method to get the
# delta time length of a single quarter note.
track.events << ProgramChange.new(0, 1, 0)
quarter_note_length = seq.note_to_delta('quarter')
[0, 2, 4, 5, 7, 9, 11, 12].each { | offset |
  track.events << NoteOnEvent.new(0, 64 + offset, 127, 0)
  track.events << NoteOffEvent.new(0, 64 + offset, 127, quarter_note_length)
}

# Calling recalc_times is not necessary, because that only sets the events'
# start times, which are not written out to the MIDI file. The delta times are
# what get written out.

# track.recalc_times

File.open('from_scratch.mid', 'wb') { | file |
	seq.write(file)
}.

</snip>

The script worked fine and generated the file however I didn't have much luck with playing it with Timidity or KMid.

Additional notes:
Installation: gem install midilib
Running on Ubuntu: require 'rubygems'

Resources:
- solutions/ - Google Code Search [google.com]
- Giles Bowkett: Archaeopteryx: A Ruby MIDI Generator [blogspot.com]
- midilib [rubyforge.org]
- Index of /cheats/ruby/projects/mc/utils/midilib-0.8.5/examples [u2me.com]

You need to create an account or log in to post comments to this site.


Click here to browse all 6643 code snippets

Related Posts