Last active
April 23, 2020 05:41
-
-
Save dsh0416/d0d33bfe3542b8a8c804ccc4654fcd7f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CHORDS_BASE = [:C2, :C2, :C2, :C2, :F2, :F2, :C2, :C2, :G2, :F2, :C2, :C2] | |
CHORDS_HARMONY = [:C3, :C3, :C3, :C3, :F3, :F3, :C3, :C3, :G3, :F3, :C3, :C3] | |
BLUES_SCALE = [:C4, :Eb4, :F4, :Gb4, :G, :Bb4, :C5] | |
FOURTH = 0.5 | |
EIGHTH = FOURTH / 2 | |
SIXTEENTH = EIGHTH / 2 | |
def generate_rhythms(candidates=[EIGHTH, FOURTH], remaining_time=FOURTH*4) | |
rhythms = [] | |
while remaining_time > candidates.min | |
rhythm = choose(candidates) | |
rhythms << rhythm | |
remaining_time -= rhythm | |
end | |
rhythms << remaining_time if remaining_time > 0 | |
# Generate Swing Pattern | |
(rhythms.length - 1).times do |i| | |
if rhythms[i] == EIGHTH and rhythms[i + 1] == EIGHTH | |
rhythms[i] = EIGHTH + SIXTEENTH | |
rhythms[i + 1] = EIGHTH - SIXTEENTH | |
end | |
end | |
rhythms | |
end | |
live_loop :bass_line do | |
use_synth :fm | |
CHORDS_BASE.each do |c| | |
notes = chord(c, '9').to_a | |
rhythms = generate_rhythms | |
play notes[0], release: rhythms[0] | |
sleep rhythms[0] | |
rhythms[1..-1].each do |t| | |
play choose([notes[1], notes[3], notes[4]]), release: t | |
sleep t | |
end | |
end | |
end | |
live_loop :chord do | |
use_synth :fm | |
CHORDS_HARMONY.each do |c| | |
notes = chord(c, '9').to_a | |
rhythms = generate_rhythms | |
rhythms.each do |t| | |
play [notes[1], notes[3], notes[4]], release: t if choose([true, false]) | |
sleep t | |
end | |
end | |
end | |
melody_reverse = false | |
live_loop :melody do | |
use_synth :beep | |
rhythm = generate_rhythms([FOURTH * 2, FOURTH, EIGHTH, SIXTEENTH], remaining_time=FOURTH*8) | |
scale_notes = BLUES_SCALE | |
scale_notes = scale_notes.reverse if melody_reverse | |
melody_reverse = !melody_reverse | |
rhythm.each_with_index do |t, i| | |
offset = choose([-2, -1, 1, 2]) | |
new_index = (i.to_f / rhythm.length).to_i + offset | |
new_index = 0 if new_index < 0 | |
new_index = scale_notes.length - 1 if new_index >= scale_notes.length | |
note = scale_notes[new_index] | |
play note, release: t if rrand(0, 1) < 0.9 | |
sleep t | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment