Esempio n. 1
0
    def clone(self):
        from structure.beam import Beam
        from structure.tuplet import Tuplet
        from structure.note import Note
        from structure.line import Line
        from timemodel.duration import Duration
        cpy = None
        if isinstance(self, Beam):
            cpy = Beam()
            for s in self.sub_notes:
                s_prime = s.clone()
                cpy.append(s_prime)
        elif isinstance(self, Tuplet):
            cpy = Tuplet(self.unit_duration, self.unit_duration_factor)
            for s in self.sub_notes:
                s_prime = s.clone()
                cpy.append(s_prime)
        elif isinstance(self, Note):
            d = Duration(self.base_duration.duration / self.contextual_reduction_factor)
            cpy = Note(self.diatonic_pitch if self.diatonic_pitch is not None else None, d, self.num_dots)
        elif isinstance(self, Line):
            cpy = Line(None, self.instrument)
            for s in self.sub_notes:
                s_prime = s.clone()
                cpy.pin(s_prime, s.relative_position)

        return cpy
Esempio n. 2
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'
Esempio n. 3
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'
Esempio n. 4
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)
Esempio n. 5
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)
Esempio n. 6
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)
Esempio n. 7
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)
Esempio n. 8
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]
Esempio n. 9
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))
Esempio n. 10
0
    def test_simple_form(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)))
        s.append(Note(DiatonicPitch.parse('E:4'), Duration(1, 8)))
        s.append(Note(DiatonicPitch.parse('F#:4'), Duration(1, 8)))
        line.pin(s)
        notes = s.get_all_notes()

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

        a = Motif(s, c, 'A')

        s1 = [
           Note(DiatonicPitch(4, 'c'), Duration(1, 8)),
           Note(DiatonicPitch(4, 'd'), Duration(1, 8)),
           Note(DiatonicPitch(4, 'e'), Duration(1, 8)),
        ]
        tuplet = Tuplet(Duration(1, 8), 2, s1)
        line.pin(tuplet, Offset(1, 2))
        notes = tuplet.get_all_notes()

        c1 = [
            EqualPitchConstraint([notes[0], notes[2]]),
            RelativeScalarStepConstraint(notes[1], notes[2], -2, 2)
        ]

        b = Motif(tuplet, c1, 'B')

        f = Form([a, b])
        print(f)

        constraints = [
            EqualPitchConstraint([a.actors[0], b.actors[1]])
        ]

        # Ensure a, b cloned for reliability - see comment in Form.
        ff = Form([a, b], constraints)
        print(ff)

        constr = ff.external_constraints
        assert len(constr) == 1

        actors = ff.actors
        assert len(actors) == 7

        assert isinstance(constr[0], EqualPitchConstraint)
        assert constr[0].actors[0] == actors[0]
        assert constr[0].actors[1] == actors[4 + 1]

        all_constr = ff.constraints
        assert len(all_constr) == 5

        # Add more notes to clone ff as:
        s3 = Beam()
        first_note = Note(DiatonicPitch.parse('C:5'), Duration(1, 8))
        s3.append(first_note)
        s3.append(Note(DiatonicPitch.parse('D:5'), Duration(1, 8)))
        s3.append(Note(DiatonicPitch.parse('E:5'), Duration(1, 8)))
        s3.append(Note(DiatonicPitch.parse('F#:5'), Duration(1, 8)))
        line.pin(s3, Offset(3))

        s2 = [
           Note(DiatonicPitch(5, 'c'), Duration(1, 8)),
           Note(DiatonicPitch(5, 'd'), Duration(1, 8)),
           Note(DiatonicPitch(5, 'e'), Duration(1, 8)),
        ]
        tuplet1 = Tuplet(Duration(1, 8), 2, s2)
        line.pin(tuplet1, Offset(7, 2))

        fff = ff.copy_to(first_note)
        assert fff is not None
        print(fff)

        constr = fff.external_constraints
        assert len(constr) == 1
        assert len(fff.constraints) == 5

        actors = fff.actors
        assert len(actors) == 7

        assert isinstance(constr[0], EqualPitchConstraint)
        assert constr[0].actors[0] == actors[0]
        assert constr[0].actors[1] == actors[4 + 1]

        all_constr = fff.constraints
        assert len(all_constr) == 5