-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.rb
301 lines (277 loc) · 8.37 KB
/
utils.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
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
297
298
299
300
301
module Polyphony
#
# Utility methods.
#
module Utils
#
# Allows evaluation of chance based on numeric value
#
module Chance
#
# Evaluates chance based on self
#
# @return [TrueClass, FalseClass] true if chance of self evaluates true
#
def evalChance?
return ((self >= 1) || ((self > 0) && (rand() < self)))
end
end
#
# Allows generation of range array from zero
#
module IntegerUtils
#
# Convert integer self to range array from zero
#
# @return [Array<Integer>] range array from zero to self
#
def toRangeAFromZero
return (0...self).to_a.freeze
end
#
# Convert integer self to range from zero
#
# @return [Range] range from zero to self
#
def toRangeFromZero
return (0...self).freeze
end
end
#
# Pair of min and max
#
class RangePair
attr_reader :min, :max
#
# Creates RangePair with min and max values, or mirror of first value if only one value is given
#
# @param [Numeric] pMin minimum
# @param [Numeric, NilClass] pMax maximum
#
def initialize(pMin, pMax = nil)
if pMax.nil?
# @type [Numeric]
@min = -(pMin.abs)
# @type [Numeric]
@max = pMin.abs
else
# @type [Numeric]
@min = pMin
# @type [Numeric]
@max = pMax
end
end
end
#
# Pair of min and max floats
#
class RangePairF < RangePair
#
# Gets random float value between min and max
#
# @return [Float] random value between min and max
#
def get
return rrand(@min, @max)
end
#
# Returns true if objects share the same min and max and are both RangePairF objects
#
# @param [Object] pOther object to compare with this
#
# @return [TrueClass, FalseClass] true if objects share the same min and max and are both RangePairF objects
#
def ==(pOther)
return false if pOther.nil?
return false unless pOther.instance_of?(RangePairF)
return ((@min == pOther.min) && (@max == pOther.max))
end
end
#
# Pair of min and max ints
#
class RangePairI < RangePair
#
# Gets random int value between min and max
#
# @return [Integer] random value between min and max
#
def get
return rrand_i(@min, @max)
end
#
# Gets range between min and max
#
# @return [RangePair] range between min and max
#
def toRange
return (@min..@max).freeze
end
#
# Gets range array between min and max
#
# @return [Array<Integer>] array of int values between min and max
#
def toRangeA
return toRange().to_a.freeze
end
#
# Returns true if objects share the same min and max and are both RangePairI objects
#
# @param [Object] pOther object to compare with this
#
# @return [TrueClass, FalseClass] true if objects share the same min and max and are both RangePairI objects
#
def ==(pOther)
return false if pOther.nil?
return false unless pOther.instance_of?(RangePairI)
return ((@min == pOther.min) && (@max == pOther.max))
end
end
#
# Chooses int from given values, favouring large or small absolute values depending on whether weight is positive or negative
#
# @param [Float] pWt weight between -1 and 1
# @param [Array<Integer>] pInts array of ints to choose from
#
# @return [Integer] selection
#
def chooseAbsIntWithWeight(pWt, pInts)
if (pWt.zero? || (pInts.length < 2))
return pInts.choose
else
# @type [Integer]
minAbsInt = pInts.min { |a, b| (a.abs <=> b.abs) }.abs
# @type [Integer]
maxAbsInt = pInts.max { |a, b| (a.abs <=> b.abs) }.abs
# @type [Array<Integer>]
weightedPool = []
while weightedPool.empty?
if (pWt < 0)
weightedPool = pInts.select { |i| (i.abs <= rrandIWithWeight(pWt, minAbsInt, maxAbsInt)) }
else
weightedPool = pInts.select { |i| (i.abs >= rrandIWithWeight(pWt, minAbsInt, maxAbsInt)) }
end
end
return weightedPool.choose
end
end
#
# Get larger value
#
# @param [Numeric] pA value
# @param [Numeric] pB other value
#
# @return [Numeric] larger value
#
def getMax(pA, pB)
return ((pA > pB) ? pA : pB)
end
#
# Get smaller value
#
# @param [Numeric] pA value
# @param [Numeric] pB other value
#
# @return [Numeric] smaller value
#
def getMin(pA, pB)
return ((pA < pB) ? pA : pB)
end
#
# Returns array of contiguous ranges from the given ascending array
#
# @param [Array<Integer>] pAscendingArray ascending integers
#
# @return [Array<Array<Integer>>] array of contiguous ranges
#
def getRangeArraysInAscendingArray(pAscendingArray)
if pAscendingArray.empty?
return [].freeze
end
# @type [Integer]
i = 0
# @type [Array<RangePairI>]
ranges = []
pAscendingArray.length.times do
start = i
while (((i + 1) != pAscendingArray.length) && ((pAscendingArray[i + 1] - pAscendingArray[i]) == 1))
i += 1
end
ranges.push((pAscendingArray[start]..pAscendingArray[i]).to_a.freeze)
if ((i + 1) == pAscendingArray.length)
break
else
i += 1
end
end
return ranges.freeze
end
#
# Gets indices of true in Sonic Pi ring of boolean values
#
# @param [Ring] pBoolsRing Sonic Pi ring of boolean values
#
# @return [Array<Integer>] indices of true
#
def getTrueIndices(pBoolsRing)
return pBoolsRing.to_a.each_index.select { |i| pBoolsRing[i] }.freeze
end
#
# Returns random int between 0 and given max, favouring large or small values depending on whether weight is positive or negative
#
# @param [Float] pWt weight between -1 and 1
# @param [Integer] pMax max value inclusive
#
# @return [Integer] random int between 0 and given max, favouring large or small values depending on whether weight is positive or negative
#
def randIWithWeight(pWt, pMax = 1)
return randWithWeight(pWt, (pMax + 1)).to_i
end
#
# Returns random float between 0 and given max, favouring large or small values depending on whether weight is positive or negative
#
# solution adapted from Jochen Hertle: https://in-thread.sonic-pi.net/t/choosing-random-ints-favouring-large-small/5234/2?u=d0lfyn
#
# @param [Float] pWt weight between -1 and 1
# @param [Float] pMax max value exclusive
#
# @return [Float] random float between 0 and given max, favouring large or small values depending on whether weight is positive or negative
#
def randWithWeight(pWt, pMax = 1)
if ((pWt < -1) || (pWt > 1))
return nil
elsif pWt.zero?
return rand(pMax)
else
return (pMax * (0.5 * (pWt + Math.sqrt(pWt**2 + 4*pWt*rand() - 2*pWt + 1) - 1) / pWt.to_f))
end
end
#
# Returns random int between min and max, favouring large or small values depending on whether weight is positive or negative
#
# @param [Float] pWt weight between -1 and 1
# @param [Integer] pMin min value inclusive
# @param [Integer] pMax max value inclusive
#
# @return [Integer] random int between min and max, favouring large or small values depending on whether weight is positive or negative
#
def rrandIWithWeight(pWt, pMin, pMax)
return (randIWithWeight(pWt, (pMax - pMin)) + pMin)
end
#
# Returns random float between min and max, favouring large or small values depending on whether weight is positive or negative
#
# @param [Float] pWt weight between -1 and 1
# @param [Float] pMin min value inclusive
# @param [Float] pMax max value exclusive
#
# @return [Float] random float between min and max, favouring large or small values depending on whether weight is positive or negative
#
def rrandWithWeight(pWt, pMin, pMax)
return (randWithWeight(pWt, (pMax - pMin)) + pMin)
end
end
end
Integer.include Polyphony::Utils::IntegerUtils
Numeric.include Polyphony::Utils::Chance