Class: MidiWriter
- Inherits:
-
Object
- Object
- MidiWriter
- Defined in:
- lib/rhythmruby/MidiWriter.rb
Overview
writes sequences of noteData (from a RhythmParser class) to midi tracks a ‘MidiWriter’ represents one midi Song, which can be written to one file, the midiSong can contain multiple tracks/noteSequences
Instance Method Summary collapse
-
#createTrack ⇒ MIDI::Track
creates a track within the midi song of current MidiWriter instance.
-
#initialize(bpm) ⇒ MidiWriter
constructor
A new instance of MidiWriter.
-
#mergeTracks ⇒ Object
merge all tracks in the midi song, this will create a one polyphonic track some editors see different tracks as different instruments (logic) if so, after merging: all notes/tracks will belong to one instrument.
-
#writeNote(midiNote, noteLength, midiTrack) ⇒ Object
writes one midiNote to a midiTrack.
-
#writeSeqToTrack(midiSeq, midiTrack) ⇒ Object
writes a note sequence generate by a RhythmParser to a midi track.
-
#writeToFile(fileName) ⇒ Object
write the midiSong to file consisting of all the created midiTracks.
Constructor Details
#initialize(bpm) ⇒ MidiWriter
Returns a new instance of MidiWriter.
20 21 22 23 |
# File 'lib/rhythmruby/MidiWriter.rb', line 20 def initialize(bpm) @song = MIDI::Sequence.new() # this is a midi song/sequence @bpm = bpm # beats per minute of the midi song/sequence end |
Instance Method Details
#createTrack ⇒ MIDI::Track
creates a track within the midi song of current MidiWriter instance
27 28 29 30 31 32 |
# File 'lib/rhythmruby/MidiWriter.rb', line 27 def createTrack() @song.tracks << (newTrack = MIDI::Track.new(@song)) newTrack.events << MIDI::Tempo.new(MIDI::Tempo.bpm_to_mpq(@bpm)) newTrack.events << MIDI::ProgramChange.new(0, 0) return newTrack end |
#mergeTracks ⇒ Object
merge all tracks in the midi song, this will create a one polyphonic track some editors see different tracks as different instruments (logic) if so, after merging: all notes/tracks will belong to one instrument
68 69 70 71 72 73 74 |
# File 'lib/rhythmruby/MidiWriter.rb', line 68 def mergeTracks masterTrack = MIDI::Track.new(@song) # create master final track @song.tracks.each do |track| # for each track in the current song masterTrack.merge(track.events) # merge it with the master track end @song.tracks = [masterTrack] # replace all tracks with the one master end |
#writeNote(midiNote, noteLength, midiTrack) ⇒ Object
writes one midiNote to a midiTrack
49 50 51 52 53 54 55 56 |
# File 'lib/rhythmruby/MidiWriter.rb', line 49 def writeNote(midiNote, noteLength, midiTrack) # if the velocity is 0, the note is not played. # use this for an initial silent note. velocity = (midiNote == 0) ? 0 : 127 midiTrack.events << MIDI::NoteOnEvent.new(0, midiNote, velocity, 0) midiTrack.events << MIDI::NoteOffEvent.new(0, midiNote, velocity, \ @song.length_to_delta(noteLength)) end |
#writeSeqToTrack(midiSeq, midiTrack) ⇒ Object
writes a note sequence generate by a RhythmParser to a midi track
38 39 40 41 42 43 |
# File 'lib/rhythmruby/MidiWriter.rb', line 38 def writeSeqToTrack(midiSeq, midiTrack) midiSeq.each do |midiNote, noteLength| writeNote(midiNote, noteLength, midiTrack) end end |
#writeToFile(fileName) ⇒ Object
write the midiSong to file consisting of all the created midiTracks
60 61 62 |
# File 'lib/rhythmruby/MidiWriter.rb', line 60 def writeToFile(fileName) open(fileName, 'w') {|f| @song.write(f) } end |