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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
|
# Solfege - free ear training software
# Copyright (C) 2007, 2008, 2011, 2016 Tom Cato Amundsen
# License is GPL, see file COPYING
import unittest
from solfege import mpd
from solfege.testlib import TmpFileBase
from solfege.mpd.track import Track, MidiEventStream
from solfege.mpd.rat import Rat
from solfege import lessonfile
from solfege import cfg
class TmpFile(TmpFileBase):
parserclass = lessonfile.QuestionsLessonfile
class TestTrack(unittest.TestCase):
def test_simple1(self):
t = Track()
t.note(4, 90, 127)
self.assertEqual(
list(MidiEventStream(t)),
[('program-change', 0, 0),
('volume', 0, 100),
('note-on', 0, 90, 127),
('notelen-time', Rat(1, 4)),
('note-off', 0, 90, 127)])
def test_1voice_setpatch(self):
t = Track()
t.note(4, 90, 127)
t.set_patch(3)
t.note(4, 91, 127)
self.assertEqual(
list(MidiEventStream(t)),
[('program-change', 0, 0),
('program-change', 1, 3),
('volume', 0, 100),
('note-on', 0, 90, 127),
('notelen-time', Rat(1, 4)),
('note-off', 0, 90, 127),
('volume', 1, 100),
('note-on', 1, 91, 127),
('notelen-time', Rat(1, 4)),
('note-off', 1, 91, 127),
])
def test_midi_overlap(self):
t1 = Track()
t1.note(8, 92, 121)
t1.note(8, 90, 122)
t1.note(8, 92, 123)
t1.note(8, 94, 124)
t2 = Track()
t2.note(2, 90, 120)
m = MidiEventStream(t1, t2)
self.assertEqual("p0:0 p1:0 v0:100 n0:92 n0:90 d1/8 o92 v1:100 n1:90 d1/8 o90 n0:92 d1/8 o92 n0:94 d1/8 o94 o90", m.str_repr(1))
def test_add(self):
t1 = Track()
t1.note(8, 92, 121)
self.assertEqual("n92 d1/8 o92", t1.str_repr())
t2 = Track()
t2.note(2, 90, 120)
self.assertEqual("n90 d1/2 o90", t2.str_repr())
self.assertEqual("n92 d1/8 o92", t1.str_repr())
self.assertEqual("n90 d1/2 o90", t2.str_repr())
m1 = MidiEventStream(t1)
m2 = MidiEventStream(t2)
t3 = t1 t2
self.assertEqual("n92 d1/8 o92 n90 d1/2 o90", t3.str_repr())
m3 = MidiEventStream(t3)
self.assertEqual("n92 d1/8 o92", t1.str_repr())
self.assertEqual("n90 d1/2 o90", t2.str_repr())
# FIXME volume bug?
self.assertEqual("p0:0 v0:100 n0:92 d1/8 o92", m1.str_repr(1))
self.assertEqual("p0:0 v0:100 n0:90 d1/2 o90", m2.str_repr(1))
self.assertEqual("p0:0 v0:100 n0:92 d1/8 o92 n0:90 d1/2 o90", m3.str_repr(1))
class TestMidiEventStream(TmpFile):
def setUp(self):
TmpFile.setUp(self)
cfg.set_bool('config/override_default_instrument', True)
def test_track1(self):
t = Track()
t.prepend_patch(33)
t.note(4, 60, 120)
def test_3instr(self):
t1 = Track()
t1.set_patch(3)
t1.note(4, 93, 127)
t2 = Track()
t2.set_patch(4)
t2.note(4, 94, 127)
t3 = Track()
t3.set_patch(5)
t3.note(4, 95, 127)
self.assertEqual(
list(MidiEventStream(t1, t2, t3)),
[('program-change', 0, 3),
('program-change', 1, 4),
('program-change', 2, 5),
('volume', 0, 100),
('note-on', 0, 93, 127),
('volume', 1, 100),
('note-on', 1, 94, 127),
('volume', 2, 100),
('note-on', 2, 95, 127),
('notelen-time', Rat(1, 4)),
('note-off', 0, 93, 127),
('note-off', 1, 94, 127),
('note-off', 2, 95, 127)])
self.assertEqual(
MidiEventStream(t1, t2, t3).str_repr(details=1),
"p0:3 p1:4 p2:5 v0:100 n0:93 v1:100 n1:94 v2:100 n2:95 d1/4 o93 o94 o95")
def test_str_repr(self):
t1 = Track()
t1.set_volume(88)
t1.set_patch(3)
t1.note(4, 93, 127)
t2 = Track()
t2.set_patch(4)
t2.note(4, 94, 127)
t3 = Track()
t3.set_patch(5)
t3.note(4, 95, 127)
self.assertEqual(
list(MidiEventStream(t1, t2, t3)),
[('program-change', 0, 3),
('program-change', 1, 4),
('program-change', 2, 5),
('volume', 0, 88),
('note-on', 0, 93, 127),
('volume', 1, 100),
('note-on', 1, 94, 127),
('volume', 2, 100),
('note-on', 2, 95, 127),
('notelen-time', Rat(1, 4)),
('note-off', 0, 93, 127),
('note-off', 1, 94, 127),
('note-off', 2, 95, 127)])
self.assertEqual(
MidiEventStream(t1, t2, t3).str_repr(details=1),
"p0:3 p1:4 p2:5 v0:88 n0:93 v1:100 n1:94 v2:100 n2:95 d1/4 o93 o94 o95")
def test_track2(self):
self.do_file("""
header { random_transpose = no }
question { music = music("\staff{ c'' }"
"\\addvoice{ e' }"
"\staff{ c }")
}
""")
self.p._idx = 0
mpd.music_to_tracklist(self.p.get_question()['music'].get_mpd_music_string(self.p))
def test_track3(self):
self.do_file("""
header { random_transpose = no }
question { music = music("\staff{ c''1 c''1 }"
"\\addvoice{ r4 e'2. e'1 }"
"\staff{ r4 r g2 g1 }"
"\\addvoice{ r4 r r c c1}")
}
""")
self.p._idx = 0
tracklist = mpd.music_to_tracklist(self.p.get_question()['music'].get_mpd_music_string(self.p))
track_fasit = ["n72 d1/1 o72 n72 d1/1 o72",
"d1/4 n64 d3/4 o64 n64 d1/1 o64",
"d1/2 n55 d1/2 o55 n55 d1/1 o55",
"d3/4 n48 d1/4 o48 n48 d1/1 o48"]
for idx, correct in enumerate(track_fasit):
self.assertEqual(tracklist[idx].str_repr(), correct)
for idx in range(4):
tracklist[idx].prepend_patch(idx 1)
track_fasit[idx] = "p%i " % (idx 1) track_fasit[idx]
for idx, correct in enumerate(track_fasit):
self.assertEqual(tracklist[idx].str_repr(), correct)
self.assertEqual(
MidiEventStream(*tracklist).str_repr(details=1),
"p0:1 p1:2 p2:3 p3:4 v0:100 n0:72 d1/4 "
"v1:100 n1:64 d1/4 "
"v2:100 n2:55 d1/4 "
"v3:100 n3:48 d1/4 "
"o72 o64 o55 o48 "
"n0:72 n1:64 n2:55 n3:48 d1/1 "
"o72 o64 o55 o48")
def test_track3_1(self):
self.do_file("""
header { random_transpose = no }
question { music = music("\staff{ c''1 c''1 }"
"\staff{ r4 r g2 g1 }")
}
""")
self.p._idx = 0
tracklist = mpd.music_to_tracklist(self.p.get_question()['music'].get_mpd_music_string(self.p))
track_fasit = ["n72 d1/1 o72 n72 d1/1 o72",
"d1/2 n55 d1/2 o55 n55 d1/1 o55"]
for idx, correct in enumerate(track_fasit):
self.assertEqual(tracklist[idx].str_repr(), correct)
for idx in range(2):
tracklist[idx].prepend_patch(idx 1)
track_fasit[idx] = "p%i " % (idx 1) track_fasit[idx]
for idx, correct in enumerate(track_fasit):
self.assertEqual(tracklist[idx].str_repr(), correct)
self.assertEqual(
MidiEventStream(*tracklist).str_repr(details=1),
"p0:1 p1:2 v0:100 n0:72 d1/2 v1:100 n1:55 d1/2 o72 o55 n0:72 n1:55 d1/1 o72 o55")
def test_track3_2(self):
self.do_file(r"""
header { random_transpose = no }
question { music = music("\staff{ c''1 c''1 }"
"\staff{ r4 r g2 g1 }"
"\staff{ r4 r r c c1}")
}
""")
self.p._idx = 0
tracklist = mpd.music_to_tracklist(self.p.get_question()['music'].get_mpd_music_string(self.p))
track_fasit = ["n72 d1/1 o72 n72 d1/1 o72",
"d1/2 n55 d1/2 o55 n55 d1/1 o55",
"d3/4 n48 d1/4 o48 n48 d1/1 o48"]
for idx, correct in enumerate(track_fasit):
self.assertEqual(tracklist[idx].str_repr(), correct)
for idx in range(3):
tracklist[idx].prepend_patch(idx 1)
track_fasit[idx] = "p%i " % (idx 1) track_fasit[idx]
for idx, correct in enumerate(track_fasit):
self.assertEqual(tracklist[idx].str_repr(), correct)
self.assertEqual(
MidiEventStream(*tracklist).str_repr(details=1),
"p0:1 p1:2 p2:3 v0:100 n0:72 d1/2 "
"v1:100 n1:55 d1/4 "
"v2:100 n2:48 d1/4 "
"o72 o55 o48 "
"n0:72 n1:55 n2:48 d1/1 "
"o72 o55 o48")
def test_bug1(self):
"""
For each moment in time, all note-off events have to be
done before the note-on events. This to avoid problems
with the same note being played two times after each other
in different tracks.
"""
t1 = Track()
t1.set_patch(3)
t1.note(4, 93, 127)
t1.note(4, 95, 127)
t2 = Track()
t2.set_patch(4)
t2.note(4, 95, 127)
t2.note(4, 97, 127)
self.assertEqual(
list(MidiEventStream(t1, t2)),
[('program-change', 0, 3),
('program-change', 1, 4),
('volume', 0, 100),
('note-on', 0, 93, 127),
('volume', 1, 100),
('note-on', 1, 95, 127),
('notelen-time', Rat(1, 4)),
('note-off', 0, 93, 127),
('note-off', 1, 95, 127),
('note-on', 0, 95, 127),
('note-on', 1, 97, 127),
('notelen-time', Rat(1, 4)),
('note-off', 0, 95, 127),
('note-off', 1, 97, 127)])
self.assertEqual(
MidiEventStream(t1, t2).str_repr(details=1),
"p0:3 p1:4 v0:100 n0:93 v1:100 n1:95 d1/4 o93 o95 n0:95 n1:97 d1/4 o95 o97")
def test_melodic_interval_2_tracks(self):
"""
In this test, only MIDI channel 0 will be allocated, even though
two different patches and volumes are used. This because the tones
from the two tracks does not sound at the same time.
"""
t1 = Track()
t1.set_patch(1)
t1.set_volume(101)
t1.note(4, 64)
t2 = Track()
t2.set_patch(2)
t2.set_volume(102)
t2.notelen_time(4)
t2.note(4, 66)
self.assertEqual(t1.str_repr(), "p1 v101 n64 d1/4 o64")
self.assertEqual(t2.str_repr(), "p2 v102 d1/4 n66 d1/4 o66")
m = MidiEventStream(t1, t2)
self.assertEqual(
m.str_repr(1),
"p0:1 p1:2 v0:101 n0:64 d1/4 o64 v1:102 n1:66 d1/4 o66")
def test_patch_volume_order(self):
"""
Assert that the order of set_patch and set_volume does not matter.
"""
t1 = Track()
t1.set_patch(1)
t1.set_volume(101)
t1.note(4, 64)
self.assertEqual(MidiEventStream(t1).str_repr(details=1), "p0:1 v0:101 n0:64 d1/4 o64")
# Then with patch and volume in reverse order
t1 = Track()
t1.set_volume(101)
t1.set_patch(1)
t1.note(4, 64)
self.assertEqual(MidiEventStream(t1).str_repr(details=1), "p0:1 v0:101 n0:64 d1/4 o64")
def test_prepend_patch(self):
"""
If multiple set_patch is done, the last will be used.
"""
t = Track()
t.prepend_patch(2)
t.prepend_patch(3)
t.note(4, 55)
self.assertEqual(
MidiEventStream(t).str_repr(), "p0:2 v0:100 n55 d1/4 o55")
def test_set_patch(self):
"""
If multiple set_patch is done, the last will be used.
"""
t = Track()
t.set_patch(2)
t.set_patch(3)
t.note(4, 55)
self.assertEqual(
MidiEventStream(t).str_repr(), "p0:3 v0:100 n55 d1/4 o55")
def test_set_patch2(self):
"""
Assert that there is not sendt a new volume event when we change patch.
"""
t = Track()
t.set_patch(2)
t.note(4, 55)
t.set_patch(3)
t.note(4, 57)
self.assertEqual(
MidiEventStream(t).str_repr(),
"p0:2 p1:3 v0:100 n55 d1/4 o55 v1:100 n57 d1/4 o57")
def test_set_volume(self):
"""
Assert that there is not sendt a new pacth event when we change volume
"""
t = Track()
t.set_volume(98)
t.note(4, 55)
t.set_volume(99)
t.note(4, 57)
self.assertEqual(
MidiEventStream(t).str_repr(),
"p0:0 p1:0 v0:98 n55 d1/4 o55 v1:99 n57 d1/4 o57")
def test_set_bpm(self):
t = Track()
t.set_bpm(120)
t.note(4, 50)
self.assertEqual(
MidiEventStream(t).str_repr(1),
"t120/4 p0:0 v0:100 n0:50 d1/4 o50")
def test_set_bpm2(self):
"""
Two set_bpm in a row should only generate MIDI events for the
last one.
"""
t = Track()
t.set_bpm(120)
t.set_bpm(121)
t.note(4, 50)
self.assertEqual(
MidiEventStream(t).str_repr(1),
"t121/4 p0:0 v0:100 n0:50 d1/4 o50")
def test_set_bpm3(self):
"""
When two tracks set a different tempo, the tempo from the
last track is used. There is not issues two MIDI events.
"""
t1 = Track()
t1.set_bpm(120)
t1.note(4, 50)
t2 = Track()
t2.set_bpm(121)
t2.note(4, 55)
self.assertEqual(
MidiEventStream(t1, t2).str_repr(1),
"t121/4 p0:0 v0:100 n0:50 n0:55 d1/4 o50 o55")
def test_x1(self):
t1 = Track()
t1.set_patch(1)
t1.note(4, 60)
t1.set_patch(2)
t1.note(4, 60)
t1.set_patch(3)
t1.note(4, 60)
t1.set_patch(4)
t1.note(4, 60)
m = MidiEventStream(t1)
self.assertEqual("p0:1 p1:2 p2:3 p3:4 v0:100 n0:60 d1/4 o60 v1:100 n1:60 d1/4 o60 v2:100 n2:60 d1/4 o60 v3:100 n3:60 d1/4 o60", m.str_repr(1))
def test_x2(self):
t1 = Track()
t1.note(4, 62)
t1.note(4, 60)
t1.note(2, 62)
t2 = Track()
t2.note(1, 60)
m = MidiEventStream(t1, t2)
self.assertEqual("p0:0 p1:0 v0:100 n0:62 n0:60 d1/4 o62 v1:100 n1:60 d1/4 o60 n0:62 d1/2 o62 o60", m.str_repr(1))
def test_x3(self):
t1 = Track()
t1.note(4, 62)
t1.note(4, 60)
t1.note(2, 62)
t2 = Track()
t2.note(1, 60)
m = MidiEventStream(t1, t2)
m.num_MIDI_channels = 1
# We don't handle running out of MIDI channels yet
self.assertRaises(Exception, m.str_repr, 1)
class TestChannelDevice(unittest.TestCase):
def setUp(self):
self.cd = MidiEventStream.ChannelDevice()
def test_1(self):
ch_dev = MidiEventStream.ChannelDevice()
self.assertEqual(0, ch_dev.require_channel(60, 0, 100))
ch_dev.start_note(0, 60)
self.assertEqual(1, ch_dev.require_channel(61, 1, 100))
ch_dev.start_note(1, 61)
self.assertEqual(2, ch_dev.require_channel(62, 1, 70))
ch_dev.start_note(2, 62)
self.assertEqual(0, ch_dev.require_channel(63, 0, 100))
ch_dev.start_note(0, 63)
# New channel since we want to play another tone with the
# same pitch as the one playing in channel 0
self.assertEqual(3, ch_dev.require_channel(63, 0, 100))
ch_dev.start_note(3, 63)
ch_dev.stop_note(0, 60)
ch_dev.stop_note(1, 61)
ch_dev.stop_note(2, 62)
ch_dev.stop_note(0, 63)
self.assertEqual(0, ch_dev.require_channel(63, 0, 100))
ch_dev.start_note(0, 63)
suite = unittest.TestLoader().loadTestsFromTestCase(TestTrack)
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestMidiEventStream))
suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestChannelDevice))
|