Class: Vizcore::Audio::MicInput
- Defined in:
- lib/vizcore/audio/mic_input.rb
Overview
Microphone input using PortAudio, with automatic fallback to silence.
Instance Attribute Summary collapse
-
#device ⇒ Object
readonly
Returns the value of attribute device.
-
#last_error ⇒ Object
readonly
Returns the value of attribute last_error.
Attributes inherited from BaseInput
Instance Method Summary collapse
-
#initialize(device: :default, sample_rate: 44_100, fallback_input: nil, portaudio_backend: PortAudioFFI, channels: 1, frames_per_buffer: 1024) ⇒ MicInput
constructor
A new instance of MicInput.
-
#read(frame_size) ⇒ Array<Float>
Microphone frame or fallback samples.
- #start ⇒ Vizcore::Audio::MicInput
- #stop ⇒ Vizcore::Audio::MicInput
- #using_fallback? ⇒ Boolean
Methods inherited from BaseInput
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.
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
#device ⇒ Object (readonly)
Returns the value of attribute device.
11 12 13 |
# File 'lib/vizcore/audio/mic_input.rb', line 11 def device @device end |
#last_error ⇒ Object (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.
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.}") switch_to_fallback @fallback_input.read(count) end |
#start ⇒ Vizcore::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 |
#stop ⇒ Vizcore::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
70 71 72 |
# File 'lib/vizcore/audio/mic_input.rb', line 70 def using_fallback? @using_fallback end |