Created
November 9, 2010 05:11
-
-
Save cornerwings/668731 to your computer and use it in GitHub Desktop.
Random note generator in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'midilib' | |
require 'simple-random' | |
include MIDI | |
def get_number | |
rand(2000) | |
end | |
seq = nil unless seq.nil? | |
seq = Sequence.new() | |
track = Track.new(seq) | |
seq.tracks << track | |
track.events << Tempo.new(Tempo.bpm_to_mpq(238)) | |
track.events << MetaEvent.new(META_SEQ_NAME, 'Rubulastic') | |
track = Track.new(seq) | |
seq.tracks << track | |
track.name = 'Rubulastic' | |
track.instrument = GM_PATCH_NAMES[0] | |
track.events << Controller.new(0, CC_VOLUME, 127) | |
track.events << ProgramChange.new(0, 1, 0) | |
note_length = seq.note_to_delta('32nd') | |
for var in 23..532 | |
channel = 0 | |
note = get_number | |
velocity = get_number | |
start = 0 | |
track.events << NoteOnEvent.new(channel, note, velocity, start) | |
track.events << NoteOffEvent.new(channel, note, velocity, note_length) | |
if (var%23 == 0) | |
controller, data = get_number, get_number | |
if (controller == CC_VOLUME) | |
controller = controller + 1 | |
end | |
track.events << Controller.new(0, controller, data) | |
end | |
end | |
File.open('output.mid', 'wb') { | file | seq.write(file) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment