예제 #1
0
 def _build_default_time_sig_tempo():
     tempo_seq = TempoEventSequence()
     ts_seq = EventSequence()
     tempo_seq.add(TempoEvent(Tempo(60, Duration(1, 4)), Position(0)))
     ts_seq.add(
         TimeSignatureEvent(TimeSignature(3, Duration(1, 4), 'sww'),
                            Position(0)))
     return ts_seq, tempo_seq
    def __init__(self, event_list=None):
        """
        Constructor.

        Args:
            event_list: list of TempoEvents to initialize the sequence.
        """
        EventSequence.__init__(self, event_list)
예제 #3
0
    def create_score_1(line, hct, instrument, ts):
        tempo_seq = TempoEventSequence()
        ts_seq = EventSequence()
        tempo_seq.add(TempoEvent(Tempo(60, Duration(1, 4)), Position(0)))
        ts_seq.add(TimeSignatureEvent(TimeSignature(ts[0], Duration(1, ts[1]), ts[2]), Position(0)))

        c = InstrumentCatalog.instance()
        instrument = c.get_instrument(instrument)

        return LiteScore(line, hct, instrument, tempo_seq, ts_seq)
예제 #4
0
    def test_basic_setup(self):
        c = InstrumentCatalog.instance()
        violin = c.get_instrument("violin")

        # Add notes to the score
        vnote0 = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
        vnote1 = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
        vnote2 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        vnote3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        vnote4 = Note(DiatonicPitch(4, 'e'), Duration(1, 8))
        vnote5 = Note(DiatonicPitch(4, 'f'), Duration(1, 8))

        # Set up a violin voice with 6 8th notes
        vline = Line([vnote0, vnote1, vnote2, vnote3, vnote4, vnote5])

        tempo_seq = TempoEventSequence()
        ts_seq = EventSequence()
        tempo_seq.add(TempoEvent(Tempo(60), Position(0)))
        ts_seq.add(
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4), 'sww'),
                               Position(0)))

        hc_track = HarmonicContextTrack()
        diatonic_tonality = Tonality.create(ModalityType.Major,
                                            DiatonicTone("C"))
        chord_t = TertianChordTemplate.parse('tIV')
        chord = chord_t.create_chord(diatonic_tonality)
        hc_track.append(HarmonicContext(diatonic_tonality, chord, Duration(2)))

        score = LiteScore(vline, hc_track, violin, tempo_seq, ts_seq)

        bp = score.beat_position(Position(0))
        print(bp)
        assert bp.beat_number == 0

        bp = score.beat_position(Position(5, 8))
        print(bp)
        assert bp.measure_number == 0
        assert bp.beat_number == Fraction(5, 2)
        assert int(bp.beat_number) == 2
        assert bp.beat_number - bp.beat == Fraction(1, 2)

        tse = score.time_signature_sequence.floor_event(Position(5, 8))
        assert tse is not None
        print(tse.object.beat_type(bp.beat))
        assert tse.object.beat_type(bp.beat) == BeatType.Weak
        assert bp.beat_fraction == Fraction(1, 2)

        bp = score.beat_position(Position(1, 16))
        print(bp)
        tse = score.time_signature_sequence.floor_event(Position(1, 16))
        print(tse.object.beat_type(bp.beat))
        assert tse.object.beat_type(bp.beat) == BeatType.Strong
    def build_simple_constraint(v_note, f, modality_type, key_str, chord_str):
        lower_policy_context = TestFitPitchToFunctionConstraint.policy_creator(
            modality_type, DiatonicTone(key_str), chord_str, 'C:2', 'C:8')

        tempo_seq = TempoEventSequence()
        ts_seq = EventSequence()
        tempo_seq.add(TempoEvent(Tempo(60, Duration(1, 4)), Position(0)))
        ts_seq.add(
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4), 'sww'),
                               Position(0)))

        return FitPitchToFunctionConstraint(v_note, f, tempo_seq,
                                            ts_seq), lower_policy_context
예제 #6
0
    def create_score(line_expression, instrument, ts):
        lge = LineGrammarExecutor()
        source_instance_line, source_instance_hct = lge.parse(line_expression)

        tempo_seq = TempoEventSequence()
        ts_seq = EventSequence()
        tempo_seq.add(TempoEvent(Tempo(60, Duration(1, 4)), Position(0)))
        ts_seq.add(TimeSignatureEvent(TimeSignature(ts[0], Duration(1, ts[1]), ts[2]), Position(0)))

        c = InstrumentCatalog.instance()
        instrument = c.get_instrument(instrument)

        return LiteScore(source_instance_line, source_instance_hct, instrument, tempo_seq, ts_seq)
예제 #7
0
    def create_score_artifacts(modality, key_tone, chords, ts):
        diatonic_tonality = Tonality.create(
            modality, DiatonicToneCache.get_tone(key_tone))

        hc_track = TestPitchFitFunction.create_track(chords, diatonic_tonality)

        tempo_seq = TempoEventSequence()
        ts_seq = EventSequence()
        tempo_seq.add(TempoEvent(Tempo(60, Duration(1, 4)), Position(0)))
        ts_seq.add(
            TimeSignatureEvent(TimeSignature(ts[0], Duration(1, ts[1]), ts[2]),
                               Position(0)))

        return hc_track, tempo_seq, ts_seq
예제 #8
0
    def test_time_conversion_simple(self):
        tempo_line = EventSequence([TempoEvent(Tempo(60), Position(0))])
        ts_line = EventSequence([
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)), Position(0))
        ])
        conversion = TimeConversion(tempo_line, ts_line, Position(1, 1))
        actual_time = conversion.position_to_actual_time(Position(3, 4))
        print(actual_time)
        self.assertTrue(actual_time == 3000,
                        'actual time = {0} should be 3000'.format(actual_time))

        position = conversion.actual_time_to_position(3000)
        print(position)
        self.assertTrue(position, Position(3, 4))
예제 #9
0
    def create_score(s_notes, modality, key_tone, chords, instrument, ts):
        diatonic_tonality = Tonality.create(modality, DiatonicToneCache.get_tone(key_tone))

        hc_track = TestTReshape.create_track(chords, diatonic_tonality)

        tempo_seq = TempoEventSequence()
        ts_seq = EventSequence()
        tempo_seq.add(TempoEvent(Tempo(60, Duration(1, 4)), Position(0)))
        ts_seq.add(TimeSignatureEvent(TimeSignature(ts[0], Duration(1, ts[1]), ts[2]), Position(0)))

        c = InstrumentCatalog.instance()
        violin = c.get_instrument(instrument)

        return LiteScore(TestTReshape.create_line(s_notes), hc_track, violin, tempo_seq, ts_seq)
예제 #10
0
def create_score(grammar_str, instrument, ts):
    lge = LineGrammarExecutor()
    target_line, target_hct = lge.parse(grammar_str)

    tempo_seq = TempoEventSequence()
    ts_seq = EventSequence()
    tempo_seq.add(TempoEvent(Tempo(60, Duration(1, 4)), Position(0)))
    ts_seq.add(
        TimeSignatureEvent(TimeSignature(ts[0], Duration(1, ts[1]), ts[2]),
                           Position(0)))

    c = InstrumentCatalog.instance()
    violin = c.get_instrument(instrument)

    return LiteScore(target_line, target_hct, violin, tempo_seq, ts_seq)
예제 #11
0
def build_vst_midi_list():
    """

    :return:
    """
    c = InstrumentCatalog.instance()

    # Add notes to the score
    vnote0 = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
    vnote1 = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
    vnote2 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
    vnote3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
    vnote4 = Note(DiatonicPitch(4, 'e'), Duration(1, 8))
    vnote5 = Note(DiatonicPitch(4, 'f'), Duration(1, 8))

    # Set up a violin voice with 6 8th notes
    vline = Line([vnote0, vnote1, vnote2, vnote3, vnote4, vnote5])

    tempo_seq = TempoEventSequence()
    ts_seq = EventSequence()
    tempo_seq.add(TempoEvent(Tempo(60), Position(0)))
    ts_seq.add(
        TimeSignatureEvent(TimeSignature(3, Duration(1, 4), 'sww'),
                           Position(0)))

    hc_track = HarmonicContextTrack()
    diatonic_tonality = Tonality.create(ModalityType.Major, DiatonicTone("C"))
    chord_t = TertianChordTemplate.parse('tIV')
    chord = chord_t.create_chord(diatonic_tonality)
    hc_track.append(HarmonicContext(diatonic_tonality, chord, Duration(2)))

    score = Score()

    score.tempo_sequence.add(TempoEvent(Tempo(60), Position(0)))
    score.time_signature_sequence.add(
        TimeSignatureEvent(TimeSignature(3, Duration(1, 4)), Position(0)))

    violin = c.get_instrument("violin")
    violin_instrument_voice = InstrumentVoice(violin, 1)
    score.add_instrument_voice(violin_instrument_voice)
    violin_instrument_voice.voice(0).pin(vline)

    return ScoreToVstMidiConverter.convert_score(score, {0: 0}), score
예제 #12
0
def create_score(line_text, instrument, tmpo, ts):
    """

    :param line_text:
    :param instrument:
    :param tmpo: (bpm, tempo beat duration)
    :param ts: (num beats, ts beat duration)
    :return:
    """
    lge = LineGrammarExecutor()
    source_instance_line, source_instance_hct = lge.parse(line_text)

    tempo_seq = TempoEventSequence()
    ts_seq = EventSequence()
    tempo_seq.add(TempoEvent(Tempo(tmpo[0], tmpo[1]), Position(0)))
    ts_seq.add(TimeSignatureEvent(TimeSignature(ts[0], ts[1]), Position(0)))

    c = InstrumentCatalog.instance()
    instrument = c.get_instrument(instrument)

    return LiteScore(source_instance_line, source_instance_hct, instrument,
                     tempo_seq, ts_seq)
예제 #13
0
    def test_position_to_actual_time(self):
        tempo_line = EventSequence([
            TempoEvent(Tempo(60), Position(0)),
            TempoEvent(Tempo(20), Position(4, 4))
        ])
        ts_line = EventSequence([
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)), Position(0)),
            TimeSignatureEvent(TimeSignature(2, Duration(1, 8)),
                               Position(5, 4))
        ])
        conversion = TimeConversion(tempo_line, ts_line, Position(2, 1))
        actual_time = conversion.position_to_actual_time(Position(6, 4))
        print(actual_time)
        # 4 quarter notes @ 60 with 1/4 beat = 4000
        # 1 quarter note @ 20 with 1/4 beat  = 3000
        # 2 eighth notes @ 20 (effective 40) with 1/8 beat  = 3000
        self.assertTrue(
            actual_time == 10000,
            'actual time = {0} should be 10000'.format(actual_time))

        position = conversion.actual_time_to_position(10000)
        print(position)
        self.assertEquals(position, Position(6, 4))
예제 #14
0
    def __init__(self, coverage_node_list, tempo_seq, ts_seq, hct, line):
        """
        Constructor
        :param coverage_node_list: List of note structures that are affected by on beat constraints.
        :param tempo_seq: TempoEventSequence
        :param ts_seq: EventSequence of TimeSignatures
        :param hct: HarmonicContextTrack
        :param line: Line
        """
        self.__coverage_node_list = coverage_node_list

        self.__coverage_node_aggregates = dict()
        self.__coverage_node_deltas = dict()

        # duplicate time signature event sequence
        new_ts_list = []
        for e in ts_seq.sequence_list:
            new_ts_list.append(
                TimeSignatureEvent(
                    TimeSignature(e.object.beats_per_measure,
                                  e.object.beat_duration,
                                  e.object.beat_pattern), e.time))
        self.__ts_event_sequence = EventSequence(new_ts_list)

        # duplicate tempo event sequence
        new_tempo_list = []
        for e in tempo_seq.sequence_list:
            new_tempo_list.append(
                TempoEvent(Tempo(e.object.tempo, e.object.beat_duration),
                           e.time))
        self.__tempo_event_sequence = TempoEventSequence(new_tempo_list)

        self.__line = line

        self.__hct = HarmonicContextTrack()
        for hc in hct.hc_list():
            self.__hct.append(
                HarmonicContext(hc.tonality, hc.chord, Duration(hc.duration),
                                Position(hc.position)))

        for n in self.coverage_node_list:
            self.__coverage_node_aggregates[n] = 0
            self.__coverage_node_deltas[n] = 0
예제 #15
0
 def test_successor(self):
     es = EventSequence()
     p1 = Event('str_1', Position(1, 2))
     es.add(p1)
     print(es)
     assert es.successor(p1) is None
     
     p2 = Event('str_2', Position(1))
     es.add(p2)
     print(es)
     assert es.successor(p1) == p2
     assert es.successor(p2) is None
     
     p3 = Event('str_2', Position(3, 4))
     es.add(p3)
     print(es)
     assert es.successor(p1) == p3
     assert es.successor(p3) == p2
     assert es.successor(p2) is None
예제 #16
0
    def test_position_to_bp(self):
        tempo_line = EventSequence(TempoEvent(Tempo(60), Position(0)))
        ts_line = EventSequence(
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)), Position(0)))
        conversion = TimeConversion(tempo_line, ts_line, Position(2, 1))

        bp = conversion.position_to_bp(Position(1, 2))
        print(bp)
        self.assertTrue(bp == BeatPosition(0, 2),
                        'bp is {0}, not BP[0, 2]'.format(bp))

        bp = conversion.position_to_bp(Position(1))
        print(bp)
        self.assertTrue(bp == BeatPosition(1, 1),
                        'bp is {0}, not BP[1 ,1]'.format(bp))

        tempo_line = EventSequence([
            TempoEvent(Tempo(60), Position(0)),
            TempoEvent(Tempo(20), Position(4, 4))
        ])
        ts_line = EventSequence([
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)), Position(0)),
            TimeSignatureEvent(TimeSignature(2, Duration(1, 8)),
                               Position(6, 4))
        ])
        conversion = TimeConversion(tempo_line, ts_line, Position(2, 1))

        bp = conversion.position_to_bp(Position(5, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(1, 2),
                        'bp is {0}, not BP[1, 2]'.format(bp))

        bp = conversion.position_to_bp(Position(13, 8))
        print(bp)
        self.assertTrue(bp == BeatPosition(2, 1),
                        'bp is {0}, not BP[2, 1]'.format(bp))

        tempo_line = EventSequence([
            TempoEvent(Tempo(60), Position(0)),
            TempoEvent(Tempo(20), Position(4, 4))
        ])
        ts_line = EventSequence([
            TimeSignatureEvent(TimeSignature(4, Duration(1, 4)), Position(0)),
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)),
                               Position(5, 2))
        ])
        conversion = TimeConversion(tempo_line, ts_line, Position(4),
                                    Duration(1, 2))

        # pickup
        bp = conversion.position_to_bp(Position(0))
        print(bp)
        self.assertTrue(bp == BeatPosition(0, 2),
                        'bp is {0}, not BP[0, 2]'.format(bp))

        bp = conversion.position_to_bp(Position(1, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(0, 3),
                        'bp is {0}, not BP[0, 3]'.format(bp))

        # measure 1
        bp = conversion.position_to_bp(Position(1, 2))
        print(bp)
        self.assertTrue(bp == BeatPosition(1, 0),
                        'bp is {0}, not BP[1, 0]'.format(bp))

        bp = conversion.position_to_bp(Position(3, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(1, 1),
                        'bp is {0}, not BP[1, 1]'.format(bp))

        bp = conversion.position_to_bp(Position(1, 1))
        print(bp)
        self.assertTrue(bp == BeatPosition(1, 2),
                        'bp is {0}, not BP[1, 2]'.format(bp))

        bp = conversion.position_to_bp(Position(5, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(1, 3),
                        'bp is {0}, not BP[1, 3]'.format(bp))

        # measure 2
        bp = conversion.position_to_bp(Position(3, 2))
        print(bp)
        self.assertTrue(bp == BeatPosition(2, 0),
                        'bp is {0}, not BP[2, 0]'.format(bp))

        bp = conversion.position_to_bp(Position(7, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(2, 1),
                        'bp is {0}, not BP[2, 1]'.format(bp))

        bp = conversion.position_to_bp(Position(2, 1))
        print(bp)
        self.assertTrue(bp == BeatPosition(2, 2),
                        'bp is {0}, not BP[2, 2]'.format(bp))

        bp = conversion.position_to_bp(Position(9, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(2, 3),
                        'bp is {0}, not BP[2, 3]'.format(bp))

        # measure 3
        bp = conversion.position_to_bp(Position(5, 2))
        print(bp)
        self.assertTrue(bp == BeatPosition(3, 0),
                        'bp is {0}, not TS[3, 0]'.format(bp))

        bp = conversion.position_to_bp(Position(11, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(3, 1),
                        'bp is {0}, not TS[3, 1]'.format(bp))

        bp = conversion.position_to_bp(Position(3, 1))
        print(bp)
        self.assertTrue(bp == BeatPosition(3, 2),
                        'bp is {0}, not TS[3, 2]'.format(bp))

        # measure 4
        bp = conversion.position_to_bp(Position(13, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(4, 0),
                        'bp is {0}, not BP[4, 0]'.format(bp))

        bp = conversion.position_to_bp(Position(7, 2))
        print(bp)
        self.assertTrue(bp == BeatPosition(4, 1),
                        'bp is {0}, not BP[4, 1]'.format(bp))

        bp = conversion.position_to_bp(Position(15, 4))
        print(bp)
        self.assertTrue(bp == BeatPosition(4, 2),
                        'bp is {0}, not BP[4, 2]'.format(bp))
예제 #17
0
    def test_basic_succ_pred_sequence(self):
        events = [Event(3, Position(0)), 
                  Event(6, Position(1, 2)), 
                  Event(2, Position(3, 4)), 
                  Event(7, Position(1)), 
                  Event(8, Position(3, 2)) 
                  ]
        es = EventSequence(events)
        print(es)
        
        assert es.successor(events[0]) == events[1]
        assert es.successor(events[1]) == events[2]
        assert es.successor(events[2]) == events[3]
        assert es.successor(events[3]) == events[4]
        assert es.successor(events[4]) is None
        
        assert es.predecessor(events[4]) == events[3]
        assert es.predecessor(events[3]) == events[2]
        assert es.predecessor(events[2]) == events[1]
        assert es.predecessor(events[1]) == events[0]
        assert es.predecessor(events[0]) is None
        
        assert es.first == events[0]
        assert es.last == events[4]
        
        es = EventSequence()
        for i in reversed(range(len(events))):
            es.add(events[i])
        print(es)
        
        assert es.successor(events[0]) == events[1]
        assert es.successor(events[1]) == events[2]
        assert es.successor(events[2]) == events[3]
        assert es.successor(events[3]) == events[4]
        assert es.successor(events[4]) is None
        
        assert es.predecessor(events[4]) == events[3]
        assert es.predecessor(events[3]) == events[2]
        assert es.predecessor(events[2]) == events[1]
        assert es.predecessor(events[1]) == events[0]
        assert es.predecessor(events[0]) is None
        
        assert es.first == events[0]
        assert es.last == events[4]
        
        es = EventSequence()
        for i in [2, 4, 1, 3, 0]:
            es.add(events[i])
        print(es)
        
        assert es.successor(events[0]) == events[1]
        assert es.successor(events[1]) == events[2]
        assert es.successor(events[2]) == events[3]
        assert es.successor(events[3]) == events[4]
        assert es.successor(events[4]) is None
        
        assert es.predecessor(events[4]) == events[3]
        assert es.predecessor(events[3]) == events[2]
        assert es.predecessor(events[2]) == events[1]
        assert es.predecessor(events[1]) == events[0]
        assert es.predecessor(events[0]) is None
        
        assert es.first == events[0]
        assert es.last == events[4]
        
        es.remove(events[3])
        print('remove object 7')
        print(es)
        print(es.print_maps())
        
        assert es.successor(events[0]) == events[1]
        assert es.successor(events[1]) == events[2]
        assert es.successor(events[2]) == events[4]
        assert es.successor(events[4]) is None
        
        assert es.predecessor(events[4]) == events[2]
        assert es.predecessor(events[2]) == events[1]
        assert es.predecessor(events[1]) == events[0]
        assert es.predecessor(events[0]) is None
        
        assert es.first == events[0]
        assert es.last == events[4]
        
        new_event = Event(23, Position(3, 4))
        print('update object 2 to 23')

        es.add(new_event)
        print(es)
        es.print_maps()       
        assert es.successor(events[0]) == events[1]
        assert es.successor(events[1]) == new_event
        assert es.successor(new_event) == events[4]
        assert es.successor(events[4]) is None
        
        assert es.predecessor(events[4]) == new_event
        assert es.predecessor(new_event) == events[1]
        assert es.predecessor(events[1]) == events[0]
        assert es.predecessor(events[0]) is None
        
        assert es.first == events[0]
        assert es.last == events[4]
        
        es.move_event(new_event, Position(1, 8))
        print('move (23, 3/4) to (23, 1/8)')
        print(es)
        es.print_maps()
        
        assert es.successor(events[0]) == new_event
        assert es.successor(new_event) == events[1]
        assert es.successor(events[1]) == events[4]
        assert es.successor(events[4]) is None
        
        assert es.predecessor(events[4]) == events[1]
        assert es.predecessor(events[1]) == new_event
        assert es.predecessor(new_event) == events[0]
        assert es.predecessor(events[0]) is None
        
        assert es.first == events[0]
        assert es.last == events[4]
예제 #18
0
    def test_bp_to_position(self):
        tempo_line = EventSequence([TempoEvent(Tempo(60), Position(0))])
        ts_line = EventSequence(
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)), Position(0)))
        conversion = TimeConversion(tempo_line, ts_line, Position(2, 1))

        bp = conversion.bp_to_position(BeatPosition(0, 2))
        print(bp)
        self.assertTrue(bp == Position(1, 2), 'bp is {0}, not 1/2'.format(bp))

        bp = conversion.bp_to_position(BeatPosition(1, 1))
        print(bp)
        self.assertTrue(bp == Position(1), 'bp is {0}, not 1'.format(bp))

        tempo_line = EventSequence([
            TempoEvent(Tempo(60), Position(0)),
            TempoEvent(Tempo(20), Position(4, 4))
        ])
        ts_line = EventSequence([
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)), Position(0)),
            TimeSignatureEvent(TimeSignature(2, Duration(1, 8)),
                               Position(6, 4))
        ])
        conversion = TimeConversion(tempo_line, ts_line, Position(2, 1))

        bp = conversion.bp_to_position(BeatPosition(1, 2))
        print(bp)
        self.assertTrue(bp == Position(5, 4), 'bp is {0}, not 5/4'.format(bp))

        bp = conversion.bp_to_position(BeatPosition(2, 1))
        print(bp)
        self.assertTrue(bp == Position(13, 8),
                        'bp is {0}, not 13/8'.format(bp))

        tempo_line = EventSequence([
            TempoEvent(Tempo(60), Position(0)),
            TempoEvent(Tempo(20), Position(4, 4))
        ])
        ts_line = EventSequence([
            TimeSignatureEvent(TimeSignature(4, Duration(1, 4)), Position(0)),
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)),
                               Position(5, 2))
        ])
        conversion = TimeConversion(tempo_line, ts_line, Position(4),
                                    Duration(1, 2))

        # pickup
        bp = conversion.bp_to_position(BeatPosition(0, 2))
        print(bp)
        self.assertTrue(bp == Position(0), 'bp is {0}, not 0'.format(bp))

        bp = conversion.bp_to_position(BeatPosition(0, 3))
        print(bp)
        self.assertTrue(bp == Position(1, 4), 'bp is {0}, not 1/4'.format(bp))

        # measure 1
        bp = conversion.bp_to_position(BeatPosition(1, 0))
        print(bp)
        self.assertTrue(bp == Position(1, 2), 'bp is {0}, not 1/2'.format(bp))

        bp = conversion.bp_to_position(BeatPosition(1, 1))
        print(bp)
        self.assertTrue(bp == Position(3, 4), 'bp is {0}, not 3/4'.format(bp))

        bp = conversion.bp_to_position(BeatPosition(1, 2))
        print(bp)
        self.assertTrue(bp == Position(1, 1), 'bp is {0}, not 1'.format(bp))

        bp = conversion.bp_to_position(BeatPosition(1, 3))
        print(bp)
        self.assertTrue(bp == Position(5, 4), 'bp is {0}, not 5/4'.format(bp))

        # measure 2
        bp = conversion.bp_to_position(BeatPosition(2, 0))
        print(bp)
        self.assertTrue(bp == Position(3, 2), 'bp is {0}, not 3/2'.format(bp))

        bp = conversion.bp_to_position(BeatPosition(2, 1))
        print(bp)
        self.assertTrue(bp == Position(7, 4), 'bp is {0}, not 7/4'.format(bp))

        bp = conversion.bp_to_position(BeatPosition(2, 2))
        print(bp)
        self.assertTrue(bp == Position(2, 1), 'bp is {0}, not 2'.format(bp))

        bp = conversion.bp_to_position(BeatPosition(2, 3))
        print(bp)
        self.assertTrue(bp == Position(9, 4), 'bp is {0}, not 9/4'.format(bp))

        # measure 3
        bp = conversion.bp_to_position(BeatPosition(3, 0))
        print(bp)
        self.assertTrue(bp == Position(5, 2), 'bp is {0}, not 5/2'.format(bp))

        bp = conversion.bp_to_position(BeatPosition(3, 1))
        print(bp)
        self.assertTrue(bp == Position(11, 4),
                        'bp is {0}, not 11/4'.format(bp))

        bp = conversion.bp_to_position(BeatPosition(3, 2))
        print(bp)
        self.assertTrue(bp == Position(3, 1), 'bp is {0}, not 3'.format(bp))

        # measure 4
        bp = conversion.bp_to_position(BeatPosition(4, 0))
        print(bp)
        self.assertTrue(bp == Position(13, 4),
                        'bp is {0}, not 13/4'.format(bp))

        bp = conversion.bp_to_position(BeatPosition(4, 1))
        print(bp)
        self.assertTrue(bp == Position(7, 2), 'bp is {0}, not 7, 2'.format(bp))

        bp = conversion.bp_to_position(BeatPosition(4, 2))
        print(bp)
        self.assertTrue(bp == Position(15, 4),
                        'bp is {0}, not 15/4'.format(bp))
    def test_simple_setup(self):
        print('--- test_simple_setup')

        line = Line()

        notes = [
            Note(DiatonicPitch.parse('a:4'), Duration(1, 4)),
            Note(DiatonicPitch.parse('b:4'), Duration(1, 4)),
            Note(DiatonicPitch.parse('c:4'), Duration(1, 4)),
            Note(DiatonicPitch.parse('d:4'), Duration(1, 4)),
            Note(DiatonicPitch.parse('e:4'), Duration(1, 2)),
            Note(DiatonicPitch.parse('f:4'), Duration(1, 2)),
        ]

        location = 0
        for note in notes:
            line.pin(note, Offset(location))
            location += note.duration.duration

        tempo_seq = TempoEventSequence()
        ts_seq = EventSequence()
        tempo_seq.add(TempoEvent(Tempo(60, Duration(1, 4)), Position(0)))
        ts_seq.add(
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4), 'sww'),
                               Position(0)))

        diatonic_tonality = Tonality.create(ModalityType.Major,
                                            DiatonicTone("C"))
        chord_t = TertianChordTemplate.parse('tIV')
        chord = chord_t.create_chord(diatonic_tonality)

        hc_track = HarmonicContextTrack()
        hc_track.append(
            HarmonicContext(diatonic_tonality, chord, Duration(1, 1)))
        hc_track.append(
            HarmonicContext(diatonic_tonality, chord, Duration(1, 2)))
        hc_track.append(
            HarmonicContext(diatonic_tonality, chord, Duration(1, 2)))

        c = InstrumentCatalog.instance()
        violin = c.get_instrument("violin")

        score = LiteScore(line, hc_track, violin, tempo_seq, ts_seq)

        constraints = [
            OnBeatConstraint(notes[1], BeatType.Strong),
            StepSequenceConstraint(notes, [1, 1, 1, -1, -1])
        ]

        solver = MelodicConstraintSolver.create(score, constraints)

        cheat = {notes[2]: DiatonicPitch.parse('E:5')}

        results = solver.solve(cheat)
        assert results is not None
        assert results.beat_results is not None
        assert results.pitch_results is not None

        print(len(results.beat_results))
        print(len(results.pitch_results))
        assert 1 == len(results.beat_results)
        assert 1 == len(results.pitch_results)

        new_line = results.apply(next(iter(results.beat_results)),
                                 next(iter(results.pitch_results)))
        assert new_line is not None

        print(new_line)
        all_notes = new_line.get_all_notes()
        assert 'C:5' == str(all_notes[0].diatonic_pitch)
        assert 'D:5' == str(all_notes[1].diatonic_pitch)
        assert 'E:5' == str(all_notes[2].diatonic_pitch)
        assert 'F:5' == str(all_notes[3].diatonic_pitch)
        assert 'E:5' == str(all_notes[4].diatonic_pitch)
        assert 'D:5' == str(all_notes[5].diatonic_pitch)

        assert Position(3, 4) == all_notes[1].get_absolute_position()