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, per_band: false) ⇒ 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

  • per_band (Boolean) (defaults to: false)

    true when band levels should track independent peaks



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

def initialize(window_size: DEFAULT_WINDOW_SIZE, target: DEFAULT_TARGET, floor: DEFAULT_FLOOR, per_band: false)
  @window_size = normalize_window_size(window_size)
  @target = normalize_unit(target, DEFAULT_TARGET)
  @floor = normalize_unit(floor, DEFAULT_FLOOR)
  @per_band = !!per_band
  @history = []
  @band_history = Hash.new { |history, key| history[key] = [] }
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



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vizcore/analysis/adaptive_normalizer.rb', line 28

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: normalize_bands(bands, gain),
    fft: scale_array(fft, gain),
    gain: gain
  }
end