Class: Wavify::Core::Stream::ProgressProcessor

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

Overview

Passive stream processor that reports cumulative processed frames.

Instance Method Summary collapse

Constructor Details

#initialize(callback, total_frames:) ⇒ ProgressProcessor

Returns a new instance of ProgressProcessor.



370
371
372
373
374
# File 'lib/wavify/core/stream.rb', line 370

def initialize(callback, total_frames:)
  @callback = callback
  @total_frames = total_frames
  @processed_frames = 0
end

Instance Method Details

#process(chunk) ⇒ Object



380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/wavify/core/stream.rb', line 380

def process(chunk)
  @processed_frames += chunk.sample_frame_count
  stats = {
    format: chunk.format,
    sample_frame_count: @processed_frames,
    duration: Duration.from_samples(@processed_frames, chunk.format.sample_rate)
  }
  stats[:total_frames] = @total_frames if @total_frames
  stats[:progress] = [@processed_frames.to_f / @total_frames, 1.0].min if @total_frames&.positive?
  @callback.call(stats)
  chunk
rescue StandardError => e
  raise UserCodeError, e
end

#resetObject



376
377
378
# File 'lib/wavify/core/stream.rb', line 376

def reset
  @processed_frames = 0
end