creating midifiles using ruby

Nikolaus Gradwohl2018-03-26T07:21:37+02:00

I have been experimenting with randomly generated drum patterns lately and found out pretty soon, that a totally random generated pattern isn't exacly what I was after, I wanted a pattern generator where I can specify propabilities for each drum hit to occure. So I wrote a ruby script using midilib that generates midi files containing patterns, which I can then import into bitwig and loop and arrange for my tracks.


Midilib is a nifty little ruby library that allows you to read or write midifiles. For my purpose I used the library to write the files.

the library can be installed using gem

gem install midilib

A midifile contains sequences, sequences contain tracks and tracks contain midi events. These events can be any midi event like noteOn, noteOf, controlChange, programChange, ... For each midiEvent in a track the delta time to the last event is stored.

in midi lib you can specify a time_from_start value and lets midilib recalculate the delta values before saving the track.

So here is a ruby script that generates a hihat pattern (channel 1, midi note 44 in my kit), where the notes are placed on a 16th grid with a random chance between 20% and 50%.

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

seq =

track =
seq.tracks << track << <<, 'Test Pattern')

track =
seq.tracks << track = 'Loop1'

s = 0
l = seq.note_to_delta('16th')
e = Random.rand(0.2..0.5)

16.times do
    if ( Random.rand < e ) then
    on =,44,127,0)
    on.time_from_start = s
    off =,44,127,0)
    off.time_from_start = s + l << on << off
s += l

track.recalc_delta_from_times'pattern3.mid', 'wb') { |file| seq.write(file) }
Tweet This! submit to reddit Digg!   Tags: | no comments | no trackbacks

See also:

Calculating Euclidean Rhythmns using the Bresenham Algorithm
Semimodular meets SonicPI
launchpad mini to play push like scales using puredata
Midified Monotron first track
new Song Ice5



Leave a response

Leave a comment