midilib 3.0.1 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ChangeLog +12 -12
- data/Credits +12 -0
- data/README.rdoc +10 -1
- data/Rakefile +0 -1
- data/TODO.rdoc +8 -7
- data/lib/midilib/event.rb +2 -2
- data/lib/midilib/info.rb +3 -3
- data/lib/midilib/io/seqreader.rb +28 -31
- data/lib/midilib/io/seqwriter.rb +6 -4
- data/lib/midilib/track.rb +38 -6
- data/test/test_event.rb +12 -0
- data/test/test_io.rb +46 -7
- data/test/test_track.rb +65 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f41957f8222283fa766d489f32dc603edf23fe904682107e216bb5646325fd27
|
4
|
+
data.tar.gz: 2c55d4cb9c3d2255b721725d622936a59456e87e70df4da77cfb1b82f230a338
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 884f3ab3488afa1c717ec571de2ebca279ce1e13caa69039b7e0dec2bcf265c3285169d5abe8500cf9cd60f55d818876774bcd3daa0782f2e76d6c4955b1511f
|
7
|
+
data.tar.gz: fce2b737b8c56017d35e27117bee86f2455a05286df63685b354c8d44666369a28353c4efc4e86a8fe01edbf803bc38a8d8549c8ea94003d66fd30eaea8eb3bd
|
data/ChangeLog
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
This change log is no longer maintaned. For further change descriptions,
|
2
2
|
see the Git logs.
|
3
3
|
|
4
|
-
2007-12-11 Jim Menard <jim@
|
4
|
+
2007-12-11 Jim Menard <jim@jimmenard.com>
|
5
5
|
|
6
6
|
* lib/midilib/io/seqreader.rb: Fixed text method and added default
|
7
7
|
implementation of key_signature.
|
8
8
|
|
9
|
-
2007-12-09 Jim Menard <jim@
|
9
|
+
2007-12-09 Jim Menard <jim@jimmenard.com>
|
10
10
|
|
11
11
|
* examples/measures_mbt.rb: Added.
|
12
12
|
|
13
|
-
2007-12-09 Jim Menard <jim@
|
13
|
+
2007-12-09 Jim Menard <jim@jimmenard.com>
|
14
14
|
|
15
15
|
* lib/midilib/event.rb: Added code that fixes bpm calculation
|
16
16
|
bugs, and his TimeSig and KeySig classes.
|
@@ -19,17 +19,17 @@ see the Git logs.
|
|
19
19
|
|
20
20
|
* lib/midilib/sequence.rb: Fixed clock. Added get_measures method.
|
21
21
|
|
22
|
-
2007-12-09 Jim Menard <jim@
|
22
|
+
2007-12-09 Jim Menard <jim@jimmenard.com>
|
23
23
|
|
24
24
|
* lib/midilib/event.rb: added program_change? and
|
25
25
|
print_channel_numbers_from_one to Event.
|
26
26
|
|
27
|
-
2007-03-17 Jim Menard <
|
27
|
+
2007-03-17 Jim Menard <jim@jimmenard.com>
|
28
28
|
|
29
29
|
* Version 1.0.0 released. I will no longer be maintaining this
|
30
30
|
change log; the Subversion comments should be sufficient.
|
31
31
|
|
32
|
-
2006-08-20 Jim Menard <
|
32
|
+
2006-08-20 Jim Menard <jim@jimmenard.com>
|
33
33
|
|
34
34
|
* lib/midilib/event.rb (PolyPressure::initialize): Fixed the
|
35
35
|
misspelled POLY_PRESSURE cosntant, thanks to Mario Pehle
|
@@ -39,7 +39,7 @@ see the Git logs.
|
|
39
39
|
|
40
40
|
* test/test.mid: created (copied examples/from_scratch.mid).
|
41
41
|
|
42
|
-
2005-03-21 Jim Menard <
|
42
|
+
2005-03-21 Jim Menard <jim@jimmenard.com>
|
43
43
|
|
44
44
|
* Version 0.8.4 released.
|
45
45
|
|
@@ -48,7 +48,7 @@ see the Git logs.
|
|
48
48
|
(SystemCommon::initialize): moved @is_system = true to here
|
49
49
|
(SystemExclusive::initialize): ...from here.
|
50
50
|
|
51
|
-
2005-03-20 Jim Menard <
|
51
|
+
2005-03-20 Jim Menard <jim@jimmenard.com>
|
52
52
|
|
53
53
|
* lib/midilib/sequence.rb (Sequence::note_to_delta): created.
|
54
54
|
(Sequence::note_to_length): created.
|
@@ -56,11 +56,11 @@ see the Git logs.
|
|
56
56
|
|
57
57
|
* examples/from_scratch.rb: created.
|
58
58
|
|
59
|
-
2004-07-16 Jim Menard <
|
59
|
+
2004-07-16 Jim Menard <jim@jimmenard.com>
|
60
60
|
|
61
61
|
* Version 0.8.3 released.
|
62
62
|
|
63
|
-
2004-07-10 Jim Menard <
|
63
|
+
2004-07-10 Jim Menard <jim@jimmenard.com>
|
64
64
|
|
65
65
|
* lib/midilib/event.rb (NoteEvent::note_to_s): created.
|
66
66
|
(Event::number_to_s): created.
|
@@ -68,7 +68,7 @@ see the Git logs.
|
|
68
68
|
attributes.
|
69
69
|
(to_s all classes): use @print_note_names and @print_decimal_numbers
|
70
70
|
|
71
|
-
2004-06-30 Jim Menard <
|
71
|
+
2004-06-30 Jim Menard <jim@jimmenard.com>
|
72
72
|
|
73
73
|
* Version 0.8.2 released.
|
74
74
|
|
@@ -103,7 +103,7 @@ see the Git logs.
|
|
103
103
|
* lib/midilib/io/seqreader.rb (SeqReader::initialize): added block
|
104
104
|
rdoc comment.
|
105
105
|
|
106
|
-
2004-06-27 Jim Menard <
|
106
|
+
2004-06-27 Jim Menard <jim@jimmenard.com>
|
107
107
|
|
108
108
|
* Version 0.8.1 released.
|
109
109
|
|
data/Credits
CHANGED
@@ -91,3 +91,15 @@ Miika Alonen (@amiika on Github)
|
|
91
91
|
@johnkolen on Github
|
92
92
|
|
93
93
|
Pointed out incorrect values for CC_GEN_PURPOSE_{5,6,7,8}.
|
94
|
+
|
95
|
+
@kaorukobo on Github
|
96
|
+
|
97
|
+
Found a bug caused by note offs coming right before note ons. See
|
98
|
+
https://github.com/jimm/midilib/pull/21 for a detailed write-up.
|
99
|
+
|
100
|
+
@mike-bourgeous on Github
|
101
|
+
|
102
|
+
Opened an issue (https://github.com/jimm/midilib/issues/24) which led to
|
103
|
+
the addition of end of track meta events when reading and writing MIDI
|
104
|
+
files.
|
105
|
+
Found and fixed a bug in SongPointer#data_as_bytes.
|
data/README.rdoc
CHANGED
@@ -91,6 +91,15 @@ This bit mask is set when the track is read from the MIDI file by a SeqReader
|
|
91
91
|
but is _not_ kept up to date by any other methods. Specifically, if you add
|
92
92
|
events to a track at any other time, the bit mask will not be updated.
|
93
93
|
|
94
|
+
When a Track is read from a MIDI file, a MIDI::META_TRACK_END event is added
|
95
|
+
to the end if there isn't one in the file already. When a Track is written
|
96
|
+
to a MIDI file, a MIDI::META_TRACK_END event is always output even if the
|
97
|
+
Track does not have one.
|
98
|
+
|
99
|
+
The Track#merge method ensures that there is only one MIDI::META_TRACK_END
|
100
|
+
event after the merge and that it's at its proper place at the end of the
|
101
|
+
list of events. It does so by calling Track#ensure_track_end_meta_event.
|
102
|
+
|
94
103
|
=== MIDI::Measure
|
95
104
|
|
96
105
|
This class contains information about a measure from the sequence. Measure
|
@@ -133,7 +142,7 @@ string. When assigning to a meta event's data, if you pass in a string it will
|
|
133
142
|
get converted to an array of bytes.
|
134
143
|
|
135
144
|
|
136
|
-
== How To Use
|
145
|
+
== How To Use midilib
|
137
146
|
|
138
147
|
The following examples show you how to use midilib to read, write, and
|
139
148
|
manipulate MIDI files and modify track events. See also the files in the
|
data/Rakefile
CHANGED
@@ -33,7 +33,6 @@ spec = Gem::Specification.new do |s|
|
|
33
33
|
s.author = 'Jim Menard'
|
34
34
|
s.email = 'jim@jimmenard.com'
|
35
35
|
s.homepage = 'http://github.com/jimm/midilib'
|
36
|
-
s.rubyforge_project = PROJECT_NAME
|
37
36
|
s.license = 'Ruby'
|
38
37
|
|
39
38
|
s.summary = 'MIDI file and event manipulation library'
|
data/TODO.rdoc
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
== Bugs
|
2
2
|
|
3
3
|
midilib does not handle tempo changes when calculating +beats_per_minute+.
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
See https://github.com/jimm/midilib/issues/8 which describes the issue. The
|
5
|
+
tempo events are correctly handled when reading/writing/moving them around,
|
6
|
+
it's just the functions that answer questions about the current tempo that
|
7
|
+
are wrong. See https://github.com/jimm/midilib/issues/8. The method
|
8
|
+
+beats_per_minute+ and related methods like +pulses_to_seconds+ will have to
|
9
|
+
take into account the possibility of more than one tempo event. They will
|
10
|
+
probably have to take new arguments specifying where in the sequence the
|
11
|
+
beats or pulses are being requested. For example we could have
|
11
12
|
+beats_per_minute(at_seconds: 0.0, at_beat: 0.0)+ (where the two keyword
|
12
13
|
args are mutually exclusive). Perhaps there should be separately named
|
13
14
|
public methods like +beats_per_minute_at_seconds+ and
|
data/lib/midilib/event.rb
CHANGED
data/lib/midilib/info.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module MIDI
|
2
|
-
VERSION_MAJOR =
|
2
|
+
VERSION_MAJOR = 4
|
3
3
|
VERSION_MINOR = 0
|
4
|
-
VERSION_TWEAK =
|
4
|
+
VERSION_TWEAK = 0
|
5
5
|
Version = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_TWEAK}"
|
6
|
-
Copyright = 'Copyright (c) 2003-
|
6
|
+
Copyright = 'Copyright (c) 2003-2023 by Jim Menard <jim@jimmenard.com>'
|
7
7
|
end
|
data/lib/midilib/io/seqreader.rb
CHANGED
@@ -8,16 +8,9 @@ module MIDI
|
|
8
8
|
# callback methods for each MIDI event and use them to build Track and
|
9
9
|
# Event objects and give the tracks to a Sequence.
|
10
10
|
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# the track's start (see end_track below).
|
15
|
-
#
|
16
|
-
# META_TRACK_END events are not added to tracks. This way, we don't have
|
17
|
-
# to worry about making sure the last event is always a track end event.
|
18
|
-
# We rely on the SeqWriter to append a META_TRACK_END event to each
|
19
|
-
# track when it is output.
|
20
|
-
|
11
|
+
# Ensures that each track ends with an end of track meta event, and that
|
12
|
+
# Track#recalc_times is called at the end of the track so it can update
|
13
|
+
# each event with its time from the track's start (see end_track below).
|
21
14
|
class SeqReader < MIDIFile
|
22
15
|
# The optional &block is called once at the start of the file and
|
23
16
|
# again at the end of each track. There are three arguments to the
|
@@ -51,12 +44,9 @@ module MIDI
|
|
51
44
|
@pending.each { |on| make_note_off(on, 64) }
|
52
45
|
@pending = nil
|
53
46
|
|
54
|
-
#
|
55
|
-
#
|
56
|
-
|
57
|
-
|
58
|
-
# Let the track calculate event times from start of track. This is
|
59
|
-
# in lieu of calling Track.add for each event.
|
47
|
+
# Make sure track has an end of track event and that all of the
|
48
|
+
# `time_from_start` values are correct.
|
49
|
+
@track.ensure_track_end_meta_event
|
60
50
|
@track.recalc_times
|
61
51
|
|
62
52
|
# Store bitmask of all channels used into track
|
@@ -81,16 +71,29 @@ module MIDI
|
|
81
71
|
def note_off(chan, note, vel)
|
82
72
|
# Find note on, create note off, connect the two, and remove
|
83
73
|
# note on from pending list.
|
74
|
+
|
75
|
+
corresp_note_on = nil
|
76
|
+
|
84
77
|
@pending.each_with_index do |on, i|
|
85
78
|
next unless on.note == note && on.channel == chan
|
86
79
|
|
87
|
-
make_note_off(on, vel)
|
88
80
|
@pending.delete_at(i)
|
89
|
-
|
81
|
+
corresp_note_on = on
|
82
|
+
break
|
90
83
|
end
|
91
|
-
|
92
|
-
|
93
|
-
|
84
|
+
|
85
|
+
if corresp_note_on
|
86
|
+
make_note_off(corresp_note_on, vel)
|
87
|
+
else
|
88
|
+
# When a corresponding note on is missing,
|
89
|
+
# keep note off as input with lefting on/off attr to nil.
|
90
|
+
off = NoteOff.new(chan, note, vel, @curr_ticks)
|
91
|
+
@track.events << off
|
92
|
+
|
93
|
+
if $DEBUG
|
94
|
+
warn "note off with no earlier note on (ch #{chan}, note" +
|
95
|
+
" #{note}, vel #{vel})"
|
96
|
+
end
|
94
97
|
end
|
95
98
|
end
|
96
99
|
|
@@ -130,6 +133,10 @@ module MIDI
|
|
130
133
|
@track.events << SystemExclusive.new(msg, @curr_ticks)
|
131
134
|
end
|
132
135
|
|
136
|
+
def eot
|
137
|
+
@track.events << MetaEvent.new(META_TRACK_END, nil, @curr_ticks)
|
138
|
+
end
|
139
|
+
|
133
140
|
def meta_misc(type, msg)
|
134
141
|
@track.events << MetaEvent.new(type, msg, @curr_ticks)
|
135
142
|
end
|
@@ -155,16 +162,6 @@ module MIDI
|
|
155
162
|
end
|
156
163
|
end
|
157
164
|
|
158
|
-
# --
|
159
|
-
# Don't bother adding the META_TRACK_END event to the track. This way,
|
160
|
-
# we don't have to worry about always making sure the last event is
|
161
|
-
# always a track end event. We just have to make sure to write one when
|
162
|
-
# the track is output back to a file.
|
163
|
-
# def eot()
|
164
|
-
# @track.events << MetaEvent.new(META_TRACK_END, nil, @curr_ticks)
|
165
|
-
# end
|
166
|
-
# ++
|
167
|
-
|
168
165
|
def time_signature(numer, denom, clocks, qnotes)
|
169
166
|
@seq.time_signature(numer, denom, clocks, qnotes)
|
170
167
|
@track.events << TimeSig.new(numer, denom, clocks, qnotes, @curr_ticks)
|
data/lib/midilib/io/seqwriter.rb
CHANGED
@@ -66,10 +66,12 @@ module MIDI
|
|
66
66
|
prev_status = status
|
67
67
|
end
|
68
68
|
|
69
|
-
# Write track end event.
|
70
|
-
|
71
|
-
|
72
|
-
|
69
|
+
# Write track end event if the track doesn't have one.
|
70
|
+
unless track.events.last.is_a?(MetaEvent) && track.events.last.meta_type == META_TRACK_END
|
71
|
+
event = MetaEvent.new(META_TRACK_END)
|
72
|
+
write_var_len(0)
|
73
|
+
@bytes_written += write_bytes(event.data_as_bytes)
|
74
|
+
end
|
73
75
|
|
74
76
|
# Go back to beginning of track data and write number of bytes,
|
75
77
|
# then come back here to end of file.
|
data/lib/midilib/track.rb
CHANGED
@@ -59,11 +59,48 @@ module MIDI
|
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
|
+
# Iterates over events, yielding each one.
|
63
|
+
def each(&block) # :yields: event
|
64
|
+
@events.each(&block)
|
65
|
+
end
|
66
|
+
|
67
|
+
# If `event` exists in @events, deletes it, updates the delta time of
|
68
|
+
# the event after it, and calls `recalc_times` by default.
|
69
|
+
def delete_event(event, call_recalc_times = true)
|
70
|
+
i = @events.index(event)
|
71
|
+
return unless i
|
72
|
+
|
73
|
+
@events[i + 1].delta_time += @events[i].delta_time if i != (@events.length - 1)
|
74
|
+
@events.delete_at(i)
|
75
|
+
recalc_times if call_recalc_times
|
76
|
+
end
|
77
|
+
|
78
|
+
# Makes sure that we have one and only one end track meta event at the
|
79
|
+
# end of this track.
|
80
|
+
def ensure_track_end_meta_event
|
81
|
+
track_ends = @events.select { |e| e.is_a?(MetaEvent) && e.meta_type == META_TRACK_END }
|
82
|
+
has_end = !@events.empty? && track_ends[-1] == @events.last
|
83
|
+
|
84
|
+
# If we only have one end event and it's the last one, there's nothing
|
85
|
+
# to do.
|
86
|
+
return if track_ends.length == 1 && has_end
|
87
|
+
|
88
|
+
# If we have an end of track event already, leave it alone.
|
89
|
+
track_ends.pop if has_end
|
90
|
+
track_ends.each { |track_end| delete_event(track_end, false) }
|
91
|
+
return if has_end
|
92
|
+
|
93
|
+
mte = MetaEvent.new(META_TRACK_END, nil, 0)
|
94
|
+
mte.time_from_start = @events.last.time_from_start + mte.delta_time if @events.last
|
95
|
+
@events << mte
|
96
|
+
end
|
97
|
+
|
62
98
|
# Merges an array of events into our event list. After merging, the
|
63
99
|
# events' time_from_start values are correct so you don't need to worry
|
64
|
-
# about calling recalc_times.
|
100
|
+
# about calling #recalc_times.
|
65
101
|
def merge(event_list)
|
66
102
|
@events = merge_event_lists(@events, event_list)
|
103
|
+
ensure_track_end_meta_event
|
67
104
|
end
|
68
105
|
|
69
106
|
# Merges two event arrays together. Does not modify this track.
|
@@ -121,11 +158,6 @@ module MIDI
|
|
121
158
|
end
|
122
159
|
end
|
123
160
|
|
124
|
-
# Iterate over events.
|
125
|
-
def each(&block) # :yields: event
|
126
|
-
@events.each(&block)
|
127
|
-
end
|
128
|
-
|
129
161
|
# Sort events by their time_from_start. After sorting,
|
130
162
|
# recalc_delta_from_times is called to make sure that the delta times
|
131
163
|
# reflect the possibly new event order.
|
data/test/test_event.rb
CHANGED
@@ -130,4 +130,16 @@ class EventTester < Test::Unit::TestCase
|
|
130
130
|
assert_equal('foobar', e.data_as_str)
|
131
131
|
assert_equal(foobar_as_array, e.data)
|
132
132
|
end
|
133
|
+
|
134
|
+
def test_song_pointer
|
135
|
+
e = MIDI::SongPointer.new(1)
|
136
|
+
b = e.data_as_bytes
|
137
|
+
assert_equal(1, b[1]) # lsb, 7 bits
|
138
|
+
assert_equal(0, b[2]) # msb, 7 bits
|
139
|
+
|
140
|
+
e.pointer = (3 << 7) + 42
|
141
|
+
b = e.data_as_bytes
|
142
|
+
assert_equal(42, b[1]) # lsb, 7 bits
|
143
|
+
assert_equal(3, b[2]) # msb, 7 bits
|
144
|
+
end
|
133
145
|
end
|
data/test/test_io.rb
CHANGED
@@ -35,7 +35,12 @@ class IOTester < Test::Unit::TestCase
|
|
35
35
|
assert_equal(1, format0_seq.tracks.length, 'number of tracks differ')
|
36
36
|
format_1_count = multitrack_seq.tracks.map { |t| t.events.count }.reduce(:+)
|
37
37
|
format_0_count = format0_seq.tracks.map { |t| t.events.count }.reduce(:+)
|
38
|
-
|
38
|
+
|
39
|
+
# The format 1 file will have one more event because there is an end of
|
40
|
+
# track meta event at the end of each track (the track 0 metadata track
|
41
|
+
# and track 1 with the notes), whereas the format 0 file only has one
|
42
|
+
# track, thus one end of track meta event.
|
43
|
+
assert_equal(format_1_count, format_0_count + 1, 'different number of total events')
|
39
44
|
end
|
40
45
|
|
41
46
|
def test_read_and_write
|
@@ -101,17 +106,46 @@ class IOTester < Test::Unit::TestCase
|
|
101
106
|
assert_equal(MIDI::GM_PATCH_NAMES[0], seq.tracks[1].instrument)
|
102
107
|
end
|
103
108
|
|
104
|
-
|
109
|
+
# This is a regression test.
|
110
|
+
def test_read_eot_preserves_delta
|
111
|
+
seq = MIDI::Sequence.new
|
112
|
+
File.open(SEQ_TEST_FILE, 'rb') { |f| seq.read(f) }
|
113
|
+
track = seq.tracks.last
|
114
|
+
mte = MIDI::MetaEvent.new(MIDI::META_TRACK_END, nil, 123)
|
115
|
+
track.events << mte
|
116
|
+
track.recalc_times
|
117
|
+
File.open(OUTPUT_FILE, 'wb') { |f| seq.write(f) }
|
118
|
+
File.open(OUTPUT_FILE, 'rb') { |f| seq.read(f) }
|
119
|
+
|
120
|
+
assert_equal(mte, seq.tracks.last.events.last)
|
121
|
+
ensure
|
122
|
+
File.delete(OUTPUT_FILE) if File.exist?(OUTPUT_FILE)
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_preserve_deltas_in_some_situations
|
105
126
|
out_seq = MIDI::Sequence.new
|
106
127
|
out_track = MIDI::Track.new(out_seq)
|
107
128
|
out_seq.tracks << out_track
|
108
129
|
out_track.events << MIDI::Tempo.new(MIDI::Tempo.bpm_to_mpq(120))
|
130
|
+
|
131
|
+
# 1) The meta events with non-zero delta time
|
109
132
|
# Normally copyright and sequence name events are at time 0, but non-zero
|
110
133
|
# start times are allowed.
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
134
|
+
begin
|
135
|
+
out_track.events << MIDI::MetaEvent.new(MIDI::META_COPYRIGHT, '(C) 1950 Donald Duck', 100)
|
136
|
+
out_track.events << MIDI::MetaEvent.new(MIDI::META_SEQ_NAME, 'Quack, Track 1', 200)
|
137
|
+
out_track.events << MIDI::NoteOn.new(0, 64, 127, 0)
|
138
|
+
out_track.events << MIDI::NoteOff.new(0, 64, 127, 100)
|
139
|
+
end
|
140
|
+
|
141
|
+
# 2) The unusual note off event with non-zero delta time
|
142
|
+
begin
|
143
|
+
out_track.events << MIDI::NoteOff.new(0, 65, 127, 120)
|
144
|
+
out_track.events << MIDI::NoteOn.new(0, 65, 127, 0)
|
145
|
+
# Add note off (which will be complemented at #end_track if missing) for later comparison.
|
146
|
+
out_track.events << MIDI::NoteOff.new(0, 65, 127, 230)
|
147
|
+
end
|
148
|
+
|
115
149
|
File.open('/tmp/midilib_test.mid', 'wb') { |file| out_seq.write(file) }
|
116
150
|
|
117
151
|
# Although start times are not written out to the MIDI file, we
|
@@ -122,9 +156,14 @@ class IOTester < Test::Unit::TestCase
|
|
122
156
|
in_seq = MIDI::Sequence.new
|
123
157
|
File.open(TEMPFILE, 'rb') { |file| in_seq.read(file) }
|
124
158
|
in_track = in_seq.tracks[0]
|
125
|
-
assert_equal(out_track.events.length, in_track.events.length)
|
159
|
+
assert_equal(out_track.events.length + 1, in_track.events.length) # read added end of track meta event
|
126
160
|
out_track.events.each_with_index do |event, i|
|
127
161
|
assert_equal(event, in_track.events[i])
|
128
162
|
end
|
163
|
+
|
164
|
+
# Last event is a end of track meta event
|
165
|
+
e = in_track.events.last
|
166
|
+
assert(e.is_a?(MIDI::MetaEvent))
|
167
|
+
assert(e.meta_type == MIDI::META_TRACK_END)
|
129
168
|
end
|
130
169
|
end
|
data/test/test_track.rb
CHANGED
@@ -61,7 +61,8 @@ class TrackTester < Test::Unit::TestCase
|
|
61
61
|
def test_merge
|
62
62
|
list = (1..12).collect { |i| MIDI::NoteOn.new(0, 64, 64, 10) }
|
63
63
|
@track.merge(list)
|
64
|
-
|
64
|
+
# We merged 15 events, but an end of track meta event was added by merge
|
65
|
+
assert_equal(16, @track.events.length)
|
65
66
|
assert_equal(10, @track.events[0].time_from_start)
|
66
67
|
assert_equal(10, @track.events[0].delta_time)
|
67
68
|
assert_equal(20, @track.events[1].time_from_start)
|
@@ -79,6 +80,7 @@ class TrackTester < Test::Unit::TestCase
|
|
79
80
|
assert_equal(120, @track.events[12].time_from_start)
|
80
81
|
assert_equal(200, @track.events[13].time_from_start)
|
81
82
|
assert_equal(300, @track.events[14].time_from_start)
|
83
|
+
assert_equal(300, @track.events[15].time_from_start) # end of track meta event
|
82
84
|
end
|
83
85
|
|
84
86
|
def test_recalc_delta_from_times
|
@@ -140,4 +142,66 @@ class TrackTester < Test::Unit::TestCase
|
|
140
142
|
x = MIDI::NoteOff.new(0, 64, 64, 10)
|
141
143
|
assert(x.is_a?(MIDI::NoteOffEvent)) # old name
|
142
144
|
end
|
145
|
+
|
146
|
+
def test_delete_event
|
147
|
+
# Event is not in the track; nothing happens
|
148
|
+
@track.delete_event(MIDI::Controller.new(0, 64, 64, 200))
|
149
|
+
assert_equal(3, @track.events.length)
|
150
|
+
|
151
|
+
# Make sure we update delta times and that start times are preserved
|
152
|
+
e = @track.events[1]
|
153
|
+
@track.delete_event(e)
|
154
|
+
assert_equal(2, @track.events.length)
|
155
|
+
assert(@track.events.index(e).nil?)
|
156
|
+
assert_equal([100, 200], @track.events.map(&:delta_time))
|
157
|
+
assert_equal([100, 300], @track.events.map(&:time_from_start))
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_ensure_track_end_meta_event
|
161
|
+
@track.ensure_track_end_meta_event
|
162
|
+
assert_equal(4, @track.events.length)
|
163
|
+
e = @track.events.last
|
164
|
+
assert(e.is_a?(MIDI::MetaEvent))
|
165
|
+
assert_equal(MIDI::META_TRACK_END, e.meta_type)
|
166
|
+
assert_equal(0, e.delta_time)
|
167
|
+
assert_equal(@track.events[-2].time_from_start, e.time_from_start)
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_ensure_track_end_meta_event_removes_duplicates
|
171
|
+
mte = MIDI::MetaEvent.new(MIDI::META_TRACK_END, nil, 0)
|
172
|
+
@track.events << mte
|
173
|
+
@track.events.unshift(mte.dup)
|
174
|
+
@track.events.unshift(mte.dup)
|
175
|
+
|
176
|
+
@track.ensure_track_end_meta_event
|
177
|
+
mtes = @track.events.select { |e| e.is_a?(MIDI::MetaEvent) && e.meta_type == MIDI::META_TRACK_END }
|
178
|
+
assert_equal(1, mtes.length)
|
179
|
+
assert(@track.events.last.is_a?(MIDI::MetaEvent) && @track.events.last.meta_type == MIDI::META_TRACK_END)
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_ensure_track_end_with_dupes_does_not_shrink_track
|
183
|
+
mte = MIDI::MetaEvent.new(MIDI::META_TRACK_END, nil, 123)
|
184
|
+
@track.events.unshift(mte.dup)
|
185
|
+
@track.events << mte
|
186
|
+
@track.recalc_times
|
187
|
+
start_time = @track.events.last.time_from_start
|
188
|
+
|
189
|
+
@track.ensure_track_end_meta_event
|
190
|
+
mtes = @track.events.select { |e| e.is_a?(MIDI::MetaEvent) && e.meta_type == MIDI::META_TRACK_END }
|
191
|
+
assert_equal(1, mtes.length)
|
192
|
+
assert_equal(mte, mtes[0])
|
193
|
+
|
194
|
+
# As a side effect, ensure_track_end_meta_event calls recalc_times which
|
195
|
+
# in this case will modify the start time of mte.
|
196
|
+
assert_equal(start_time, mte.time_from_start)
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_ensure_track_end_adds_to_empty_track
|
200
|
+
t = MIDI::Track.new(@seq)
|
201
|
+
t.ensure_track_end_meta_event
|
202
|
+
|
203
|
+
assert_equal(1, t.events.length)
|
204
|
+
mte = t.events.first
|
205
|
+
assert(mte.is_a?(MIDI::MetaEvent) && mte.meta_type == MIDI::META_TRACK_END)
|
206
|
+
end
|
143
207
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: midilib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Menard
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-02-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
midilib is a pure Ruby MIDI library useful for reading and
|
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
79
|
version: '0'
|
80
80
|
requirements:
|
81
81
|
- none
|
82
|
-
rubygems_version: 3.3
|
82
|
+
rubygems_version: 3.4.3
|
83
83
|
signing_key:
|
84
84
|
specification_version: 4
|
85
85
|
summary: MIDI file and event manipulation library
|