Class: Vizcore::Audio::FileInput

Inherits:
BaseInput show all
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

Attributes inherited from BaseInput

#sample_rate

Instance Method Summary collapse

Methods inherited from BaseInput

#running?, #start, #stop

Constructor Details

#initialize(path:, sample_rate: 44_100, command_runner: Open3, ffmpeg_checker: nil) ⇒ FileInput

Returns a new instance of FileInput.

Parameters:

  • path (String, Pathname)
  • sample_rate (Integer) (defaults to: 44_100)
  • command_runner (#capture3) (defaults to: Open3)
  • ffmpeg_checker (#call, nil) (defaults to: nil)


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_errorObject (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_rateObject (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.

Parameters:

  • frame_size (Integer)

Returns:

  • (Array<Float>)

    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).

Parameters:

  • playing (Boolean)
  • position_seconds (Numeric)

Returns:



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 = !playing
    @cursor = seconds_to_cursor(seconds)
  end
  self
rescue StandardError
  self
end