Class: Vizcore::Audio::FileInput
- Defined in:
- lib/vizcore/audio/file_input.rb
Overview
File-backed audio input for WAV and ffmpeg-decoded formats.
Constant Summary collapse
- SUPPORTED_EXTENSIONS =
Supported file extensions.
%w[.wav .mp3 .flac].freeze
Instance Attribute Summary collapse
-
#last_error ⇒ Object
readonly
Returns the value of attribute last_error.
-
#stream_sample_rate ⇒ Object
readonly
Returns the value of attribute stream_sample_rate.
Attributes inherited from BaseInput
Instance Method Summary collapse
-
#initialize(path:, sample_rate: 44_100, command_runner: Open3, ffmpeg_checker: nil) ⇒ FileInput
constructor
A new instance of FileInput.
-
#read(frame_size) ⇒ Array<Float>
File samples (looped), or silence when unavailable.
-
#sync_transport(playing:, position_seconds:) ⇒ Vizcore::Audio::FileInput
Synchronize file cursor with an external playback transport (browser audio element).
Methods inherited from BaseInput
Constructor Details
#initialize(path:, sample_rate: 44_100, command_runner: Open3, ffmpeg_checker: nil) ⇒ FileInput
Returns a new instance of FileInput.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/vizcore/audio/file_input.rb', line 21 def initialize(path:, sample_rate: 44_100, command_runner: Open3, ffmpeg_checker: nil) super(sample_rate: sample_rate) @path = path @command_runner = command_runner @ffmpeg_checker = ffmpeg_checker || method(:ffmpeg_available?) @cursor = 0 @last_error = nil @stream_sample_rate = sample_rate @state_mutex = Mutex.new @transport_paused = false @samples = load_samples end |
Instance Attribute Details
#last_error ⇒ Object (readonly)
Returns the value of attribute last_error.
14 15 16 |
# File 'lib/vizcore/audio/file_input.rb', line 14 def last_error @last_error end |
#stream_sample_rate ⇒ Object (readonly)
Returns the value of attribute stream_sample_rate.
15 16 17 |
# File 'lib/vizcore/audio/file_input.rb', line 15 def stream_sample_rate @stream_sample_rate end |
Instance Method Details
#read(frame_size) ⇒ Array<Float>
Returns file samples (looped), or silence when unavailable.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/vizcore/audio/file_input.rb', line 36 def read(frame_size) count = Integer(frame_size) return Array.new(count, 0.0) unless running? return Array.new(count, 0.0) if @samples.empty? @state_mutex.synchronize do return Array.new(count, 0.0) if @transport_paused Array.new(count) do sample = @samples[@cursor] @cursor = (@cursor + 1) % @samples.length sample end end end |
#sync_transport(playing:, position_seconds:) ⇒ Vizcore::Audio::FileInput
Synchronize file cursor with an external playback transport (browser audio element).
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/vizcore/audio/file_input.rb', line 57 def sync_transport(playing:, position_seconds:) return self if @samples.empty? seconds = Float(position_seconds) @state_mutex.synchronize do @transport_paused = ! @cursor = seconds_to_cursor(seconds) end self rescue StandardError self end |