Class: Wavify::Core::Stream::MeterProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/wavify/core/stream.rb

Overview

Passive stream processor that reports per-chunk audio levels.

Instance Method Summary collapse

Constructor Details

#initialize(callback) ⇒ MeterProcessor

Returns a new instance of MeterProcessor.



328
329
330
# File 'lib/wavify/core/stream.rb', line 328

def initialize(callback)
  @callback = callback
end

Instance Method Details

#process(chunk) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/wavify/core/stream.rb', line 332

def process(chunk)
  float_chunk = chunk.convert(chunk.format.with(sample_format: :float, bit_depth: 32))
  peak = 0.0
  square_sum = 0.0
  float_chunk.samples.each do |sample|
    peak = [peak, sample.abs].max
    square_sum += sample * sample
  end
  rms = if float_chunk.samples.empty?
          0.0
        else
          Math.sqrt(square_sum / float_chunk.samples.length)
        end
  @callback.call(
    format: chunk.format,
    sample_frame_count: chunk.sample_frame_count,
    duration: chunk.duration,
    peak_amplitude: peak,
    rms_amplitude: rms,
    peak_dbfs: amplitude_to_dbfs(peak),
    rms_dbfs: amplitude_to_dbfs(rms)
  )
  chunk
rescue StandardError => e
  raise UserCodeError, e
end