Module: Deftones::Effects::ModulationControl

Included in:
AutoFilter, AutoPanner, Chorus, Phaser, Tremolo, Vibrato
Defined in:
lib/deftones/effect/modulation_control.rb

Instance Method Summary collapse

Instance Method Details

#bipolar_modulation_value(phase, default: 0.0) ⇒ Object (private)



101
102
103
104
105
# File 'lib/deftones/effect/modulation_control.rb', line 101

def bipolar_modulation_value(phase, default: 0.0)
  return default if phase.nil?

  modulation_sample_for(phase)
end

#cancel_stopObject Also known as: cancelStop



26
27
28
29
30
# File 'lib/deftones/effect/modulation_control.rb', line 26

def cancel_stop
  clear_modulation_event(:stop)
  @modulation_stop_time = nil
  self
end

#clear_modulation_event(kind) ⇒ Object (private)



136
137
138
139
140
141
142
# File 'lib/deftones/effect/modulation_control.rb', line 136

def clear_modulation_event(kind)
  event_id = @modulation_transport_event_ids.delete(kind)
  return self unless event_id

  Deftones.transport.clear(event_id)
  self
end

#disposeObject



52
53
54
55
# File 'lib/deftones/effect/modulation_control.rb', line 52

def dispose
  unsync
  super
end

#initialize_modulation_controlObject (private)



61
62
63
64
65
66
# File 'lib/deftones/effect/modulation_control.rb', line 61

def initialize_modulation_control
  @modulation_start_time = Float::INFINITY
  @modulation_stop_time = nil
  @modulation_synced = false
  @modulation_transport_event_ids = {}
end

#modulation_active_at?(time) ⇒ Boolean (private)

Returns:

  • (Boolean)


107
108
109
# File 'lib/deftones/effect/modulation_control.rb', line 107

def modulation_active_at?(time)
  time >= @modulation_start_time && (@modulation_stop_time.nil? || time < @modulation_stop_time)
end

#modulation_frequencyObject (private)



76
77
78
# File 'lib/deftones/effect/modulation_control.rb', line 76

def modulation_frequency
  @frequency.to_f
end

#modulation_phase_for(current_time) ⇒ Object (private)



68
69
70
71
72
73
74
# File 'lib/deftones/effect/modulation_control.rb', line 68

def modulation_phase_for(current_time)
  return nil unless modulation_active_at?(current_time)

  current_phase = @phase
  @phase = (@phase + (modulation_frequency / context.sample_rate)) % 1.0
  current_phase
end

#modulation_sample_for(phase, type = modulation_type) ⇒ Object (private)



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/deftones/effect/modulation_control.rb', line 80

def modulation_sample_for(phase, type = modulation_type)
  normalized_phase = phase.to_f % 1.0

  case normalize_modulation_type(type)
  when :square
    normalized_phase < 0.5 ? 1.0 : -1.0
  when :triangle
    normalized_phase < 0.5 ? ((4.0 * normalized_phase) - 1.0) : (3.0 - (4.0 * normalized_phase))
  when :sawtooth
    (2.0 * normalized_phase) - 1.0
  else
    Math.sin(2.0 * Math::PI * normalized_phase)
  end
end

#modulation_typeObject (private)



144
145
146
147
148
# File 'lib/deftones/effect/modulation_control.rb', line 144

def modulation_type
  return :sine unless respond_to?(:type)

  normalize_modulation_type(type)
end

#normalize_modulation_type(type) ⇒ Object (private)

Raises:

  • (ArgumentError)


150
151
152
153
154
155
156
# File 'lib/deftones/effect/modulation_control.rb', line 150

def normalize_modulation_type(type)
  normalized = type.to_sym
  normalized = :sawtooth if normalized == :ramp
  return normalized if %i[sine square triangle sawtooth].include?(normalized)

  raise ArgumentError, "Unsupported modulation type: #{type}"
end

#resolve_modulation_time(time) ⇒ Object (private)



111
112
113
114
115
# File 'lib/deftones/effect/modulation_control.rb', line 111

def resolve_modulation_time(time)
  return context.current_time if time.nil?

  Deftones::Music::Time.parse(time)
end

#resolve_modulation_transport_time(time) ⇒ Object (private)



117
118
119
120
121
# File 'lib/deftones/effect/modulation_control.rb', line 117

def resolve_modulation_transport_time(time)
  return Deftones.transport.seconds if time.nil?

  time
end

#restart(time = nil) ⇒ Object



21
22
23
24
# File 'lib/deftones/effect/modulation_control.rb', line 21

def restart(time = nil)
  stop(time)
  start(time)
end

#schedule_modulation_event(kind, time) ⇒ Object (private)



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/deftones/effect/modulation_control.rb', line 123

def schedule_modulation_event(kind, time)
  clear_modulation_event(kind)
  @modulation_transport_event_ids[kind] = Deftones.transport.schedule(resolve_modulation_transport_time(time)) do |scheduled_time|
    if kind == :start
      @modulation_start_time = scheduled_time
      @modulation_stop_time = nil if @modulation_stop_time && @modulation_stop_time <= @modulation_start_time
    else
      @modulation_stop_time = scheduled_time
    end
  end
  self
end

#start(time = nil) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/deftones/effect/modulation_control.rb', line 6

def start(time = nil)
  return schedule_modulation_event(:start, time) if synced?

  @modulation_start_time = resolve_modulation_time(time)
  @modulation_stop_time = nil if @modulation_stop_time && @modulation_stop_time <= @modulation_start_time
  self
end

#state(time = context.current_time) ⇒ Object



32
33
34
# File 'lib/deftones/effect/modulation_control.rb', line 32

def state(time = context.current_time)
  modulation_active_at?(resolve_modulation_time(time)) ? :started : :stopped
end

#stop(time = nil) ⇒ Object



14
15
16
17
18
19
# File 'lib/deftones/effect/modulation_control.rb', line 14

def stop(time = nil)
  return schedule_modulation_event(:stop, time) if synced?

  @modulation_stop_time = resolve_modulation_time(time)
  self
end

#syncObject



36
37
38
39
# File 'lib/deftones/effect/modulation_control.rb', line 36

def sync
  @modulation_synced = true
  self
end

#synced?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/deftones/effect/modulation_control.rb', line 48

def synced?
  !!@modulation_synced
end

#unipolar_modulation_value(phase, default: 0.0) ⇒ Object (private)



95
96
97
98
99
# File 'lib/deftones/effect/modulation_control.rb', line 95

def unipolar_modulation_value(phase, default: 0.0)
  return default if phase.nil?

  (modulation_sample_for(phase) + 1.0) * 0.5
end

#unsyncObject



41
42
43
44
45
46
# File 'lib/deftones/effect/modulation_control.rb', line 41

def unsync
  @modulation_synced = false
  clear_modulation_event(:start)
  clear_modulation_event(:stop)
  self
end