midi_lyrics 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -5
- data/lib/midi_lyrics/version.rb +1 -1
- data/lib/midi_lyrics.rb +34 -17
- data/spec/fixtures/complete_example.mid +0 -0
- data/spec/fixtures/complete_example.nwc +0 -0
- data/spec/fixtures/repeating_lyrics.mid +0 -0
- data/spec/fixtures/repeating_lyrics.nwc +0 -0
- data/spec/fixtures/spaces_and_returns.mid +0 -0
- data/spec/fixtures/spaces_and_returns.nwc +0 -0
- data/spec/midi_lyrics_spec.rb +44 -41
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f734c528919168e0348b3b936bd4dbd8c0e7041a
|
4
|
+
data.tar.gz: ace7320df7fbaab6d2bda807e1220368cdf1273e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c216ecd753faf7535e8044b3a93204623039ab741dbe9f24ea51e8c1cb2e889a14ef00aab78422b7e87d83832902ea97f6af48f7285c9e5058835f817b62bcce
|
7
|
+
data.tar.gz: 3a554fef7e8359b4983b6f192e23e9913cd116630843c3d2b8ecd08de11c4c94531943b63fa6064746a8b5fd4958a7601c456b4cf491a78c7245c3273ec09066
|
data/README.md
CHANGED
@@ -20,12 +20,11 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
>> MidiLyrics::Parser.new("test.mid").extract
|
23
|
+
>> MidiLyrics::Parser.new("test.mid").extract.collect(&:as_json)
|
24
24
|
=> [
|
25
|
-
{ text: "Test",
|
26
|
-
{ text: "ing
|
27
|
-
{ text: "\r",
|
28
|
-
{ text: "\n", start: 0.917, start2: 0.0, duration: 0 }
|
25
|
+
{ text: "Test", start: 0, start2: 0.0, duration: 0.417 },
|
26
|
+
{ text: "ing", start: 0.5, start2: 0.0, duration: 0.417 },
|
27
|
+
{ text: " \r\n", start: 0.917, start2: 0.0, duration: 0 }
|
29
28
|
]
|
30
29
|
|
31
30
|
## Contributing
|
data/lib/midi_lyrics/version.rb
CHANGED
data/lib/midi_lyrics.rb
CHANGED
@@ -28,6 +28,10 @@ module MidiLyrics
|
|
28
28
|
format_time(duration_in_pulses)
|
29
29
|
end
|
30
30
|
|
31
|
+
def blank?
|
32
|
+
text.strip == ""
|
33
|
+
end
|
34
|
+
|
31
35
|
def similar_to?(another)
|
32
36
|
self.duration_in_pulses == another.duration_in_pulses && self.text == another.text
|
33
37
|
end
|
@@ -73,10 +77,7 @@ module MidiLyrics
|
|
73
77
|
@noteon_track = ::MIDI::Track.new(@sequence)
|
74
78
|
@sequence.tracks[1].each do | event |
|
75
79
|
if event.kind_of?(::MIDI::MetaEvent) && event.meta_type == ::MIDI::META_LYRIC
|
76
|
-
|
77
|
-
if text.gsub(" ", "") != ""
|
78
|
-
@lyrics_track.events << event
|
79
|
-
end
|
80
|
+
@lyrics_track.events << event
|
80
81
|
end
|
81
82
|
if event.kind_of?(::MIDI::NoteOn)
|
82
83
|
@noteon_track.events << event
|
@@ -92,13 +93,25 @@ module MidiLyrics
|
|
92
93
|
end
|
93
94
|
|
94
95
|
def load_lyrics
|
95
|
-
@lyrics =
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
96
|
+
@lyrics = []
|
97
|
+
@lyrics_track.each do |event|
|
98
|
+
parts = event.data.collect{|x| x.chr(Encoding::UTF_8)}.join.match(/([^\s]*)(\s*)/)
|
99
|
+
if parts[1] != ""
|
100
|
+
@lyrics << LyricSyllable.new(
|
101
|
+
sequence: @sequence,
|
102
|
+
start_in_pulses: event.time_from_start,
|
103
|
+
duration_in_pulses: @durations[event.time_from_start],
|
104
|
+
text: parts[1]
|
105
|
+
)
|
106
|
+
end
|
107
|
+
if parts[2] != ""
|
108
|
+
@lyrics << LyricSyllable.new(
|
109
|
+
sequence: @sequence,
|
110
|
+
start_in_pulses: event.time_from_start,
|
111
|
+
duration_in_pulses: 0,
|
112
|
+
text: parts[2]
|
113
|
+
)
|
114
|
+
end
|
102
115
|
end
|
103
116
|
end
|
104
117
|
|
@@ -108,13 +121,17 @@ module MidiLyrics
|
|
108
121
|
end
|
109
122
|
end
|
110
123
|
|
111
|
-
def
|
124
|
+
def consolidate_empty_syllables
|
112
125
|
new_lyrics = []
|
113
126
|
@lyrics.each do |l|
|
114
|
-
if
|
115
|
-
|
116
|
-
|
117
|
-
|
127
|
+
if l.blank?
|
128
|
+
if new_lyrics.last.blank?
|
129
|
+
new_lyrics.last.text += l.text
|
130
|
+
else
|
131
|
+
l.start_in_pulses = new_lyrics.last.start_in_pulses + new_lyrics.last.duration_in_pulses
|
132
|
+
l.duration_in_pulses = 0.0
|
133
|
+
new_lyrics << l
|
134
|
+
end
|
118
135
|
else
|
119
136
|
new_lyrics << l
|
120
137
|
end
|
@@ -151,7 +168,7 @@ module MidiLyrics
|
|
151
168
|
calculate_durations
|
152
169
|
load_lyrics
|
153
170
|
remove_heading_blank_lines
|
154
|
-
|
171
|
+
consolidate_empty_syllables
|
155
172
|
remove_repeating unless repeating
|
156
173
|
@lyrics
|
157
174
|
end
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/spec/midi_lyrics_spec.rb
CHANGED
@@ -12,7 +12,7 @@ describe MidiLyrics do
|
|
12
12
|
let(:lyrics) { MidiLyrics::Parser.new("spec/fixtures/repeating_lyrics.mid").extract }
|
13
13
|
|
14
14
|
it "has text method" do
|
15
|
-
expect(lyrics[1].text).to eq("ing
|
15
|
+
expect(lyrics[1].text).to eq("ing")
|
16
16
|
end
|
17
17
|
|
18
18
|
it "has start_in_pulses method" do
|
@@ -40,7 +40,7 @@ describe MidiLyrics do
|
|
40
40
|
end
|
41
41
|
|
42
42
|
it "has as_json method" do
|
43
|
-
expect(lyrics[1].as_json).to eq({text: "ing
|
43
|
+
expect(lyrics[1].as_json).to eq({text: "ing", start: 0.5, start2: 3.0, duration: 0.417})
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
@@ -49,9 +49,8 @@ describe MidiLyrics do
|
|
49
49
|
expect(
|
50
50
|
MidiLyrics::Parser.new("spec/fixtures/one_note_one_syllable.mid").extract.collect(&:as_json)
|
51
51
|
).to eq([
|
52
|
-
{ text: "Test
|
53
|
-
{ text: "\r", start: QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 }
|
54
|
-
{ text: "\n", start: QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 }
|
52
|
+
{ text: "Test", start: 0.0, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
53
|
+
{ text: " \r\n", start: QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 }
|
55
54
|
])
|
56
55
|
end
|
57
56
|
|
@@ -59,9 +58,8 @@ describe MidiLyrics do
|
|
59
58
|
expect(
|
60
59
|
MidiLyrics::Parser.new("spec/fixtures/two_notes_one_syllable.mid").extract.collect(&:as_json)
|
61
60
|
).to eq([
|
62
|
-
{ text: "Test
|
63
|
-
{ text: "\r", start: 0.5 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0 }
|
64
|
-
{ text: "\n", start: 0.5 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0 }
|
61
|
+
{ text: "Test", start: 0, start2: 0.0, duration: 0.5 + QUARTER_NOTE_DURATION },
|
62
|
+
{ text: " \r\n", start: 0.5 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 }
|
65
63
|
])
|
66
64
|
end
|
67
65
|
|
@@ -70,9 +68,8 @@ describe MidiLyrics do
|
|
70
68
|
MidiLyrics::Parser.new("spec/fixtures/two_notes_two_syllables.mid").extract.collect(&:as_json)
|
71
69
|
).to eq([
|
72
70
|
{ text: "Test", start: 0, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
73
|
-
{ text: "ing
|
74
|
-
{ text: "\r", start: 0.5 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0 }
|
75
|
-
{ text: "\n", start: 0.5 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0 }
|
71
|
+
{ text: "ing", start: 0.5, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
72
|
+
{ text: " \r\n", start: 0.5 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 }
|
76
73
|
])
|
77
74
|
end
|
78
75
|
|
@@ -81,13 +78,14 @@ describe MidiLyrics do
|
|
81
78
|
MidiLyrics::Parser.new("spec/fixtures/spaces_and_returns.mid").extract.collect(&:as_json)
|
82
79
|
).to eq([
|
83
80
|
{ text: "Test", start: 0.5, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
84
|
-
{ text: "ing
|
85
|
-
{ text: "\r", start: 1 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0 },
|
81
|
+
{ text: "ing", start: 1, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
82
|
+
{ text: " \r", start: 1 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
86
83
|
{ text: "One", start: 1.5, start2: 0.0, duration: HALF_NOTE_DURATION },
|
84
|
+
{ text: " ", start: 1.5 + HALF_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
87
85
|
{ text: "Two", start: 2.5, start2: 0.0, duration: HALF_NOTE_DURATION },
|
88
|
-
{ text: "
|
89
|
-
{ text: "
|
90
|
-
{ text: "\n", start: 3.5 + HALF_NOTE_DURATION, start2: 0.0, duration: 0 }
|
86
|
+
{ text: " ", start: 2.5 + HALF_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
87
|
+
{ text: "Three", start: 3.5, start2: 0.0, duration: HALF_NOTE_DURATION },
|
88
|
+
{ text: " \r\n", start: 3.5 + HALF_NOTE_DURATION, start2: 0.0, duration: 0.0 }
|
91
89
|
])
|
92
90
|
end
|
93
91
|
|
@@ -96,21 +94,23 @@ describe MidiLyrics do
|
|
96
94
|
MidiLyrics::Parser.new("spec/fixtures/repeating_lyrics.mid", repeating: true).extract.collect(&:as_json)
|
97
95
|
).to eq([
|
98
96
|
{ text: "Test", start: 0.0, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
99
|
-
{ text: "ing
|
100
|
-
{ text: "\r", start: 0.5 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
97
|
+
{ text: "ing", start: 0.5, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
98
|
+
{ text: " \r", start: 0.5 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
101
99
|
{ text: "One", start: 1.0, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
100
|
+
{ text: " ", start: 1.0 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
102
101
|
{ text: "Two", start: 1.5, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
103
|
-
{ text: "
|
104
|
-
{ text: "
|
105
|
-
{ text: "\n", start: 2.0 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
102
|
+
{ text: " ", start: 1.5 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
103
|
+
{ text: "Three", start: 2.0, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
104
|
+
{ text: " \r\n", start: 2.0 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
106
105
|
{ text: "Test", start: 2.5, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
107
|
-
{ text: "ing
|
108
|
-
{ text: "\r", start: 3.0 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
106
|
+
{ text: "ing", start: 3.0, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
107
|
+
{ text: " \r", start: 3.0 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
109
108
|
{ text: "One", start: 3.5, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
109
|
+
{ text: " ", start: 3.5 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
110
110
|
{ text: "Two", start: 4.0, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
111
|
-
{ text: "
|
112
|
-
{ text: "
|
113
|
-
{ text: "\n", start: 4.5 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 }
|
111
|
+
{ text: " ", start: 4.0 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
112
|
+
{ text: "Three", start: 4.5, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
113
|
+
{ text: " \r\n", start: 4.5 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 }
|
114
114
|
])
|
115
115
|
end
|
116
116
|
|
@@ -119,34 +119,37 @@ describe MidiLyrics do
|
|
119
119
|
MidiLyrics::Parser.new("spec/fixtures/repeating_lyrics.mid").extract.collect(&:as_json)
|
120
120
|
).to eq([
|
121
121
|
{ text: "Test", start: 0.0, start2: 2.5, duration: QUARTER_NOTE_DURATION },
|
122
|
-
{ text: "ing
|
123
|
-
{ text: "\r", start: 0.5 + QUARTER_NOTE_DURATION, start2: 3.0 + QUARTER_NOTE_DURATION, duration: 0.0 },
|
122
|
+
{ text: "ing", start: 0.5, start2: 3.0, duration: QUARTER_NOTE_DURATION },
|
123
|
+
{ text: " \r", start: 0.5 + QUARTER_NOTE_DURATION, start2: 3.0 + QUARTER_NOTE_DURATION, duration: 0.0 },
|
124
124
|
{ text: "One", start: 1.0, start2: 3.5, duration: QUARTER_NOTE_DURATION },
|
125
|
+
{ text: " ", start: 1.0 + QUARTER_NOTE_DURATION, start2: 3.5 + QUARTER_NOTE_DURATION, duration: 0.0 },
|
125
126
|
{ text: "Two", start: 1.5, start2: 4.0, duration: QUARTER_NOTE_DURATION },
|
126
|
-
{ text: "
|
127
|
-
{ text: "
|
128
|
-
{ text: "\n", start: 2.0 + QUARTER_NOTE_DURATION, start2: 4.5 + QUARTER_NOTE_DURATION, duration: 0.0 }
|
127
|
+
{ text: " ", start: 1.5 + QUARTER_NOTE_DURATION, start2: 4.0 + QUARTER_NOTE_DURATION, duration: 0.0 },
|
128
|
+
{ text: "Three", start: 2.0, start2: 4.5, duration: QUARTER_NOTE_DURATION },
|
129
|
+
{ text: " \r\n", start: 2.0 + QUARTER_NOTE_DURATION, start2: 4.5 + QUARTER_NOTE_DURATION, duration: 0.0 }
|
129
130
|
])
|
130
131
|
end
|
131
132
|
|
132
133
|
let :parsed_complete_example do
|
133
134
|
[
|
134
135
|
{ text: "Test", start: 0.5, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
135
|
-
{ text: "ing
|
136
|
-
{ text: "\r", start: 1.0 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
136
|
+
{ text: "ing", start: 1.0, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
137
|
+
{ text: " \r", start: 1.0 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
137
138
|
{ text: "One", start: 1.5, start2: 0.0, duration: HALF_NOTE_DURATION },
|
139
|
+
{ text: " ", start: 1.5 + HALF_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
138
140
|
{ text: "Two", start: 2.5, start2: 0.0, duration: HALF_NOTE_DURATION },
|
139
|
-
{ text: "
|
140
|
-
{ text: "
|
141
|
-
{ text: "\n", start: 3.5 + HALF_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
141
|
+
{ text: " ", start: 2.5 + HALF_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
142
|
+
{ text: "Three", start: 3.5, start2: 0.0, duration: HALF_NOTE_DURATION },
|
143
|
+
{ text: " \r\n", start: 3.5 + HALF_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
142
144
|
{ text: "Test", start: 4.5, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
143
|
-
{ text: "ing
|
144
|
-
{ text: "\r", start: 5.0 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
145
|
+
{ text: "ing", start: 5.0, start2: 0.0, duration: QUARTER_NOTE_DURATION },
|
146
|
+
{ text: " \r", start: 5.0 + QUARTER_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
145
147
|
{ text: "Four", start: 5.5, start2: 0.0, duration: HALF_NOTE_DURATION },
|
148
|
+
{ text: " ", start: 5.5 + HALF_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
146
149
|
{ text: "Five", start: 6.5, start2: 0.0, duration: HALF_NOTE_DURATION },
|
147
|
-
{ text: "
|
148
|
-
{ text: "
|
149
|
-
{ text: "\n", start: 7.5 + HALF_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
150
|
+
{ text: " ", start: 6.5 + HALF_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
151
|
+
{ text: "Six", start: 7.5, start2: 0.0, duration: HALF_NOTE_DURATION },
|
152
|
+
{ text: " \r\n", start: 7.5 + HALF_NOTE_DURATION, start2: 0.0, duration: 0.0 },
|
150
153
|
]
|
151
154
|
end
|
152
155
|
|