コード例 #1
0
def generate_lead_track(scale, tempo, signature, n_measures,
                        n_notes_per_measure):
    n_mul = 4

    n_notes = int(n_measures * n_notes_per_measure * n_mul)
    p = [0.0005, 0.01, 0.1, 0.3, 0.0, 0.3, 0.1, 0.01, 0.0005]
    p = p / np.sum(p)
    steps = np.random.choice([-4, -3, -2, -1, 0, 1, 2, 3, 4], n_notes - 1, p=p)
    notes = np.array(scale.generate(4, 6))
    rw = elastic_bounded_random_walk(steps, np.random.randint(0, len(notes)),
                                     0, len(notes))
    notes = notes[rw.astype(int)]

    cisd = ChordInScaleDefinition(scale)

    # TODO: use proper measures, instead of one big measure with all notes
    measure = Measure(tempo, signature)
    for index, note in enumerate(notes):
        vel = 2 if (index % 8) in [0, 3, 5] else 1.2
        cnotes = cisd.generate_chord(note)
        cnotes.notes[1].increment(0)
        for n in cnotes.notes[0:1]:
            measure.add_note(n, index * 1 / n_mul, 1 / n_mul, vel)
    track = Track([measure])

    return track
コード例 #2
0
def vader_jacob():

    theme1 = Measure(Tempo(120), Signature(4, 4)) \
        .add_note(Note('C', 3), 0, 1) \
        .add_note(Note('D', 3), 1, 1) \
        .add_note(Note('E', 3), 2, 1) \
        .add_note(Note('C', 3), 3, 1)

    theme2 = Measure(Tempo(120), Signature(4, 4)) \
        .add_note(Note('E', 3), 0, 1) \
        .add_note(Note('F', 3), 1, 1) \
        .add_note(Note('G', 3), 2, 2)

    theme3 = Measure(Tempo(120), Signature(4, 4)) \
        .add_note(Note('G', 3), 0, 0.5) \
        .add_note(Note('A', 3), 0.5, 0.5) \
        .add_note(Note('G', 3), 1, 0.5) \
        .add_note(Note('F', 3), 1.5, 0.5) \
        .add_note(Note('E', 3), 2, 1) \
        .add_note(Note('C', 3), 3, 1)

    theme4 = Measure(Tempo(120), Signature(4, 4)) \
        .add_note(Note('C', 3), 0, 1) \
        .add_note(Note('G', 2), 1, 1) \
        .add_note(Note('C', 3), 2, 2)

    track = Track([theme1] * 2 + [theme2] * 2 + [theme3] * 2 + [theme4] * 2)

    return track
コード例 #3
0
ファイル: instrument.py プロジェクト: norpol/music-generator
    def generate_chord(self, chord: Chord, time: Duration):

        tempo = Tempo(120)
        measure = Measure(tempo, Signature(4, 4))

        for n in chord.notes:
            measure.add_note(n, 0, duration=time.beats(tempo))

        track = Track([measure])

        return self.generate_track(track)
コード例 #4
0
ファイル: test_score.py プロジェクト: norpol/music-generator
def test_measure_note_generate():
    measure = Measure(Tempo(120), Signature(4, 4))
    measure.add_note(Note('C#', 3), 0, 1)
    measure.add_note(Note('D', 3), 1, 1)
    measure.add_note(Note('E', 3), 2, 1)
    measure.add_note(Note('C#', 3), 3, 1)
    result = measure.generate_notes(Duration(1.5))
    assert result[0].offset.seconds == (measure.notes[0].offset +
                                        Duration(1.5)).seconds
    pass
コード例 #5
0
ファイル: random_lead.py プロジェクト: norpol/music-generator
def generate_bass(chords, signature: Signature, tempo: Tempo):

    measures = []
    for c in chords:
        measure = Measure(tempo, signature)
        note = c.get_root().clone()
        note.set_octave(1)

        for i in range(int(signature.get_num_quarter_notes())):
            measure.add_note(note, i, 0.5)
        measures.append(measure)

    track = Track(measures)

    return track
コード例 #6
0
def generate_chord_track(scale, tempo, signature, n_measures):
    cisd = ChordInScaleDefinition(scale)
    root_chord_scale = scale.generate(3, 4)

    chords = [
        cisd.generate_chord(root_chord_scale[0]),
        cisd.generate_chord(root_chord_scale[4]),
        cisd.generate_chord(root_chord_scale[5]),
        cisd.generate_chord(root_chord_scale[3])
    ]

    chord_track = Track([])
    i = 0
    while i < n_measures:
        i_chord = i % 4

        measure = Measure(tempo, signature)
        for note in chords[i_chord].notes:
            measure.add_note(note, 0, 4)

        chord_track.measures.append(measure)
        i += 1

    return chord_track
コード例 #7
0
ファイル: test_score.py プロジェクト: norpol/music-generator
def test_measure():

    measure = Measure(Tempo(120), Signature(4, 4))

    measure.add_note(Note('C', 3), 0, 1)
    measure.add_note(Note('D', 3), 1, 1)
    measure.add_note(Note('E', 3), 2, 1)
    measure.add_note(Note('C', 3), 3, 1)

    assert len(measure.notes) == 4
コード例 #8
0
def test_max_duration():
    measure = Measure(Tempo(120), Signature(4, 4))
    measure.add_note(Note('C#', 3), 0, 1)
    measure.add_note(Note('F', 3), 2, 5)
    measure.add_note(Note('D', 3), 1, 1)
    measure.add_note(Note('E', 3), 2, 1)
    measure.add_note(Note('C#', 3), 3, 1)

    max_duration = get_max_duration(measure.generate_notes(Duration(1.5)))

    assert max_duration.seconds == 5.0
コード例 #9
0
ファイル: test_score.py プロジェクト: norpol/music-generator
def test_bar():
    bar = Measure(Tempo(120), Signature(3, 4))
    assert bar.total_time().seconds == 1.5
コード例 #10
0
def generate_bass_track(scale, tempo, signature, n_measures):
    scale = scale.generate(1, 2)

    roots = [scale[0], scale[4], scale[5], scale[3]]

    track = Track([])
    i = 0
    while i < n_measures:
        i_note = i % 4

        note = roots[i_note]

        measure = Measure(tempo, signature)
        measure.add_note(note, 0, 0.5)
        measure.add_note(note, 1.0, 0.5)
        measure.add_note(note, 2.0, 0.125)
        measure.add_note(note, 2.5, 0.5)
        measure.add_note(note, 3.5, 0.25)

        track.measures.append(measure)
        i += 1

    return track