-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.rb
150 lines (135 loc) · 2.57 KB
/
util.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
define :major do |n|
return [n, n+4, n+7]
end
define :minor do |n|
return [n, n+3, n+7]
end
define :mb5 do |n|
return [n, n+3, n+6]
end
define :major7 do |n|
return major(n).push(n+11)
end
define :minor7 do |n|
return minor(n).push(n+10)
end
define :dom7 do |n|
return major(n).push(n+10)
end
define :m7b5 do |n|
return mb5(n).push(n+10)
end
define :add_random_tension do |key, chord|
ten = {"9" => 14, "11" => 17, "#11" => 18, "b13" => 20, "13" => 21}
deg = chord[0] - key
t = 0
case deg
when 0 then
r = rand_i(2)
if r == 0 then
t = ten["9"]
else
t = ten["13"]
end
when 2 then
r = rand_i(3)
if r == 0 then
t = ten["9"]
elsif r == 1 then
t = ten["11"]
else
t = ten["13"]
end
when 4 then
t = ten["11"]
when 5 then
r = rand_i(3)
if r == 0 then
t = ten["9"]
elsif r == 1 then
t = ten["#11"]
else
t = ten["13"]
end
when 7 then
r = rand_i(2)
if r == 0 then
t = ten["9"]
else
t = ten["13"]
end
when 9 then
r = rand_i(2)
if r == 0 then
t = ten["9"]
else
t = ten["11"]
end
when 11 then
r = rand_i(2)
if r == 0 then
t = ten["11"]
else
t = ten["b13"]
end
end
tension = chord[0] + t
return chord.push(tension)
end
define :major_roots do |n|
roots = [0, 2, 4, 5, 7, 9, 11]
return roots.map { |i| n+i }
end
define :diatonic_chords do |n|
return [major(n), minor(n+2), minor(n+4), major(n+5), major(n+7), minor(n+9), mb5(n+11)]
end
define :diatonic_chords7 do |key|
return [major7(key), minor7(key+2), minor7(key+4), major7(key+5), dom7(key+7), minor7(key+9), m7b5(key+11)]
end
define :inversion do |chord, target|
if target > chord.length then
puts 'error'
return
end
target.times do |i|
chord[i] = chord[i] + 12
end
return chord
end
define :play! do |is_osc=false, msg=nil, note|
if is_osc
if note.instance_of?(Array)
osc msg + ' ' + note.to_s.gsub!(/\[|\]|,/, '')
elsif note.instance_of?(Integer)
osc msg + ' ' + note.to_s
else
puts 'error in play_or_osc'
end
else
play note
end
end
define :sample! do |is_osc=false, msg=nil, note, smpl|
if is_osc
osc msg + ' ' + note.to_s
else
sample smpl
end
end
define :initTrackStates do |n|
array = [0]
(n - 1).times do
array += [0]
end
return array
end
define :random_choice do |n, states|
indexes = []
(0..states.length).each do |index|
if states[index] == n
indexes.push(index)
end
end
target = indexes.shuffle[0]
return target
end