def test_enharmonic_fixed_tone(self):
        logging.debug('Start test_enharmonic_fixed_tone')
        note = Note(DiatonicPitch.parse("C:5"), Duration(1, 4))

        policy = FixedToneConstraint(note, DiatonicToneCache.get_tone("Bbb"))

        lower_policy_context = TestFixedToneConstraint.policy_creator(ModalityType.Major, DiatonicTone('G'), 'tV',
                                                                      'C:2', 'C:8')
        lower_contextual_note = ContextualNote(lower_policy_context)
        m = dict([(note, lower_contextual_note)])

        v_result = policy.values(m, note)

        assert len(v_result) == 6
        for note in v_result:
            print('test_simple_fixed_tone pitch = {0}'.format(note.diatonic_pitch))

            assert note.diatonic_pitch.diatonic_tone == DiatonicToneCache.get_tone('A')
            assert note.base_duration == Duration(1, 4)

            lower_contextual_note.note = note

            result = policy.verify(m)
            assert result is True

            lower_contextual_note.note = None

        logging.debug('End test_enharmonic_fixed_tone')
예제 #2
0
    def test_reversal_on_policy(self):
        logging.debug('Start test_reversal_on_policy')
        lower_policy_context = TestRelativeScalarStepConstraint.policy_creator(
            ModalityType.Major, DiatonicTone('G'), 'tV', 'C:2', 'C:8')
        note1 = Note(DiatonicPitch.parse('C:5'), Duration(1, 8))
        note2 = Note(DiatonicPitch.parse('D:5'), Duration(1, 8))
        lower_note_1 = ContextualNote(lower_policy_context)
        lower_note_2 = ContextualNote(
            lower_policy_context,
            Note(DiatonicPitch.parse('C:5'), Duration(1, 8)))

        p_map = dict([(note1, lower_note_1), (note2, lower_note_2)])

        # F#:5 --> G Major two below and 3 above
        policy = RelativeScalarStepConstraint(note1, note2, -2, 3)

        result = policy.values(p_map, note1)
        pitches = [n.diatonic_pitch for n in result]

        for pitch in pitches:
            logging.debug(pitch)

        # Check that each returned verifies
        for n in result:
            lower_note_1.note = n
            assert policy.verify(p_map)

        logging.debug('End test_reversal_on_policy')
예제 #3
0
    def test_basic_policy(self):
        logging.debug('Start test_basic_policy')
        lower_policy_context = TestRelativeScalarStepConstraint.policy_creator(
            ModalityType.Major, DiatonicTone('G'), 'tV', 'C:2', 'C:8')
        note1 = Note(DiatonicPitch.parse('C:5'), Duration(1, 8))
        note2 = Note(DiatonicPitch.parse('D:5'), Duration(1, 8))
        lower_note_1 = ContextualNote(
            lower_policy_context,
            Note(DiatonicPitch.parse('F#:5'), Duration(1, 8)))
        lower_note_2 = ContextualNote(lower_policy_context)

        p_map = dict([(note1, lower_note_1), (note2, lower_note_2)])

        # F#:5 --> G Major two below and 3 above
        policy = RelativeScalarStepConstraint(note1, note2, -2, 3)

        v_result = policy.values(p_map, note2)
        pitches = [n.diatonic_pitch for n in v_result]
        assert len(pitches) == 6
        for s in ['D:5', 'E:5', 'F#:5', 'G:5', 'A:5', 'B:5']:
            assert DiatonicPitch.parse(s) in pitches

        for note in v_result:
            logging.debug(note)

        # Check verify for each answer
        for n in v_result:
            lower_note_2.note = n
            assert policy.verify(p_map)

        logging.debug('End test_basic_policy')
    def test_non_scale_note(self):
        logging.debug('Start test_non_scale_note')

        note = Note(DiatonicPitch.parse("Bb:4"), Duration(1, 4))
        policy = FixedPitchConstraint(note, DiatonicPitch.parse("Ab:5"))

        policy_context = TestFixedPitchConstraint.policy_creator(
            ModalityType.Major, DiatonicTone('C'), 'tIV', 'C:2', 'C:8')
        contextual_note = ContextualNote(policy_context)
        p_map = PMap()
        p_map[note] = contextual_note

        v_result = policy.values(p_map, note)

        result = next(iter(v_result))
        print('test_non_scale_note note= {0}'.format(result))

        assert result.diatonic_pitch == DiatonicPitch.parse("Ab:5")
        assert result.base_duration == Duration(1, 4)

        contextual_note.note = result

        result = policy.verify(p_map)
        assert result is True

        logging.debug('end test_non_scale_note')
예제 #5
0
    def test_simple_fixed_pitch(self):
        logging.debug('Start test_simple_fixed_pitch')

        note = Note(DiatonicPitch.parse("C:5"), Duration(1, 4))

        select_notes = {'A:5', 'C:4', 'Eb:2', 'F#:6'}

        constraint = FixedPitchSelectSetConstraint(
            note, {DiatonicPitch.parse(p)
                   for p in select_notes})

        policy_context = TestFixedPitchSelectSetConstraint.policy_creator(
            ModalityType.Major, DiatonicTone('C'), 'tIV', 'C:2', 'C:8')
        lower_contextual = ContextualNote(policy_context)
        p_map = PMap()
        p_map[note] = lower_contextual

        v_results = constraint.values(p_map, note)
        assert v_results is not None
        assert len(v_results) == len(select_notes)
        for result in v_results:
            print(str(result))

        assert select_notes == {str(n.diatonic_pitch) for n in v_results}

        assert not constraint.verify(p_map)

        lower_contextual.note = Note(DiatonicPitch.parse('Eb:2'),
                                     Duration(1, 4))
        assert constraint.verify(p_map)
    def test_simple_diatonic_test(self):
        logging.debug('Start test_simple_diatonic_test')
        lower_policy_context = TestRelativeDiatonicConstraint.policy_creator(
            ModalityType.Major, DiatonicTone('G'), 'tV', 'C:2', 'C:8')
        upper_note_1 = Note(DiatonicPitch.parse('C:5'), Duration(1, 8))
        upper_note_2 = Note(DiatonicPitch.parse('D:5'), Duration(1, 8))
        lower_note_1 = ContextualNote(
            lower_policy_context,
            Note(DiatonicPitch.parse('F#:5'), Duration(1, 8)))
        lower_note_2 = ContextualNote(lower_policy_context)

        p_map = dict([(upper_note_1, lower_note_1),
                      (upper_note_2, lower_note_2)])

        policy = RelativeDiatonicConstraint(upper_note_1, upper_note_2,
                                            Interval(3, IntervalType.Minor),
                                            Interval(3, IntervalType.Major))

        v_result = policy.values(p_map, upper_note_2)

        for note in v_result:
            logging.debug(note)

        pitches = [note.diatonic_pitch for note in v_result]
        assert {str(p)
                for p in pitches} == {'D:5', 'E:5', 'F#:5', 'G:5', 'A:5'}

        # test for determining note 1
        logging.debug('Determining note 1')
        lower_note_2.note = lower_note_1.note
        lower_note_1.note = None

        v_result = policy.values(p_map, upper_note_1)

        for note in v_result:
            logging.debug(note)

        pitches = [note.diatonic_pitch for note in v_result]
        assert {str(p) for p in pitches} == {'A:5', 'G:5', 'F#:5', 'E:5'}

        logging.debug('End test_simple_diatonic_test')
    def test_in_range(self):
        logging.debug('Start test_in_range')
        lower_policy_context = TestPitchRangeConstraint.policy_creator(ModalityType.Major, DiatonicTone('G'), 'tV',
                                                                       'C:2', 'C:8')

        v_notes = []
        p_map = PMap()
        for s in ['C:5', 'B:4', 'D:5', 'G:4']:
            v_note = Note(DiatonicPitch.parse(s), Duration(1, 8))
            v_notes.append(v_note)
            p_map[v_note] = ContextualNote(lower_policy_context)

        policy = PitchRangeConstraint(v_notes, PitchRange.create('G:3', 'A:4'))

        values = policy.values(p_map, v_notes[0])

        assert values is not None
        assert len(values) is not 0
        pitches = set()
        for v in values:
            print(v.diatonic_pitch)
            pitches.add(v.diatonic_pitch)

        answers_str = {'G:3', 'A:3', 'B:3', 'C:4', 'D:4', 'E:4', 'F#:4', 'G:4', 'A:4'}
        answers = {DiatonicPitch.parse(s) for s in answers_str}
        assert pitches == answers

        index = 0
        it = iter(values)
        for v_note in v_notes:
            p_map[v_note].note = next(it)
            index = index + 1
        assert policy.verify(p_map) is True

        # Change one and get false
        p_map[v_notes[0]].note = Note(DiatonicPitch.parse('B:4'), Duration(1, 8))
        assert policy.verify(p_map) is False

        logging.debug('End test_in_range')
    def test_compute_simple_function_tone(self):
        print('--- test_compute_simple_function_tone ---')
        line = Line()

        f = GenericUnivariatePitchFunction(
            TestFitPitchToFunctionConstraint.sinasoidal, Position(0),
            Position(2))
        v_note = Note(DiatonicPitch.parse('A:4'), Duration(1, 32))
        line.pin(v_note, Offset(0))

        constraint, lower_policy_context = TestFitPitchToFunctionConstraint.build_simple_constraint(
            v_note, f, ModalityType.Major, 'G', 'tV')
        p_map = PMap()
        p_map[v_note] = ContextualNote(lower_policy_context)

        results = constraint.values(p_map, v_note)
        assert results is not None
        assert len(results) == 1
        print(next(iter(results)).diatonic_pitch)
        assert 'C:4' == str(next(iter(results)).diatonic_pitch)

        v_note = Note(DiatonicPitch.parse('A:4'), Duration(1, 32))
        line.pin(v_note, Offset(1, 32))

        constraint, lower_policy_context = TestFitPitchToFunctionConstraint.build_simple_constraint(
            v_note, f, ModalityType.Major, 'G', 'tV')
        p_map = PMap()
        p_map[v_note] = ContextualNote(lower_policy_context)

        results = constraint.values(p_map, v_note)
        assert results is not None
        assert len(results) == 1
        print(next(iter(results)).diatonic_pitch)
        assert 'E:4' == str(next(iter(results)).diatonic_pitch)

        p_map[v_note].note = next(iter(results))
        assert constraint.verify(p_map)
    def test_basic_policy(self):
        logging.debug('Start test_basic_policy')

        lower_policy_context = TestComparativePitchConstraint.policy_creator(
            ModalityType.Major, DiatonicTone('G'), 'tV', 'C:4', 'C:6')
        upper_note_1 = Note(DiatonicPitch.parse('C:5'), Duration(1, 8))
        upper_note_2 = Note(DiatonicPitch.parse('D:5'), Duration(1, 8))
        lower_note_1 = ContextualNote(
            lower_policy_context,
            Note(DiatonicPitch.parse('F#:5'), Duration(1, 8)))
        lower_note_2 = ContextualNote(lower_policy_context)

        p_map = dict([(upper_note_1, lower_note_1),
                      (upper_note_2, lower_note_2)])

        policy = ComparativePitchConstraint(
            upper_note_1, upper_note_2, ComparativePitchConstraint.LESS_THAN)
        result = policy.values(p_map, upper_note_2)

        pitches = sorted([note.diatonic_pitch for note in result])

        for pitch in pitches:
            logging.debug(pitch)

        # validate
        assert DiatonicPitch.parse('F#:5') not in pitches
        assert len(pitches) == 4
        for pitch in pitches:
            assert pitch.chromatic_distance > DiatonicPitch.parse('F#:5').chromatic_distance, \
                "{0} <= {1}".format(pitch,  DiatonicPitch.parse('F#:5'))

        for note in result:
            lower_note_2.note = note
            assert policy.verify(p_map) is True
        lower_note_2.note = None

        # Do less than
        logging.debug('------')
        policy = ComparativePitchConstraint(
            upper_note_1, upper_note_2,
            ComparativePitchConstraint.GREATER_THAN)
        result = policy.values(p_map, upper_note_2)

        pitches = sorted([note.diatonic_pitch for note in result])
        for pitch in pitches:
            logging.debug(pitch)

        assert DiatonicPitch.parse('F#:5') not in pitches
        assert len(pitches) == 10

        for pitch in pitches:
            assert pitch.chromatic_distance < DiatonicPitch.parse('F#:5').chromatic_distance, \
                "{0} >= {1}".format(pitch,  DiatonicPitch.parse('F#:5'))

        for note in result:
            lower_note_2.note = note
            assert policy.verify(p_map) is True
        lower_note_2.note = None

        # Do greater than or equal
        logging.debug('------')
        policy = ComparativePitchConstraint(
            upper_note_1, upper_note_2, ComparativePitchConstraint.LESS_EQUAL)
        result = policy.values(p_map, upper_note_2)

        pitches = sorted([note.diatonic_pitch for note in result])
        for pitch in pitches:
            logging.debug(pitch)

        assert DiatonicPitch.parse('F#:5') in pitches
        assert len(pitches) == 5

        for pitch in pitches:
            assert pitch.chromatic_distance >= DiatonicPitch.parse('F#:5').chromatic_distance, \
                "{0} < {1}".format(pitch,  DiatonicPitch.parse('F#:5'))

        for note in result:
            lower_note_2.note = note
            assert policy.verify(p_map) is True
        lower_note_2.note = None

        # Do less than or equal
        logging.debug('------')
        policy = ComparativePitchConstraint(
            upper_note_1, upper_note_2,
            ComparativePitchConstraint.GREATER_EQUAL)
        result = policy.values(p_map, upper_note_2)

        pitches = sorted([note.diatonic_pitch for note in result])
        for pitch in pitches:
            logging.debug(pitch)

        assert DiatonicPitch.parse('F#:5') in pitches
        assert len(pitches) == 11

        for pitch in pitches:
            assert pitch.chromatic_distance <= DiatonicPitch.parse('F#:5').chromatic_distance, \
                "{0} > {1}".format(pitch,  DiatonicPitch.parse('F#:5'))

        for note in result:
            lower_note_2.note = note
            assert policy.verify(p_map) is True
        lower_note_2.note = None

        # Do equal
        logging.debug('------')
        policy = ComparativePitchConstraint(upper_note_1, upper_note_2,
                                            ComparativePitchConstraint.EQUAL)
        result = policy.values(p_map, upper_note_2)

        pitches = sorted([note.diatonic_pitch for note in result])
        for pitch in pitches:
            logging.debug(pitch)

        assert DiatonicPitch.parse('F#:5') in pitches
        assert len(pitches) == 1

        for pitch in pitches:
            assert pitch.chromatic_distance == DiatonicPitch.parse('F#:5').chromatic_distance, \
                "{0} != {1}".format(pitch,  DiatonicPitch.parse('F#:5'))

        for note in result:
            lower_note_2.note = note
            assert policy.verify(p_map) is True
        lower_note_2.note = None

        logging.debug('End test_basic_policy')