Module: Cyclotone::Transforms::Condition

Included in:
Pattern
Defined in:
lib/cyclotone/transforms/condition.rb

Instance Method Summary collapse

Instance Method Details

#contrast(true_function, false_function, control_pattern) ⇒ Object

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cyclotone/transforms/condition.rb', line 39

def contrast(true_function, false_function, control_pattern)
  raise ArgumentError, "contrast requires a true function" unless true_function.respond_to?(:call)
  raise ArgumentError, "contrast requires a false function" unless false_function.respond_to?(:call)

  true_pattern = true_function.call(self)
  false_pattern = false_function.call(self)

  Pattern.new do |span|
    query_span(span).map do |event|
      time = event.onset || event.part.start
      target_pattern = truthy?(Pattern.ensure_pattern(control_pattern).query_point(time)) ? true_pattern : false_pattern
      target_pattern.query_event_at(time)&.with_span(new_whole: event.whole, new_part: event.part) || event
    end
  end
end

#fix(control_pattern, &block) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cyclotone/transforms/condition.rb', line 16

def fix(control_pattern, &block)
  raise ArgumentError, "fix requires a block" unless block

  transformed = block.call(self)

  Pattern.new do |span|
    query_span(span).map do |event|
      time = event.onset || event.part.start
      control_value = Pattern.ensure_pattern(control_pattern).query_point(time)
      next event unless truthy?(control_value)

      transformed_event = transformed.query_event_at(time)
      transformed_event ? transformed_event.with_span(new_whole: event.whole, new_part: event.part) : event
    end
  end
end

#mask(bool_pattern) ⇒ Object



55
56
57
58
59
# File 'lib/cyclotone/transforms/condition.rb', line 55

def mask(bool_pattern)
  select_events do |event|
    truthy?(Pattern.ensure_pattern(bool_pattern).query_point(event.onset || event.part.start))
  end
end

#struct(bool_pattern) ⇒ Object



61
62
63
64
65
# File 'lib/cyclotone/transforms/condition.rb', line 61

def struct(bool_pattern)
  Pattern.ensure_pattern(bool_pattern).combine_left(self) do |gate, value|
    truthy?(gate) ? value : nil
  end.select_events { |event| !event.value.nil? }
end

#unfix(control_pattern, &block) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
# File 'lib/cyclotone/transforms/condition.rb', line 33

def unfix(control_pattern, &block)
  raise ArgumentError, "unfix requires a block" unless block

  contrast(block, proc { |pattern| pattern }, control_pattern)
end

#when_mod(period, minimum, &block) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
# File 'lib/cyclotone/transforms/condition.rb', line 6

def when_mod(period, minimum, &block)
  normalized_period = Pattern.to_rational(period)
  raise ArgumentError, "when_mod period must be positive" unless normalized_period.positive?
  raise ArgumentError, "when_mod requires a block" unless block

  Pattern.new do |span|
    (span.cycle_number % normalized_period) >= Pattern.to_rational(minimum) ? block.call(self).query_span(span) : query_span(span)
  end
end