Module: Xi::Pattern::Transforms
- Included in:
- Xi::Pattern
- Defined in:
- lib/xi/pattern/transforms.rb
Instance Method Summary collapse
-
#%(numeric) ⇒ Pattern
Performs a scalar modulo against
numeric. -
#*(numeric) ⇒ Pattern
Performs a scalar multiplication with
numeric. -
#**(numeric) ⇒ Pattern
(also: #^)
Raises each value to the power of
numeric, which may be negative or fractional. -
#+(object) ⇒ Pattern
Concatenate
objectpattern or perform a scalar sum withobject. -
#-(numeric) ⇒ Pattern
Performs a scalar substraction with
numeric. -
#-@ ⇒ Pattern
Negates every number in the pattern.
-
#/(numeric) ⇒ Pattern
Performs a scalar division by
numeric. -
#bounce(skip_extremes = true) ⇒ Pattern
Traverses the pattern in order and then in reverse order, skipping first and last values if
skip_extremesis true. -
#denormalize(min, max) ⇒ Pattern
(also: #denorm)
Scales a pattern of normalized values (0..1) to a custom range
min..max. -
#every(n) {|Pattern| ... } ⇒ Pattern
Splices a new pattern returned from
blockeveryncycles. -
#every_iter(n) {|Pattern| ... } ⇒ Pattern
Splices a new pattern returned from
blockeveryniterations. -
#fast(num) ⇒ Pattern
Advance a pattern by shrinking start and duration of events
numtimes. -
#normalize(min, max) ⇒ Pattern
(also: #norm)
Normalizes a pattern of values that range from
mintomaxto 0..1. -
#rand(repeats = 1) ⇒ Pattern
Choose items from the list randomly,
repeatsnumber of times. -
#repeat_each(times) ⇒ Pattern
Repeats each value
times. -
#scale(min_from, max_from, min_to, max_to) ⇒ Pattern
Scale from one range of values to another range of values.
-
#seq(repeats = 1, offset = 0) ⇒ Pattern
Cycles pattern
repeatsnumber of times, shifted byoffset. -
#shuf(repeats = 1) ⇒ Pattern
Shuffle the list in random order, and use the same random order
repeatstimes. -
#slow(num) ⇒ Pattern
Slows down a pattern by stretching start and duration of events
numtimes. -
#sometimes(probability = 0.5) ⇒ Pattern
Based on
probability, it yields original value or nil. -
#when(test_proc) {|Pattern| ... } ⇒ Pattern, Enumerator
Returns a new Pattern where values for which
test_procare true are yielded as a pattern to anotherblock. -
#xrand(repeats = 1) ⇒ Pattern
Choose randomly, but only allow repeating the same item after yielding all items from the list.
Instance Method Details
#%(numeric) ⇒ Pattern
Performs a scalar modulo against numeric
For each value from pattern, return modulo of value divided by numeric. Values from pattern that do not respond to #% are ignored.
106 107 108 |
# File 'lib/xi/pattern/transforms.rb', line 106 def %(numeric) map { |v| v.respond_to?(:%) ? v % numeric : v } end |
#*(numeric) ⇒ Pattern
Performs a scalar multiplication with numeric
For each value from pattern, multiplicate with numeric. Values that do not respond to #* are ignored.
74 75 76 |
# File 'lib/xi/pattern/transforms.rb', line 74 def *(numeric) map { |v| v.respond_to?(:*) ? v * numeric : v } end |
#**(numeric) ⇒ Pattern Also known as: ^
Raises each value to the power of numeric, which may be negative or fractional.
Values from pattern that do not respond to #** are ignored.
122 123 124 |
# File 'lib/xi/pattern/transforms.rb', line 122 def **(numeric) map { |v| v.respond_to?(:**) ? v ** numeric : v } end |
#+(object) ⇒ Pattern
Concatenate object pattern or perform a scalar sum with object
If object is a Pattern, concatenate the two patterns. Else, for each value from pattern, sum with object. Values that do not respond to #+ are ignored.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/xi/pattern/transforms.rb', line 35 def +(object) if object.is_a?(Pattern) Pattern.new(self, size: size + object.size) { |y, d| each { |v| y << v } object.each { |v| y << v } } else map { |v| v.respond_to?(:+) ? v + object : v } end end |
#-(numeric) ⇒ Pattern
Performs a scalar substraction with numeric
For each value from pattern, substract with numeric. Values that do not respond to #- are ignored.
58 59 60 |
# File 'lib/xi/pattern/transforms.rb', line 58 def -(numeric) map { |v| v.respond_to?(:-) ? v - numeric : v } end |
#-@ ⇒ Pattern
Negates every number in the pattern
Non-numeric values are ignored.
14 15 16 |
# File 'lib/xi/pattern/transforms.rb', line 14 def -@ map { |v| v.respond_to?(:-@) ? -v : v } end |
#/(numeric) ⇒ Pattern
Performs a scalar division by numeric
For each value from pattern, divide by numeric. Values that do not respond to #/ are ignored.
90 91 92 |
# File 'lib/xi/pattern/transforms.rb', line 90 def /(numeric) map { |v| v.respond_to?(:/) ? v / numeric : v } end |
#bounce(skip_extremes = true) ⇒ Pattern
Traverses the pattern in order and then in reverse order, skipping first and last values if skip_extremes is true.
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/xi/pattern/transforms.rb', line 192 def bounce(skip_extremes=true) return self if size == 0 || size == 1 new_size = skip_extremes ? size * 2 - 2 : size * 2 Pattern.new(self, size: new_size) { |y| each { |v| y << v } last_i = size - 1 reverse_each.with_index { |v, i| y << v unless skip_extremes && (i == 0 || i == last_i) } } end |
#denormalize(min, max) ⇒ Pattern Also known as: denorm
Scales a pattern of normalized values (0..1) to a custom range min..max
This is inverse of #normalize Values from pattern that do not respond to #* are ignored.
240 241 242 |
# File 'lib/xi/pattern/transforms.rb', line 240 def denormalize(min, max) map { |v| v.respond_to?(:*) ? (max - min) * v + min : v } end |
#every(n) {|Pattern| ... } ⇒ Pattern
Splices a new pattern returned from block every n cycles
447 448 449 450 451 452 453 |
# File 'lib/xi/pattern/transforms.rb', line 447 def every(n, &block) fn = proc { |_, s, _| m = (s + 1) % n m >= 0 && m < 1 } self.when(fn, &block) end |
#every_iter(n) {|Pattern| ... } ⇒ Pattern
Splices a new pattern returned from block every n iterations
464 465 466 467 468 469 470 |
# File 'lib/xi/pattern/transforms.rb', line 464 def every_iter(n, &block) fn = proc { |_, _, _, i| m = (i + 1) % n m >= 0 && m < 1 } self.when(fn, &block) end |
#fast(num) ⇒ Pattern
Advance a pattern by shrinking start and duration of events num times.
It is the inverse operation of #slow
293 294 295 |
# File 'lib/xi/pattern/transforms.rb', line 293 def fast(num) Pattern.new(self, delta: delta.p / num) end |
#normalize(min, max) ⇒ Pattern Also known as: norm
Normalizes a pattern of values that range from min to max to 0..1
Values from pattern that do not respond to #- are ignored.
219 220 221 |
# File 'lib/xi/pattern/transforms.rb', line 219 def normalize(min, max) map { |v| v.respond_to?(:-) ? (v - min) / (max - min) : v } end |
#rand(repeats = 1) ⇒ Pattern
Choose items from the list randomly, repeats number of times
370 371 372 |
# File 'lib/xi/pattern/transforms.rb', line 370 def rand(repeats=1) P.rand(self, repeats) end |
#repeat_each(times) ⇒ Pattern
Repeats each value times
times can also be an enumerable or a finite Pattern. In this case, for each value in times, it will yield each value of original pattern repeated a number of times based on that times value.
345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/xi/pattern/transforms.rb', line 345 def repeat_each(times) times_pat = times.p if times_pat.infinite? fail ArgumentError, 'times must be finite' end Pattern.new(self, size: size * times_pat.size) do |y| times_pat.each do |t| each { |v| t.times { y << v } } end end end |
#scale(min_from, max_from, min_to, max_to) ⇒ Pattern
Scale from one range of values to another range of values
257 258 259 |
# File 'lib/xi/pattern/transforms.rb', line 257 def scale(min_from, max_from, min_to, max_to) normalize(min_from, max_from).denormalize(min_to, max_to) end |
#seq(repeats = 1, offset = 0) ⇒ Pattern
Cycles pattern repeats number of times, shifted by offset
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 |
# File 'lib/xi/pattern/transforms.rb', line 139 def seq(repeats=1, offset=0) unless repeats.is_a?(Integer) && repeats >= 0 fail ArgumentError, "repeats must be a non-negative Integer" end unless offset.is_a?(Integer) && offset >= 0 fail ArgumentError, "offset must be a non-negative Integer" end Pattern.new(self, size: size * repeats) do |y| rep = repeats loop do if rep != inf rep -= 1 break if rep < 0 end c = offset offset_items = [] is_empty = true each do |v| is_empty = false if c > 0 offset_items << v c -= 1 else y << v end end offset_items.each { |v| y << v } break if is_empty end end end |
#shuf(repeats = 1) ⇒ Pattern
Shuffle the list in random order, and use the same random order repeats times
402 403 404 |
# File 'lib/xi/pattern/transforms.rb', line 402 def shuf(repeats=1) P.shuf(self, repeats) end |
#slow(num) ⇒ Pattern
Slows down a pattern by stretching start and duration of events num times.
It is the inverse operation of #fast
275 276 277 |
# File 'lib/xi/pattern/transforms.rb', line 275 def slow(num) Pattern.new(self, delta: delta.p * num) end |
#sometimes(probability = 0.5) ⇒ Pattern
Based on probability, it yields original value or nil
probability can also be an enumerable or a finite Pattern. In this case, for each value in probability it will enumerate original pattern based on that probability value.
314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/xi/pattern/transforms.rb', line 314 def sometimes(probability=0.5) prob_pat = probability.p if prob_pat.infinite? fail ArgumentError, 'times must be finite' end Pattern.new(self, size: size * prob_pat.size) do |y| prob_pat.each do |prob| each { |v| y << (Kernel.rand < prob ? v : nil) } end end end |
#when(test_proc) {|Pattern| ... } ⇒ Pattern, Enumerator
Returns a new Pattern where values for which test_proc are true are yielded as a pattern to another block
If no block is given, an Enumerator is returned.
These values are grouped together as a “subpattern”, then yielded to block for further transformation and finally spliced into the original pattern. test_proc will be called with value, start and duration as parameters.
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/xi/pattern/transforms.rb', line 421 def when(test_proc, &block) return enum_for(__method__, test_proc) if block.nil? Pattern.new(self) do |y| each_event do |v, s, d, i| if test_proc.call(v, s, d, i) new_pat = block.call(self) new_pat.each_event(s) .take_while { |_, s_, d_| s_ + d_ <= s + d } .each { |v_, _| y << v_ } else y << v end end end end |