Class: Deftones::Source::Noise

Inherits:
Core::Source show all
Defined in:
lib/deftones/source/noise.rb

Constant Summary collapse

TYPES =
%i[white pink brown].freeze

Instance Attribute Summary collapse

Attributes inherited from Core::Source

#mute, #onstop, #volume

Instance Method Summary collapse

Methods inherited from Core::Source

#active_at?, #apply_volume!, #cancel_stop, #clear_transport_event, #dispose, #mute?, #notify_stop_in_window, #number_of_inputs, #render, #render_block, #resolve_time, #resolve_transport_time, #restart, #schedule_transport_event, #source_type, #start, #state, #stop, #sync, #synced?, #unsync, #uses_legacy_render_for_block?

Constructor Details

#initialize(type: :white, playback_rate: 1.0, fade_in: 0.0, fade_out: 0.0, seed: nil, rng: nil, context: Deftones.context) ⇒ Noise

Returns a new instance of Noise.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/deftones/source/noise.rb', line 11

def initialize(type: :white, playback_rate: 1.0, fade_in: 0.0, fade_out: 0.0, seed: nil, rng: nil,
               context: Deftones.context)
  super(context: context)
  @playback_rate = playback_rate.to_f
  @fade_in = fade_in.to_f
  @fade_out = fade_out.to_f
  @rng = rng || (seed.nil? ? Random : Random.new(seed))
  reset_colored_state
  self.type = type
  @held_sample = next_noise_sample
  @playback_phase = 0.0
end

Instance Attribute Details

#fade_inObject Also known as: fadeIn

Returns the value of attribute fade_in.



8
9
10
# File 'lib/deftones/source/noise.rb', line 8

def fade_in
  @fade_in
end

#fade_outObject Also known as: fadeOut

Returns the value of attribute fade_out.



8
9
10
# File 'lib/deftones/source/noise.rb', line 8

def fade_out
  @fade_out
end

#playback_rateObject Also known as: playbackRate

Returns the value of attribute playback_rate.



9
10
11
# File 'lib/deftones/source/noise.rb', line 9

def playback_rate
  @playback_rate
end

#typeObject

Returns the value of attribute type.



9
10
11
# File 'lib/deftones/source/noise.rb', line 9

def type
  @type
end

Instance Method Details

#advance_playbackObject (private)



59
60
61
62
63
64
65
66
# File 'lib/deftones/source/noise.rb', line 59

def advance_playback
  @playback_phase += [@playback_rate, 1.0e-6].max
  steps = @playback_phase.floor
  return if steps <= 0

  steps.times { @held_sample = next_noise_sample }
  @playback_phase -= steps
end

#brown_noise_sample(white) ⇒ Object (private)



110
111
112
113
# File 'lib/deftones/source/noise.rb', line 110

def brown_noise_sample(white)
  @brown_state = (@brown_state + (0.02 * white)) / 1.02
  Deftones::DSP::Helpers.clamp(@brown_state * 3.5, -1.0, 1.0)
end

#envelope_gain(current_time) ⇒ Object (private)



68
69
70
# File 'lib/deftones/source/noise.rb', line 68

def envelope_gain(current_time)
  fade_in_gain(current_time) * fade_out_gain(current_time)
end

#fade_in_gain(current_time) ⇒ Object (private)



72
73
74
75
76
# File 'lib/deftones/source/noise.rb', line 72

def fade_in_gain(current_time)
  return 1.0 if @fade_in <= 0.0

  ((current_time - @start_time) / @fade_in).clamp(0.0, 1.0)
end

#fade_out_gain(current_time) ⇒ Object (private)



78
79
80
81
82
# File 'lib/deftones/source/noise.rb', line 78

def fade_out_gain(current_time)
  return 1.0 unless @stop_time && @fade_out > 0.0

  ((@stop_time - current_time) / @fade_out).clamp(0.0, 1.0)
end

#next_noise_sampleObject (private)



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/deftones/source/noise.rb', line 84

def next_noise_sample
  white = (@rng.rand * 2.0) - 1.0

  case normalize_type(@type)
  when :white
    white
  when :pink
    pink_noise_sample(white)
  when :brown
    brown_noise_sample(white)
  end
end

#next_sampleObject (private)



53
54
55
56
57
# File 'lib/deftones/source/noise.rb', line 53

def next_sample
  sample = @held_sample
  advance_playback
  sample
end

#normalize_type(type) ⇒ Object (private)

Raises:

  • (ArgumentError)


120
121
122
123
124
125
# File 'lib/deftones/source/noise.rb', line 120

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

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

#pink_noise_sample(white) ⇒ Object (private)



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/deftones/source/noise.rb', line 97

def pink_noise_sample(white)
  @pink_state[0] = (0.99886 * @pink_state[0]) + (white * 0.0555179)
  @pink_state[1] = (0.99332 * @pink_state[1]) + (white * 0.0750759)
  @pink_state[2] = (0.96900 * @pink_state[2]) + (white * 0.1538520)
  @pink_state[3] = (0.86650 * @pink_state[3]) + (white * 0.3104856)
  @pink_state[4] = (0.55000 * @pink_state[4]) + (white * 0.5329522)
  @pink_state[5] = (-0.7616 * @pink_state[5]) - (white * 0.0168980)

  sample = @pink_state[0..6].sum + (white * 0.5362)
  @pink_state[6] = white * 0.115926
  Deftones::DSP::Helpers.clamp(sample * 0.11, -1.0, 1.0)
end

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



37
38
39
40
41
42
43
44
# File 'lib/deftones/source/noise.rb', line 37

def process(_input_buffer, num_frames, start_frame, _cache)
  Array.new(num_frames) do |index|
    current_time = (start_frame + index).to_f / context.sample_rate
    next 0.0 unless active_at?(current_time)

    next_sample * envelope_gain(current_time)
  end
end

#reset_colored_stateObject (private)



115
116
117
118
# File 'lib/deftones/source/noise.rb', line 115

def reset_colored_state
  @pink_state = Array.new(7, 0.0)
  @brown_state = 0.0
end