Esempio n. 1
0
def simple_reshape_no_pf():
    print('----- test_simple_reshape_no_pf -----')

    line_str = '{<C-Major: I> iE:4 E E E E E E E <:IV> qE ie e <:V> qe ie e <:VI>  qE E iE E E E}'

    score = create_score(line_str, 'violin', (3, 4, 'sww'))

    all_notes = score.line.get_all_notes()

    pitch_function = GenericUnivariatePitchFunction(sinasoidal, Position(0),
                                                    Position(3))
    time_range = Range(0, 3)

    # The first note should have one of 3 values, C:4, E:4, G:4
    constraints = {
        ChordalPitchConstraint(all_notes[0]),
        ChordalPitchConstraint(all_notes[9]),
        PitchRangeConstraint([all_notes[0]], PitchRange.create('C:4', 'G:4')),
    }

    motif = Motif(score.line, constraints, 'A')
    melodic_form = MelodicForm([motif])
    treshape = TReshape(score, pitch_function, time_range, melodic_form, False)

    results = treshape.apply()

    mc_filter = MinCurveFitFilter(pitch_function, results)
    print('{0} filtered results'.format(len(mc_filter.scored_results)))

    for index in range(0, min(5, len(mc_filter.scored_results))):
        result = mc_filter.scored_results[index]
        print('[{0}] {1} ({2})'.format(index, str_line(result[0].line),
                                       result[1]))
    def test_is_chordal(self):
        logging.debug('Start test_is_chordal')

        upper_context_note = Note(DiatonicPitch.parse('F:5'), Duration(1, 4))

        lower_policy_context = TestChordalToneConstraint.policy_creator(ModalityType.Major, DiatonicTone('G'), 'tV',
                                                                        'C:2', 'C:8')
        lower_context_note = ContextualNote(lower_policy_context)
        parameter_map = dict([(upper_context_note, lower_context_note)])
        policy = ChordalPitchConstraint(upper_context_note)

        v_result = policy.values(parameter_map, upper_context_note)

        results = {DiatonicTone('D'), DiatonicTone('F#'), DiatonicTone('A')}
        for note in v_result:
            print('test_is_chordal; note = {0}'.format(note))
            tone = note.diatonic_pitch.diatonic_tone
            octave = note.diatonic_pitch.octave
            assert tone in results
            assert octave in range(2, 9)

        assert len(v_result) == 6 * 3

        for note in v_result:
            parameter_map[upper_context_note].note = note
            assert policy.verify(parameter_map) is True
        logging.debug('End test_is_chordal')
Esempio n. 3
0
    def test_for_book_example_2(self):
        print('----- test for book example 2 -----')

        source_instance_expression = '{<A-Major:i> [sA:4 A A A] qA:4 [iA:4 A] <:iv> qA:4 [sA:4 A A A] qA:4}'
        target_instance_expression = '{<G-Major:i> wA:4 <:iv> wA:4}'
        lge = LineGrammarExecutor()

        source_instance_line, source_instance_hct = lge.parse(
            source_instance_expression)

        actors = source_instance_line.get_all_notes()
        for a in actors:
            print("{0}".format(a))

        target_instance_line, target_instance_hct = lge.parse(
            target_instance_expression)
        target_hcs = target_instance_hct.hc_list()
        for hc in target_hcs:
            print("{0}".format(hc))

        pitch_range = PitchRange(
            DiatonicPitch.parse('C:4').chromatic_distance,
            DiatonicPitch.parse('C:6').chromatic_distance)

        p_map = PMap.create(source_instance_expression, pitch_range,
                            [('G-Major:I', Duration(3, 4)),
                             ('G-Major:IV', Duration(3, 4))])

        actors = p_map.actors

        policies = OrderedSet()
        policies.add(
            StepSequenceConstraint(
                [actors[0], actors[1], actors[2], actors[3]], [1, 1, 1]))
        policies.add(ChordalPitchConstraint(actors[0]))
        policies.add(ChordalPitchConstraint(actors[4]))
        policies.add(ChordalPitchConstraint(actors[8]))
        policies.add(
            StepSequenceConstraint(
                [actors[8], actors[9], actors[10], actors[11]], [1, -1, -1]))
        policies.add(EqualPitchConstraint([actors[0], actors[12]]))
        policies.add(EqualPitchConstraint([actors[4], actors[7]]))
        policies.add(
            RelativeDiatonicConstraint(actors[4], actors[5],
                                       Interval(3, IntervalType.Major),
                                       Interval(1, IntervalType.Perfect)))
        policies.add(StepSequenceConstraint([actors[5], actors[6]], [-1]))

        # policies.add(ChordalPitchConstraint(actors[7]))

        solver = PitchConstraintSolver(policies)

        full_results, partial_results = solver.solve(p_map)
        print('Results has {0} results.'.format(len(full_results)))

        for pm in full_results:
            if str(pm[actors[7]].note.diatonic_pitch) == 'D:4' and str(
                    pm[actors[0]].note.diatonic_pitch) == 'G:4':
                print("{0}".format(pm))
Esempio n. 4
0
    def _build_constraints(self, pattern_to_target):
        pair_annotations = self.substitution_pattern.target_analysis.note_pair_annotation
        note_annotations = self.substitution_pattern.target_analysis.note_annotation

        constraints = list()

        for pair_annotation in pair_annotations:
            t1 = pattern_to_target[pair_annotation.first_note]
            t2 = pattern_to_target[pair_annotation.second_note]
            if pair_annotation.relationship == NotePairInformation.Relationship.LT:
                rel = ComparativePitchConstraint.LESS_THAN
            elif pair_annotation.relationship == NotePairInformation.Relationship.GT:
                rel = ComparativePitchConstraint.GREATER_THAN
            else:
                rel = ComparativePitchConstraint.EQUAL
            constraint = ComparativePitchConstraint(t1, t2, rel)
            constraints.append(constraint)

        for annotation in note_annotations:
            if annotation.is_chordal:
                constraint = ChordalPitchConstraint(
                    pattern_to_target[annotation.note])
                constraints.append(constraint)

        # Get the constraints off the motifs
        if self.substitution_pattern.target_melodic_form:
            form_constraints = self.substitution_pattern.target_melodic_form.constraints
            for c in form_constraints:
                c_prime = c.clone([pattern_to_target[n] for n in c.actors])
                constraints.append(c_prime)

        return constraints
def simple_reshape_cpf():
    print('----- test_simple_reshape_cpf (Fgure 17.21) -----')

    line_str = '{<C-Major: I> iE:4 E E E E E E E <:IV> qE ie e <:V> qe ie e <:VI>  qE E iE E E E}'
    score = create_score(line_str, 'violin', (3, 4, 'sww'))

    all_notes = score.line.get_all_notes()

    # 11 scalar notes to C:4 (0) to G:5 (1) with pitch unit 1/19
    interpreter = ChromaticRangeInterpreter(DiatonicPitch.parse('C:4'), 0,
                                            Fraction(1, 19))

    pitch_function = GenericUnivariatePitchFunction(three_sin,
                                                    Position(0),
                                                    Position(3),
                                                    interp=interpreter)

    # The first note should have one of 3 values, C:4, E:4, G:4
    constraints = {
        ChordalPitchConstraint(all_notes[0]),
        ChordalPitchConstraint(all_notes[8]),
        ChordalPitchConstraint(all_notes[11]),
        ChordalPitchConstraint(all_notes[14]),
        PitchRangeConstraint([all_notes[0]], PitchRange.create('C:4', 'E:4')),
    }

    # motif = Motif(score.line, constraints, 'A')
    motif = Motif([all_notes[0], all_notes[8], all_notes[11], all_notes[14]],
                  constraints, 'A')
    melodic_form = MelodicForm([motif])
    t_reshape = TReshape(score, pitch_function, Range(0, 3), melodic_form,
                         True)

    results = t_reshape.apply()

    filters = MinCurveFitFilter(pitch_function, results)
    print('{0} filtered results'.format(len(filters.scored_results)))

    for index in range(0, min(5, len(filters.scored_results))):
        result = filters.scored_results[index]
        print('[{0}] {1} ({2})'.format(index, str_line(result[0].line),
                                       result[1]))

    print('Chords: {0}'.format(','.join([str(c)
                                         for c in score.hct.hc_list()])))
Esempio n. 6
0
    def test_simple_reshape(self):
        print('----- test_hct_simple_shift -----')

        s_notes = [
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),

            ('E:4', 'h'),
            ('E:4', 'h'),

            ('E:4', 'q'),
            ('E:4', 'q'),
            ('E:4', 'q'),
            ('E:4', 'q'),
        ]
        chords = [('tI', 1), ('tIV', (1, 2)), ('tV', (1, 2)), ('tVI', 1)]

        score = TestTReshape.create_score(s_notes, ModalityType.Major, 'C', chords, 'violin', (3, 4, 'sww'))

        all_notes = score.line.get_all_notes()

        pitch_function = GenericUnivariatePitchFunction(TestTReshape.sinasoidal, Position(0), Position(3))
        time_range = Range(0, 3)

        # The first note should have one of 3 values, C:4, E:4, G:4
        constraints = {
            ChordalPitchConstraint(all_notes[0]),
            PitchRangeConstraint([all_notes[0]], PitchRange.create('C:4', 'G:4')),
        }

        motif = Motif(score.line, constraints, 'A')
        melodic_form = MelodicForm([motif])
        treshape = TReshape(score, pitch_function, time_range, melodic_form, False)

        results = treshape.apply()
        assert results is not None
        assert len(results) == 3
        for result in results:
            print('-----')
            print(result.line)

        first_pitch_set = {str(result.line.get_all_notes()[0].diatonic_pitch) for result in results}
        assert {'E:4', 'C:4', 'G:4'} == first_pitch_set

        assert abs(TestTReshape.sinasoidal(Fraction(1, 8)) - DiatonicPitch.parse('D:5').chromatic_distance) < 1
def reshape_to_scale():
    print('----- test_reshape_to_scale (Fgure 17.24) -----')

    line_str = '{<C-Major: I> iE:4 E E E E E E E E E E E E E E E E E E E E E E E wE}'

    score = create_score(line_str, 'violin', (4, 4, 'swww'))

    tonality = score.hct.get_hc_by_position(0).tonality
    all_notes = score.line.get_all_notes()

    plf = PiecewiseLinearFunction([(0, 0), (1, 8), (Fraction(3, 2), 4), (2, 8),
                                   (3, 0)])
    for i in range(0, 17):
        x = Fraction(1, 8) * i
        y = plf(x)
        print('({0}, {1})'.format(x, y))
    time_range = Range(0, 3)

    interpreter = ScalarRangeInterpreter(tonality, DiatonicPitch.parse('C:4'),
                                         0, 1)

    pitch_function = GenericUnivariatePitchFunction(plf, Position(0),
                                                    Position(3), False,
                                                    interpreter)

    constraints = {
        ChordalPitchConstraint(all_notes[0]),
        PitchRangeConstraint([all_notes[0]], PitchRange.create('C:4', 'G:4')),
    }

    motif = Motif([all_notes[0]], constraints, 'A')
    melodic_form = MelodicForm([motif])
    t_reshape = TReshape(score, pitch_function, time_range, melodic_form,
                         False)

    results = t_reshape.apply()

    filters = MinCurveFitFilter(pitch_function, results)
    print('{0} filtered results'.format(len(filters.scored_results)))

    for index in range(0, min(5, len(filters.scored_results))):
        result = filters.scored_results[index]
        print('[{0}] {1} ({2})'.format(index, str_line(result[0].line),
                                       result[1]))
    def _build_constraints(self, source_to_target, tag_map):
        # Constraints:
        #    contour based on pair analysis
        #    chordal if original note is chordal
        #    melodic form constraints
        #    Tunnel: for diatonic notes, a pitch range constraint based on the specified "tunnel" over target notes.
        pair_annotations = self.source_analysis.note_pair_annotation
        note_annotations = self.source_analysis.note_annotation

        constraints = list()

        for pair_annotation in pair_annotations:
            t1 = source_to_target[pair_annotation.first_note]
            t2 = source_to_target[pair_annotation.second_note]
            if pair_annotation.relationship == NotePairInformation.Relationship.LT:
                rel = ComparativePitchConstraint.LESS_THAN
            elif pair_annotation.relationship == NotePairInformation.Relationship.GT:
                rel = ComparativePitchConstraint.GREATER_THAN
            else:
                rel = ComparativePitchConstraint.EQUAL
            constraint = ComparativePitchConstraint(t1, t2, rel)
            constraints.append(constraint)

        for annotation in note_annotations:
            if annotation.is_chordal:
                constraint = ChordalPitchConstraint(
                    source_to_target[annotation.note])
                constraints.append(constraint)

        # Get the constraints off the motifs
        if self.source_melodic_form:
            form_constraints = self.source_melodic_form.constraints
            for c in form_constraints:
                c_prime = c.clone([source_to_target[n] for n in c.actors])
                constraints.append(c_prime)

        tunnel_constraints = self._build_tunnel_constraints(
            source_to_target, tag_map)
        constraints.extend(tunnel_constraints)

        return constraints
Esempio n. 9
0
    def test_piecewise_linear_reshape(self):
        """
        This is a very interesting test case. Although it uses a piecewise linear based reshape function, more
        importantly, the line's harmonic context track is in the key Bb-major, but the pitch function interprets
        pitches in E-major!
        The rule is to always use what the pitch function returns over the hct, except when the computed pitch has
        an enharmonic equivalent - in this case, the only case is Eb == D#, and Eb is preferred.
        :return:
        """
        print('----- test_piecewise_linear_reshape -----')

        s_notes = [
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),

            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
        ]
        chords = [('tI', 1), ('tIV', (1, 2)), ('tV', (1, 2)), ('tVI', 1)]

        score = TestTReshape.create_score(s_notes, ModalityType.Major, 'Bb', chords, 'violin', (4, 4, 'swww'))

        e_interp = ScalarRangeInterpreter(Tonality.create(ModalityType.Major, DiatonicToneCache.get_tone('E')), 'E:4',
                                          0)

        array = [(0, 0), (Fraction(1, 2), 4), (Fraction(1), 0), (Fraction(2), 8)]
        f = PiecewiseLinearFunction(array)
        pitch_function = GenericUnivariatePitchFunction(f, Position(0), Position(2), False,
                                                        e_interp)
        all_notes = score.line.get_all_notes()

        # The first note should have one of 2 values, Eb:4, G:4
        constraints = {
            ChordalPitchConstraint(all_notes[0]),
        }

        motif = Motif(score.line, constraints, 'A')

        melodic_form = MelodicForm([motif])

        time_range = Range(0, 3)

        treshape = TReshape(score, pitch_function, time_range, melodic_form, True)
        results = treshape.apply()
        assert results is not None
        for i in range(0, len(results)):
            print('--- result[{0}] ---'.format(i))
            print(results[i].line)

        assert len(results) == 2
        first_pitch_set = {str(result.line.get_all_notes()[0].diatonic_pitch) for result in results}
        assert {'D:4', 'F:4'} == first_pitch_set

        all_notes = results[0].line.get_all_notes()
        assert 'F#:4' == str(all_notes[1].diatonic_pitch)
        assert 'G#:4' == str(all_notes[2].diatonic_pitch)
        assert 'A:4' == str(all_notes[3].diatonic_pitch)
        assert 'B:4' == str(all_notes[4].diatonic_pitch)
        assert 'A:4' == str(all_notes[5].diatonic_pitch)
        assert 'G#:4' == str(all_notes[6].diatonic_pitch)
        assert 'F#:4' == str(all_notes[7].diatonic_pitch)
        assert 'E:4' == str(all_notes[8].diatonic_pitch)
        assert 'F#:4' == str(all_notes[9].diatonic_pitch)
        assert 'G#:4' == str(all_notes[10].diatonic_pitch)
        assert 'A:4' == str(all_notes[11].diatonic_pitch)
        assert 'B:4' == str(all_notes[12].diatonic_pitch)
        assert 'C#:5' == str(all_notes[13].diatonic_pitch)
        assert 'Eb:5' == str(all_notes[14].diatonic_pitch)

        all_notes = results[1].line.get_all_notes()
        assert 'F#:4' == str(all_notes[1].diatonic_pitch)
        assert 'G#:4' == str(all_notes[2].diatonic_pitch)
        assert 'A:4' == str(all_notes[3].diatonic_pitch)
        assert 'B:4' == str(all_notes[4].diatonic_pitch)
        assert 'A:4' == str(all_notes[5].diatonic_pitch)
        assert 'G#:4' == str(all_notes[6].diatonic_pitch)
        assert 'F#:4' == str(all_notes[7].diatonic_pitch)
        assert 'E:4' == str(all_notes[8].diatonic_pitch)
        assert 'F#:4' == str(all_notes[9].diatonic_pitch)
        assert 'G#:4' == str(all_notes[10].diatonic_pitch)
        assert 'A:4' == str(all_notes[11].diatonic_pitch)
        assert 'B:4' == str(all_notes[12].diatonic_pitch)
        assert 'C#:5' == str(all_notes[13].diatonic_pitch)
        assert 'Eb:5' == str(all_notes[14].diatonic_pitch)
Esempio n. 10
0
    def test_pitch_sequence_shape(self):
        print('----- test_pitch_sequence_shape -----')

        s_notes = [
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),

            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
        ]
        chords = [('tI', 1), ('tIV', 1), ('tV', 1), ('tVI', 1)]

        score = TestTReshape.create_score(s_notes, ModalityType.Major, 'Bb', chords, 'violin', (4, 4, 'swww'))
        all_notes = score.line.get_all_notes()

        eflat_interp = ScalarRangeInterpreter(Tonality.create(ModalityType.Major, DiatonicToneCache.get_tone('Bb')),
                                              'Bb:3', 0)
        pitch_function = GenericUnivariatePitchFunction(TestTReshape.e_flat_linear, Position(0), Position(1), False,
                                                        eflat_interp)

        # The first note should have one of 2 values, Eb:4, G:4
        constraints = {
            ChordalPitchConstraint(all_notes[0]),
            # You need this kind of constraint to limit possibilities.
            PitchRangeConstraint([all_notes[0]], PitchRange.create('Bb:3', 'A:4')),
            StepSequenceConstraint([all_notes[3], all_notes[4], all_notes[5], all_notes[6]], [-1, -1, -1]),
        }

        motif = Motif(score.line, constraints, 'A')

        melodic_form = MelodicForm([motif])

        time_range = Range(0, 3)

        treshape = TReshape(score, pitch_function, time_range, melodic_form, True)
        results = treshape.apply()
        assert results is not None
        for i in range(0, len(results)):
            print('--- result[{0}] ---'.format(i))
            print(results[i].line)

        for result in results:
            notes = result.line.get_all_notes()
            if str(notes[3].diatonic_pitch) == 'Eb:4':
                assert 'D:4' == str(notes[4].diatonic_pitch)
                assert 'C:4' == str(notes[5].diatonic_pitch)
                assert 'Bb:3' == str(notes[6].diatonic_pitch)
            else:
                assert 'D:4' == str(notes[3].diatonic_pitch)
                assert 'C:4' == str(notes[4].diatonic_pitch)
                assert 'Bb:3' == str(notes[5].diatonic_pitch)
                assert 'A:3' == str(notes[6].diatonic_pitch)
Esempio n. 11
0
    def test_onbeat_shape(self):
        print('----- test_onbeat_shape -----')

        s_notes = [
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
        ]
        chords = [('tI', 1), ('tIV', (1, 2)), ('tV', (1, 2)), ('tVI', 1)]

        score = TestTReshape.create_score(s_notes, ModalityType.Major, 'Bb', chords, 'violin', (4, 4, 'swww'))
        all_notes = score.line.get_all_notes()

        # The first note should have one of 2 values, Eb:4, G:4
        constraints = {
            ChordalPitchConstraint(all_notes[0]),
            OnBeatConstraint(all_notes[2], BeatType.Strong),
            # You need this kind of constraint to limit possibilities.
            PitchRangeConstraint([all_notes[0]], PitchRange.create('Bb:3', 'A:4')),
        }

        motif = Motif(score.line, constraints, 'A')

        melodic_form = MelodicForm([motif])

        eflat_interp = ScalarRangeInterpreter(Tonality.create(ModalityType.Major, DiatonicToneCache.get_tone('Bb')),
                                              'Bb:3', 0)
        pitch_function = GenericUnivariatePitchFunction(TestTReshape.e_flat_linear, Position(0), Position(1), False,
                                                        eflat_interp)
        time_range = Range(0, 3)

        treshape = TReshape(score, pitch_function, time_range, melodic_form, False)

        results = treshape.apply()
        assert results is not None
        for i in range(0, len(results)):
            print('--- result[{0}] ---'.format(i))
            print(results[i].line)

        first_pitch_set = {str(result.line.get_all_notes()[0].diatonic_pitch) for result in results}
        assert {'Bb:3', 'D:4', 'F:4'} == first_pitch_set

        all_notes = results[0].line.get_all_notes()
        assert 'C:4' == str(all_notes[1].diatonic_pitch)
        assert 'C:5' == str(all_notes[2].diatonic_pitch)
        assert 'D:5' == str(all_notes[3].diatonic_pitch)
        assert 'Eb:5' == str(all_notes[4].diatonic_pitch)
        assert 'F:5' == str(all_notes[5].diatonic_pitch)
        assert 'G:5' == str(all_notes[6].diatonic_pitch)
        assert 'A:5' == str(all_notes[7].diatonic_pitch)
        assert Position(1) == all_notes[2].get_absolute_position()

        all_notes = results[1].line.get_all_notes()
        assert 'C:4' == str(all_notes[1].diatonic_pitch)
        assert 'C:5' == str(all_notes[2].diatonic_pitch)
        assert 'D:5' == str(all_notes[3].diatonic_pitch)
        assert 'Eb:5' == str(all_notes[4].diatonic_pitch)
        assert 'F:5' == str(all_notes[5].diatonic_pitch)
        assert 'G:5' == str(all_notes[6].diatonic_pitch)
        assert 'A:5' == str(all_notes[7].diatonic_pitch)
        assert Position(1) == all_notes[2].get_absolute_position()
Esempio n. 12
0
    def test_linear_scale(self):
        print('----- test_linear_scale -----')

        s_notes = [
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
            ('E:4', 'e'),
        ]
        chords = [('tI', 1), ('tIV', (1, 2)), ('tV', (1, 2)), ('tVI', 1)]

        score = TestTReshape.create_score(s_notes, ModalityType.Major, 'Eb', chords, 'violin', (3, 4, 'sww'))
        all_notes = score.line.get_all_notes()

        eflat_interp = ScalarRangeInterpreter(Tonality.create(ModalityType.Major, DiatonicToneCache.get_tone('Eb')),
                                              'Eb:4', 0)
        pitch_function = GenericUnivariatePitchFunction(TestTReshape.e_flat_linear, Position(0), Position(1), False,
                                                        eflat_interp)
        time_range = Range(0, 3)

        # The first note should have one of 2 values, Eb:4, G:4
        constraints = {
            ChordalPitchConstraint(all_notes[0]),
            PitchRangeConstraint([all_notes[0]], PitchRange.create('C:4', 'G:4')),
        }

        motif = Motif(score.line, constraints, 'A')

        melodic_form = MelodicForm([motif])

        treshape = TReshape(score, pitch_function, time_range, melodic_form)

        results = treshape.apply()
        assert results is not None
        assert len(results) == 2
        for result in results:
            print('-----')
            print(result.line)

        first_pitch_set = {str(result.line.get_all_notes()[0].diatonic_pitch) for result in results}
        assert {'Eb:4', 'G:4'} == first_pitch_set

        all_notes = results[0].line.get_all_notes()
        assert 'F:4' == str(all_notes[1].diatonic_pitch)
        assert 'G:4' == str(all_notes[2].diatonic_pitch)
        assert 'Ab:4' == str(all_notes[3].diatonic_pitch)
        assert 'Bb:4' == str(all_notes[4].diatonic_pitch)
        assert 'C:5' == str(all_notes[5].diatonic_pitch)
        assert 'D:5' == str(all_notes[6].diatonic_pitch)
        assert 'Eb:5' == str(all_notes[7].diatonic_pitch)

        all_notes = results[1].line.get_all_notes()
        assert 'F:4' == str(all_notes[1].diatonic_pitch)
        assert 'G:4' == str(all_notes[2].diatonic_pitch)
        assert 'Ab:4' == str(all_notes[3].diatonic_pitch)
        assert 'Bb:4' == str(all_notes[4].diatonic_pitch)
        assert 'C:5' == str(all_notes[5].diatonic_pitch)
        assert 'D:5' == str(all_notes[6].diatonic_pitch)
        assert 'Eb:5' == str(all_notes[7].diatonic_pitch)
Esempio n. 13
0
def reshape_with_spf():
    print('----- test_reshape_with_spf (Fgure 17.22) -----')

    line_str = '{<C-Major: I> iE:4 E E E E q@E <:IV> qE ie e <:V> qe ie e <:VI>  qE E iE E E E}'

    score = create_score(line_str, 'piano', (3, 4, 'sww'))

    tonality = score.hct.get_hc_by_position(0).tonality
    all_notes = score.line.get_all_notes()

    # 11 scalar notes to C:4 (0) to G:5 (11) with pitch unit 1/11
    interpreter = ScalarRangeInterpreter(tonality, DiatonicPitch.parse('C:4'),
                                         0, Fraction(1, 11))

    pitch_function = GenericUnivariatePitchFunction(three_sin, Position(0),
                                                    Position(3), False,
                                                    interpreter)

    # The first note should have one of 3 values, C:4, E:4, G:4
    constraints = {
        ChordalPitchConstraint(all_notes[0]),
        ChordalPitchConstraint(all_notes[6]),
        ChordalPitchConstraint(all_notes[9]),
        ChordalPitchConstraint(all_notes[12]),
        PitchRangeConstraint([all_notes[0]], PitchRange.create('C:4', 'E:4')),
    }

    motif = Motif([all_notes[0], all_notes[6], all_notes[9], all_notes[12]],
                  constraints, 'A')
    melodic_form = MelodicForm([motif])
    t_reshape = TReshape(score, pitch_function, Range(0, 3), melodic_form,
                         True)

    results = t_reshape.apply()

    filters = MinCurveFitFilter(pitch_function, results)
    print('{0} filtered results'.format(len(filters.scored_results)))

    for index in range(0, min(5, len(filters.scored_results))):
        result = filters.scored_results[index]
        print('[{0}] {1} ({2})'.format(index, str_line(result[0].line),
                                       result[1]))

    constraints = {
        ChordalPitchConstraint(all_notes[0]),
        ChordalPitchConstraint(all_notes[4]),
        ChordalPitchConstraint(all_notes[6]),
        ChordalPitchConstraint(all_notes[8]),
        ChordalPitchConstraint(all_notes[12]),
        ChordalPitchConstraint(all_notes[14]),
        PitchRangeConstraint([all_notes[0]], PitchRange.create('C:4', 'G:4')),
    }

    motif = Motif(score.line, constraints, 'A')
    melodic_form = MelodicForm([motif])
    t_reshape = TReshape(score, pitch_function, Range(0, 3), melodic_form,
                         True)

    results = t_reshape.apply()

    filters = MinCurveFitFilter(pitch_function, results)
    print('{0} filtered results'.format(len(filters.scored_results)))

    for index in range(0, min(5, len(filters.scored_results))):
        result = filters.scored_results[index]
        print('[{0}] {1} ({2})'.format(index, str_line(result[0].line),
                                       result[1]))
Esempio n. 14
0
    def test_for_book_example_1(self):
        print('----- test for book example 1 -----')

        source_instance_expression = '{<C-Major:IV> [sC:5 B:4 A G] qF:4 [sA:4 B C:5 D] qD:5}'
        target_instance_expression = '{<G-Major:V> wA:4}'

        lge = LineGrammarExecutor()

        target_instance_line, target_instance_hct = lge.parse(
            target_instance_expression)
        target_hcs = target_instance_hct.hc_list()
        for hc in target_hcs:
            print("{0}".format(hc))

        pitch_range = PitchRange.create('C:2', 'C:8')

        p_map = PMap.create(source_instance_expression, pitch_range,
                            [('G-Major:V', 1)])

        actors = p_map.actors
        for a in actors:
            print("{0}".format(a))

        policies = set()
        policies.add(
            PitchStepConstraint(actors[0], actors[1], 1,
                                PitchStepConstraint.Down))
        policies.add(
            PitchStepConstraint(actors[1], actors[2], 1,
                                PitchStepConstraint.Down))
        policies.add(
            PitchStepConstraint(actors[2], actors[3], 1,
                                PitchStepConstraint.Down))
        policies.add(
            PitchStepConstraint(actors[5], actors[6], 1,
                                PitchStepConstraint.UP))
        policies.add(
            PitchStepConstraint(actors[6], actors[7], 1,
                                PitchStepConstraint.UP))
        policies.add(
            PitchStepConstraint(actors[7], actors[8], 1,
                                PitchStepConstraint.UP))
        policies.add(EqualPitchConstraint([actors[3], actors[4]]))
        policies.add(EqualPitchConstraint([actors[8], actors[9]]))
        policies.add(
            RelativeDiatonicConstraint(actors[4], actors[5],
                                       Interval(3, IntervalType.Major),
                                       Interval(1, IntervalType.Perfect)))
        policies.add(
            RelativeDiatonicConstraint(actors[3], actors[8],
                                       Interval(5, IntervalType.Perfect),
                                       Interval(1, IntervalType.Perfect)))
        policies.add(ChordalPitchConstraint(actors[4]))
        policies.add(ChordalPitchConstraint(actors[9]))

        solver = PitchConstraintSolver(policies)

        full_results, _ = solver.solve(p_map)
        print('Results has {0} results.'.format(len(full_results)))

        for pm in full_results:
            print("{0}".format(pm))