Class: MIDI::IO::SeqWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/midilib/io/seqwriter.rb

Instance Method Summary collapse

Constructor Details

#initialize(seq, midi_format = 1, &block) ⇒ SeqWriter

:yields: num_tracks, index



9
10
11
12
13
# File 'lib/midilib/io/seqwriter.rb', line 9

def initialize(seq, midi_format = 1, &block) # :yields: num_tracks, index
  @seq = seq
  @midi_format = midi_format || 1
  @update_block = block
end

Instance Method Details

#possibly_munge_due_to_running_status_byte(data, prev_status) ⇒ Object

If we can use a running status byte, delete the status byte from the given data. Return the status to remember for next time as the running status byte for this event.



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
110
111
112
113
114
115
116
117
# File 'lib/midilib/io/seqwriter.rb', line 84

def possibly_munge_due_to_running_status_byte(data, prev_status)
  status = data[0]
  return status if status >= 0xf0 || prev_status >= 0xf0

  chan = (status & 0x0f)
  return status if chan != (prev_status & 0x0f)

  status = (status & 0xf0)
  prev_status = (prev_status & 0xf0)

  # Both events are on the same channel. If the two status bytes are
  # exactly the same, the rest is trivial. If it's note on/note off,
  # we can combine those further.
  if status == prev_status
    data[0, 1] = [] # delete status byte from data
    status + chan
  elsif status == NOTE_OFF && data[2] == 64
    # If we see a note off and the velocity is 64, we can store
    # a note on with a velocity of 0. If the velocity isn't 64
    # then storing a note on would be bad because the would be
    # changed to 64 when reading the file back in.
    data[2] = 0 # set vel to 0; do before possible shrinking
    status = NOTE_ON + chan
    if prev_status == NOTE_ON
      data[0, 1] = [] # delete status byte
    else
      data[0] = status
    end
    status
  else
    # Can't compress data
    status + chan
  end
end

#write16(val) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/midilib/io/seqwriter.rb', line 133

def write16(val)
  val = (-val | 0x8000) if val < 0

  @io.putc((val >> 8) & 0xff)
  @io.putc(val & 0xff)
  @bytes_written += 2
end

#write32(val) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/midilib/io/seqwriter.rb', line 141

def write32(val)
  val = (-val | 0x80000000) if val < 0

  @io.putc((val >> 24) & 0xff)
  @io.putc((val >> 16) & 0xff)
  @io.putc((val >> 8) & 0xff)
  @io.putc(val & 0xff)
  @bytes_written += 4
end

#write_bytes(bytes) ⇒ Object



151
152
153
154
# File 'lib/midilib/io/seqwriter.rb', line 151

def write_bytes(bytes)
  bytes.each { |b| @io.putc(b) }
  bytes.length
end

#write_headerObject



38
39
40
41
42
43
44
# File 'lib/midilib/io/seqwriter.rb', line 38

def write_header
  @io.print 'MThd'
  write32(6)
  write16(@midi_format) # Ignore sequence format; write as format 1 or 0, default 1
  write16(@seq.tracks.length)
  write16(@seq.ppqn)
end

#write_instrument(instrument) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/midilib/io/seqwriter.rb', line 119

def write_instrument(instrument)
  return if instrument.nil?

  event = MetaEvent.new(META_INSTRUMENT, instrument)
  write_var_len(0)
  data = event.data_as_bytes
  @bytes_written += write_bytes(data)
end

#write_to(io) ⇒ Object

Writes a MIDI format 1 file.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/midilib/io/seqwriter.rb', line 16

def write_to(io)
  if @midi_format == 0
    # merge tracks before writing
    merged_seq = Sequence.new
    merged_track = Track.new(merged_seq)
    merged_seq.tracks << merged_track
    @seq.each do |track|
      merged_track.merge(track.events)
    end
    @seq = merged_seq # replace
  end

  @io = io
  @bytes_written = 0
  write_header
  @update_block.call(nil, @seq.tracks.length, 0) if @update_block
  @seq.tracks.each_with_index do |track, i|
    write_track(track)
    @update_block.call(track, @seq.tracks.length, i) if @update_block
  end
end

#write_track(track) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/midilib/io/seqwriter.rb', line 46

def write_track(track)
  @io.print 'MTrk'
  track_size_file_pos = @io.tell
  write32(0)                # Dummy byte count; overwritten later
  @bytes_written = 0        # Reset after previous write

  write_instrument(track.instrument)

  prev_status = 0
  track.events.each do |event|
    write_var_len(event.delta_time) unless event.is_a?(Realtime)

    data = event.data_as_bytes
    status = data[0] # status byte plus channel number, if any

    # running status byte
    status = possibly_munge_due_to_running_status_byte(data, prev_status)

    @bytes_written += write_bytes(data)

    prev_status = status
  end

  # Write track end event.
  event = MetaEvent.new(META_TRACK_END)
  write_var_len(0)
  @bytes_written += write_bytes(event.data_as_bytes)

  # Go back to beginning of track data and write number of bytes,
  # then come back here to end of file.
  @io.seek(track_size_file_pos)
  write32(@bytes_written)
  @io.seek(0, ::IO::SEEK_END)
end

#write_var_len(val) ⇒ Object



128
129
130
131
# File 'lib/midilib/io/seqwriter.rb', line 128

def write_var_len(val)
  buffer = Utils.as_var_len(val)
  @bytes_written += write_bytes(buffer)
end