def test_book_example(self): # Interpreter 0__.C:4 and each step of 1/12 maps to noew chromatic interpreter = ChromaticRangeInterpreter(DiatonicPitch.parse('C:4'), 0, Fraction(1, 12)) # local_sin maps 0->0 .25->1 .5->0 .75->-1 1->0 and so on. f = GenericUnivariatePitchFunction(local_sin, Position(0), Position(2), False, interpreter) for i in range(0, 9): p = f.eval_as_nearest_pitch(i * 0.25) print('[{0}] {1}'.format((i * 0.25), str(p)))
def test_generic_sin(self): f = GenericUnivariatePitchFunction( TestGenericUnivariatePitchFunction.sinasoidal, Position(0), Position(2)) factor = 32 for i in range(0, factor + 1): p = Fraction(i, factor) d = f.eval_as_chromatic_distance(p) np = f.eval_as_nearest_pitch(p) choices = TestGenericUnivariatePitchFunction.print_choices( f.eval_as_pitch(p)) txt = '[{0}] chrom={1} near_p={2} choices={3}'.format( i, d, np, choices) print(txt) # test p = Fraction(5, factor) d = f.eval_as_chromatic_distance(p) np = f.eval_as_nearest_pitch(p) choices = f.eval_as_pitch(p) assert 2 == len(choices) assert 'D#:5' == str(choices[0]) assert 'E:5' == str(choices[1])
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_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 test_compute_with_minor_key(self): print('-- test_compute_with_minor_key ---') line = Line() f = GenericUnivariatePitchFunction( TestFitPitchToFunctionConstraint.sinasoidal, Position(0), Position(2)) v_notes = [ Note(DiatonicPitch.parse('A:4'), Duration(1, 16)) for _ in range(0, 33) ] for i in range(0, 33): line.pin(v_notes[i], Offset(i, 16)) constraint, lower_policy_context = \ TestFitPitchToFunctionConstraint.build_simple_constraint(v_notes[0], f, ModalityType.NaturalMinor, 'C', 'tV') constraints = list() constraints.append(constraint) for i in range(1, 33): c, _ = \ TestFitPitchToFunctionConstraint.build_simple_constraint(v_notes[i], f, ModalityType.NaturalMinor, 'C', 'tV') constraints.append(c) p_map = PMap() p_map[v_notes[0]] = ContextualNote(lower_policy_context) results = constraint.values(p_map, v_notes[0]) assert results is not None assert len(results) == 1 print(next(iter(results)).diatonic_pitch) assert 'C:4' == str(next(iter(results)).diatonic_pitch) result_pitches = [] for i in range(0, 33): p_map = PMap() p_map[v_notes[i]] = ContextualNote(lower_policy_context) results = constraints[i].values(p_map, v_notes[i]) result_pitches.append(next(iter(results)).diatonic_pitch) assert len(result_pitches) == 33 for i in range(0, 33): print('[{0}] {1}'.format(i, str(result_pitches[i]))) checks = [ 'C:4', 'G:4', 'D:5', 'F:5', 'G:5', 'F:5', 'D:5', 'G:4', 'C:4' ] for i in range(0, len(checks)): assert checks[i] == str(result_pitches[i])
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()])))
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 motif_example(): print('----- motif_example (Fgure 17.25) -----') line_str = '{<C-Major: I> iC:4 D E D E E E E C D E D E E E E C D E D E E E E wE}' score = create_score(line_str, 'piano', (4, 4, 'swww')) tonality = score.hct.get_hc_by_position(0).tonality all_notes = score.line.get_all_notes() constraints = { StepSequenceConstraint( [all_notes[0], all_notes[1], all_notes[2], all_notes[3]], [1, 1, -1]) } motif = Motif([all_notes[0], all_notes[1], all_notes[2], all_notes[3]], constraints) motif1 = motif.copy_to(all_notes[8]) motif2 = motif.copy_to(all_notes[16]) form = MelodicForm([motif, motif1, motif2]) # 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) t_reshape = TReshape(score, pitch_function, Range(0, 3), 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]))
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_differing_keys(self): print('--- test_differing_keys ---') bflat_interp = ScalarRangeInterpreter( Tonality.create(ModalityType.Major, DiatonicToneCache.get_tone('Bb')), 'Bb:3', 0) pitch_function = GenericUnivariatePitchFunction( TestPitchFitFunction.e_flat_linear, Position(0), Position(1), False, bflat_interp) chords = [('tI', 1), ('tIV', (1, 2)), ('tV', (1, 2)), ('tVI', 1)] hct, tempo_sequence, ts_sequence = TestPitchFitFunction.create_score_artifacts( ModalityType.Major, 'E', chords, (4, 4, 'swww')) pitch_fit_function = PitchFitFunction(pitch_function, tempo_sequence, ts_sequence, hct) p = Position(0) d = Duration(1, 16) pitches = list() for i in range(0, 32): pitch = pitch_fit_function(p) print('f({0}) = {1}'.format(p, pitch)) pitches.append(pitch) p = p + d assert pitches[0] is None assert 'C:4' == str(pitches[1]) assert 'D:4' == str(pitches[2]) assert 'D#:4' == str(pitches[3]) assert 'F:4' == str(pitches[4]) assert 'G:4' == str(pitches[5]) assert 'A:4' == str(pitches[6]) assert 'Bb:4' == str(pitches[7]) assert 'C:5' == str(pitches[8]) assert 'D:5' == str(pitches[9])
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)
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)
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()
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)
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]))