Class: Vizcore::Audio::MicInput

Inherits:
BaseInput show all
Defined in:
lib/vizcore/audio/mic_input.rb

Overview

Microphone input using PortAudio, with automatic fallback to silence.

Instance Attribute Summary collapse

Attributes inherited from BaseInput

#sample_rate

Instance Method Summary collapse

Methods inherited from BaseInput

#running?

Constructor Details

#initialize(device: :default, sample_rate: 44_100, fallback_input: nil, portaudio_backend: PortAudioFFI, channels: 1, frames_per_buffer: 1024) ⇒ MicInput

Returns a new instance of MicInput.

Parameters:

  • device (Symbol, String) (defaults to: :default)
  • sample_rate (Integer) (defaults to: 44_100)
  • fallback_input (Vizcore::Audio::BaseInput, nil) (defaults to: nil)
  • portaudio_backend (Module) (defaults to: PortAudioFFI)
  • channels (Integer) (defaults to: 1)
  • frames_per_buffer (Integer) (defaults to: 1024)


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vizcore/audio/mic_input.rb', line 19

def initialize(device: :default, sample_rate: 44_100, fallback_input: nil, portaudio_backend: PortAudioFFI, channels: 1, frames_per_buffer: 1024)
  super(sample_rate: sample_rate)
  @device = device
  @channels = Integer(channels)
  @frames_per_buffer = Integer(frames_per_buffer)
  @fallback_input = fallback_input || BaseInput.new(sample_rate: sample_rate)
  @portaudio_backend = portaudio_backend
  @stream = nil
  @using_fallback = false
  @last_error = nil
end

Instance Attribute Details

#deviceObject (readonly)

Returns the value of attribute device.



11
12
13
# File 'lib/vizcore/audio/mic_input.rb', line 11

def device
  @device
end

#last_errorObject (readonly)

Returns the value of attribute last_error.



11
12
13
# File 'lib/vizcore/audio/mic_input.rb', line 11

def last_error
  @last_error
end

Instance Method Details

#read(frame_size) ⇒ Array<Float>

Returns microphone frame or fallback samples.

Parameters:

  • frame_size (Integer)

Returns:

  • (Array<Float>)

    microphone frame or fallback samples



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vizcore/audio/mic_input.rb', line 52

def read(frame_size)
  count = Integer(frame_size)
  return Array.new(count, 0.0) unless running?

  if @stream
    samples = @stream.read(count)
    @last_error = nil
    normalize_samples(samples, count)
  else
    @fallback_input.read(count)
  end
rescue StandardError => e
  @last_error = AudioSourceError.new("Microphone read failed: #{e.message}")
  switch_to_fallback
  @fallback_input.read(count)
end

#startVizcore::Audio::MicInput



32
33
34
35
36
37
38
39
40
# File 'lib/vizcore/audio/mic_input.rb', line 32

def start
  super
  @using_fallback = false

  @stream = open_stream
  @using_fallback = !@stream
  @fallback_input.start if @using_fallback
  self
end

#stopVizcore::Audio::MicInput



43
44
45
46
47
48
# File 'lib/vizcore/audio/mic_input.rb', line 43

def stop
  close_stream
  @fallback_input.stop if @using_fallback
  @using_fallback = false
  super
end

#using_fallback?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/vizcore/audio/mic_input.rb', line 70

def using_fallback?
  @using_fallback
end