Class: MIDI::IO::SeqWriter

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

Instance Method Summary collapse

Constructor Details

#initialize(seq, proc = nil) ⇒ SeqWriter

:yields: num_tracks, index


12
13
14
15
# File 'lib/midilib/io/seqwriter.rb', line 12

def initialize(seq, proc = nil) # :yields: num_tracks, index
	@seq = seq
	@update_block = block_given?() ? Proc.new() : proc
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.


79
80
81
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
110
111
112
# File 'lib/midilib/io/seqwriter.rb', line 79

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
    return 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
    return status
	else
    # Can't compress data
    return status + chan
	end
end

#write16(val) ⇒ Object


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

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

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

#write32(val) ⇒ Object


135
136
137
138
139
140
141
142
143
# File 'lib/midilib/io/seqwriter.rb', line 135

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


145
146
147
148
# File 'lib/midilib/io/seqwriter.rb', line 145

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

#write_headerObject


29
30
31
32
33
34
35
# File 'lib/midilib/io/seqwriter.rb', line 29

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

#write_instrument(instrument) ⇒ Object


114
115
116
117
118
119
# File 'lib/midilib/io/seqwriter.rb', line 114

def write_instrument(instrument)
	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.


18
19
20
21
22
23
24
25
26
27
# File 'lib/midilib/io/seqwriter.rb', line 18

def write_to(io)
	@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


37
38
39
40
41
42
43
44
45
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
# File 'lib/midilib/io/seqwriter.rb', line 37

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_event = nil
	prev_status = 0
	track.events.each do |event|
    if !event.kind_of?(Realtime)
      write_var_len(event.delta_time)
    end

    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_event = event
    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


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

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