-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbass.rb
98 lines (76 loc) · 2.35 KB
/
bass.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require_relative "music_common"
# Simple walking on C.Ts.
# will only play first chord in measure.
def gen_bass_walk(leading,chord,next_chord,len)
low = 28
hi = 52
mid = ((low + hi) / 2)
walk_up = (leading < mid) if leading
events = []
chord_root, chord_quality = get_intervals(chord[0])
chord_quality.map!{|i| i += 12}
chord_quality.map!{|i| i + chord_root}
if(leading && walk_up)
# invert up 52.
chord_quality = invert(chord_quality, 100)
# overshoots.
chord_quality = invert(chord_quality,-1) while (chord_quality[0] > leading)
invert(chord_quality,1)
(chord_quality << (chord_quality[0] + 12)) if (chord_quality.count < 4)
end
if(leading && !walk_up)
chord_quality = invert(chord_quality, -100)
# overshoots.
chord_quality = invert(chord_quality,1) while (chord_quality[-1] < leading)
invert(chord_quality,-1)
chord_quality.reverse!
(chord_quality << (chord_quality[0] - 12)) if (chord_quality.count < 4)
end
#if no leading, we're walking up.
(chord_quality << (chord_quality[0] + 12)) if (chord_quality.count < 4)
# decide to go up or down from leading tone
chord_quality.each do |i|
events << MIDI::NoteOn.new(1, i, 127, 0)
events << MIDI::NoteOff.new(1, i, 127, len)
end
[events, chord_quality[3]]
end
def walk_sec(chords_flat,qnl,track)
walk = gen_bass_walk(nil,chords_flat[0],chords_flat[1],qnl)
track.events += walk[0]
walk_lead = walk[1]
chords_flat.shift
chords_flat.each_with_index do |c,i|
walk = gen_bass_walk(walk_lead,c,chords_flat[i+1],qnl)
track.events += walk[0]
walk_lead = walk[1]
end
end
def add_bass_track(json,seq)
# Create a track to hold the notes. Add it to the sequence.
track = MIDI::Track.new(seq)
seq.tracks << track
# Give the track a name and an instrument name (optional).
track.name = 'Bass'
track.instrument = MIDI::GM_PATCH_NAMES[36]
# Add a volume controller event (optional).
track.events << MIDI::Controller.new(1, MIDI::CC_VOLUME, 127)
track.events << MIDI::ProgramChange.new(1, 33, 0)
qnl = seq.note_to_delta('quarter')
json["structure"].flatten.each do |s|
if s.include? "A"
chords_flat = []
json["a_section"].each do |bar|
chords_flat << bar
end
walk_sec(chords_flat,qnl,track)
else
chords_flat = []
json["b_section"].each do |bar|
chords_flat << bar
end
walk_sec(chords_flat,qnl,track)
end
end
# Assumes one chord per bar!
end