Class: Deftones::Core::WaveShaper
- Inherits:
-
ComputedSignal
- Object
- Signal
- ComputedSignal
- Deftones::Core::WaveShaper
- Defined in:
- lib/deftones/core/signal_shapers.rb
Constant Summary
Constants inherited from Signal
Instance Attribute Summary
Attributes inherited from ComputedSignal
Attributes inherited from Signal
#clamp_values, #context, #convert_values, #default_value, #input, #max_value, #min_value, #output, #units
Instance Method Summary collapse
-
#initialize(input:, curve: nil, mapping: nil, context: nil, &block) ⇒ WaveShaper
constructor
A new instance of WaveShaper.
- #normalize_curve(curve) ⇒ Object private
- #sample_curve(sample) ⇒ Object private
- #value_at(time) ⇒ Object
Methods inherited from ComputedSignal
#coerce_signal, #default_units, #exponentiate, #integer_like?, #process, #ramp_to, #resolve_context, #value, #value=
Methods inherited from Signal
#add_event, #apply, #automation_anchor_time, #automation_event_count, #automation_events?, #bounded_value, #cancel_and_hold_at_time, #cancel_scheduled_values, #coerce_value, #connect, #convert, #convert=, #convert_frequency, #convert_generic, #convert_without_units, #curve_value, #db_to_gain, #disconnect, #dispose, #disposed?, #event_sort_key, #exponential_approach_value_at_time, #exponential_ramp_to, #exponential_ramp_to_value_at_time, #get, #get_defaults, #get_value_at_time, #immediate, #insert_event, #interpolate, #linear_ramp_to, #linear_ramp_to_value_at_time, #name, #now, #overridden?, #process, #ramp_to, #resolve_automation_start_time, #resolve_time, #safe_exponential_value, #sample_time, #schedule_automation, #set, #set_ramp_point, #set_target_at_time, #set_value_at_time, #set_value_curve_at_time, #target_ramp_to, #target_value, #to_frequency, #to_s, #to_seconds, #to_ticks, #value, #value=
Methods included from SignalOperatorMethods
#abs, #add, #equal_power_gain, #greater_than, #greater_than_zero, #modulo, #multiply, #negate, #normalize, #pow, #scale, #scale_exp, #subtract, #to_audio, #to_gain, #wave_shaper
Constructor Details
#initialize(input:, curve: nil, mapping: nil, context: nil, &block) ⇒ WaveShaper
Returns a new instance of WaveShaper.
47 48 49 50 51 52 |
# File 'lib/deftones/core/signal_shapers.rb', line 47 def initialize(input:, curve: nil, mapping: nil, context: nil, &block) super(input: input, units: :number, context: context) @mapping = mapping || block @curve = normalize_curve(curve) raise ArgumentError, "curve or mapping is required" unless @mapping || @curve end |
Instance Method Details
#normalize_curve(curve) ⇒ Object (private)
63 64 65 66 67 68 69 70 |
# File 'lib/deftones/core/signal_shapers.rb', line 63 def normalize_curve(curve) return if curve.nil? values = curve.to_a.map(&:to_f) raise ArgumentError, "curve must contain at least two points" if values.length < 2 values.freeze end |
#sample_curve(sample) ⇒ Object (private)
72 73 74 75 76 77 78 79 80 |
# File 'lib/deftones/core/signal_shapers.rb', line 72 def sample_curve(sample) position = ((sample.clamp(-1.0, 1.0) + 1.0) * 0.5) * (@curve.length - 1) index = position.floor upper_index = [index + 1, @curve.length - 1].min lower = @curve[index] upper = @curve[upper_index] lower + ((upper - lower) * (position - index)) end |
#value_at(time) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/deftones/core/signal_shapers.rb', line 54 def value_at(time) sample = @input.value_at(time) return @mapping.call(sample, time) if @mapping sample_curve(sample) end |