Class: Sidtool::MidiFileWriter
- Inherits:
-
Object
- Object
- Sidtool::MidiFileWriter
- Defined in:
- lib/sidtool/midi_file_writer.rb
Defined Under Namespace
Classes: DeltaTime, EndOfTrack, KeySignature, NoteOff, NoteOn, ProgramChange, TimeSignature, TrackName
Instance Method Summary collapse
- #build_track(synths) ⇒ Object
-
#initialize(synths_for_voices) ⇒ MidiFileWriter
constructor
A new instance of MidiFileWriter.
- #write_to(path) ⇒ Object
Constructor Details
#initialize(synths_for_voices) ⇒ MidiFileWriter
Returns a new instance of MidiFileWriter.
93 94 95 |
# File 'lib/sidtool/midi_file_writer.rb', line 93 def initialize(synths_for_voices) @synths_for_voices = synths_for_voices end |
Instance Method Details
#build_track(synths) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/sidtool/midi_file_writer.rb', line 106 def build_track(synths) waveforms = [:tri, :saw, :pulse, :noise] track = [] current_frame = 0 synths.each do |synth| channel = waveforms.index(synth.waveform) || raise("Unknown waveform #{synth.waveform}") track << DeltaTime[synth.start_frame - current_frame] track << NoteOn[channel, synth.tone] current_frame = synth.start_frame current_tone = synth.tone synth.controls.each do |start_frame, tone| track << DeltaTime[start_frame - current_frame] track << NoteOff[channel, current_tone] track << DeltaTime[0] track << NoteOn[channel, tone] current_tone = tone current_frame = start_frame end end_frame = [current_frame, synth.start_frame + (FRAMES_PER_SECOND * (synth.attack + synth.decay + synth.sustain_length)).to_i].max track << DeltaTime[end_frame - current_frame] track << NoteOff[channel, current_tone] current_frame = end_frame end track end |
#write_to(path) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/sidtool/midi_file_writer.rb', line 97 def write_to(path) tracks = @synths_for_voices.map { |synths| build_track(synths) } File.open(path, 'wb') do |file| write_header(file) tracks.each_with_index { |track, index| write_track(file, track, "Voice #{index + 1}") } end end |