예제 #1
0
    def recursive_dilate(self, strct, new_to_old_map):
        if isinstance(strct, Note):
            d = strct.base_duration.duration / strct.contextual_reduction_factor
            n = Note(strct.diatonic_pitch, Duration(d * self.dilation_factor),
                     strct.num_dots)
            new_to_old_map[n] = strct
            return n
        else:
            collect = list()
            for s in strct.sub_notes:
                collect.append(self.recursive_dilate(s, new_to_old_map))

            if isinstance(strct, Beam):
                b = Beam(collect)
                new_to_old_map[b] = strct
                return b
            elif isinstance(strct, Tuplet):
                t = Tuplet(strct.unit_duration * self.dilation_factor,
                           strct.unit_duration_factor, collect)
                new_to_old_map[t] = strct
                return t
            elif isinstance(strct, Line):
                line = Line()
                new_to_old_map[line] = strct
                for s in collect:
                    line.pin(
                        s, new_to_old_map[s].relative_position *
                        self.dilation_factor)
                return line
예제 #2
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)
예제 #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']))
예제 #4
0
    def test_line_structures(self):
        print('----- test line structures -----')
        source_instance_expression = '{<C-Major:I> iC:4 D E F G A B C:5 D E F G} }'
        lge = LineGrammarExecutor()
        source_instance_line, source_instance_hct = lge.parse(source_instance_expression)
        notes = source_instance_line.get_all_notes()

        line = Line()
        line.pin(notes[0], Offset(Fraction(1, 2)))
        line.pin(notes[1], Offset(Fraction(3, 4)))

        line1 = Line()
        line1.pin(notes[3], Offset(Fraction(1, 4)))
        line1.pin(notes[4], Offset(Fraction(1, 2)))
        line.pin(line1, Offset(Fraction(2)))

        lite_score = TestTDilation.create_score_1(line, source_instance_hct, 'piano', (4, 4, 'swww'))

        trans = TDilation(lite_score)
        new_score = trans.apply(Fraction(2), False, True)
        all_notes = new_score.line.get_all_notes()
        assert all_notes[2].get_absolute_position().position == Fraction(9, 2)

        print(line)
        print(new_score.line)
예제 #5
0
    def test_overlap_vs_start(self):
        c = InstrumentCatalog.instance()
        violin = c.get_instrument("violin")

        note0 = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
        note1 = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))

        line = Line()
        line.pin(note0, Offset(1, 4))
        line.pin(note1, Offset(3, 8))
        line.pin(note2, Offset(1, 2))
        line.pin(note3, Offset(5, 8))

        voice = Voice(violin)
        voice.pin(line, Offset(0))

        interval = Interval(Position(5, 16), Position(5, 8))

        notes = voice.get_notes_by_interval(interval)
        assert len(notes) == 3
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('A:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('B:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('C:4'))

        notes = voice.get_notes_starting_in_interval(interval)
        assert len(notes) == 2
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('B:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('C:4'))

        notee0 = Note(DiatonicPitch(5, 'a'), Duration(1, 2))
        notee1 = Note(DiatonicPitch(5, 'b'), Duration(1, 2))
        line1 = Line()
        line1.pin([notee0, notee1], Offset(0))

        voice.pin(line1, Offset(1, 4))

        notes = voice.get_notes_by_interval(interval)
        assert len(notes) == 4
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('A:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('B:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('C:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('A:5'))

        notes = voice.get_notes_by_interval(interval, line1)
        assert len(notes) == 1
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('A:5'))

        notes = voice.get_notes_starting_in_interval(interval)
        assert len(notes) == 2
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('B:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('C:4'))

        notes = voice.get_notes_starting_in_interval(
            Interval(Position(1, 8), Position(3, 8)), line1)
        assert len(notes) == 1
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('A:5'))
예제 #6
0
    def _build_target_line(self):
        target_line = Line()
        initial_pitch = DiatonicPitch.parse('C:4')
        source_notes = self.substitution_pattern.target_pattern_line.get_all_notes(
        )
        for note in source_notes:
            t_note = note.clone()
            t_note.diatonic_pitch = initial_pitch
            target_line.append(t_note)

        return target_line
예제 #7
0
    def _build_target_line(self):
        # Build a target line, all notes C:4 with onsets/durations of original line.
        target_line = Line()
        initial_pitch = DiatonicPitch.parse('C:4')
        source_notes = self.source_line.get_all_notes()
        for note in source_notes:
            t_note = note.clone()
            t_note.diatonic_pitch = initial_pitch
            target_line.append(t_note)

        return target_line
예제 #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')
    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])
예제 #10
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')
예제 #11
0
    def test_add_notes_to_line(self):
        c = InstrumentCatalog.instance()
        violin = c.get_instrument("violin")

        note0 = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
        note1 = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))

        line = Line()
        line.pin(note0, Offset(1, 4))
        line.pin(note1, Offset(3, 8))
        line.pin(note2, Offset(1, 2))
        line.pin(note3, Offset(5, 8))

        voice = Voice(violin)
        voice.pin(line, Offset(0))

        notee0 = Note(DiatonicPitch(5, 'a'), Duration(1, 8))
        notee1 = Note(DiatonicPitch(5, 'b'), Duration(1, 8))
        line.pin([notee0, notee1], Offset(3, 4))

        notes = voice.get_notes_starting_in_interval(
            Interval(Position(5, 8), Position(2, 1)))
        assert len(notes) == 3
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('D:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('A:5'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('B:5'))
예제 #12
0
    def test_two_voices(self):
        print('test two voices')
        c = InstrumentCatalog.instance()
        violin = c.get_instrument("violin")

        note0 = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
        note1 = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        tuplet = Tuplet(Duration(1, 8), 2, [note0, note1, note2])

        note3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        note4 = Note(DiatonicPitch(4, 'e'), Duration(1, 8))
        subbeam = Beam([note3, note4])
        beam = Beam(subbeam)
        line1 = Line([tuplet, beam])
        print(line1)

        notee0 = Note(DiatonicPitch(5, 'a'), Duration(1, 8))
        notee1 = Note(DiatonicPitch(5, 'b'), Duration(1, 8), 1)
        notee2 = Note(DiatonicPitch(5, 'c'), Duration(1, 8))
        notee3 = Note(DiatonicPitch(5, 'd'), Duration(1, 16))
        line2 = Line([notee0, notee1, notee2])
        line2.pin(notee3, Offset(1, 2))
        print(line2)

        voice = Voice(violin)
        voice.pin(line1, Offset(1, 4))
        voice.pin(line2, Offset(0, 1))

        print(voice)

        interval = Interval(Position(1, 2), Position(1))
        notes = voice.get_notes_by_interval(interval)

        assert len(notes) == 3
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('D:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('E:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('D:5'))

        interval = Interval(Position(1, 4), Position(7, 16))
        notes = voice.get_notes_by_interval(interval)

        assert len(notes) == 5
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('A:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('B:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('C:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('B:5'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('C:5'))
 def build_line(note_spec_list):
     note_list = list()
     for spec in note_spec_list:
         pitch = DiatonicPitch.parse(spec[0])
         n = Note(pitch, Duration(spec[1][0], spec[1][1]))
         note_list.append(n)
     return Line(note_list)
예제 #14
0
    def clone(self):
        from structure.beam import Beam
        from structure.tuplet import Tuplet
        from structure.note import Note
        from structure.line import Line
        from timemodel.duration import Duration
        cpy = None
        if isinstance(self, Beam):
            cpy = Beam()
            for s in self.sub_notes:
                s_prime = s.clone()
                cpy.append(s_prime)
        elif isinstance(self, Tuplet):
            cpy = Tuplet(self.unit_duration, self.unit_duration_factor)
            for s in self.sub_notes:
                s_prime = s.clone()
                cpy.append(s_prime)
        elif isinstance(self, Note):
            d = Duration(self.base_duration.duration / self.contextual_reduction_factor)
            cpy = Note(self.diatonic_pitch if self.diatonic_pitch is not None else None, d, self.num_dots)
        elif isinstance(self, Line):
            cpy = Line(None, self.instrument)
            for s in self.sub_notes:
                s_prime = s.clone()
                cpy.pin(s_prime, s.relative_position)

        return cpy
예제 #15
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')
예제 #16
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')
예제 #17
0
 def test_another_book_example(self):
     # Creating a simple line of 2 eighth notes a quarter note and a half note.
     vnote0 = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
     vnote1 = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
     vnote2 = Note(DiatonicPitch(4, 'c'), Duration(1, 4))
     vnote3 = Note(DiatonicPitch(4, 'd'), Duration(1, 2))
     line = Line([vnote0, vnote1, vnote2, vnote3])
     AbstractNote.print_structure(line)
예제 #18
0
    def test_add_notes_to_tuplet(self):
        c = InstrumentCatalog.instance()
        violin = c.get_instrument("violin")

        note0 = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
        note1 = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))
        beam = Beam([note0, note1])

        line = Line()
        line.pin(beam, Offset(1, 2))

        voice = Voice(violin)
        voice.pin(line, Offset(0))

        tuplet = Tuplet(Duration(1, 8), 3, [note2, note3])
        beam.append(tuplet)

        notee0 = Note(DiatonicPitch(5, 'a'), Duration(1, 8))
        tuplet.append(notee0)

        print(voice)
        notes = voice.get_notes_starting_in_interval(
            Interval(Position(1, 2), Position(5, 4)))

        assert len(notes) == 5
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('A:5'))

        beam_prime = beam.clone()
        notes = beam_prime.get_all_notes()
        AbstractNote.print_structure(beam_prime)
        assert notes[0].duration == Duration(1, 8)
        assert str(notes[0].diatonic_pitch) == 'A:4'

        line_prime = line.clone()
        notes = line_prime.get_all_notes()
        AbstractNote.print_structure(line_prime)
        assert notes[0].duration == Duration(1, 8)
        assert str(notes[0].diatonic_pitch) == 'A:4'
        assert notes[2].duration == Duration(1, 8)
        assert str(notes[2].diatonic_pitch) == 'C:4'
예제 #19
0
def get_hough_p_lines(frame, minLineLength=100, maxLineGap=10):
    gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
    canny = cv2.Canny(gray, 100, 200, apertureSize=3)
    lines_data = cv2.HoughLinesP(canny, 1, np.pi / 180, 100, minLineLength,
                                 maxLineGap)
    lines = []
    for line_data in lines_data:
        x1, y1, x2, y2 = line_data[0]
        line = Line(x1, y1, x2, y2)
        lines.append(line)
    return lines
예제 #20
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
예제 #21
0
    def test_line_convert(self):
        note1 = Note(DiatonicPitch(4, 'c'), Duration(1, 16))
        note2 = Note(DiatonicPitch(4, 'd'), Duration(1, 16))
        note3 = Note(DiatonicPitch(4, 'e'), Duration(1, 16))
        note4 = Note(DiatonicPitch(4, 'f'), Duration(1, 16))
        beam = Beam([note1, note2, note3, note4])

        line = Line(beam)

        ScoreToMidiConverter.convert_line(line, 'line_output_file.mid')

        TestScoreToMidiConverter.read_midi_file('line_output_file.mid')
예제 #22
0
    def test_single_voice(self):
        print('test single voice')
        note0 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note1 = Note(DiatonicPitch(4, 'd'), Duration(1, 8), 1)
        note2 = Note(DiatonicPitch(4, 'e'), Duration(1, 16))
        beam = Beam([note0, note1, note2])
        print(beam)
        assert beam.cardinality() == 3

        line = Line(beam)

        assert line.length() == Duration(3, 8)

        c = InstrumentCatalog.instance()

        violin = c.get_instrument("violin")

        voice = Voice(violin)
        voice.pin(line, Offset(3, 4))

        coverage = voice.coverage()
        print('Coverage = {0}'.format(coverage))
        assert coverage.lower == Position(3, 4)
        assert coverage.upper == Position(9, 8)

        assert voice.length() == Duration(9, 8)

        interval = Interval(Position(15, 16), Position(9, 8))
        notes = voice.get_notes_by_interval(interval)
        print(', '.join([str(n) for n in notes]))
        for n in notes:
            intvl = TestVoice.compute_note_interval(n)
            print('{0} intersect {1} = {2}'.format(
                intvl, interval, intvl.intersection(interval)))

        assert len(notes) == 2
        print(notes[0].get_absolute_position(),
              notes[1].get_absolute_position())
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('D:4'))
        assert TestVoice.has_pitch(notes, DiatonicPitch.parse('E:4'))
예제 #23
0
    def test_remove_notes(self):
        c = InstrumentCatalog.instance()
        violin = c.get_instrument("violin")

        note0 = Note(DiatonicPitch(4, 'a'), Duration(1, 8))
        note1 = Note(DiatonicPitch(4, 'b'), Duration(1, 8))
        note2 = Note(DiatonicPitch(4, 'c'), Duration(1, 8))
        note3 = Note(DiatonicPitch(4, 'd'), Duration(1, 8))

        line = Line()
        line.pin(note0, Offset(1, 4))
        line.pin(note1, Offset(3, 8))
        line.pin(note2, Offset(1, 2))
        line.pin(note3, Offset(5, 8))

        voice = Voice(violin)
        voice.pin(line, Offset(0))

        line.unpin([note1, note2])
        notes = voice.get_all_notes()
        assert len(notes) == 2
        assert note0 in notes
        assert note3 in notes
        assert note1 not in notes
        assert note2 not in notes

        notes = voice.get_notes_starting_in_interval(
            Interval(Position(0), Position(2, 1)))
        assert len(notes) == 2

        line.clear()
        notes = voice.get_all_notes()
        assert len(notes) == 0

        notes = voice.get_notes_starting_in_interval(
            Interval(Position(0), Position(2, 1)))
        assert len(notes) == 0
예제 #24
0
    def test_simple_motif(self):
        line = Line()
        notes = [
            Note(DiatonicPitch.parse('C:4'), Duration(1, 4)),
            Note(DiatonicPitch.parse('D:4'), Duration(1, 8)),
            Note(DiatonicPitch.parse('E:4'), Duration(1, 8)),
            Note(DiatonicPitch.parse('F#:4'), Duration(1, 2)),
        ]

        line.pin(notes)

        c = [
            EqualPitchConstraint([notes[0], notes[2]]),
            NotEqualPitchConstraint([notes[1], notes[3]])
        ]

        nm = Phrase(notes, c, 'B')

        print(nm)

        assert nm.name == 'B'
        actors = nm.actors

        assert len(actors) == 4

        cc = nm.constraints
        assert isinstance(cc[0], EqualPitchConstraint)
        cc_a = cc[0].actors
        assert len(cc_a) == 2
        assert cc_a[0] == actors[0]
        assert cc_a[1] == actors[2]

        assert isinstance(cc[1], NotEqualPitchConstraint)
        cc_b = cc[1].actors
        assert len(cc_a) == 2
        assert cc_b[0] == actors[1]
        assert cc_b[1] == actors[3]
        assert 'F#:4' == str(actors[3].diatonic_pitch)

        # More notes for copy to:
        first_note = Note(DiatonicPitch.parse('C:3'), Duration(1, 4))
        notes1 = [
            first_note,
            Note(DiatonicPitch.parse('D:3'), Duration(1, 8)),
            Note(DiatonicPitch.parse('E:3'), Duration(1, 8)),
            Note(DiatonicPitch.parse('F#:3'), Duration(1, 2)),
        ]
        line.pin(notes1, Offset(2))

        nm_clone = nm.copy_to(first_note)
        assert isinstance(nm_clone, Phrase)
        assert nm_clone.name == 'B'
        c_actors = nm_clone.actors

        assert len(c_actors) == 4
        assert 'F#:3' == str(c_actors[3].diatonic_pitch)
예제 #25
0
    def __init__(self):
        """
        Constructor.
        """
        self.tie_list = list()
        self.__line = Line()
        self.current_level = Level(self.__line, LineConstructor.DEFAULT_LINE_DURATION)
        self.level_stack = list()
        self.current_tonality = LineConstructor.DEFAULT_TONALITY
        self.harmonic_tag_list = list()
        self.current_harmonic_tag = None

        # Set up a default harmonic tag. In cases where a tag is immediately specified, this is discarded
        self.construct_harmonic_tag(LineConstructor.DEFAULT_TONALITY,
                                    ChordTemplate.generic_chord_template_parse('ti'))
예제 #26
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))
예제 #27
0
    def test_line_convert(self):
        print("test line convert")

        note1 = Note(DiatonicPitch(4, 'c'), Duration(1, 16))
        note2 = Note(DiatonicPitch(4, 'd'), Duration(1, 16))
        note3 = Note(DiatonicPitch(4, 'e'), Duration(1, 16))
        note4 = Note(DiatonicPitch(4, 'f'), Duration(1, 16))
        beam = Beam([note1, note2, note3, note4])

        line = Line(beam)

        meta_track, tracks = ScoreToVstMidiConverter.convert_line(line)
        assert meta_track is not None
        assert tracks is not None

        assert len(tracks) == 1
        assert len(tracks[0]) == 8

        for i in range(0, len(tracks[0])):
            print("{0}: {1}".format(i, tracks[0][i]))

        assert isinstance(tracks[0][0], NoteMessage)
        assert tracks[0][0].msg_type == 144
        assert tracks[0][0].note_value == 60  # C
        assert tracks[0][0].abs_frame_time == 0
        assert tracks[0][0].rel_frame_time == 0

        assert isinstance(tracks[0][2], NoteMessage)
        assert tracks[0][2].msg_type == 144
        assert tracks[0][2].note_value == 62  # D
        assert tracks[0][2].abs_frame_time == 10525
        assert tracks[0][2].rel_frame_time == 0

        assert isinstance(tracks[0][4], NoteMessage)
        assert tracks[0][4].msg_type == 144
        assert tracks[0][4].note_value == 64  # E
        assert tracks[0][4].abs_frame_time == 21050
        assert tracks[0][4].rel_frame_time == 0

        assert isinstance(tracks[0][6], NoteMessage)
        assert tracks[0][6].msg_type == 144
        assert tracks[0][6].note_value == 65  # F
        assert tracks[0][6].abs_frame_time == 31575
        assert tracks[0][6].rel_frame_time == 0
예제 #28
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')
예제 #29
0
    def test_pin(self):
        print('----- test_pin -----')

        l1_notes = [Note(DiatonicPitch(4, y), Duration(1, 8)) for y in 'abcd']
        l2_notes = [Note(DiatonicPitch(4, y), Duration(1, 8)) for y in 'efgab']
        line = Line()
        line.pin(Line(l1_notes), Offset(1, 8))
        line.pin(Line(l2_notes), Offset(1, 4))

        all_notes = line.get_all_notes()
        print('... pinned notes ...')
        for n in all_notes:
            print('({0}) : {1}'.format(n.get_absolute_position(),
                                       n.diatonic_pitch))
        print('... end pinned notes ...')

        print('End test_pin')
예제 #30
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