def test_simple_beam_reverse(self):
        print('test simple beam reverse')
        a = Note(DiatonicPitch(3, 'a'), Duration(1, 8))
        b = Note(DiatonicPitch(3, 'b'), Duration(1, 8))
        c = Note(DiatonicPitch(3, 'c'), Duration(1, 8))
        d = Note(DiatonicPitch(3, 'd'), Duration(1, 8))
        beam = Beam([a, b, c, d])

        print(beam)

        beam.reverse()

        print(beam)

        notes = beam.get_all_notes()
        assert notes is not None
        assert len(notes) == 4
        assert notes[0].diatonic_pitch == DiatonicPitch(3, 'd')
        assert notes[1].diatonic_pitch == DiatonicPitch(3, 'c')
        assert notes[2].diatonic_pitch == DiatonicPitch(3, 'b')
        assert notes[3].diatonic_pitch == DiatonicPitch(3, 'a')

        assert notes[0].relative_position == Offset(0)
        assert notes[1].relative_position == Offset(1, 8)
        assert notes[2].relative_position == Offset(1, 4)
        assert notes[3].relative_position == Offset(3, 8)
Exemple #2
0
    def test_simple_beam(self):
        note = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        beam = Beam([note])
        print(beam)
        assert beam.cardinality() == 1
        assert beam.get_all_notes()[0].diatonic_pitch == DiatonicPitch(4, 'c')

        notes = beam.get_all_notes()
        assert len(notes) == 1
        TestBeam.print_all_notes(notes)
    def test_absolute_position(self):
        print('test absolute position')

        a = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
        b = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
        c = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        d = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        tuplet = Tuplet(Duration(1, 8), 3, [a, b, c, d])

        e = Note(DiatonicPitch(4, 'e'), Duration(1, 8))
        f = Note(DiatonicPitch(4, 'f'), Duration(1, 8))
        g = Note(DiatonicPitch(4, 'g'), Duration(1, 8))
        a1 = Note(DiatonicPitch(3, 'a'), Duration(1, 8))
        sub_beam = Beam([e, f, g, a1])

        b1 = Note(DiatonicPitch(3, 'b'), Duration(1, 8))
        c1 = Note(DiatonicPitch(3, 'c'), Duration(1, 8))
        d1 = Note(DiatonicPitch(3, 'd'), Duration(1, 8))
        e1 = Note(DiatonicPitch(3, 'e'), Duration(1, 8))

        beam = Beam([b1, tuplet, c1, sub_beam, d1, e1])

        AbstractNote.print_structure(beam)

        notes = beam.get_all_notes()
        assert notes is not None
        assert len(notes) == 12

        results = [
            Position(0),
            Position(1, 8),
            Position(7, 32),
            Position(5, 16),
            Position(13, 32),
            Position(1, 2),
            Position(5, 8),
            Position(11, 16),
            Position(3, 4),
            Position(13, 16),
            Position(7, 8),
            Position(1)
        ]
        index = 0
        for n in notes:
            print('{0} abs. position = {1}'.format(n,
                                                   n.get_absolute_position()))
            assert n.get_absolute_position() == results[index]
            index += 1
Exemple #4
0
    def test_two_voices(self):
        print('test two voices')
        c = InstrumentCatalog.instance()
        violin = c.get_instrument("violin")

        note0 = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
        note1 = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        tuplet = Tuplet(Duration(1, 8), 2, [note0, note1, note2])

        note3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        note4 = Note(DiatonicPitch(4, 'e'), Duration(1, 8))
        subbeam = Beam([note3, note4])
        beam = Beam(subbeam)
        line1 = Line([tuplet, beam])
        print(line1)

        notee0 = Note(DiatonicPitch(5, 'a'), Duration(1, 8))
        notee1 = Note(DiatonicPitch(5, 'b'), Duration(1, 8), 1)
        notee2 = Note(DiatonicPitch(5, 'c'), Duration(1, 8))
        notee3 = Note(DiatonicPitch(5, 'd'), Duration(1, 16))
        line2 = Line([notee0, notee1, notee2])
        line2.pin(notee3, Offset(1, 2))
        print(line2)

        voice = Voice(violin)
        voice.pin(line1, Offset(1, 4))
        voice.pin(line2, Offset(0, 1))

        print(voice)

        interval = Interval(Position(1, 2), Position(1))
        notes = voice.get_notes_by_interval(interval)

        assert len(notes) == 3
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('D:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('E:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('D:5'))

        interval = Interval(Position(1, 4), Position(7, 16))
        notes = voice.get_notes_by_interval(interval)

        assert len(notes) == 5
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('A:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('B:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('C:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('B:5'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('C:5'))
Exemple #5
0
    def recursive_dilate(self, strct, new_to_old_map):
        if isinstance(strct, Note):
            d = strct.base_duration.duration / strct.contextual_reduction_factor
            n = Note(strct.diatonic_pitch, Duration(d * self.dilation_factor),
                     strct.num_dots)
            new_to_old_map[n] = strct
            return n
        else:
            collect = list()
            for s in strct.sub_notes:
                collect.append(self.recursive_dilate(s, new_to_old_map))

            if isinstance(strct, Beam):
                b = Beam(collect)
                new_to_old_map[b] = strct
                return b
            elif isinstance(strct, Tuplet):
                t = Tuplet(strct.unit_duration * self.dilation_factor,
                           strct.unit_duration_factor, collect)
                new_to_old_map[t] = strct
                return t
            elif isinstance(strct, Line):
                line = Line()
                new_to_old_map[line] = strct
                for s in collect:
                    line.pin(
                        s, new_to_old_map[s].relative_position *
                        self.dilation_factor)
                return line
Exemple #6
0
 def test_book_example(self):
     # Creating a beam with 1/8, 3/8, and 1/16 notes
     note0 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
     note1 = Note(DiatonicPitch(4, 'd'), Duration(1, 8), 1)
     note2 = Note(DiatonicPitch(4, 'e'), Duration(1, 16))
     beam = Beam([note0, note1, note2])
     print(beam)
Exemple #7
0
    def test_add_post_beam_note(self):
        print('test add post beam note.')

        note1 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        note3 = Note(DiatonicPitch(4, 'e'), Duration(1, 16))
        sub_beam = Beam([note1, note2, note3])

        parent_beam = Beam()
        parent_beam.append(Note(DiatonicPitch(4, 'f'), Duration(1, 8)))
        parent_beam.append(sub_beam)
        print(parent_beam)

        note4 = Note(DiatonicPitch(4, 'e'), Duration(1, 16))
        sub_beam.append(note4)
        print(parent_beam)
Exemple #8
0
    def test_tie_break(self):
        print('test tie break')
        a = Note(DiatonicPitch(3, 'a'), Duration(1, 8))
        b = Note(DiatonicPitch(3, 'b'), Duration(1, 8))
        c = Note(DiatonicPitch(3, 'c'), Duration(1, 8))
        d = Note(DiatonicPitch(3, 'a'), Duration(1, 8))
        sub_beam = Beam([b, c])

        beam = Beam([a, d])
        a.tie()
        assert a.is_tied_to
        assert d.is_tied_from

        beam.add(sub_beam, 1)

        assert not a.is_tied_to
        assert not d.is_tied_from
Exemple #9
0
    def test_add_note_at_lower_level(self):
        print('start test_add_note_at_lower_level')
        sub_beam = Beam([
            Note(DiatonicPitch(2, 'c'), Duration(1, 8)),
            Note(DiatonicPitch(2, 'd'), Duration(1, 8))
        ])
        beam = Beam([
            Note(DiatonicPitch(3, 'c'), Duration(1, 8)), sub_beam,
            Note(DiatonicPitch(3, 'd'), Duration(1, 8))
        ])

        AbstractNote.print_structure(beam)

        notes = beam.get_all_notes()
        assert len(notes) == 4

        assert beam.sub_notes[1].duration == Duration(1, 8)
        assert beam.sub_notes[1].relative_position == Offset(1, 8)
        assert beam.sub_notes[1].sub_notes[1].duration == Duration(1, 16)
        assert beam.sub_notes[1].sub_notes[1].relative_position == Offset(
            1, 16)

        sub_beam.add(Note(DiatonicPitch(2, 'c'), Duration(1, 8)), 1)
        AbstractNote.print_structure(beam)

        assert beam.sub_notes[1].duration == Duration(3, 16)
        assert beam.sub_notes[1].relative_position == Offset(1, 8)
        assert beam.sub_notes[1].sub_notes[1].duration == Duration(1, 16)
        assert beam.sub_notes[1].sub_notes[1].relative_position == Offset(
            1, 16)
Exemple #10
0
    def test_multi_notes(self):
        note0 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note1 = Note(DiatonicPitch(4, 'd'), Duration(1, 8), 1)
        note2 = Note(DiatonicPitch(4, 'e'), Duration(1, 16))
        beam = Beam([note0, note1, note2])
        print(beam)
        assert beam.cardinality() == 3
        notes = beam.get_all_notes()
        assert notes[0].diatonic_pitch == DiatonicPitch(4, 'c')
        assert notes[1].diatonic_pitch == DiatonicPitch(4, 'd')
        assert notes[2].diatonic_pitch == DiatonicPitch(4, 'e')
        assert beam.duration == Duration(3, 8)
        assert notes[0].relative_position == Offset(0)
        assert notes[1].relative_position == Offset(1, 8)
        assert notes[2].relative_position == Offset(5, 16)

        assert len(notes) == 3
        TestBeam.print_all_notes(notes)
    def test_ties_reverse(self):
        print('test ties reverse')
        a = Note(DiatonicPitch(3, 'b'), Duration(1, 8))
        b = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
        c = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        d = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        tuplet = Tuplet(Duration(1, 8), 3, [a, b, c, d])

        e = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        f = Note(DiatonicPitch(4, 'f'), Duration(1, 8))
        g = Note(DiatonicPitch(4, 'g'), Duration(1, 8))
        a1 = Note(DiatonicPitch(3, 'd'), Duration(1, 8))
        sub_beam = Beam([e, f, g, a1])

        b1 = Note(DiatonicPitch(3, 'b'), Duration(1, 8))
        c1 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        d1 = Note(DiatonicPitch(3, 'd'), Duration(1, 8))
        e1 = Note(DiatonicPitch(3, 'e'), Duration(1, 8))

        beam = Beam([b1, tuplet, c1, sub_beam, d1, e1])

        b1.tie()
        d.tie()
        c1.tie()
        a1.tie()

        AbstractNote.print_structure(beam)

        beam.reverse()

        AbstractNote.print_structure(beam)

        assert a.is_tied_to and a.tied_to == b1
        assert b1.is_tied_from and b1.tied_from == a

        assert c1.is_tied_to and c1.tied_to == d
        assert d.is_tied_from and d.tied_from == c1

        assert e.is_tied_to and e.tied_to == c1
        assert c1.is_tied_from and c1.tied_from == e

        assert d1.is_tied_to and d1.tied_to == a1
        assert a1.is_tied_from and a1.tied_from == d1
Exemple #12
0
    def test_add_notes_to_tuplet(self):
        c = InstrumentCatalog.instance()
        violin = c.get_instrument("violin")

        note0 = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
        note1 = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        beam = Beam([note0, note1])

        line = Line()
        line.pin(beam, Offset(1, 2))

        voice = Voice(violin)
        voice.pin(line, Offset(0))

        tuplet = Tuplet(Duration(1, 8), 3, [note2, note3])
        beam.append(tuplet)

        notee0 = Note(DiatonicPitch(5, 'a'), Duration(1, 8))
        tuplet.append(notee0)

        print(voice)
        notes = voice.get_notes_starting_in_interval(
            Interval(Position(1, 2), Position(5, 4)))

        assert len(notes) == 5
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('A:5'))

        beam_prime = beam.clone()
        notes = beam_prime.get_all_notes()
        AbstractNote.print_structure(beam_prime)
        assert notes[0].duration == Duration(1, 8)
        assert str(notes[0].diatonic_pitch) == 'A:4'

        line_prime = line.clone()
        notes = line_prime.get_all_notes()
        AbstractNote.print_structure(line_prime)
        assert notes[0].duration == Duration(1, 8)
        assert str(notes[0].diatonic_pitch) == 'A:4'
        assert notes[2].duration == Duration(1, 8)
        assert str(notes[2].diatonic_pitch) == 'C:4'
Exemple #13
0
    def test_tuplet_with_nested_beam(self):
        note1 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))

        n_list = [
            Note(DiatonicPitch(3, 'c'), Duration(1, 8)),
            Note(DiatonicPitch(3, 'd'), Duration(1, 8))
        ]
        add_beam = Beam(n_list)

        tuplet = Tuplet(Duration(1, 8), 2, [note1, add_beam, note2])

        print(tuplet)
        AbstractNote.print_structure(tuplet)

        notes = tuplet.get_all_notes()
        assert len(notes) == 4

        assert tuplet.sub_notes[0].duration == Duration(1, 16)
        assert tuplet.sub_notes[0].relative_position == Offset(0)
        assert tuplet.sub_notes[1].duration == Duration(1, 8)
        assert tuplet.sub_notes[1].relative_position == Offset(1, 16)
        assert tuplet.sub_notes[2].duration == Duration(1, 16)
        assert tuplet.sub_notes[2].relative_position == Offset(3, 16)
        assert notes[1].duration == Duration(1, 16)
        assert notes[1].relative_position == Offset(0)
        assert notes[2].duration == Duration(1, 16)
        assert notes[2].relative_position == Offset(1, 16)

        add_beam_prime = add_beam.clone()
        notes = add_beam_prime.get_all_notes()
        AbstractNote.print_structure(add_beam_prime)
        assert notes[0].duration == Duration(1, 8)
        assert str(notes[0].diatonic_pitch) == 'C:3'

        tuplet_prime = tuplet.clone()
        notes = tuplet_prime.get_all_notes()
        AbstractNote.print_structure(tuplet_prime)
        assert notes[0].duration == Duration(1, 16)
        assert str(notes[0].diatonic_pitch) == 'C:4'
        assert notes[1].duration == Duration(1, 16)
Exemple #14
0
    def test_line_convert(self):
        note1 = Note(DiatonicPitch(4, 'c'), Duration(1, 16))
        note2 = Note(DiatonicPitch(4, 'd'), Duration(1, 16))
        note3 = Note(DiatonicPitch(4, 'e'), Duration(1, 16))
        note4 = Note(DiatonicPitch(4, 'f'), Duration(1, 16))
        beam = Beam([note1, note2, note3, note4])

        line = Line(beam)

        ScoreToMidiConverter.convert_line(line, 'line_output_file.mid')

        TestScoreToMidiConverter.read_midi_file('line_output_file.mid')
Exemple #15
0
    def test_single_voice(self):
        print('test single voice')
        note0 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note1 = Note(DiatonicPitch(4, 'd'), Duration(1, 8), 1)
        note2 = Note(DiatonicPitch(4, 'e'), Duration(1, 16))
        beam = Beam([note0, note1, note2])
        print(beam)
        assert beam.cardinality() == 3

        line = Line(beam)

        assert line.length() == Duration(3, 8)

        c = InstrumentCatalog.instance()

        violin = c.get_instrument("violin")

        voice = Voice(violin)
        voice.pin(line, Offset(3, 4))

        coverage = voice.coverage()
        print('Coverage = {0}'.format(coverage))
        assert coverage.lower == Position(3, 4)
        assert coverage.upper == Position(9, 8)

        assert voice.length() == Duration(9, 8)

        interval = Interval(Position(15, 16), Position(9, 8))
        notes = voice.get_notes_by_interval(interval)
        print(', '.join([str(n) for n in notes]))
        for n in notes:
            intvl = TestVoice.compute_note_interval(n)
            print('{0} intersect {1} = {2}'.format(
                intvl, interval, intvl.intersection(interval)))

        assert len(notes) == 2
        print(notes[0].get_absolute_position(),
              notes[1].get_absolute_position())
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('D:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('E:4'))
Exemple #16
0
    def test_book_example(self):
        print("test_book_example")
        a = Note(DiatonicPitch(3, 'a'), Duration(1, 8))
        b = Note(DiatonicPitch(3, 'b'), Duration(1, 8))
        c = Note(DiatonicPitch(3, 'c'), Duration(1, 8))
        d = Note(DiatonicPitch(3, 'd'), Duration(1, 8))
        e = Note(DiatonicPitch(3, 'e'), Duration(1, 8))

        tuplet = Tuplet(Duration(1, 8), 2, [b, c, d])

        beam = Beam([a, tuplet, e])
        print(beam)

        a1 = Note(DiatonicPitch(3, 'a'), Duration(1, 8))
        b1 = Note(DiatonicPitch(3, 'b'), Duration(1, 8))
        c1 = Note(DiatonicPitch(3, 'c'), Duration(1, 8))
        d1 = Note(DiatonicPitch(3, 'd'), Duration(1, 8))
        e1 = Note(DiatonicPitch(3, 'e'), Duration(1, 8))

        tuplet1 = Tuplet(Duration(1, 8), 2, [b1, c1, d1])

        beam1 = Beam([tuplet1])

        final_beam = Beam([a1, beam1, e1])

        print(final_beam)

        a2 = Note(DiatonicPitch(3, 'a'), Duration(1, 8))
        b2 = Note(DiatonicPitch(3, 'b'), Duration(1, 8))
        c2 = Note(DiatonicPitch(3, 'c'), Duration(1, 8))
        d2 = Note(DiatonicPitch(3, 'd'), Duration(1, 8))
        e2 = Note(DiatonicPitch(3, 'e'), Duration(1, 8))

        beam2 = Beam([b2, c2, d2])
        tuplet2 = Tuplet(Duration(1, 16), 2, [beam2])
        final_beam2 = Beam([a2, tuplet2, e2])

        print(final_beam2)

        print("end test_book_example")
Exemple #17
0
    def test_dilation(self):
        print('test dilation')

        a = Note(DiatonicPitch(3, 'a'), Duration(1, 8))
        b = Note(DiatonicPitch(3, 'b'), Duration(1, 8))
        c = Note(DiatonicPitch(3, 'c'), Duration(1, 8))
        d = Note(DiatonicPitch(3, 'd'), Duration(1, 8))
        e = Note(DiatonicPitch(3, 'e'), Duration(1, 8))

        tuplet = Tuplet(Duration(1, 8), 2, [a, b, c])
        sub_beam = Beam([tuplet])

        beam = Beam([d, sub_beam, e])

        AbstractNote.print_structure(beam)

        f = Note(DiatonicPitch(3, 'f'), Duration(1, 8))
        tuplet.add(f, 2)

        AbstractNote.print_structure(beam)

        print('end test dilation')
Exemple #18
0
    def test_odd_structure(self):
        print('test odd structure')

        n1 = Note(DiatonicPitch(4, 'c'), Duration(1, 12))
        n2 = Note(DiatonicPitch(4, 'c'), Duration(1, 12))

        from structure.tuplet import Tuplet
        tuplet = Tuplet(Duration(1, 12), 1, [n1, n2])

        n3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))

        beam = Beam([tuplet, n3])
        AbstractNote.print_structure(beam)
Exemple #19
0
    def test_next_prior_note(self):
        print('test_next_note')
        a = Note(DiatonicPitch(3, 'a'), Duration(1, 8))  
        b = Note(DiatonicPitch(3, 'b'), Duration(1, 8)) 
        c = Note(DiatonicPitch(4, 'c'), Duration(1, 8)) 
        d = Note(DiatonicPitch(4, 'd'), Duration(1, 8)) 
        e = Note(DiatonicPitch(4, 'e'), Duration(1, 8)) 
        f = Note(DiatonicPitch(4, 'f'), Duration(1, 8)) 
        g = Note(DiatonicPitch(4, 'g'), Duration(1, 8))  
        h = Note(DiatonicPitch(4, 'a'), Duration(1, 8))   
        full_list = [a, b, c, d, e, f, g, h]

        b3 = Beam([d, e])
        b2 = Beam([c, b3, f])
        b1 = Beam([b, b2, g])
        Beam([a, b1, h])
        
        print('next note test')
        n = a
        index = 1
        while True:
            n = n.next_note()
            if n is None:
                break
            print(n)
            assert n.diatonic_pitch == full_list[index].diatonic_pitch
            index += 1
        
        print('prior note test')
        n = h
        print(n)
        index = len(full_list) - 2
        while True:
            n = n.prior_note()
            if n is None:
                break
            print(n)
            assert n.diatonic_pitch == full_list[index].diatonic_pitch
            index -= 1
Exemple #20
0
    def test_TBB_layers(self):
        print('start test_TBB_layers')
        sub_sub_beam = Beam([
            Note(DiatonicPitch(2, 'c'), Duration(1, 8)),
            Note(DiatonicPitch(2, 'd'), Duration(1, 8))
        ])
        sub_beam = Beam([
            Note(DiatonicPitch(3, 'c'), Duration(1, 8)), sub_sub_beam,
            Note(DiatonicPitch(3, 'd'), Duration(1, 8))
        ])

        AbstractNote.print_structure(sub_beam)

        note1 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        print('-----------')
        tuplet = Tuplet(Duration(1, 8), 2, [note1, sub_beam, note2])
        AbstractNote.print_structure(tuplet)

        notes = tuplet.get_all_notes()
        assert len(notes) == 6

        assert tuplet.sub_notes[1].duration == Duration(3, 20)
        assert tuplet.sub_notes[1].relative_position == Offset(1, 20)
        assert tuplet.sub_notes[1].sub_notes[1].duration == Duration(1, 20)
        assert tuplet.sub_notes[1].sub_notes[1].relative_position == Offset(
            1, 20)

        sub_sub_beam.add(Note(DiatonicPitch(2, 'c'), Duration(1, 8)), 1)
        AbstractNote.print_structure(tuplet)

        assert tuplet.sub_notes[1].duration == Duration(7, 44)
        assert tuplet.sub_notes[1].relative_position == Offset(1, 22)
        assert tuplet.sub_notes[1].sub_notes[1].duration == Duration(3, 44)
        assert tuplet.sub_notes[1].sub_notes[1].relative_position == Offset(
            1, 22)

        print('end test_TBB_layers')
Exemple #21
0
    def test_line_convert(self):
        print("test line convert")

        note1 = Note(DiatonicPitch(4, 'c'), Duration(1, 16))
        note2 = Note(DiatonicPitch(4, 'd'), Duration(1, 16))
        note3 = Note(DiatonicPitch(4, 'e'), Duration(1, 16))
        note4 = Note(DiatonicPitch(4, 'f'), Duration(1, 16))
        beam = Beam([note1, note2, note3, note4])

        line = Line(beam)

        meta_track, tracks = ScoreToVstMidiConverter.convert_line(line)
        assert meta_track is not None
        assert tracks is not None

        assert len(tracks) == 1
        assert len(tracks[0]) == 8

        for i in range(0, len(tracks[0])):
            print("{0}: {1}".format(i, tracks[0][i]))

        assert isinstance(tracks[0][0], NoteMessage)
        assert tracks[0][0].msg_type == 144
        assert tracks[0][0].note_value == 60  # C
        assert tracks[0][0].abs_frame_time == 0
        assert tracks[0][0].rel_frame_time == 0

        assert isinstance(tracks[0][2], NoteMessage)
        assert tracks[0][2].msg_type == 144
        assert tracks[0][2].note_value == 62  # D
        assert tracks[0][2].abs_frame_time == 10525
        assert tracks[0][2].rel_frame_time == 0

        assert isinstance(tracks[0][4], NoteMessage)
        assert tracks[0][4].msg_type == 144
        assert tracks[0][4].note_value == 64  # E
        assert tracks[0][4].abs_frame_time == 21050
        assert tracks[0][4].rel_frame_time == 0

        assert isinstance(tracks[0][6], NoteMessage)
        assert tracks[0][6].msg_type == 144
        assert tracks[0][6].note_value == 65  # F
        assert tracks[0][6].abs_frame_time == 31575
        assert tracks[0][6].rel_frame_time == 0
Exemple #22
0
 def test_tie(self):
     print('test_next_note')
     a = Note(DiatonicPitch(3, 'a'), Duration(1, 8))  
     b = Note(DiatonicPitch(3, 'a'), Duration(1, 8)) 
     c = Note(DiatonicPitch(3, 'c'), Duration(1, 8))
     Beam([a, b, c])
     
     assert not a.is_tied_to
     assert not b.is_tied_from
     
     a.tie()
     assert a.is_tied_to
     assert b.is_tied_from
     assert a.tied_to == b
     assert b.tied_from == a
     
     a.untie()
     assert not a.is_tied_to
     assert not b.is_tied_from
     assert a.tied_to is None
     assert b.tied_from is None
     
     with self.assertRaises(Exception):
         c.tie()
    def test_nested_structure_reverse(self):
        print('test nested structure reverse')
        a = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
        b = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
        c = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        d = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        tuplet = Tuplet(Duration(1, 8), 3, [a, b, c, d])

        e = Note(DiatonicPitch(4, 'e'), Duration(1, 8))
        f = Note(DiatonicPitch(4, 'f'), Duration(1, 8))
        g = Note(DiatonicPitch(4, 'g'), Duration(1, 8))
        a1 = Note(DiatonicPitch(3, 'a'), Duration(1, 8))
        sub_beam = Beam([e, f, g, a1])

        b1 = Note(DiatonicPitch(3, 'b'), Duration(1, 8))
        c1 = Note(DiatonicPitch(3, 'c'), Duration(1, 8))
        d1 = Note(DiatonicPitch(3, 'd'), Duration(1, 8))
        e1 = Note(DiatonicPitch(3, 'e'), Duration(1, 8))

        beam = Beam([b1, tuplet, c1, sub_beam, d1, e1])

        AbstractNote.print_structure(beam)

        beam.reverse()

        AbstractNote.print_structure(beam)

        notes = beam.get_all_notes()
        assert notes is not None
        assert len(notes) == 12

        assert notes[0].diatonic_pitch == DiatonicPitch(3, 'e')
        assert notes[1].diatonic_pitch == DiatonicPitch(3, 'd')
        assert notes[2].diatonic_pitch == DiatonicPitch(3, 'a')
        assert notes[3].diatonic_pitch == DiatonicPitch(4, 'g')
        assert notes[4].diatonic_pitch == DiatonicPitch(4, 'f')
        assert notes[5].diatonic_pitch == DiatonicPitch(4, 'e')
        assert notes[6].diatonic_pitch == DiatonicPitch(3, 'c')
        assert notes[7].diatonic_pitch == DiatonicPitch(4, 'd')
        assert notes[8].diatonic_pitch == DiatonicPitch(4, 'c')
        assert notes[9].diatonic_pitch == DiatonicPitch(4, 'b')
        assert notes[10].diatonic_pitch == DiatonicPitch(4, 'a')
        assert notes[11].diatonic_pitch == DiatonicPitch(3, 'b')
Exemple #24
0
    def test_richer_structure(self):
        line = Line()
        s = Beam()
        s.append(Note(DiatonicPitch.parse('C:4'), Duration(1, 8)))
        s.append(Note(DiatonicPitch.parse('D:4'), Duration(1, 8)))
        line.pin(s)

        q1 = Note(DiatonicPitch.parse('E:4'), Duration(1, 4))
        line.pin(q1, Offset(1, 4))
        q2 = Note(DiatonicPitch.parse('F#:4'), Duration(1, 4))
        line.pin(q2, Offset(1, 2))

        cs = Beam()
        first_note = Note(DiatonicPitch.parse('C:3'), Duration(1, 8))
        cs.append(first_note)
        cs.append(Note(DiatonicPitch.parse('D:3'), Duration(1, 8)))
        line.pin(cs, Offset(2))

        cq1 = Note(DiatonicPitch.parse('E:3'), Duration(1, 4))
        line.pin(cq1, Offset(9, 4))
        cq2 = Note(DiatonicPitch.parse('F#:3'), Duration(1, 4))
        line.pin(cq2, Offset(5, 2))

        notes = line.get_all_notes()

        c = [
            EqualPitchConstraint([notes[0], notes[2]]),
            NotEqualPitchConstraint([notes[1], notes[3]])
        ]

        m = Motif([s, q1, q2], c, 'A')
        print(m)

        actors = m.actors

        assert 'A' == m.name
        assert len(actors) == 4

        cc = m.constraints
        assert len(cc) == len(c)

        assert isinstance(cc[0], EqualPitchConstraint)
        cc_a = cc[0].actors
        assert len(cc_a) == 2
        assert cc_a[0] == actors[0]
        assert cc_a[1] == actors[2]

        assert isinstance(cc[1], NotEqualPitchConstraint)
        cc_b = cc[1].actors
        assert len(cc_a) == 2
        assert cc_b[0] == actors[1]
        assert cc_b[1] == actors[3]
        assert 'F#:4' == str(actors[3].diatonic_pitch)

        c_motif = m.copy_to(first_note)
        assert c_motif is not None
        c_actors = c_motif.actors

        assert 'A' == c_motif.name
        assert len(c_actors) == 4

        ccc = c_motif.constraints
        assert len(ccc) == len(c)

        assert isinstance(ccc[0], EqualPitchConstraint)
        ccc_a = ccc[0].actors
        assert len(ccc_a) == 2
        assert ccc_a[0] == c_actors[0]
        assert ccc_a[1] == c_actors[2]

        assert isinstance(ccc[1], NotEqualPitchConstraint)
        ccc_b = ccc[1].actors
        assert len(ccc_a) == 2
        assert ccc_b[0] == c_actors[1]
        assert ccc_b[1] == c_actors[3]
        assert 'F#:3' == str(c_actors[3].diatonic_pitch)

        print(c_motif)
Exemple #25
0
    def test_simple_motif(self):
        s = Beam()
        s.append(Note(DiatonicPitch.parse('C:4'), Duration(1, 8)))
        s.append(Note(DiatonicPitch.parse('D:4'), Duration(1, 8)))
        s.append(Note(DiatonicPitch.parse('E:4'), Duration(1, 8)))
        s.append(Note(DiatonicPitch.parse('F#:4'), Duration(1, 8)))
        notes = s.get_all_notes()

        c = [
            EqualPitchConstraint([notes[0], notes[2]]),
            NotEqualPitchConstraint([notes[1], notes[3]])
        ]

        m = Motif(s, c, 'A')
        actors = m.actors

        assert 'A' == m.name
        assert len(actors) == len(notes)

        cc = m.constraints
        assert len(cc) == len(c)

        assert isinstance(cc[0], EqualPitchConstraint)
        cc_a = cc[0].actors
        assert len(cc_a) == 2
        assert cc_a[0] == actors[0]
        assert cc_a[1] == actors[2]

        assert isinstance(cc[1], NotEqualPitchConstraint)
        cc_b = cc[1].actors
        assert len(cc_a) == 2
        assert cc_b[0] == actors[1]
        assert cc_b[1] == actors[3]
        assert 'F#:4' == str(actors[3].diatonic_pitch)

        print(m)

        cs = Beam()
        first_note = Note(DiatonicPitch.parse('C:3'), Duration(1, 8))
        cs.append(first_note)
        cs.append(Note(DiatonicPitch.parse('D:3'), Duration(1, 8)))
        cs.append(Note(DiatonicPitch.parse('E:3'), Duration(1, 8)))
        cs.append(Note(DiatonicPitch.parse('F#:3'), Duration(1, 8)))

        c_motif = m.copy_to(first_note)
        c_actors = c_motif.actors

        assert 'A' == c_motif.name
        assert len(c_actors) == len(notes)

        ccc = c_motif.constraints
        assert len(ccc) == len(c)

        assert isinstance(ccc[0], EqualPitchConstraint)
        ccc_a = ccc[0].actors
        assert len(ccc_a) == 2
        assert ccc_a[0] == c_actors[0]
        assert ccc_a[1] == c_actors[2]

        assert isinstance(ccc[1], NotEqualPitchConstraint)
        ccc_b = ccc[1].actors
        assert len(ccc_a) == 2
        assert ccc_b[0] == c_actors[1]
        assert ccc_b[1] == c_actors[3]
        assert 'F#:3' == str(c_actors[3].diatonic_pitch)

        print(c_motif)
Exemple #26
0
    def test_motif_book_example(self):

        s = Beam()
        s.append(Note(DiatonicPitch.parse('C:4'), Duration(1, 8)))
        s.append(Note(DiatonicPitch.parse('D:4'), Duration(1, 8)))
        s.append(Note(DiatonicPitch.parse('C:4'), Duration(1, 8)))
        s.append(Note(DiatonicPitch.parse('F#:4'), Duration(1, 8)))
        notes = s.get_all_notes()

        c = [
            EqualPitchConstraint([notes[0], notes[2]]),
            NotEqualPitchConstraint([notes[1], notes[3]])
        ]

        m = Motif(s, c, 'A')
        cs = Beam()

        cs.append(Note(DiatonicPitch.parse('C:5'), Duration(1, 8)))
        cs.append(Note(DiatonicPitch.parse('D:5'), Duration(1, 8)))
        cs.append(Note(DiatonicPitch.parse('C:5'), Duration(1, 8)))
        cs.append(Note(DiatonicPitch.parse('F#:5'), Duration(1, 8)))

        c_motif = m.copy_to(cs.get_all_notes()[0])

        assert 'A' == c_motif.name
        assert len(c_motif.actors) == len(notes)
        assert len(c_motif.constraints) == len(c)

        assert isinstance(c_motif.constraints[0], EqualPitchConstraint)
        assert c_motif.constraints[0].actors[0] == c_motif.actors[0]
        assert c_motif.constraints[0].actors[1] == c_motif.actors[2]

        assert isinstance(c_motif.constraints[1], NotEqualPitchConstraint)
        assert c_motif.constraints[1].actors[0] == c_motif.actors[1]
        assert c_motif.constraints[1].actors[1] == c_motif.actors[3]
Exemple #27
0
    def test_insert_notes(self):
        print('test_insert_notes')
        # same as test_nested_notes
        note1 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        note3 = Note(DiatonicPitch(4, 'e'), Duration(1, 16))
        sub_beam = Beam([note1, note2, note3])

        beam = Beam()
        beam.append(Note(DiatonicPitch(4, 'f'), Duration(1, 8)))
        beam.append(sub_beam)
        beam.append(Note(DiatonicPitch(4, 'g'), Duration(1, 8)))

        AbstractNote.print_structure(beam)

        # add a beam
        n_list = [
            Note(DiatonicPitch(3, 'c'), Duration(1, 8)),
            Note(DiatonicPitch(3, 'd'), Duration(1, 8))
        ]
        add_beam = Beam(n_list)
        sub_beam.add(add_beam, 1)

        print(beam)
        AbstractNote.print_structure(beam)

        notes = beam.get_all_notes()
        assert len(notes) == 7
        TestBeam.print_all_notes(notes)
        assert notes[0].diatonic_pitch == DiatonicPitch(4, 'f')
        assert notes[1].diatonic_pitch == DiatonicPitch(4, 'c')
        assert notes[2].diatonic_pitch == DiatonicPitch(3, 'c')
        assert notes[3].diatonic_pitch == DiatonicPitch(3, 'd')
        assert notes[4].diatonic_pitch == DiatonicPitch(4, 'd')
        assert notes[5].diatonic_pitch == DiatonicPitch(4, 'e')
        assert notes[6].diatonic_pitch == DiatonicPitch(4, 'g')

        assert notes[0].relative_position == Offset(0)
        assert notes[1].relative_position == Offset(0)
        assert notes[2].relative_position == Offset(0)
        assert notes[3].relative_position == Offset(1, 32)
        assert notes[4].relative_position == Offset(1, 8)
        assert notes[5].relative_position == Offset(3, 16)
        assert notes[6].relative_position == Offset(11, 32)
Exemple #28
0
    def test_tuplet_variations(self):
        print('test tuplet variations')

        # 1. Beam with 3 1/8 notes
        note1 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        note3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        beam = Beam([note1, note2, note3])
        AbstractNote.print_structure(beam)

        notes = beam.get_all_notes()
        assert len(notes) == 3
        assert notes[0].duration == Duration(1, 8)
        assert notes[1].duration == Duration(1, 8)
        assert notes[2].duration == Duration(1, 8)
        assert notes[0].relative_position == Offset(0)
        assert notes[1].relative_position == Offset(1, 8)
        assert notes[2].relative_position == Offset(1, 4)

        # 2. beam with a tuplet
        note1 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        note3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        tuplet = Tuplet(Duration(1, 8), 2, [note1, note2, note3])
        beam = Beam([tuplet])
        AbstractNote.print_structure(beam)

        notes = beam.get_all_notes()
        assert len(notes) == 3
        assert notes[0].duration == Duration(1, 12)
        assert notes[1].duration == Duration(1, 12)
        assert notes[2].duration == Duration(1, 12)
        assert notes[0].relative_position == Offset(0)
        assert notes[1].relative_position == Offset(1, 12)
        assert notes[2].relative_position == Offset(1, 6)

        # 3. tuplet with a beam
        note1 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        note3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        beam = Beam([note1, note2, note3])
        tuplet = Tuplet(Duration(1, 8), 2, beam)
        AbstractNote.print_structure(tuplet)

        notes = beam.get_all_notes()
        assert len(notes) == 3

        assert notes[0].duration == Duration(1, 12)
        assert notes[1].duration == Duration(1, 12)
        assert notes[2].duration == Duration(1, 12)
        assert notes[0].relative_position == Offset(0)
        assert notes[1].relative_position == Offset(1, 12)
        assert notes[2].relative_position == Offset(1, 6)

        # 4. beam a beam of a tuplet
        note1 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        note3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        tuplet = Tuplet(Duration(1, 8), 2, [note1, note2, note3])
        sub_beam = Beam([tuplet])
        beam = Beam(sub_beam)
        AbstractNote.print_structure(beam)

        notes = beam.get_all_notes()
        assert len(notes) == 3

        assert notes[0].duration == Duration(1, 24)
        assert notes[1].duration == Duration(1, 24)
        assert notes[2].duration == Duration(1, 24)
        assert notes[0].relative_position == Offset(0)
        assert notes[1].relative_position == Offset(1, 24)
        assert notes[2].relative_position == Offset(1, 12)
Exemple #29
0
    def test_add_notes_to_beam(self):
        c = InstrumentCatalog.instance()
        violin = c.get_instrument("violin")

        note0 = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
        note1 = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        beam = Beam([note2, note3])

        line = Line()
        line.pin(note0, Offset(1, 4))
        line.pin(note1, Offset(3, 8))
        line.pin(beam, Offset(1, 2))

        voice = Voice(violin)
        voice.pin(line, Offset(0))

        notee0 = Note(DiatonicPitch(5, 'a'), Duration(1, 8))
        notee1 = Note(DiatonicPitch(5, 'b'), Duration(1, 8))
        beam.append([notee0, notee1])

        print(voice)
        notes = voice.get_notes_starting_in_interval(
            Interval(Position(3, 4), Position(1, 1)))

        assert len(notes) == 2
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('A:5'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('B:5'))

        # Do the same but append a Beam to a line
        violin = c.get_instrument("violin")

        note0 = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
        note1 = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))

        line = Line()
        line.pin(note0, Offset(1, 4))
        line.pin(note1, Offset(3, 8))

        voice = Voice(violin)
        voice.pin(line, Offset(0))

        beam = Beam([note2, note3])
        line.pin(beam, Offset(1, 2))

        print(voice)
        notes = voice.get_notes_starting_in_interval(
            Interval(Position(1, 2), Position(1, 1)))

        assert len(notes) == 2
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('C:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('D:4'))

        # Do the same but append a Beam to a Beam
        violin = c.get_instrument("violin")

        note0 = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
        note1 = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))

        line = Line()
        beam = Beam([note0, note1])
        line.pin(beam, Offset(1, 4))

        voice = Voice(violin)
        voice.pin(line, Offset(0))

        beam1 = Beam([note2, note3])
        beam.append(beam1)

        print(voice)
        notes = voice.get_notes_starting_in_interval(
            Interval(Position(1, 2), Position(1, 1)))

        assert len(notes) == 2
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('C:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('D:4'))

        # try to add note out of range on violin.
        notex = Note(DiatonicPitch(7, 'b'), Duration(1, 8))
        with self.assertRaises(Exception):
            line.pin(notex, Offset(3, 1))
Exemple #30
0
    def test_nested_notes(self):
        note1 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        note3 = Note(DiatonicPitch(4, 'e'), Duration(1, 16))
        sub_beam = Beam([note1, note2, note3])

        beam = Beam()
        beam.append(Note(DiatonicPitch(4, 'f'), Duration(1, 8)))
        beam.append(sub_beam)
        beam.append(Note(DiatonicPitch(4, 'g'), Duration(1, 8)))

        print(beam)

        notes = beam.get_all_notes()
        assert len(notes) == 5
        assert notes[0].diatonic_pitch == DiatonicPitch(4, 'f')
        assert notes[1].diatonic_pitch == DiatonicPitch(4, 'c')
        assert notes[2].diatonic_pitch == DiatonicPitch(4, 'd')
        assert notes[3].diatonic_pitch == DiatonicPitch(4, 'e')
        assert notes[4].diatonic_pitch == DiatonicPitch(4, 'g')

        assert notes[0].relative_position == Offset(0)
        assert notes[1].relative_position == Offset(0)
        assert notes[2].relative_position == Offset(1, 16)
        assert notes[3].relative_position == Offset(1, 8)
        assert notes[4].relative_position == Offset(9, 32)
        TestBeam.print_all_notes(notes)

        notes = sub_beam.get_all_notes()
        TestBeam.print_all_notes(notes)

        b = Beam()
        b.append(beam)

        notes = sub_beam.get_all_notes()
        TestBeam.print_all_notes(notes)
        assert notes[0].duration == Duration(1, 32)
        assert str(notes[0].diatonic_pitch) == 'C:4'

        sub_beam_prime = sub_beam.clone()
        notes = sub_beam_prime.get_all_notes()
        TestBeam.print_all_notes(notes)
        assert notes[0].duration == Duration(1, 8)
        assert str(notes[0].diatonic_pitch) == 'C:4'

        beam_prime = beam.clone()
        notes = beam_prime.get_all_notes()
        TestBeam.print_all_notes(notes)
        assert notes[0].duration == Duration(1, 8)
        assert str(notes[0].diatonic_pitch) == 'F:4'

        b_prime = b.clone()
        notes = b_prime.get_all_notes()
        TestBeam.print_all_notes(notes)
        assert notes[0].duration == Duration(1, 16)
        assert str(notes[0].diatonic_pitch) == 'F:4'