-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsorting-algo-toolbox.rb
More file actions
296 lines (239 loc) · 10.6 KB
/
Copy pathsorting-algo-toolbox.rb
File metadata and controls
296 lines (239 loc) · 10.6 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# BUBBLE SORT
def bubble_sort(list: DEFAULT_LIST_BUBBLE, sorted: DEFAULT_SORTED, amp: 1, play_list: true, synth: :sine,
drums: {:bd => :bd_tek, :cyms => :drum_cymbal_closed}, sleep: 0.25, shutup_drums: false,
drums_amp: 0, bleeps_amp: 0, synths_amp: 0, shutup_synths: false,
shutup_bleeps: false, silence: false)
arr = list.dup
swapped = false
r = arr.length - 2
num_iters = 0
use_synth synth
if play_list
arr.each { |n|
play n, amp: silence ? 0 : amp + synths_amp < 0 ? 0 : amp + synths_amp, release: 0.2
sleep sleep
}
end
while true
swaps = 0
num_iters += 1
in_thread do
sample drums[:bd], amp: silence ? 0 : shutup_drums ? 0 : amp + 0.5 + drums_amp < 0 ? 0 : amp + 0.5 + drums_amp
end
in_thread do
num_iters.times do |i|
sample drums[:cyms], amp: silence ? 0 : shutup_drums ? 0 : amp + (i.to_f / 2.0) + drums_amp < 0 ? 0 : amp + (i.to_f / 2.0) + drums_amp, rate: 2
sleep (2.0 / num_iters).round(2)
end
end
for i in 0..r
play arr[i], amp: silence ? 0 : shutup_synths ? 0 : amp + synths_amp < 0 ? 0 : amp + synths_amp, release: 0.1
sleep sleep
if arr[i] > arr[i+1]
arr[i], arr[i+1] = arr[i+1], arr[i]
swapped = true if !swapped
sample :elec_blip2, amp: silence ? 0 : shutup_bleeps ? 0 : amp + 0.5 + bleeps_amp < 0 ? 0 : amp + 0.5 + bleeps_amp
sleep sleep
play arr[i], amp: silence ? 0 : shutup_synths ? 0 : amp + synths_amp < 0 ? 0 : amp + synths_amp
sleep sleep
swaps += 1
end
end
swapped ? swapped = false : break
end
if not silence
sorted.call(arr, "Bubble Sort")
end
end
# SELECTION SORT
def selection_sort(list: DEFAULT_LIST_SELECT_INSERT, amp: 1, play_list: true, sleep: 0.125, shutup_drums: false, shutup_synths: false, silence: false,
drums: {:bd => :bd_tek, :cyms => :drum_cymbal_closed, :sn => :drum_snare_soft},
synths: {:main => :sine, :opt => :tb303}, sorted: DEFAULT_SORTED,
drums_amp: 0, synths_amp: 0)
arr = list.dup
r = arr.length - 1
swaps = 0
use_synth synths[:main]
if play_list
arr.each {|n| play n, amp: silence ? 0 : amp + synths_amp < 0 ? 0 : amp + synths_amp, release: 0.3; sleep sleep * 2}
end
for i in 0..r
min = arr[i]
min_idx = i
sample drums[:bd], amp: silence ? 0 : shutup_drums ? 0 : amp * 2 + drums_amp < 0 ? 0 : amp * 2 + drums_amp
use_synth synths[:main]
sub = arr[i+1..-1].reverse
sub.each { |n|
play n, amp: silence ? 0 : shutup_synths ? 0 : amp + synths_amp < 0 ? 0 : amp + synths_amp, release: 0.02, cutoff: 60, decay: 0.05
sleep sleep
}
replacements = 0
for j in (i + 1)..r
if arr[j] < min
min = arr[j]
min_idx = j
replacements += 1
end
end
in_thread do
replacements.times do
sample drums[:cyms], rate: 2, amp: silence ? 0 : shutup_drums ? 0 : amp + 0.3 + drums_amp < 0 ? 0 : amp + 0.3 + drums_amp
sleep sleep
end
end
in_thread do
if min_idx != i
arr[i], arr[min_idx] = arr[min_idx], arr[i]
swaps += 1
use_synth synths[:opt]
play arr[min_idx], amp: silence ? 0 : shutup_synths ? 0 : amp / 2.0 + synths_amp < 0 ? 0 : amp / 2.0 + synths_amp, sustain: 0.1, decay: 0.2, release: 0.1, cutoff: 60
else
sample drums[:sn], amp: silence ? 0 : 2, rate: -1
end
end
use_synth synths[:opt]
sl = list.length * sleep > 2 ? 4 - (sub.length * sleep) : 2 - (sub.length * sleep)
play arr[i], amp: silence ? 0 : shutup_synths ? 0 : amp / 2.0 + synths_amp < 0 ? 0 : amp / 2.0 + synths_amp, cutoff: 70, sustain: 0.1, decay: 0.2, release: 0.1
sleep sl
end
if not silence
sorted.call(arr, "Selection Sort")
end
end
# INSERTION SORT
def insertion_sort(list: DEFAULT_LIST_SELECT_INSERT, sorted: DEFAULT_SORTED, amp: 1, play_list: true,
drums: {:bd => :bd_tek, :cyms => :drum_cymbal_closed, :sn => :drum_splash_soft}, sleep: 0.125,
synths: {:main => :sine, :opt1 => :tri, :opt2 => :square}, shutup_drums: false,
shutup_synths: false, silence: false, drums_amp: 0, synths_amp: 0)
arr = list.dup
r = arr.length - 1
swaps = 0
use_synth synths[:main]
if play_list
arr.each {|n|
play n, amp: silence ? 0 : amp + synths_amp < 0 ? 0 : amp + synths_amp, release: 0.3
sleep sleep * 2
}
end
for i in 1..r
sample drums[:bd], amp: silence ? 0 : shutup_drums ? 0 : amp * 2 + drums_amp < 0 ? 0 : amp * 2 + drums_amp
key = arr[i]
j = i - 1
overwrites = 0
while j >= 0 and arr[j] > key
arr[j + 1] = arr[j]
j -= 1
overwrites += 1
end
note_swapped = false
if arr[j+1] != key
note_swapped = arr[j+1]
arr[j+1] = key
swaps += 1
else
sample drums[:sn], amp: silence ? 0 : shutup_drums ? 0 : amp + 0.5 + drums_amp < 0 ? 0 : amp + 0.5 + drums_amp, rate: -1
end
in_thread do
sleep 1
overwrites.times do
sample drums[:cyms], rate: 2, amp: silence ? 0 : shutup_drums ? 0 : amp + 0.3 + drums_amp < 0 ? 0 : amp + 0.3 + drums_amp
sleep sleep
end
end
sub = arr[0..i]
sl = sub.length * sleep > 2 ? 4.0 - (sub.length * sleep) : 2.0 - (sub.length * sleep)
sub.each { |n|
n === key ? (inserted = true; use_synth synths[:opt1]) : (inserted = false; use_synth synths[:opt2])
if inserted
play n, amp: silence ? 0 : shutup_synths ? 0 : amp / 2.0 + synths_amp < 0 ? 0 : amp / 2.0 + synths_amp, cutoff: 70, release: 0.3
if note_swapped
play note_swapped, cutoff: 60, release: 0.3, amp: silence ? 0 : shutup_synths ? 0 : amp / 2.0 + synths_amp < 0 ? 0 : amp / 2.0 + synths_amp
end
else
play n, release: 0.3, amp: silence ? 0 : shutup_synths ? 0 : amp + (amp / 2.0) + synths_amp < 0 ? 0 : amp + (amp / 2.0) + synths_amp
end
sleep sleep
}
sleep sl
end
if not silence
sorted.call(arr, "Insertion Sort")
end
end
# MERGE SORT FUNCTIONS
def play_list(list:, amp:, pan: 0, sleep:, percs:, percs_amp:, synths_amp:, shutup_synths:, shutup_percs:, silence:)
if list.length < 1
sample percs[:opt3], pan: pan, amp: silence ? 0 : shutup_percs ? 0 : amp + (amp / 2.0) + percs_amp < 0 ? 0 : amp + (amp / 2.0) + percs_amp, rate: [1, 2, -1].choose
else
list.each do |n|
play n, pan: pan, amp: silence ? 0 : shutup_synths ? 0 : amp + synths_amp < 0 ? 0 : amp + synths_amp, release: 0.05, sustain: 0.1, decay: 0.05
sleep sleep
end
end
end
def merge(left:, right:, run_sorted:, sorted:, amp:, sleep:, synths:, percs:,
percs_amp:, synths_amp:, shutup_synths:, shutup_percs:, silence:)
sample percs[:opt3], amp: silence ? 0 : shutup_percs ? 0 : 1 + percs_amp < 0 ? 0 : 1 + percs_amp
use_synth synths[:opt]
in_thread do
play_list(list: left, amp: (amp / 2.0) + (amp / 4.0), pan: -1, sleep: sleep,
percs: percs, percs_amp: percs_amp, synths_amp: synths_amp,
shutup_synths: shutup_synths, shutup_percs: shutup_percs, silence: silence)
end
play_list(list: right, amp: (amp / 2.0) + (amp / 4.0), pan: 1, sleep: sleep,
percs: percs, percs_amp: percs_amp, synths_amp: synths_amp,
shutup_synths: shutup_synths, shutup_percs: shutup_percs, silence: silence)
sorted_list = []
while !left.empty? && !right.empty?
if left.first < right.first
sorted_list.push(left.shift)
else
sorted_list.push(right.shift)
end
in_thread do
play_list(list: left, amp: amp / 2.0, pan: -1, sleep: sleep, percs: percs, shutup_synths: shutup_synths,
shutup_percs: shutup_percs, percs_amp: percs_amp, synths_amp: synths_amp,
silence: silence)
end
play_list(list: right, amp: amp / 2.0, pan: 1, sleep: sleep,
percs: percs, percs_amp: percs_amp, synths_amp: synths_amp,
shutup_synths: shutup_synths, shutup_percs: shutup_percs, silence: silence)
end
play_list(list: left, amp: amp, pan: -1, sleep: sleep, percs: percs, percs_amp: percs_amp,
synths_amp: synths_amp, shutup_synths: shutup_synths, shutup_percs: shutup_percs,
silence: silence)
play_list(list: right, amp: amp, pan: 1, sleep: sleep, percs: percs, percs_amp: percs_amp,
synths_amp: synths_amp, shutup_synths: shutup_synths, shutup_percs: shutup_percs,
silence: silence)
play_list(list: sorted_list.concat(right).concat(left), amp: amp, sleep: sleep, percs: percs,
percs_amp: percs_amp, synths_amp: synths_amp, shutup_synths: shutup_synths,
shutup_percs: shutup_percs, silence: silence)
if run_sorted and sorted_list.length > 3
sorted.call(sorted_list, "Merge")
end
sorted_list
end
def merge_sort(list: DEFAULT_LIST_MERGE, side: nil, sorted: DEFAULT_SORTED, run_sorted: true, amp: 1, sleep: 0.25,
synths: {:main => :square, :opt => :beep}, shutup_synths: false, shutup_percs: false, silence: false,
percs: {:opt1 => :elec_flip, :opt2 => :elec_plip, :opt3 => :ambi_swoosh}, percs_amp: 0, synths_amp: 0)
sample percs[:opt1], amp: silence ? 0 : shutup_percs ? 0 : amp + 0.5 + percs_amp < 0 ? 0 : amp + 0.5 + percs_amp
use_synth synths[:main]
p = (side == "left") ? -1 : (side == "right") ? 1 : 0
play_list(list: list, amp: amp, pan: p, sleep: sleep, percs: percs, shutup_synths: shutup_synths,
shutup_percs: shutup_percs, percs_amp: percs_amp, synths_amp: synths_amp, silence: silence)
if list.length <= 1
sample percs[:opt2], amp: silence ? 0 : shutup_percs ? 0 : amp + (amp / 2.0) + percs_amp < 0 ? 0 : amp + (amp / 2.0) + percs_amp
sleep sleep * 2
return list
end
mid = list.length / 2
left = merge_sort(list: list.slice(0...mid), side: "left", sorted: sorted, run_sorted: run_sorted,
amp: amp, sleep: sleep, synths: synths, percs: percs, percs_amp: percs_amp, synths_amp: synths_amp,
shutup_synths: shutup_synths, shutup_percs: shutup_percs, silence: silence)
right = merge_sort(list: list.slice(mid..list.length), side: "right", sorted: sorted, run_sorted: run_sorted,
sleep: sleep, amp: amp, synths: synths, percs: percs, percs_amp: percs_amp, synths_amp: synths_amp,
shutup_synths: shutup_synths, shutup_percs: shutup_percs, silence: silence)
merge(left: left, right: right, sorted: sorted, run_sorted: run_sorted, amp: amp,
sleep: sleep, synths: synths, percs: percs, percs_amp: percs_amp, synths_amp: synths_amp,
shutup_synths: shutup_synths, shutup_percs: shutup_percs, silence: silence)
end