Class: Cyclotone::Pattern
Constant Summary
collapse
- SAMPLE_EPSILON =
Rational(1, 1024)
- CACHE_LIMIT =
128
- CACHE_MUTEX =
Mutex.new
- COMPILER_MUTEX =
Mutex.new
Transforms::Alteration::MAX_SEGMENTS
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.append(first, second) ⇒ Object
-
.atom_at(value, at:, duration: 0) ⇒ Object
-
.cat(patterns) ⇒ Object
-
.coerce_span(value) ⇒ Object
-
.compiler ⇒ Object
-
.continuous(sample: :midpoint, &sampler) ⇒ Object
-
.ensure_pattern(value, strings: :literal) ⇒ Object
-
.fast_append(first, second) ⇒ Object
-
.fastcat(patterns) ⇒ Object
-
.map_event(event, &block) ⇒ Object
-
.map_span(span, &block) ⇒ Object
-
.mn(string) ⇒ Object
-
.mn!(string) ⇒ Object
-
.overlay(first, second) ⇒ Object
-
.parser ⇒ Object
-
.pure(value) ⇒ Object
(also: atom)
-
.randcat(patterns, namespace: :randcat) ⇒ Object
-
.sample_epsilon ⇒ Object
-
.sample_epsilon=(value) ⇒ Object
-
.shift_event(event, amount) ⇒ Object
-
.silence ⇒ Object
-
.sort_events(events) ⇒ Object
-
.stack(patterns, empty: :error) ⇒ Object
-
.stack_or_silence(patterns) ⇒ Object
-
.timecat(weighted_patterns) ⇒ Object
-
.to_rational(value) ⇒ Object
-
.try_mn(string) ⇒ Object
Instance Method Summary
collapse
-
#%(other) ⇒ Object
-
#*(other) ⇒ Object
-
#+(other) ⇒ Object
-
#-(other) ⇒ Object
-
#/(other) ⇒ Object
-
#add(other, structure: :both) ⇒ Object
-
#arp(mode = :up) ⇒ Object
(also: #arpeggiate)
-
#combine_both(other, &operation) ⇒ Object
-
#combine_left(other, &operation) ⇒ Object
-
#combine_right(other, &operation) ⇒ Object
-
#continuous? ⇒ Boolean
-
#control(name, pattern_or_value) ⇒ Object
-
#div(other, structure: :both) ⇒ Object
-
#flat_map_events(&transform) ⇒ Object
-
#fmap(&transform) ⇒ Object
-
#initialize(continuous: false, &query_func) ⇒ Pattern
constructor
A new instance of Pattern.
-
#map_events(&transform) ⇒ Object
-
#merge(other) ⇒ Object
-
#merge_deep(other) ⇒ Object
-
#merge_left(other) ⇒ Object
-
#merge_right(other) ⇒ Object
-
#mod(other, structure: :both) ⇒ Object
-
#mul(other, structure: :both) ⇒ Object
-
#query_cycle(cycle_number) ⇒ Object
-
#query_event_at(time) ⇒ Object
-
#query_point(time) ⇒ Object
-
#query_points(time) ⇒ Object
-
#query_span(span) ⇒ Object
-
#scale(name, root: 0) ⇒ Object
-
#select_events(&predicate) ⇒ Object
-
#sub(other, structure: :both) ⇒ Object
-
#up(semitones) ⇒ Object
#bite, #chew, #chop, #loop_at, #randslice, #segment, #slice, #splice, #striate
#contrast, #fix, #mask, #struct, #unfix, #when_mod
#almost_always, #almost_never, #chunk, #degrade, #degrade_by, #every, #every_with_offset, #fastspread, #fold_every, #iter, #iter_back, #linger, #rarely, #scramble, #shuffle, #slowstripe, #sometimes, #sometimes_by, #spread, #stripe, #trunc, #zoom
#jux, #jux_by, #layer, #overlay, #superimpose, #weave, #weave_with
#append, #fast_append
#early, #fast, #hurry, #inside, #late, #off, #outside, #palindrome, #rev, #slow, #swing
Constructor Details
#initialize(continuous: false, &query_func) ⇒ Pattern
Returns a new instance of Pattern.
19
20
21
22
23
24
25
|
# File 'lib/cyclotone/pattern.rb', line 19
def initialize(continuous: false, &query_func)
raise ArgumentError, "Pattern requires a query block" unless query_func
@continuous = continuous
@query = query_func
freeze
end
|
Instance Attribute Details
#query ⇒ Object
Returns the value of attribute query.
17
18
19
|
# File 'lib/cyclotone/pattern.rb', line 17
def query
@query
end
|
Class Method Details
.append(first, second) ⇒ Object
336
337
338
|
# File 'lib/cyclotone/pattern.rb', line 336
def append(first, second)
cat([first, second])
end
|
.atom_at(value, at:, duration: 0) ⇒ Object
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/cyclotone/pattern.rb', line 223
def atom_at(value, at:, duration: 0)
onset_offset = to_rational(at)
event_duration = to_rational(duration)
raise ArgumentError, "atom duration must be non-negative" if event_duration.negative?
Pattern.new do |span|
cycle_start = Rational(span.cycle_number)
onset = cycle_start + onset_offset
whole = TimeSpan.new(onset, onset + event_duration)
trigger_span = TimeSpan.new(onset, onset + sample_epsilon)
part = span.intersection(trigger_span)
part ? [Event.new(whole: whole, part: part, value: value)] : []
end
end
|
.cat(patterns) ⇒ Object
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
# File 'lib/cyclotone/pattern.rb', line 309
def cat(patterns)
normalized = Array(patterns)
raise ArgumentError, "cat requires patterns" if normalized.empty?
Pattern.new do |span|
cycle = span.cycle_number
index = cycle % normalized.length
local_cycle = cycle / normalized.length
cycle_offset = cycle - local_cycle
local_span = span.shift(-cycle_offset)
ensure_pattern(normalized[index]).query_span(local_span).map do |event|
shift_event(event, cycle_offset)
end
end
end
|
.coerce_span(value) ⇒ Object
404
405
406
407
408
409
|
# File 'lib/cyclotone/pattern.rb', line 404
def coerce_span(value)
return value if value.is_a?(TimeSpan)
return TimeSpan.new(value.fetch(0), value.fetch(1)) if value.respond_to?(:fetch)
raise ArgumentError, "expected TimeSpan or [start, stop], got #{value.class}"
end
|
.compiler ⇒ Object
386
387
388
389
390
391
392
|
# File 'lib/cyclotone/pattern.rb', line 386
def compiler
return @compiler if @compiler
COMPILER_MUTEX.synchronize do
@compiler ||= MiniNotation::Compiler.new
end
end
|
.continuous(sample: :midpoint, &sampler) ⇒ Object
243
244
245
246
247
248
249
|
# File 'lib/cyclotone/pattern.rb', line 243
def continuous(sample: :midpoint, &sampler)
raise ArgumentError, "continuous requires a sampler block" unless sampler
Pattern.new(continuous: true) do |span|
[Event.new(whole: nil, part: span, value: sampler.call(sample_time(span, sample)))]
end
end
|
.ensure_pattern(value, strings: :literal) ⇒ Object
251
252
253
254
255
256
|
# File 'lib/cyclotone/pattern.rb', line 251
def ensure_pattern(value, strings: :literal)
return value if value.is_a?(Pattern)
return mn(value) if value.is_a?(String) && strings == :mini_notation
pure(value)
end
|
.fast_append(first, second) ⇒ Object
340
341
342
|
# File 'lib/cyclotone/pattern.rb', line 340
def fast_append(first, second)
fastcat([first, second])
end
|
.fastcat(patterns) ⇒ Object
305
306
307
|
# File 'lib/cyclotone/pattern.rb', line 305
def fastcat(patterns)
timecat(Array(patterns).map { |pattern| [1, pattern] })
end
|
.map_event(event, &block) ⇒ Object
428
429
430
431
432
433
434
|
# File 'lib/cyclotone/pattern.rb', line 428
def map_event(event, &block)
Event.new(
whole: map_span(event.whole, &block),
part: map_span(event.part, &block),
value: event.value
)
end
|
.map_span(span, &block) ⇒ Object
422
423
424
425
426
|
# File 'lib/cyclotone/pattern.rb', line 422
def map_span(span, &block)
return nil unless span
TimeSpan.new(block.call(span.start), block.call(span.stop))
end
|
.mn(string) ⇒ Object
367
368
369
370
|
# File 'lib/cyclotone/pattern.rb', line 367
def mn(string)
source = string.to_s
cached_pattern(source) { compiler.compile(parser.parse(source)) }
end
|
.mn!(string) ⇒ Object
372
373
374
|
# File 'lib/cyclotone/pattern.rb', line 372
def mn!(string)
mn(string)
end
|
.overlay(first, second) ⇒ Object
363
364
365
|
# File 'lib/cyclotone/pattern.rb', line 363
def overlay(first, second)
stack([first, second])
end
|
.parser ⇒ Object
382
383
384
|
# File 'lib/cyclotone/pattern.rb', line 382
def parser
MiniNotation::Parser.new
end
|
.pure(value) ⇒ Object
Also known as:
atom
213
214
215
216
217
218
219
|
# File 'lib/cyclotone/pattern.rb', line 213
def pure(value)
Pattern.new do |span|
cycle_start = Rational(span.cycle_number)
whole = TimeSpan.new(cycle_start, cycle_start + 1)
[Event.new(whole: whole, part: span, value: value)]
end
end
|
.randcat(patterns, namespace: :randcat) ⇒ Object
326
327
328
329
330
331
332
333
334
|
# File 'lib/cyclotone/pattern.rb', line 326
def randcat(patterns, namespace: :randcat)
normalized = Array(patterns)
raise ArgumentError, "randcat requires patterns" if normalized.empty?
Pattern.new do |span|
index = Support::Deterministic.int(normalized.length, namespace, span.cycle_number)
ensure_pattern(normalized[index]).query_span(span)
end
end
|
.sample_epsilon ⇒ Object
411
412
413
|
# File 'lib/cyclotone/pattern.rb', line 411
def sample_epsilon
@sample_epsilon ||= SAMPLE_EPSILON
end
|
.sample_epsilon=(value) ⇒ Object
415
416
417
418
419
420
|
# File 'lib/cyclotone/pattern.rb', line 415
def sample_epsilon=(value)
normalized = to_rational(value)
raise ArgumentError, "sample epsilon must be positive" unless normalized.positive?
@sample_epsilon = normalized
end
|
.shift_event(event, amount) ⇒ Object
436
437
438
439
|
# File 'lib/cyclotone/pattern.rb', line 436
def shift_event(event, amount)
offset = to_rational(amount)
map_event(event) { |time| time + offset }
end
|
.silence ⇒ Object
239
240
241
|
# File 'lib/cyclotone/pattern.rb', line 239
def silence
Pattern.new { |_span| [] }
end
|
.sort_events(events) ⇒ Object
394
395
396
397
398
399
400
401
402
|
# File 'lib/cyclotone/pattern.rb', line 394
def sort_events(events)
events.sort_by do |event|
[
event.onset || event.part.start,
event.offset || event.part.stop,
Support::Deterministic.canonical_key(event.value)
]
end
end
|
.stack(patterns, empty: :error) ⇒ Object
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
# File 'lib/cyclotone/pattern.rb', line 344
def stack(patterns, empty: :error)
normalized_patterns = Array(patterns).map { |pattern| ensure_pattern(pattern) }
if normalized_patterns.empty?
return silence if empty == :silence
raise ArgumentError, "stack requires at least one pattern"
end
Pattern.new do |span|
normalized_patterns.flat_map { |pattern| pattern.query_span(span) }.then do |events|
sort_events(events)
end
end
end
|
.stack_or_silence(patterns) ⇒ Object
359
360
361
|
# File 'lib/cyclotone/pattern.rb', line 359
def stack_or_silence(patterns)
stack(patterns, empty: :silence)
end
|
.timecat(weighted_patterns) ⇒ Object
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
302
303
|
# File 'lib/cyclotone/pattern.rb', line 267
def timecat(weighted_patterns)
normalized = Array(weighted_patterns).map do |weight, pattern|
normalized_weight = to_rational(weight)
raise ArgumentError, "timecat weights must be positive" unless normalized_weight.positive?
[normalized_weight, pattern]
end
raise ArgumentError, "timecat requires patterns" if normalized.empty?
total_weight = normalized.sum { |weight, _pattern| weight }
raise ArgumentError, "timecat total weight must be positive" unless total_weight.positive?
Pattern.new do |span|
cycle_start = Rational(span.cycle_number)
cursor = cycle_start
normalized.flat_map do |weight, pattern|
segment_length = weight / total_weight
segment_span = TimeSpan.new(cursor, cursor + segment_length)
overlap = span.intersection(segment_span)
cursor += segment_length
next [] unless overlap
local_span = TimeSpan.new(
(overlap.start - segment_span.start) / segment_length,
(overlap.stop - segment_span.start) / segment_length
)
ensure_pattern(pattern).query_span(local_span).map do |event|
map_event(event) do |time|
segment_span.start + (time * segment_length)
end
end
end
end
end
|
.to_rational(value) ⇒ Object
258
259
260
261
262
263
264
265
|
# File 'lib/cyclotone/pattern.rb', line 258
def to_rational(value)
return value if value.is_a?(Rational)
return Rational(value, 1) if value.is_a?(Integer)
Rational(value.to_s)
rescue ArgumentError, TypeError => error
raise InvalidRationalError, "invalid rational value #{value.inspect}: #{error.message}"
end
|
.try_mn(string) ⇒ Object
376
377
378
379
380
|
# File 'lib/cyclotone/pattern.rb', line 376
def try_mn(string)
mn(string)
rescue ParseError, ArgumentError
nil
end
|
Instance Method Details
#%(other) ⇒ Object
208
209
210
|
# File 'lib/cyclotone/pattern.rb', line 208
def %(other)
mod(other)
end
|
#*(other) ⇒ Object
200
201
202
|
# File 'lib/cyclotone/pattern.rb', line 200
def *(other)
mul(other)
end
|
#+(other) ⇒ Object
192
193
194
|
# File 'lib/cyclotone/pattern.rb', line 192
def +(other)
add(other)
end
|
#-(other) ⇒ Object
196
197
198
|
# File 'lib/cyclotone/pattern.rb', line 196
def -(other)
sub(other)
end
|
#/(other) ⇒ Object
204
205
206
|
# File 'lib/cyclotone/pattern.rb', line 204
def /(other)
div(other)
end
|
#add(other, structure: :both) ⇒ Object
150
151
152
|
# File 'lib/cyclotone/pattern.rb', line 150
def add(other, structure: :both)
apply_binary_operation(other, structure: structure) { |left, right| combine_scalar(left, right, :+) }
end
|
#arp(mode = :up) ⇒ Object
Also known as:
arpeggiate
207
208
209
|
# File 'lib/cyclotone/harmony.rb', line 207
def arp(mode = :up)
Harmony.arpeggiate(self, mode: mode)
end
|
#combine_both(other, &operation) ⇒ Object
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
|
# File 'lib/cyclotone/pattern.rb', line 111
def combine_both(other, &operation)
other_pattern = self.class.ensure_pattern(other)
Pattern.new do |span|
left_events = query_span(span).sort_by { |event| event.active_span.start }
right_events = other_pattern.query_span(span)
right_events_by_start = right_events.sort_by { |event| event.active_span.start }
first_candidate = 0
left_events.flat_map do |left_event|
left_span = left_event.active_span
left_stop = left_span.stop
while first_candidate < right_events_by_start.length && right_events_by_start[first_candidate].active_span.stop <= left_span.start
first_candidate += 1
end
first_candidate.upto(right_events_by_start.length - 1).filter_map do |index|
right_event = right_events_by_start.fetch(index)
break [] if right_event.active_span.start >= left_stop
overlap = left_span.intersection(right_event.active_span)
next unless overlap
part = overlap.intersection(span)
next unless part
whole = left_event.whole && right_event.whole ? left_event.whole.intersection(right_event.whole) : nil
Event.new(
whole: whole,
part: part,
value: operation.call(left_event.value, right_event.value)
)
end
end
end
end
|
#combine_left(other, &operation) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/cyclotone/pattern.rb', line 93
def combine_left(other, &operation)
other_pattern = self.class.ensure_pattern(other)
Pattern.new(continuous: continuous?) do |span|
query_span(span).map do |event|
time = event.onset || event.part.start
other_value = other_pattern.query_point(time)
event.with_value(operation.call(event.value, other_value))
end
end
end
|
#combine_right(other, &operation) ⇒ Object
105
106
107
108
109
|
# File 'lib/cyclotone/pattern.rb', line 105
def combine_right(other, &operation)
self.class.ensure_pattern(other).combine_left(self) do |right_value, left_value|
operation.call(left_value, right_value)
end
end
|
#continuous? ⇒ Boolean
27
28
29
|
# File 'lib/cyclotone/pattern.rb', line 27
def continuous?
@continuous
end
|
#control(name, pattern_or_value) ⇒ Object
186
187
188
|
# File 'lib/cyclotone/controls.rb', line 186
def control(name, pattern_or_value)
merge(Controls.factory(name, pattern_or_value))
end
|
#div(other, structure: :both) ⇒ Object
162
163
164
|
# File 'lib/cyclotone/pattern.rb', line 162
def div(other, structure: :both)
apply_binary_operation(other, structure: structure) { |left, right| combine_scalar(left, right, :/) }
end
|
#flat_map_events(&transform) ⇒ Object
81
82
83
84
85
|
# File 'lib/cyclotone/pattern.rb', line 81
def flat_map_events(&transform)
Pattern.new(continuous: continuous?) do |span|
query_span(span).flat_map { |event| Array(transform.call(event)) }.compact
end
end
|
#fmap(&transform) ⇒ Object
67
68
69
70
71
72
73
|
# File 'lib/cyclotone/pattern.rb', line 67
def fmap(&transform)
raise ArgumentError, "fmap requires a block" unless transform
Pattern.new(continuous: continuous?) do |span|
query_span(span).map { |event| event.with_value(transform.call(event.value)) }
end
end
|
#map_events(&transform) ⇒ Object
75
76
77
78
79
|
# File 'lib/cyclotone/pattern.rb', line 75
def map_events(&transform)
Pattern.new(continuous: continuous?) do |span|
query_span(span).map { |event| transform.call(event) }.compact
end
end
|
#merge(other) ⇒ Object
170
171
172
|
# File 'lib/cyclotone/pattern.rb', line 170
def merge(other)
merge_right(other)
end
|
#merge_deep(other) ⇒ Object
186
187
188
189
190
|
# File 'lib/cyclotone/pattern.rb', line 186
def merge_deep(other)
combine_left(other) do |left, right|
deep_merge_values(left, right)
end
end
|
#merge_left(other) ⇒ Object
174
175
176
177
178
|
# File 'lib/cyclotone/pattern.rb', line 174
def merge_left(other)
combine_left(other) do |left, right|
merge_values(right, left)
end
end
|
#merge_right(other) ⇒ Object
180
181
182
183
184
|
# File 'lib/cyclotone/pattern.rb', line 180
def merge_right(other)
combine_left(other) do |left, right|
merge_values(left, right)
end
end
|
#mod(other, structure: :both) ⇒ Object
166
167
168
|
# File 'lib/cyclotone/pattern.rb', line 166
def mod(other, structure: :both)
apply_binary_operation(other, structure: structure) { |left, right| combine_scalar(left, right, :%) }
end
|
#mul(other, structure: :both) ⇒ Object
158
159
160
|
# File 'lib/cyclotone/pattern.rb', line 158
def mul(other, structure: :both)
apply_binary_operation(other, structure: structure) { |left, right| combine_scalar(left, right, :*) }
end
|
#query_cycle(cycle_number) ⇒ Object
45
46
47
|
# File 'lib/cyclotone/pattern.rb', line 45
def query_cycle(cycle_number)
query_span(TimeSpan.new(cycle_number, Rational(cycle_number) + 1))
end
|
#query_event_at(time) ⇒ Object
49
50
51
52
53
54
|
# File 'lib/cyclotone/pattern.rb', line 49
def query_event_at(time)
sample_time = self.class.to_rational(time)
query_span(TimeSpan.new(sample_time, sample_time + self.class.sample_epsilon)).find do |event|
event.covers_time?(sample_time)
end
end
|
#query_point(time) ⇒ Object
56
57
58
|
# File 'lib/cyclotone/pattern.rb', line 56
def query_point(time)
query_event_at(time)&.value
end
|
#query_points(time) ⇒ Object
60
61
62
63
64
65
|
# File 'lib/cyclotone/pattern.rb', line 60
def query_points(time)
sample_time = self.class.to_rational(time)
query_span(TimeSpan.new(sample_time, sample_time + self.class.sample_epsilon)).select do |event|
event.covers_time?(sample_time)
end.map(&:value)
end
|
#query_span(span) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/cyclotone/pattern.rb', line 31
def query_span(span)
span = self.class.coerce_span(span)
emitted_cycle_span = false
events = []
span.each_cycle_span do |cycle_span|
emitted_cycle_span = true
events.concat(query.call(cycle_span))
end
events.concat(query.call(span)) if !emitted_cycle_span && continuous?
self.class.sort_events(events)
end
|
#scale(name, root: 0) ⇒ Object
203
204
205
|
# File 'lib/cyclotone/harmony.rb', line 203
def scale(name, root: 0)
Harmony.scale(name, self, root: root)
end
|
#select_events(&predicate) ⇒ Object
87
88
89
90
91
|
# File 'lib/cyclotone/pattern.rb', line 87
def select_events(&predicate)
Pattern.new(continuous: continuous?) do |span|
query_span(span).select { |event| predicate.call(event) }
end
end
|
#sub(other, structure: :both) ⇒ Object
154
155
156
|
# File 'lib/cyclotone/pattern.rb', line 154
def sub(other, structure: :both)
apply_binary_operation(other, structure: structure) { |left, right| combine_scalar(left, right, :-) }
end
|
#up(semitones) ⇒ Object
190
191
192
193
194
195
196
197
198
199
200
201
|
# File 'lib/cyclotone/harmony.rb', line 190
def up(semitones)
fmap do |value|
if value.is_a?(Hash) && value.key?(:note)
notes = value[:note].is_a?(Array) ? value[:note].map { |note| note + semitones.to_i } : value[:note] + semitones.to_i
value.merge(note: notes)
elsif value.is_a?(Array)
value.map { |note| note + semitones.to_i }
else
value + semitones.to_i
end
end
end
|