def test_is_not_equal(self): logging.debug('Start test_is_not_equal') note1 = Note(DiatonicPitch.parse('C:5'), Duration(1, 8)) lower_policy_context = TestNotEqualPitchConstraint.policy_creator( ModalityType.Major, DiatonicTone('G'), 'tV', 'C:2', 'C:8') lower_context_note_a = ContextualNote( lower_policy_context, Note(DiatonicPitch.parse('F#:6'), Duration(1, 8))) p_map = PMap() p_map[note1] = lower_context_note_a other_source = [] for tone in ['B:5', 'D:5', 'E:5']: n = Note(DiatonicPitch.parse(tone), Duration(1, 8)) lower_context_note = ContextualNote(lower_policy_context) p_map[n] = lower_context_note other_source.append(n) params = list([note1]) params.extend(other_source) policy = NotEqualPitchConstraint(params) for c_note in p_map.unassigned_actors(policy): print(c_note) v_result = policy.values(p_map, c_note) assert v_result is not None assert DiatonicPitch.parse('F#:6') not in { n.diatonic_pitch for n in v_result } p_map[other_source[1]].note = Note(DiatonicPitch.parse('E:6'), Duration(1, 8)) for c_note in p_map.unassigned_actors(policy): print(c_note) v_result = policy.values(p_map, c_note) assert v_result is not None ret_pitches = {n.diatonic_pitch for n in v_result} assert len( ret_pitches.intersection( {DiatonicPitch.parse('F#:6'), DiatonicPitch.parse('E:6')})) == 0 assert policy.verify(p_map) is False p_map[other_source[2]].note = Note(DiatonicPitch.parse('F#:6'), Duration(1, 8)) assert policy.verify(p_map) is False p_map[other_source[0]].note = Note(DiatonicPitch.parse('D:6'), Duration(1, 8)) assert policy.verify(p_map) is False p_map[other_source[2]].note = Note(DiatonicPitch.parse('A:6'), Duration(1, 8)) assert policy.verify(p_map) is True logging.debug('End test_is_not_equal') return
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]
def test_simple_motif(self): line = Line() notes = [ Note(DiatonicPitch.parse('C:4'), Duration(1, 4)), Note(DiatonicPitch.parse('D:4'), Duration(1, 8)), Note(DiatonicPitch.parse('E:4'), Duration(1, 8)), Note(DiatonicPitch.parse('F#:4'), Duration(1, 2)), ] line.pin(notes) c = [ EqualPitchConstraint([notes[0], notes[2]]), NotEqualPitchConstraint([notes[1], notes[3]]) ] nm = Phrase(notes, c, 'B') print(nm) assert nm.name == 'B' actors = nm.actors assert len(actors) == 4 cc = nm.constraints 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) # More notes for copy to: first_note = Note(DiatonicPitch.parse('C:3'), Duration(1, 4)) notes1 = [ first_note, Note(DiatonicPitch.parse('D:3'), Duration(1, 8)), Note(DiatonicPitch.parse('E:3'), Duration(1, 8)), Note(DiatonicPitch.parse('F#:3'), Duration(1, 2)), ] line.pin(notes1, Offset(2)) nm_clone = nm.copy_to(first_note) assert isinstance(nm_clone, Phrase) assert nm_clone.name == 'B' c_actors = nm_clone.actors assert len(c_actors) == 4 assert 'F#:3' == str(c_actors[3].diatonic_pitch)
def test_book_example(self): line_str = '{<C-Major: I> iC:4 D C F F A G A iC:5 D C F f A G A}' lge = LineGrammarExecutor() target_line, _ = lge.parse(line_str) notes = target_line.get_all_notes() ca = [ EqualPitchConstraint([notes[0], notes[2]]), NotEqualPitchConstraint([notes[1], notes[3]]) ] cb = [ NotEqualPitchConstraint([notes[4], notes[5]]), EqualPitchConstraint([notes[5], notes[7]]) ] a = Motif([notes[0], notes[1], notes[2], notes[3]], ca, 'A') b = Motif([notes[4], notes[5], notes[6], notes[7]], cb, 'B') phrase_constraints = [ EqualPitchConstraint([notes[3], notes[4]]), ] phrase = Phrase([notes[2], notes[3], notes[4], notes[5]], phrase_constraints, 'P1') mf_constraints = [ NotEqualPitchConstraint([notes[2], notes[5]]), ] mf = MelodicForm([a, b], [phrase], mf_constraints, 'MF1') print('[{0}]'.format(','.join( [str(n.diatonic_pitch) for n in mf.actors]))) mf_dup = mf.copy_to(notes[8]) print('[{0}]'.format(','.join( [str(n.diatonic_pitch) for n in mf_dup.actors])))
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)
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)
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
def test_simple_form(self): line = Line() notes = [ Note(DiatonicPitch.parse('C:4'), Duration(1, 8)), Note(DiatonicPitch.parse('D:4'), Duration(1, 8)), Note(DiatonicPitch.parse('E:4'), Duration(1, 8)), Note(DiatonicPitch.parse('F:4'), Duration(1, 8)), Note(DiatonicPitch.parse('G:4'), Duration(1, 8)), Note(DiatonicPitch.parse('A:4'), Duration(1, 8)), Note(DiatonicPitch.parse('B:4'), Duration(1, 8)), Note(DiatonicPitch.parse('C:4'), Duration(1, 4)), Note(DiatonicPitch.parse('C:3'), Duration(1, 8)), Note(DiatonicPitch.parse('D:3'), Duration(1, 8)), Note(DiatonicPitch.parse('E:3'), Duration(1, 8)), Note(DiatonicPitch.parse('F:3'), Duration(1, 8)), Note(DiatonicPitch.parse('G:3'), Duration(1, 8)), Note(DiatonicPitch.parse('A:3'), Duration(1, 8)), Note(DiatonicPitch.parse('B:3'), Duration(1, 8)), Note(DiatonicPitch.parse('C:3'), Duration(1, 4)), ] f = Fraction(0) for n in notes: line.pin(n, Offset(f)) f = f + n.duration.duration ca = [ EqualPitchConstraint([notes[0], notes[2]]), NotEqualPitchConstraint([notes[1], notes[3]]) ] cb = [ NotEqualPitchConstraint([notes[4], notes[5]]), EqualPitchConstraint([notes[6], notes[7]]) ] a = Motif([notes[0], notes[1], notes[2], notes[3]], ca, 'A') b = Motif([notes[4], notes[5], notes[6], notes[7]], cb, 'B') p1_constraints = [ EqualPitchConstraint([notes[4], notes[5]]), ] p1 = Phrase([notes[2], notes[3], notes[4], notes[5]], p1_constraints, 'P1') mf_constraints = [ EqualPitchConstraint([notes[2], notes[5]]), ] mf = MelodicForm([a, b], [p1], mf_constraints, 'MF1') actors = mf.actors assert actors is not None assert len(actors) == 8 assert actors[0] == notes[0] motifs = mf.motifs assert motifs is not None assert len(motifs) == 2 assert motifs[0].name == 'A' assert motifs[1].name == 'B' p_constraints = mf.phrase_constraints assert p_constraints is not None assert len(p_constraints) == 1 assert type(p_constraints[0]) == EqualPitchConstraint p_actors = mf.phrase_actors assert p_actors is not None assert len(p_actors) == 4 mf1 = mf.copy_to(notes[8]) assert mf1 is not None actors = mf1.actors assert actors is not None assert len(actors) == 8 assert actors[0] == notes[8] motifs = mf1.motifs assert motifs is not None assert len(motifs) == 2 assert motifs[0].name == 'A' assert motifs[1].name == 'B' p_constraints = mf1.phrase_constraints assert p_constraints is not None assert len(p_constraints) == 1 assert type(p_constraints[0]) == EqualPitchConstraint p_actors = mf1.phrase_actors assert p_actors is not None assert len(p_actors) == 4 phs = mf1.phrases assert phs is not None assert len(phs) == 1 assert phs[0].actors[0] == notes[10]