Class: Deftones::Component::CrossFade

Inherits:
Deftones::Core::AudioNode show all
Defined in:
lib/deftones/component/cross_fade.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, #input_for_index, #inputs, #mix_source_blocks, #multichannel_process?, #name, #normalize_connection_index, #normalize_output_block, #now, #number_of_inputs, #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(fade: 0.5, context: Deftones.context) ⇒ CrossFade

Returns a new instance of CrossFade.



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

def initialize(fade: 0.5, context: Deftones.context)
  super(context: context)
  @a = Core::Gain.new(context: context)
  @b = Core::Gain.new(context: context)
  @fade = Core::Signal.new(value: fade, units: :number, context: context)
  @input = @a
  @output = self
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



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

def a
  @a
end

#bObject (readonly)

Returns the value of attribute b.



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

def b
  @b
end

#fadeObject

Returns the value of attribute fade.



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

def fade
  @fade
end

#inputObject (readonly)

Returns the value of attribute input.



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

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



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

def output
  @output
end

Instance Method Details

#leftObject



21
22
23
# File 'lib/deftones/component/cross_fade.rb', line 21

def left
  @a
end

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



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

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)

  fades = @fade.process(num_frames, start_frame)
  a_buffer = @a.render(num_frames, start_frame, cache)
  b_buffer = @b.render(num_frames, start_frame, cache)
  output_buffer = Array.new(num_frames) do |index|
    DSP::Helpers.mix(a_buffer[index], b_buffer[index], fades[index].clamp(0.0, 1.0))
  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
57
58
59
60
61
62
63
64
# File 'lib/deftones/component/cross_fade.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)

  fades = @fade.process(num_frames, start_frame)
  a_block = @a.send(:render_block, num_frames, start_frame, cache)
  b_block = @b.send(:render_block, num_frames, start_frame, cache)
  channels = [a_block.channels, b_block.channels].max
  mixed = Core::AudioBlock.from_channel_data(
    Array.new(channels) do |channel_index|
      a_channel = a_block.fit_channels(channels).channel(channel_index)
      b_channel = b_block.fit_channels(channels).channel(channel_index)
      Array.new(num_frames) do |index|
        DSP::Helpers.mix(a_channel[index], b_channel[index], fades[index].clamp(0.0, 1.0))
      end
    end
  )

  cache[cache_key] = mixed
  mixed.dup
end

#rightObject



25
26
27
# File 'lib/deftones/component/cross_fade.rb', line 25

def right
  @b
end