示例#1
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))
示例#2
0
文件: test_span.py 项目: akortman/mud
    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)
示例#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)
示例#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)
示例#5
0
文件: test_span.py 项目: akortman/mud
 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())
示例#6
0
文件: test_span.py 项目: akortman/mud
    def test_overlay(self):
        span_a = mud.Span([
            (mud.Note('C4', 1), mud.Time(0)),
            (mud.Note('G5', 1), mud.Time(1)),
        ])
        span_b = mud.Span([
            (mud.Note('A4', 1), mud.Time(0)),
            (mud.Note('C4', 1), mud.Time(1)),
        ])
        events_target = [
            (mud.Note('C4', 1), mud.Time(0)),
            (mud.Note('A4', 1), mud.Time(0)),
            (mud.Note('C4', 1), mud.Time(1)),
            (mud.Note('G5', 1), mud.Time(1)),
        ]

        span_overlaid = mud.Span.overlay(span_a, span_b)
        self.assertAlmostEqual(span_overlaid.length().in_beats(), 2.0)
        self.assertAlmostEqual(span_overlaid.offset().in_beats(), 0.0)
        for i, (event, time) in enumerate(events_target):
            self.assertEqual(span_overlaid[i], mud.Event(event, time))
示例#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)),
         ]))
     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))
示例#8
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)
示例#9
0
文件: test_span.py 项目: akortman/mud
    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))
示例#10
0
文件: test_span.py 项目: akortman/mud
    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))
示例#11
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))