Class: MelodyToMidi

Inherits:
Object
  • Object
show all
Defined in:
lib/melody_to_mid.rb

Overview

Converts a programmatically-defined melody to Midi.

Instance Method Summary collapse

Instance Method Details

#make_midi(melody_file, output_file) ⇒ Object

Creates a Midi file from a ruby program defining a melody. It writes its output to the path specified by output_file.

melody_file is expected to contain the following functions:

melody() - contains a melody (see process_melody for details)
mybpm() - optional, sets the BPM (or default to 60 if not specified)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/melody_to_mid.rb', line 82

def make_midi(melody_file, output_file)
  seq = Sequence.new()

  require_relative 'shared_functions'
  load melody_file

  track = Track.new(seq)
  seq.tracks << track
  bpm = 60
  if defined? mybpm
    bpm = mybpm()
  end
  track.events << Tempo.new(Tempo.bpm_to_mpq(bpm))
  track.events << MetaEvent.new(META_SEQ_NAME, 'Sequence Name')

  track = Track.new(seq)
  seq.tracks << track

  track.name = 'Melody Track'
  track.instrument = GM_PATCH_NAMES[0]

  track.events << Controller.new(0, CC_VOLUME, 127)
  track.events << ProgramChange.new(0, 1, 0)

  process_melody(track, melody())

  File.open(output_file, 'wb') { |file| seq.write(file) }
end