-
Notifications
You must be signed in to change notification settings - Fork 111
/
test.py
195 lines (166 loc) · 6 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
75
76
77
78
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from midi_util import *
from util import *
import unittest
class TestMIDIUtil(unittest.TestCase):
def test_encode(self):
composition = [
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 1],
[0, 1, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 0]
]
replay = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
volume = [
[0, 0.5, 0, 0],
[0, 0.5, 0, 0],
[0, 0.5, 0, 0.5],
[0, 0.5, 0, 0.5],
[0, 0, 0, 0.5],
[0, 0, 0, 0]
]
pattern = midi_encode(np.stack([composition, replay, volume], 2), step=1)
self.assertEqual(pattern.resolution, NOTES_PER_BEAT)
self.assertEqual(len(pattern), 1)
track = pattern[0]
self.assertEqual(len(track), 4 + 1)
on1, on2, off1, off2 = track[:-1]
self.assertIsInstance(on1, midi.NoteOnEvent)
self.assertIsInstance(on2, midi.NoteOnEvent)
self.assertIsInstance(off1, midi.NoteOffEvent)
self.assertIsInstance(off2, midi.NoteOffEvent)
self.assertEqual(on1.tick, 0)
self.assertEqual(on1.pitch, 1)
self.assertEqual(on2.tick, 2)
self.assertEqual(on2.pitch, 3)
self.assertEqual(off1.tick, 2)
self.assertEqual(off1.pitch, 1)
self.assertEqual(off2.tick, 1)
self.assertEqual(off2.pitch, 3)
def test_decode(self):
# Instantiate a MIDI Pattern (contains a list of tracks)
pattern = midi.Pattern(resolution=96)
# Instantiate a MIDI Track (contains a list of MIDI events)
track = midi.Track()
# Append the track to the pattern
pattern.append(track)
track.append(midi.NoteOnEvent(tick=0, velocity=127, pitch=0))
track.append(midi.NoteOnEvent(tick=96, velocity=127, pitch=1))
track.append(midi.NoteOffEvent(tick=0, velocity=127, pitch=0))
track.append(midi.NoteOffEvent(tick=48, velocity=127, pitch=1))
track.append(midi.EndOfTrackEvent(tick=1))
note_sequence = midi_decode(pattern, 4, step=DEFAULT_RES // 2)
composition = note_sequence[:, :, 0]
np.testing.assert_array_equal(composition, [
[1, 0, 0, 0],
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0]
])
def test_encode_decode(self):
composition = [
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 1],
[0, 1, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 0]
]
replay = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
]
volume = [
[0, 0.5, 0, 0],
[0, 0.5, 0, 0],
[0, 0.5, 0, 0.5],
[0, 0.5, 0, 0.5],
[0, 0, 0, 0.5],
[0, 0, 0, 0]
]
note_seq = midi_decode(midi_encode(np.stack([composition, replay, volume], 2), step=1), 4, step=1)
np.testing.assert_array_equal(composition, note_seq[:, :, 0])
def test_replay_decode(self):
# Instantiate a MIDI Pattern (contains a list of tracks)
pattern = midi.Pattern(resolution=96)
# Instantiate a MIDI Track (contains a list of MIDI events)
track = midi.Track()
# Append the track to the pattern
pattern.append(track)
track.append(midi.NoteOnEvent(tick=0, velocity=127, pitch=1))
track.append(midi.NoteOnEvent(tick=0, velocity=127, pitch=3))
track.append(midi.NoteOffEvent(tick=1, velocity=127, pitch=1))
track.append(midi.NoteOnEvent(tick=2, velocity=127, pitch=1))
track.append(midi.NoteOnEvent(tick=2, velocity=127, pitch=3))
track.append(midi.EndOfTrackEvent(tick=1))
note_seq = midi_decode(pattern, 4, step=3)
np.testing.assert_array_equal(note_seq[:, :, 1], [
[0., 0., 0., 0.],
[0., 0., 0., 1.],
[0., 0., 0., 0.]
])
def test_volume_decode(self):
# Instantiate a MIDI Pattern (contains a list of tracks)
pattern = midi.Pattern(resolution=96)
# Instantiate a MIDI Track (contains a list of MIDI events)
track = midi.Track()
# Append the track to the pattern
pattern.append(track)
track.append(midi.NoteOnEvent(tick=0, velocity=24, pitch=0))
track.append(midi.NoteOnEvent(tick=96, velocity=89, pitch=1))
track.append(midi.NoteOffEvent(tick=0, pitch=0))
track.append(midi.NoteOffEvent(tick=48, pitch=1))
track.append(midi.EndOfTrackEvent(tick=1))
note_seq = midi_decode(pattern, 4, step=DEFAULT_RES // 2)
np.testing.assert_array_almost_equal(note_seq[:, :, 2], [
[24/127, 0., 0., 0.],
[24/127, 0., 0., 0.],
[0., 89/127, 0., 0.],
[0., 0., 0., 0.]
], decimal=5)
def test_replay_encode_decode(self):
# TODO: Fix this test
composition = [
[0, 1, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 1],
[0, 1, 0, 1],
[0, 1, 0, 1],
[0, 1, 0, 1],
[0, 0, 0, 0]
]
replay = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 1],
[0, 1, 0, 1],
[0, 0, 0, 0]
]
volume = [
[0, 0.5, 0, 0.5],
[0, 0, 0, 0.5],
[0, 0, 0, 0.5],
[0, 0.5, 0, 0.5],
[0, 0.5, 0, 0.5],
[0, 0.5, 0, 0.5],
[0, 0, 0, 0]
]
note_seq = midi_decode(midi_encode(np.stack([composition, replay, volume], 2), step=2), 4, step=2)
np.testing.assert_array_equal(composition, note_seq[:, :, 0])
# TODO: Downsampling might have caused loss of information
# np.testing.assert_array_equal(replay, note_seq[:, :, 1])
unittest.main()