Esempio n. 1
0
    def test_slice(self):
        span = mud.Span([
            (mud.Note('C4', 1), mud.Time(0)),
            (mud.Note('G5', 1), mud.Time(0)),
            (mud.Rest(1), mud.Time(1)),
            (mud.Note('C4', 2), mud.Time(2)),
            (mud.Note('A4', 2), mud.Time(2)),
        ])

        ts = span.get_slice((0.0, 0.5))
        self.assertEqual(ts.num_events(), 2)
        self.assertTrue(ts.is_atomic_slice())
        ts = list(ts.sliced_events())
        self.assertEqual(ts[0].time(), mud.Time(0.0))
        self.assertEqual(ts[0].duration(), mud.Time(0.5))
        self.assertEqual(ts[0].pitch(), mud.Pitch('C4'))
        self.assertEqual(ts[1].time(), mud.Time(0.0))
        self.assertEqual(ts[1].duration(), mud.Time(0.5))
        self.assertEqual(ts[1].pitch(), mud.Pitch('G5'))

        ts = span.get_slice((1.5, 2.5))
        self.assertEqual(ts.num_events(), 3)
        self.assertFalse(ts.is_atomic_slice())

        ts = list(span.generate_slices(0.5))
        self.assertAlmostEqual(len(ts), 8)
        self.assertAlmostEqual(float(len(ts)), span.length().in_beats() / 0.5)
Esempio n. 2
0
    def test(self):
        span = mud.Span([
            (mud.Note('C4', 1), mud.Time(0)),
            (mud.Note('G5', 1), mud.Time(0)),
            (mud.Rest(      1), mud.Time(1)),
            (mud.Note('C4', 2), mud.Time(2)),
            (mud.Note('A4', 2), mud.Time(2)),
        ])
        bar_data = mud.fmt.BarData(span, formatter, slice_resolution=0.5)

        self.assertEqual(len(bar_data.timeslices), 8)
        self.assertTrue([isinstance(ts_data, mud.fmt.TimeSliceData)
                            for ts_data in bar_data.timeslices],
                        [True for _ in bar_data.timeslices])
        
        ts_data = bar_data.timeslices[1]
        self.assertEqual(len(ts_data.events), 2)

        self.assertEqual(ts_data.events[0].vec.tolist(),
                         [1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
        self.assertEqual(ts_data.events[0].labels, (0, 0))

        self.assertEqual(ts_data.events[1].vec.tolist(),
                         [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0])
        self.assertEqual(ts_data.events[1].labels, (7, 0))

        # Test rest.
        ts_rest_data = bar_data.timeslices[2]
        self.assertEqual(len(ts_rest_data.events), 1)
        self.assertEqual(ts_rest_data.events[0].vec.tolist(),
                         [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0])
        self.assertEqual(ts_rest_data.events[0].labels, (None, 1))
Esempio n. 3
0
    def test_raw_events(self):
        events = [
            mud.Event(mud.Note('C4', 1), mud.Time(0)),
            mud.Event(mud.Note('G5', 1), mud.Time(0)),
            mud.Event(mud.Rest(      1), mud.Time(1)),
            mud.Event(mud.Note('C4', 2), mud.Time(2)),
            mud.Event(mud.Note('A4', 2), mud.Time(2)),
        ]
        span = mud.Span(events, length=4, offset=4)
        
        # Test slices which intersect with the first two notes
        slices_first_two = [
            mud.TimeSlice(span, (0.0, 0.4)),
            mud.TimeSlice(span, (0.6, 1.0)),
            mud.TimeSlice(span, (0.3, 0.7)),
            mud.TimeSlice(span, (0.0, 1.0)),
        ]
        for ts in slices_first_two:
            self.assertEqual(ts.num_events(), 2)
            for i, event in enumerate(ts.raw_events()):
                self.assertEqual(event, events[i])
                self.assertLess(i, 2)

        # Test slicing the rest.
        slices_rest = [
            mud.TimeSlice(span, (1.0, 2.0)),
            mud.TimeSlice(span, (1.1, 1.2)),
        ]
        for ts in slices_rest:
            self.assertEqual(ts.num_events(), 1)
            for i, event in enumerate(ts.raw_events()):
                self.assertEqual(event, events[2])
                self.assertLess(i, 1)
Esempio n. 4
0
    def test_sliced_events(self):
        events = [
            mud.Event(mud.Note('C4', 1), mud.Time(0)),
            mud.Event(mud.Note('G5', 1), mud.Time(0)),
            mud.Event(mud.Rest(      1), mud.Time(1)),
            mud.Event(mud.Note('C4', 2), mud.Time(2)),
            mud.Event(mud.Note('A4', 2), mud.Time(2)),
        ]
        span = mud.Span(events, length=4, offset=4)

        ts = mud.TimeSlice(span, (2.5, 3.0))
        self.assertEqual(ts.num_events(), 2)

        sliced_events = list(ts.sliced_events())
        self.assertEqual(len(sliced_events), 2)

        self.assertNotEqual(sliced_events[0],
                            mud.Event(mud.Note('C4', 2.0), mud.Time(2)))
        self.assertNotEqual(sliced_events[0],
                            mud.Event(mud.Note('C4', 0.5), mud.Time(2)))
        self.assertEqual(sliced_events[0].time(), mud.Time(2.5))
        self.assertEqual(sliced_events[0].duration(), mud.Time(0.5))
        se0 = mud.SlicedEvent((2.5, 3.0), mud.Event(mud.Note('C4', 2), mud.Time(2)))
        self.assertEqual(sliced_events[0], se0)

        self.assertEqual(sliced_events[1].time(), mud.Time(2.5))
        self.assertEqual(sliced_events[1].duration(), mud.Time(0.5))
        se1 = mud.SlicedEvent((2.5, 3.0), mud.Event(mud.Note('A4', 2), mud.Time(2)))
        self.assertEqual(sliced_events[1], se1)
Esempio n. 5
0
    def test_pitch_labels_with_include_rest(self):
        pitch_labels = label.PitchLabels(octave_range=(4, 6),
                                         include_rest=True)
        formatter = mud.fmt.EventDataBuilder(features=(feature.IsNote(), ),
                                             labels=(pitch_labels, ))
        event_rest = mud.Event(mud.Rest(mud.Time(3.0)), mud.Time(1.0))

        rest_label = pitch_labels.get_label_of(None)
        self.assertEqual(rest_label, pitch_labels.num_labels - 1)
        l = formatter.make_labels(event_rest)
        self.assertEqual(len(l), 1)
        self.assertEqual(l[0], rest_label)
Esempio n. 6
0
 def test(self):
     p = mud.Piece()
     p.build_from_spans(
         mud.Span([
             (mud.Note('C4', 1), mud.Time(0)),
             (mud.Note('G5', 1), mud.Time(0)),
             (mud.Rest(1), mud.Time(1)),
             (mud.Note('C4', 2), mud.Time(2)),
             (mud.Note('A4', 2), mud.Time(2)),
         ]))
     self.assertTrue(mud.piece_filter.AtomicSlicable(0.25)(p))
     self.assertTrue(mud.piece_filter.AtomicSlicable(1.0)(p))
     self.assertFalse(mud.piece_filter.AtomicSlicable(1.5)(p))
Esempio n. 7
0
 def test(self):
     p = mud.Piece()
     p.build_from_spans(mud.Span([
         (mud.Note('C4', 1), mud.Time(0)),
         (mud.Note('G5', 1), mud.Time(0)),
         (mud.Rest(      1), mud.Time(1)),
         (mud.Note('C4', 2), mud.Time(2)),
         (mud.Note('A4', 2), mud.Time(2)),
     ]))
     piece_data = mud.fmt.PieceData(p, formatter, slice_resolution=0.25)
     
     self.assertEqual(len(piece_data.bars), 1)
     self.assertTrue(isinstance(piece_data.bars[0], mud.fmt.BarData))
     self.assertTrue(len(piece_data.bars[0].timeslices), 16)
Esempio n. 8
0
    def test_concat(self):
        span_a = mud.Span([
            (mud.Note('C4', 1), mud.Time(0)),
            (mud.Note('G5', 1), mud.Time(0)),
        ])
        span_b = mud.Span([
            (mud.Rest(1), mud.Time(0)),
            (mud.Note('C4', 2), mud.Time(1)),
            (mud.Note('A4', 2), mud.Time(1)),
        ])
        events_target = [
            (mud.Note('C4', 1), mud.Time(0)),
            (mud.Note('G5', 1), mud.Time(0)),
            (mud.Rest(1), mud.Time(1)),
            (mud.Note('C4', 2), mud.Time(2)),
            (mud.Note('A4', 2), mud.Time(2)),
        ]

        span_concat = mud.Span.concat(span_a, span_b)
        self.assertAlmostEqual(span_concat.length().in_beats(), 4.0)
        self.assertAlmostEqual(span_concat.offset().in_beats(), 0.0)
        for i, (event, time) in enumerate(events_target):
            self.assertEqual(span_concat[i], mud.Event(event, time))
Esempio n. 9
0
    def test(self):
        events = [
            (mud.Note('C4', 1), mud.Time(0)),
            (mud.Note('G5', 1), mud.Time(0)),
            (mud.Rest(1), mud.Time(1)),
            (mud.Note('C4', 2), mud.Time(2)),
            (mud.Note('A4', 2), mud.Time(2)),
        ]
        span = mud.Span(events, length=4, offset=4)
        self.assertAlmostEqual(span.length().in_beats(), 4.0)
        self.assertAlmostEqual(span.offset().in_beats(), 4.0)

        for i, (event, time) in enumerate(events):
            self.assertEqual(span[i], mud.Event(event, time))
Esempio n. 10
0
 def test_is_monophonic(self):
     span1 = mud.Span([
         (mud.Note('C4', 1), mud.Time(0)),
         (mud.Note('A4', 2), mud.Time(2)),
     ],
                      length=4,
                      offset=4)
     self.assertTrue(span1.is_monophonic())
     span2 = mud.Span([
         (mud.Note('C4', 1), mud.Time(0)),
         (mud.Note('G5', 1), mud.Time(0)),
         (mud.Rest(1), mud.Time(1)),
         (mud.Note('C4', 2), mud.Time(2)),
         (mud.Note('A4', 2), mud.Time(2)),
     ],
                      length=4,
                      offset=4)
     self.assertTrue(not span2.is_monophonic())
Esempio n. 11
0
    def test(self):
        pitch_labels = label.PitchLabels(octave_range=(4, 6))
        formatter = mud.fmt.EventDataBuilder(features=(
            feature.IsNote(),
            feature.IsRest(),
            feature.NotePitch(pitch_labels),
        ),
                                             labels=(pitch_labels, ))

        # TEST INPUT VECTOR CONSTRUCTION
        event_note = mud.Event(mud.Note('C#4', mud.Time(1.0)), mud.Time(0.0))
        v = formatter.make_vector(event_note)
        self.assertEqual(
            v.shape,
            (12 * 3 + 2, ))  # 3 octaves + 1 note indicator + 1 rest indicator
        note_label = pitch_labels.get_label_of('C#4')
        self.assertEqual(note_label, 1)
        for i in range(v.shape[0]):
            if (i == 0  # The note indicator
                    or i == 2 + note_label):  # The pitch marker
                self.assertAlmostEqual(v[i], 1.0)
            else:
                self.assertAlmostEqual(v[i], 0.0)

        event_rest = mud.Event(mud.Rest(mud.Time(3.0)), mud.Time(1.0))
        v = formatter.make_vector(event_rest)
        self.assertEqual(
            v.shape,
            (12 * 3 + 2, ))  # 3 octaves + 1 note indicator + 1 rest indicator
        for i in range(v.shape[0]):
            if i == 1:  # note indicator
                self.assertAlmostEqual(v[i], 1.0)
            else:
                self.assertAlmostEqual(v[i], 0.0)

        # TEST LABEL GENERATION
        l = formatter.make_labels(event_note)
        self.assertEqual(len(l), 1)
        self.assertEqual(l[0], note_label)
        self.assertEqual(l[0], 1)

        l = formatter.make_labels(event_rest)
        self.assertEqual(len(l), 1)
        self.assertEqual(l[0], None)
Esempio n. 12
0
    def test(self):
        span = mud.Span([
            (mud.Note('C4', 1), mud.Time(0)),
            (mud.Note('G5', 1), mud.Time(0)),
            (mud.Rest(      1), mud.Time(1)),
            (mud.Note('C4', 2), mud.Time(2)),
            (mud.Note('A4', 2), mud.Time(2)),
        ])
        ts = mud.TimeSlice(span, (2.0, 3.0))
        ts_data = mud.fmt.TimeSliceData(ts, formatter)
        
        self.assertEqual(len(ts_data.events), 2)
        self.assertTrue([isinstance(event_data, mud.fmt.EventData)
                            for event_data in ts_data.events],
                        [True for _ in ts_data.events])
                        
        self.assertEqual(ts_data.events[0].vec.tolist(),
                         [1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0])
        self.assertEqual(ts_data.events[0].labels, (0, 1))

        self.assertEqual(ts_data.events[1].vec.tolist(),
                         [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0])
        self.assertEqual(ts_data.events[1].labels, (9, 1))
Esempio n. 13
0
import unittest
import mud
import mud.fmt.feature as feature
import mud.fmt.label as label
import numpy as np

note = mud.Event(mud.Note('G7', 2.0), mud.Time(0.0))
rest = mud.Event(mud.Rest(1.0), mud.Time(0.0))


class TestIsNote(unittest.TestCase):
    def test(self):
        f = feature.IsNote()
        self.assertEqual(f.dim(), 1)
        self.assertEqual(f.make_subvector(note).shape, (1, ))
        self.assertEqual(f.make_subvector(note), np.ones(1))
        self.assertEqual(f.make_subvector(note)[0], 1)
        self.assertEqual(f.make_subvector(rest).shape, (1, ))
        self.assertEqual(f.make_subvector(rest), np.zeros(1))
        self.assertEqual(f.make_subvector(rest)[0], 0)


class TestIsRest(unittest.TestCase):
    def test(self):
        f = feature.IsRest()
        self.assertEqual(f.dim(), 1)
        self.assertEqual(f.make_subvector(note).shape, (1, ))
        self.assertEqual(f.make_subvector(note), np.zeros(1))
        self.assertEqual(f.make_subvector(note)[0], 0)
        self.assertEqual(f.make_subvector(rest).shape, (1, ))
        self.assertEqual(f.make_subvector(rest), np.ones(1))
Esempio n. 14
0
 def test(self):
     r = mud.Rest(0.25)
     self.assertEqual(r.pitch(), None)
     self.assertAlmostEqual(r.duration().in_beats(), 0.25)
     self.assertEqual(r, mud.Rest(0.251))