Class: Deftones::Component::Merge

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

Instance Attribute Summary collapse

Attributes inherited from Deftones::Core::AudioNode

#context

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, #inputs, #mix_source_blocks, #multichannel_process?, #name, #normalize_connection_index, #normalize_output_block, #now, #number_of_outputs, #output_for_connection, #output_for_index, #outputs, #process, #raise_connection_index_error!, #reaches_node?, #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(context: Deftones.context) ⇒ Merge

Returns a new instance of Merge.



8
9
10
11
12
13
14
# File 'lib/deftones/component/merge.rb', line 8

def initialize(context: Deftones.context)
  super(context: context)
  @left = Core::Gain.new(context: context)
  @right = Core::Gain.new(context: context)
  @input = @left
  @output = self
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



6
7
8
# File 'lib/deftones/component/merge.rb', line 6

def input
  @input
end

#leftObject (readonly)

Returns the value of attribute left.



6
7
8
# File 'lib/deftones/component/merge.rb', line 6

def left
  @left
end

#outputObject (readonly)

Returns the value of attribute output.



6
7
8
# File 'lib/deftones/component/merge.rb', line 6

def output
  @output
end

#rightObject (readonly)

Returns the value of attribute right.



6
7
8
# File 'lib/deftones/component/merge.rb', line 6

def right
  @right
end

Instance Method Details

#input_for_index(index) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/deftones/component/merge.rb', line 22

def input_for_index(index)
  case index
  when 0 then @left
  when 1 then @right
  else raise_connection_index_error!(:input_index, index, number_of_inputs)
  end
end

#number_of_inputsObject Also known as: numberOfInputs



16
17
18
# File 'lib/deftones/component/merge.rb', line 16

def number_of_inputs
  2
end

#render(num_frames, start_frame = 0, cache = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/deftones/component/merge.rb', line 30

def render(num_frames, start_frame = 0, cache = {})
  cache_key = [object_id, start_frame, num_frames]
  return cache.fetch(cache_key).dup if cache.key?(cache_key)

  left_buffer = @left.render(num_frames, start_frame, cache)
  right_buffer = @right.render(num_frames, start_frame, cache)
  output_buffer = Array.new(num_frames) do |index|
    (left_buffer[index] + right_buffer[index]) * 0.5
  end

  cache[cache_key] = output_buffer
  output_buffer.dup
end

#render_block(num_frames, start_frame = 0, cache = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/deftones/component/merge.rb', line 44

def render_block(num_frames, start_frame = 0, cache = {})
  cache_key = [object_id, :block, start_frame, num_frames]
  return cache.fetch(cache_key).dup if cache.key?(cache_key)

  left_block = @left.send(:render_block, num_frames, start_frame, cache)
  right_block = @right.send(:render_block, num_frames, start_frame, cache)
  output_block = Core::AudioBlock.from_channel_data([
    left_block.mono,
    right_block.mono
  ])
  cache[cache_key] = output_block
  output_block.dup
end