Beispiel #1
0
    def test_adding_notes(self):
        score = Score()

        # set up 3 instrument voices: 2 violins, 1 trumpet, 1 clarinet
        catalogue = InstrumentCatalog.instance()
        score.add_instrument_voice(
            InstrumentVoice(catalogue.get_instrument("violin"), 2))

        #  1 beat == 1 sec, 3/4 TS + 60 beats per minute,
        score.tempo_sequence.add(TempoEvent(Tempo(60), Position(0)))
        score.time_signature_sequence.add(
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)), Position(0)))

        violin_voice = score.get_instrument_voice("violin")[0]

        line = Line([Note(DiatonicPitch(4, y), Duration(1, 8)) for y in 'afd'])
        violin_voice.voice(0).pin(line)

        notes = violin_voice.get_all_notes()
        for n in notes:
            print(n)

        line.append(Note(DiatonicPitch(4, 'g'), Duration(1, 8)))
        notes = violin_voice.get_all_notes()
        for n in notes:
            print(n)
Beispiel #2
0
    def test_score_book_example(self):
        score = Score()

        catalogue = InstrumentCatalog.instance()
        score.add_instrument_voice(
            InstrumentVoice(catalogue.get_instrument("violin")))

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

        violin_voice = score.get_instrument_voice("violin")[0]
        note_1 = Note(DiatonicPitch(4, 'A'), Duration(1, 4))
        note_2 = Note(DiatonicPitch(5, 'C'), Duration(1, 8))
        note_3 = Note(DiatonicPitch(5, 'B'), Duration(1, 8))
        note_4 = Note(DiatonicPitch(5, 'D'), Duration(1, 4))
        note_5 = Note(DiatonicPitch(5, 'E'), Duration(1, 8))
        note_6 = Note(DiatonicPitch(5, 'D'), Duration(1, 8))
        note_7 = Note(DiatonicPitch(4, 'G'), Duration(1, 4))
        note_8 = Note(DiatonicPitch(4, 'C'), Duration(1, 4))
        line = Line(
            [note_1, note_2, note_3, note_4, note_5, note_6, note_7, note_8])
        violin_voice.voice(0).pin(line)

        smc = ScoreToMidiConverter(score)
        smc.create('book_example_midi_file.mid', True)

        ScoreToMidiConverter.convert_line(line,
                                          'line_example_midi_file.mid',
                                          Tempo(90, Duration(1, 8)),
                                          instrument_name='violin')
Beispiel #3
0
    def test_book_example(self):

        score = Score()

        # set up 3 instrument voices: 2 violins, 1 trumpet, 1 clarinet
        catalogue = InstrumentCatalog.instance()
        score.add_instrument_voice(
            InstrumentVoice(catalogue.get_instrument("violin"), 2))
        score.add_instrument_voice(
            InstrumentVoice(catalogue.get_instrument("trumpet")))
        score.add_instrument_voice(
            InstrumentVoice(catalogue.get_instrument("clarinet")))

        #  1 beat == 1 sec, 3/4 TS + 60 beats per minute,
        score.tempo_sequence.add(TempoEvent(Tempo(60), Position(0)))
        score.time_signature_sequence.add(
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)), Position(0)))

        # set up some notes in the two violins
        violin_voice = score.get_instrument_voice("violin")[0]
        violin_voice.voice(0).pin(
            Line([Note(DiatonicPitch(4, y), Duration(1, 8))
                  for y in 'afdecd']))
        violin_voice.voice(0).pin(
            Line([Note(DiatonicPitch(4, y), Duration(1, 4)) for y in 'cdc']))
    def convert_line(line,
                     tempo=Tempo(60, Duration(1, 4)),
                     time_signature=TimeSignature(4, Duration(1, 4)),
                     channel_assignments=None,
                     fps=42100):
        """
        Static method to convert a Line to a midi file

        Args:
          :param line: Class Line object
          :param tempo: Tempo for playback, default is 60 BPM tempo beat = quarter note
          :param time_signature: TimeSiganture on playback, default is 4 quarter notes
          :param channel_assignments:
          :param fps: frames per second setting

        """
        score = Score()
        tempo_sequence = score.tempo_sequence
        tempo_sequence.add(TempoEvent(tempo, Position(0)))

        ts_sequence = score.time_signature_sequence
        ts_sequence.add(TimeSignatureEvent(time_signature, Position(0)))

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

        piano_instrument_voice = InstrumentVoice(piano, 1)
        piano_voice = piano_instrument_voice.voice(0)

        piano_voice.pin(line, Offset(0))

        score.add_instrument_voice(piano_instrument_voice)
        return ScoreToVstMidiConverter.convert_score(score,
                                                     channel_assignments, fps)
Beispiel #5
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
Beispiel #6
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)
Beispiel #7
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
Beispiel #8
0
    def test_tempo_change(self):
        c = InstrumentCatalog.instance()

        score = Score()

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

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

        violin_voice_0.dynamics_sequence.add(
            DynamicsEvent(Dynamics(Dynamics.F), Position(0)))

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

        vnote4 = Note(DiatonicPitch(4, 'a'), Duration(1, 4))
        vnote5 = Note(DiatonicPitch(4, 'b'), Duration(1, 4))
        vnote6 = Note(DiatonicPitch(5, 'c'), Duration(1, 4))
        vnote7 = Note(DiatonicPitch(5, 'd'), Duration(1, 4))

        vnote8 = Note(DiatonicPitch(4, 'a'), Duration(1, 4))
        vnote9 = Note(DiatonicPitch(4, 'b'), Duration(1, 4))
        vnote10 = Note(DiatonicPitch(5, 'c'), Duration(1, 4))
        vnote11 = Note(DiatonicPitch(5, 'd'), Duration(1, 4))

        # Set up a violin voice with 6 8th notes
        vline_0 = Line([
            vnote0, vnote1, vnote2, vnote3, vnote4, vnote5, vnote6, vnote7,
            vnote8, vnote9, vnote10, vnote11
        ])
        violin_voice_0.pin(vline_0)

        # Set up a violin voice with 6 8th notes
        vline_1 = Line([vnote0, vnote1, vnote2, vnote3])
        violin_voice_0.pin(vline_1)

        smc = ScoreToMidiConverter(score)
        smc.create('score_tempo_change_file.mid')

        TestScoreToMidiConverter.read_midi_file('score_tempo_change_file.mid')
Beispiel #9
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
Beispiel #11
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)
Beispiel #12
0
    def test_multi_track(self):
        c = InstrumentCatalog.instance()

        score = Score()

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

        violin = c.get_instrument("violin")
        piano = c.get_instrument("piano")

        note0 = Note(DiatonicPitch(4, 'a'), Duration(1, 4))
        note1 = Note(DiatonicPitch(4, 'b'), Duration(1, 4))
        note2 = Note(DiatonicPitch(4, 'c'), Duration(1, 4))
        note3 = Note(DiatonicPitch(4, 'd'), Duration(1, 4))
        note4 = Note(DiatonicPitch(5, 'g'), Duration(1, 4))
        note5 = Note(DiatonicPitch(5, 'f'), Duration(1, 4))
        note6 = Note(DiatonicPitch(5, 'e'), Duration(1, 4))
        note7 = Note(DiatonicPitch(5, 'd'), Duration(1, 4))

        violin_instrument_voice = InstrumentVoice(violin, 1)
        violin_voice = violin_instrument_voice.voice(0)
        assert violin_voice

        vline = Line([note0, note1, note2, note3])
        violin_voice.pin(vline)

        score.add_instrument_voice(violin_instrument_voice)

        piano_instrument_voice = InstrumentVoice(piano, 1)
        piano_voice = piano_instrument_voice.voice(0)
        assert piano_voice

        pline = Line([note4, note5, note6, note7])
        piano_voice.pin(pline, Offset(1, 8))

        score.add_instrument_voice(piano_instrument_voice)

        violin_voice.dynamics_sequence.add(
            DynamicsEvent(Dynamics(Dynamics.F), Position(0)))
        piano_voice.dynamics_sequence.add(
            DynamicsEvent(Dynamics(Dynamics.P), Position(0)))

        smc = ScoreToMidiConverter(score)
        smc.create('score_multi_trackoutput_file.mid')

        TestScoreToMidiConverter.read_midi_file(
            'score_multi_trackoutput_file.mid')
Beispiel #13
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))
Beispiel #14
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))
Beispiel #15
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)
    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
    def test_book_example(self):
        print('----- test_book_example -----')

        seq = TimeSignatureEventSequence()
        seq.add(
            TimeSignatureEvent(
                TimeSignature(3, TSBeatType(TSBeatType.Quarter)), Position(0)))
        seq.add(
            TimeSignatureEvent(TimeSignature(2, TSBeatType(TSBeatType.Half)),
                               Position(10)))
        seq.add(
            TimeSignatureEvent(TimeSignature(6, TSBeatType(TSBeatType.Eighth)),
                               Position(25)))
        seq.add(
            TimeSignatureEvent(
                TimeSignature(3, TSBeatType(TSBeatType.Quarter)),
                Position(50)))

        event = seq.first
        while event is not None:
            print(event)
            event = seq.successor(event)

        print('----- End test_book_example -----')
Beispiel #18
0
    def test_get(self):
        ordered_list = [(10, 100), (5, 20), (7, 70), (2, 50)]
        om = OrderedMap(ordered_list)

        a = om.get(5)
        assert a == 20

        from structure.time_signature import TimeSignature

        from timemodel.time_signature_event import TimeSignatureEvent
        tse = TimeSignatureEvent(TimeSignature(3, Duration(1, 4)), Position(0))

        d = OrderedMap([(tse.time, tse)])
        x = d.get(0)
        assert x == tse
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)
Beispiel #20
0
    def test_score_convert(self):
        c = InstrumentCatalog.instance()

        score = Score()

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

        violin = c.get_instrument("violin")
        violin_instrument_voice = InstrumentVoice(violin, 2)
        violin_voice_0 = violin_instrument_voice.voice(0)
        violin_voice_1 = violin_instrument_voice.voice(1)
        assert violin_voice_0
        assert violin_voice_1
        score.add_instrument_voice(violin_instrument_voice)

        violin_voice_0.dynamics_sequence.add(
            DynamicsEvent(Dynamics(Dynamics.P), Position(0)))
        violin_voice_0.dynamics_sequence.add(
            DynamicsEvent(Dynamics(Dynamics.FFF), Position(1, 4)))
        violin_voice_1.dynamics_sequence.add(
            DynamicsEvent(Dynamics(Dynamics.P), Position(0)))
        violin_voice_1.dynamics_sequence.add(
            DynamicsEvent(Dynamics(Dynamics.FFF), Position(1, 4)))

        # Add notes to the score
        vnote0 = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
        vnote1 = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
        vnote2 = Note(DiatonicPitch(5, '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_0 = Line([vnote0, vnote1, vnote2])
        violin_voice_0.pin(vline_0)

        # Set up a violin voice with 6 8th notes
        vline_1 = Line([vnote3, vnote4, vnote5])
        violin_voice_1.pin(vline_1)

        smc = ScoreToMidiConverter(score)
        smc.create('score_output_file.mid')

        TestScoreToMidiConverter.read_midi_file('score_output_file.mid')
Beispiel #21
0
    def dilate_ts_sequence(self, tses_sequence):
        if tses_sequence is None:
            return None
        new_tses = TimeSignatureEventSequence()
        tss = tses_sequence.sequence_list
        for tse in tss:
            ts = tse.object
            t = TimeSignature(
                ts.beats_per_measure, ts.beat_duration * self.dilation_factor
                if self.apply_to_notes else ts.beat_duration,
                ts.beat_pattern if ts.beat_pattern is not None else None)
            new_tse = TimeSignatureEvent(
                t, tse.time *
                self.dilation_factor if self.apply_to_notes else tse.time)
            new_tses.add(new_tse)

        return new_tses
Beispiel #22
0
    def test_dynamic_volume(self):
        c = InstrumentCatalog.instance()

        score = Score()

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

        violin = c.get_instrument("violin")

        note0 = Note(DiatonicPitch(4, 'a'), Duration(3, 2))
        note1 = Note(DiatonicPitch(4, 'b'), Duration(1, 2))
        note2 = Note(DiatonicPitch(5, 'c'), Duration(1, 2))
        note3 = Note(DiatonicPitch(5, 'd'), Duration(1, 4))
        note4 = Note(DiatonicPitch(5, 'g'), Duration(1, 4))
        note5 = Note(DiatonicPitch(5, 'f'), Duration(1, 4))
        note6 = Note(DiatonicPitch(5, 'e'), Duration(1, 4))
        note7 = Note(DiatonicPitch(5, 'd'), Duration(1, 4))

        violin_instrument_voice = InstrumentVoice(violin, 1)
        violin_voice = violin_instrument_voice.voice(0)
        assert violin_voice

        vline = Line([note0, note1, note2, note3, note4, note5, note6, note7])
        violin_voice.pin(vline)

        score.add_instrument_voice(violin_instrument_voice)

        # create a crescendo

        v_low = Dynamics.PPP.velocity
        v_hi = Dynamics.FFFF.velocity
        array = [(Position(0), v_low), (Position(3, 2), v_hi)]
        f = PiecewiseLinearFunction(array)
        event1 = DynamicsFunctionEvent(f, Position(0))

        violin_voice.dynamics_sequence.add(event1)

        smc = ScoreToMidiConverter(score)
        smc.create('score_vary_volume_file.mid')

        TestScoreToMidiConverter.read_midi_file('score_vary_volume_file.mid')
Beispiel #23
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
Beispiel #24
0
    def test_variable_tempo_change(self):
        c = InstrumentCatalog.instance()

        score = Score()

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

        score.time_signature_sequence.add(
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)), Position(0)))

        score.tempo_sequence.add(
            TempoEvent(Tempo(60, Duration(1, 4)), Position(0)))

        t_low = 60
        t_hi = 250
        array = [(Position(0), t_low), (Position(1), t_hi),
                 (Position(2), t_hi), (Position(3), t_low)]
        f = PiecewiseLinearFunction(array)
        event1 = TempoFunctionEvent(f, Position(1))
        score.tempo_sequence.add(event1)

        score.tempo_sequence.add(
            TempoEvent(Tempo(t_low, Duration(1, 4)), Position(4)))

        # create notes
        notes = []
        for _ in range(0, 6):
            notes.append(Note(DiatonicPitch(4, 'c'), Duration(1, 4)))
            notes.append(Note(DiatonicPitch(4, 'd'), Duration(1, 4)))
            notes.append(Note(DiatonicPitch(4, 'e'), Duration(1, 4)))
            notes.append(Note(DiatonicPitch(4, 'f'), Duration(1, 4)))
        vline = Line(notes)
        violin_voice.pin(vline)

        smc = ScoreToMidiConverter(score)
        smc.create('score_var_tempo_change_file.mid')

        TestScoreToMidiConverter.read_midi_file(
            'score_var_tempo_change_file.mid')
Beispiel #25
0
    def test_add_notes_to_two_level_line(self):
        score = Score()

        catalogue = InstrumentCatalog.instance()
        score.add_instrument_voice(
            InstrumentVoice(catalogue.get_instrument("violin")))

        #  1 beat == 1 sec, 3/4 TS + 60 beats per minute,
        score.tempo_sequence.add(TempoEvent(Tempo(60), Position(0)))
        score.time_signature_sequence.add(
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)), Position(0)))

        violin_voice = score.get_instrument_voice("violin")[0]
        top_line = Line(
            [Note(DiatonicPitch(4, y), Duration(1, 8)) for y in 'afdecd'])
        violin_voice.voice(0).pin(top_line, Offset(0))
        level1_line = Line(
            [Note(DiatonicPitch(5, y), Duration(1, 8)) for y in 'af'])
        top_line.pin(level1_line, Offset(2))
        level2_line = Line(
            [Note(DiatonicPitch(6, y), Duration(1, 8)) for y in 'de'])
        level1_line.pin(level2_line, Offset(1))
Beispiel #26
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)
    def convert_line(line,
                     filename,
                     tempo=Tempo(60, Duration(1, 4)),
                     time_signature=TimeSignature(4, Duration(1, 4)),
                     instrument_name='piano'):
        """
        Static method to convert a Line to a midi file
        
        Args:
          line: Class Line object
          filename: The name of the midi file, should have filetype .mid
          tempo: Tempo for playback, default is 60 BPM tempo beat = quarter note
          time_signature: TimeSiganture on playback, default is 4 quarter notes
          instrument_name: Name of instrument ot use for playback.
        """
        score = Score()
        tempo_sequence = score.tempo_sequence
        tempo_sequence.add(TempoEvent(tempo, Position(0)))

        ts_sequence = score.time_signature_sequence
        ts_sequence.add(TimeSignatureEvent(time_signature, Position(0)))

        c = InstrumentCatalog.instance()
        instrument = c.get_instrument(instrument_name)
        if instrument is None:
            print('Error: instrument {0} cannnot be found'.format(
                instrument_name))
            return

        instrument_voice = InstrumentVoice(instrument, 1)
        piano_voice = instrument_voice.voice(0)

        piano_voice.pin(line, Offset(0))

        score.add_instrument_voice(instrument_voice)
        ScoreToMidiConverter.convert_score(score, filename)
Beispiel #28
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))
Beispiel #29
0
    def test_acquire_notes(self):
        c = InstrumentCatalog.instance()

        score = Score()

        violin = c.get_instrument("violin")
        violin_instrument_voice = InstrumentVoice(violin)
        violin_voice = violin_instrument_voice.voice(0)
        assert violin_voice
        score.add_instrument_voice(violin_instrument_voice)

        clarinet = c.get_instrument("clarinet")
        clarinet_instrument_voice = InstrumentVoice(clarinet)
        clarinet_voice = clarinet_instrument_voice.voice(0)
        assert clarinet_voice
        score.add_instrument_voice(clarinet_instrument_voice)

        # Add notes to the score
        note_list = [
            Note(DiatonicPitch(4, y), Duration(1, 8)) for y in 'abcdef'
        ]

        # Set up a violin voice with 6 8th notes
        vline = Line(note_list)
        violin_voice.pin(vline)

        cnote0 = Note(DiatonicPitch(5, 'a'), Duration(1, 4))
        cnote1 = Note(DiatonicPitch(5, 'b'), Duration(1, 4))
        cnote2 = Note(DiatonicPitch(5, 'c'), Duration(1, 4))
        cnote3 = Note(DiatonicPitch(5, 'd'), Duration(1, 4))

        # set up a clarinet line with 4 1`/4 notes
        cline = Line([cnote0, cnote1, cnote2, cnote3])
        clarinet_voice.pin(cline)

        # search for notes sounding in [1/4, 3/4)
        voice_note_map = score.get_notes_by_wnt_interval(
            Interval(Position(1, 4), Position(3, 4)))
        assert voice_note_map
        for (k, v) in voice_note_map.items():
            print('InstrumentVoice {0}:'.format(k))
            for (k1, v1) in v.items():
                print('     {0} --> [{1}]'.format(
                    k1.instrument, ', '.join(str(n) for n in v1)))

        cvoice_map = voice_note_map.get(clarinet_instrument_voice)
        cvoices = list(cvoice_map.keys())
        assert cvoice_map
        assert cvoices[0] in cvoice_map
        cnotes = cvoice_map[cvoices[0]]
        assert len(cnotes) == 2
        assert TestScore.has_pitch(cnotes, DiatonicPitch.parse('B:5'))
        assert TestScore.has_pitch(cnotes, DiatonicPitch.parse('C:5'))

        vvoice_map = voice_note_map.get(violin_instrument_voice)
        vvoices = list(vvoice_map.keys())
        assert vvoice_map
        assert vvoices[0] in vvoice_map
        vnotes = vvoice_map[vvoices[0]]
        assert len(vnotes) == 4
        assert TestScore.has_pitch(vnotes, DiatonicPitch.parse('C:4'))
        assert TestScore.has_pitch(vnotes, DiatonicPitch.parse('F:4'))

        # search for notes starting in [3/8, 3/4)
        voice_note_map = score.get_notes_starting_in_wnt_interval(
            Interval(Position(3, 8), Position(3, 4)))
        assert voice_note_map
        for (k, v) in voice_note_map.items():
            print('InstrumentVoice {0}:'.format(k))
            for (k1, v1) in v.items():
                print('    {0} --> [{1}]'.format(k1.instrument,
                                                 ', '.join(str(n)
                                                           for n in v1)))

        cvoice_map = voice_note_map.get(clarinet_instrument_voice)
        cvoices = list(cvoice_map.keys())
        assert cvoice_map
        assert cvoices[0] in cvoice_map
        cnotes = cvoice_map[cvoices[0]]
        assert len(cnotes) == 1
        assert TestScore.has_pitch(cnotes, DiatonicPitch.parse('C:5'))

        vvoice_map = voice_note_map.get(violin_instrument_voice)
        vvoices = list(vvoice_map.keys())
        assert vvoice_map
        assert vvoices[0] in vvoice_map
        vnotes = vvoice_map[vvoices[0]]
        assert len(vnotes) == 3
        assert TestScore.has_pitch(vnotes, DiatonicPitch.parse('D:4'))
        assert TestScore.has_pitch(vnotes, DiatonicPitch.parse('E:4'))
        assert TestScore.has_pitch(vnotes, DiatonicPitch.parse('F:4'))

        duration = score.duration
        print('full wnt duration = {0}'.format(duration))
        assert duration == Duration(1, 1)

        with self.assertRaises(Exception):
            bd = score.beat_duration
            print(bd)

        # 3/4 TS + 60 beats per minute, 1 beat == 1 sec
        score.tempo_sequence.add(TempoEvent(Tempo(60), Position(0)))
        score.time_signature_sequence.add(
            TimeSignatureEvent(TimeSignature(3, Duration(1, 4)), Position(0)))
        bp_duration = score.beat_duration
        print('full beat duration = {0}'.format(bp_duration))

        assert bp_duration == BeatPosition(1, 1)

        real_duration = score.real_duration
        print('real duration = {0}'.format(real_duration))
        assert real_duration == 4000
Beispiel #30
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))