Class: Vizcore::Audio::Calibration

Inherits:
Object
  • Object
show all
Defined in:
lib/vizcore/audio/calibration.rb

Overview

Samples an audio input and derives practical level/noise-gate metrics.

Defined Under Namespace

Classes: Result

Constant Summary collapse

DEFAULT_DURATION =
3.0
DEFAULT_FPS =
20.0

Instance Method Summary collapse

Constructor Details

#initialize(source: :mic, file_path: nil, audio_device: nil, duration: DEFAULT_DURATION, fps: DEFAULT_FPS, input_manager_factory: nil, sleeper: ->(seconds) { sleep(seconds) }) ⇒ Calibration

Returns a new instance of Calibration.

Parameters:

  • source (String, Symbol) (defaults to: :mic)
  • file_path (String, nil) (defaults to: nil)
  • audio_device (String, nil) (defaults to: nil)
  • duration (Numeric) (defaults to: DEFAULT_DURATION)
  • fps (Numeric) (defaults to: DEFAULT_FPS)
  • input_manager_factory (#call) (defaults to: nil)
  • sleeper (#call) (defaults to: ->(seconds) { sleep(seconds) })


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vizcore/audio/calibration.rb', line 46

def initialize(
  source: :mic,
  file_path: nil,
  audio_device: nil,
  duration: DEFAULT_DURATION,
  fps: DEFAULT_FPS,
  input_manager_factory: nil,
  sleeper: ->(seconds) { sleep(seconds) }
)
  @source = source.to_sym
  @file_path = file_path
  @audio_device = audio_device
  @duration = positive_float(duration, "duration")
  @fps = positive_float(fps, "fps")
  @input_manager_factory = input_manager_factory || method(:build_input_manager)
  @sleeper = sleeper
end

Instance Method Details

#callResult

Returns:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vizcore/audio/calibration.rb', line 65

def call
  manager = @input_manager_factory.call(source: @source, file_path: @file_path, audio_device: @audio_device)
  rms_values = []
  peak_values = []
  manager.start
  frame_count.times do
    samples = manager.capture_frame(manager.realtime_capture_size(@fps))
    rms_values << rms(samples)
    peak_values << peak(samples)
    @sleeper.call(1.0 / @fps) if @source == :mic
  end

  build_result(manager, rms_values, peak_values)
ensure
  manager&.stop
end