Class: Vizcore::Analysis::AdaptiveNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/vizcore/analysis/adaptive_normalizer.rb

Overview

Scales audio features against a rolling amplitude peak for repeatable mappings.

Constant Summary collapse

DEFAULT_WINDOW_SIZE =
128
DEFAULT_TARGET =
0.85
DEFAULT_FLOOR =
0.05

Instance Method Summary collapse

Constructor Details

#initialize(window_size: DEFAULT_WINDOW_SIZE, target: DEFAULT_TARGET, floor: DEFAULT_FLOOR) ⇒ AdaptiveNormalizer

Returns a new instance of AdaptiveNormalizer.

Parameters:

  • window_size (Integer) (defaults to: DEFAULT_WINDOW_SIZE)

    number of recent active frames used to track the peak

  • target (Numeric) (defaults to: DEFAULT_TARGET)

    desired level for the rolling peak

  • floor (Numeric) (defaults to: DEFAULT_FLOOR)

    minimum peak level used when calculating gain



14
15
16
17
18
19
# File 'lib/vizcore/analysis/adaptive_normalizer.rb', line 14

def initialize(window_size: DEFAULT_WINDOW_SIZE, target: DEFAULT_TARGET, floor: DEFAULT_FLOOR)
  @window_size = normalize_window_size(window_size)
  @target = normalize_unit(target, DEFAULT_TARGET)
  @floor = normalize_unit(floor, DEFAULT_FLOOR)
  @history = []
end

Instance Method Details

#call(amplitude:, bands:, fft:) ⇒ Hash

Returns normalized feature values plus the applied gain.

Parameters:

  • amplitude (Numeric)

    current RMS amplitude

  • bands (Hash)

    current frequency band values

  • fft (Array<Numeric>)

    current FFT preview values

Returns:

  • (Hash)

    normalized feature values plus the applied gain



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vizcore/analysis/adaptive_normalizer.rb', line 25

def call(amplitude:, bands:, fft:)
  current_amplitude = normalize_unit(amplitude, 0.0)
  @history << current_amplitude
  @history.shift while @history.length > @window_size

  gain = @target / [@history.max.to_f, @floor].max
  {
    amplitude: scale_value(current_amplitude, gain),
    bands: scale_hash(bands, gain),
    fft: scale_array(fft, gain),
    gain: gain
  }
end