Class: Deftones::Component::OnePoleFilter

Inherits:
Deftones::Core::AudioNode show all
Defined in:
lib/deftones/component/one_pole_filter.rb

Constant Summary collapse

TYPES =
%i[lowpass highpass].freeze

Instance Attribute Summary collapse

Attributes inherited from Deftones::Core::AudioNode

#context, #input

Instance Method Summary collapse

Methods inherited from Deftones::Core::AudioNode

#>>, #attach_destination, #attach_source, #block_time, #chain, #channel_count, #channel_count_mode, #channel_interpretation, #connect, #connected?, #default_input_channels, #default_output_channels, #destination_for_connection, #detach_all_destinations, #detach_destination, #detach_source, #disconnect, #dispose, #disposed?, #fan, #get, #immediate, #input_for_index, #inputs, #mix_source_blocks, #name, #normalize_connection_index, #normalize_output_block, #now, #number_of_inputs, #number_of_outputs, #output, #output_for_connection, #output_for_index, #outputs, #raise_connection_index_error!, #reaches_node?, #render, #render_block, #sample_time, #set, #to_destination, #to_frequency, #to_master, #to_midi, #to_output, #to_s, #to_seconds, #to_ticks, #uses_legacy_render_for_block?, #validate_connectable!, #validate_connection_index!

Constructor Details

#initialize(frequency: 880.0, type: :lowpass, context: Deftones.context) ⇒ OnePoleFilter

Returns a new instance of OnePoleFilter.



10
11
12
13
14
15
# File 'lib/deftones/component/one_pole_filter.rb', line 10

def initialize(frequency: 880.0, type: :lowpass, context: Deftones.context)
  super(context: context)
  @frequency = Core::Signal.new(value: frequency, units: :frequency, context: context)
  @lowpass_state = []
  self.type = type
end

Instance Attribute Details

#frequencyObject

Returns the value of attribute frequency.



8
9
10
# File 'lib/deftones/component/one_pole_filter.rb', line 8

def frequency
  @frequency
end

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/deftones/component/one_pole_filter.rb', line 8

def type
  @type
end

Instance Method Details

#coefficient_for(frequency) ⇒ Object (private)



60
61
62
63
# File 'lib/deftones/component/one_pole_filter.rb', line 60

def coefficient_for(frequency)
  normalized = [[frequency.to_f, 1.0].max, (context.sample_rate * 0.49)].min
  Math.exp((-2.0 * Math::PI * normalized) / context.sample_rate)
end

#ensure_state(channels) ⇒ Object (private)



72
73
74
75
# File 'lib/deftones/component/one_pole_filter.rb', line 72

def ensure_state(channels)
  required = [channels.to_i, 1].max
  @lowpass_state.fill(0.0, @lowpass_state.length...required)
end

#multichannel_process?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/deftones/component/one_pole_filter.rb', line 29

def multichannel_process?
  true
end

#normalize_type(type) ⇒ Object (private)

Raises:

  • (ArgumentError)


65
66
67
68
69
70
# File 'lib/deftones/component/one_pole_filter.rb', line 65

def normalize_type(type)
  normalized = type.to_sym
  return normalized if TYPES.include?(normalized)

  raise ArgumentError, "Unsupported one pole filter type: #{type}"
end

#process(input_block, num_frames, start_frame, _cache) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/deftones/component/one_pole_filter.rb', line 33

def process(input_block, num_frames, start_frame, _cache)
  frequencies = @frequency.process(num_frames, start_frame)
  ensure_state(input_block.channels)
  output_channels = input_block.channel_data.each_with_index.map do |channel, channel_index|
    Array.new(num_frames) do |index|
      input_sample = channel[index]
      coefficient = coefficient_for(frequencies[index])
      @lowpass_state[channel_index] += (1.0 - coefficient) * (input_sample - @lowpass_state[channel_index])

      if normalize_type(@type) == :lowpass
        @lowpass_state[channel_index]
      else
        input_sample - @lowpass_state[channel_index]
      end
    end
  end

  Core::AudioBlock.from_channel_data(output_channels)
end

#reset!Object



53
54
55
56
# File 'lib/deftones/component/one_pole_filter.rb', line 53

def reset!
  @lowpass_state = []
  self
end