Module: Muze::IO::AudioLoader::WavifyBackend

Defined in:
lib/muze/io/audio_loader/wavify_backend.rb

Overview

WAV backend implemented with wavify's pure-Ruby WAV codec surface.

Constant Summary collapse

SUPPORTED_EXTENSIONS =
%w[.wav .wave].freeze

Class Method Summary collapse

Class Method Details

.applies_time_window?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/muze/io/audio_loader/wavify_backend.rb', line 31

def applies_time_window?
  true
end

.available?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/muze/io/audio_loader/wavify_backend.rb', line 26

def available?
  true
end

.info(path) ⇒ Hash

Parameters:

  • path (String)

Returns:

  • (Hash)


70
71
72
73
74
75
76
77
78
79
# File 'lib/muze/io/audio_loader/wavify_backend.rb', line 70

def info(path)
   = Wavify::Codecs::Wav.(path)
  format = .fetch(:format)
  {
    sample_rate: format.sample_rate,
    channels: format.channels,
    duration: .fetch(:duration).total_seconds,
    format: format_label(path)
  }
end

.installation_message(extension) ⇒ String

Parameters:

  • extension (String)

Returns:

  • (String)


37
38
39
# File 'lib/muze/io/audio_loader/wavify_backend.rb', line 37

def installation_message(extension)
  "Unable to load #{extension.delete_prefix('.')} because the WAV backend is unavailable."
end

.read(path, offset: 0.0, duration: nil) ⇒ Array(Array<Float>, Integer, Integer)

Parameters:

  • path (String)
  • offset (Float) (defaults to: 0.0)
  • duration (Float, nil) (defaults to: nil)

Returns:

  • (Array(Array<Float>, Integer, Integer))


45
46
47
48
49
50
51
52
# File 'lib/muze/io/audio_loader/wavify_backend.rb', line 45

def read(path, offset: 0.0, duration: nil)
   = Wavify::Codecs::Wav.(path)
  source_format = .fetch(:format)
  float_format = source_format.with(sample_format: :float, bit_depth: 32)
  samples = stream_samples(path, float_format:, offset:, duration:)

  [samples_from_interleaved(samples, float_format.channels), float_format.sample_rate, float_format.channels]
end

.read_stream(path, chunk_frames:, offset: 0.0, duration: nil) {|samples, sample_rate, channels| ... } ⇒ void

This method returns an undefined value.

Yield Parameters:

  • samples (Array<Float>, Array<Array<Float>>)
  • sample_rate (Integer)
  • channels (Integer)


58
59
60
61
62
63
64
65
66
# File 'lib/muze/io/audio_loader/wavify_backend.rb', line 58

def read_stream(path, chunk_frames:, offset: 0.0, duration: nil)
   = Wavify::Codecs::Wav.(path)
  source_format = .fetch(:format)
  float_format = source_format.with(sample_format: :float, bit_depth: 32)

  stream_sample_chunks(path, float_format:, offset:, duration:, chunk_frames:) do |samples|
    yield samples_from_interleaved(samples, float_format.channels), float_format.sample_rate, float_format.channels
  end
end

.supported_extension?(extension) ⇒ Boolean

Parameters:

  • extension (String)

Returns:

  • (Boolean)


21
22
23
# File 'lib/muze/io/audio_loader/wavify_backend.rb', line 21

def supported_extension?(extension)
  SUPPORTED_EXTENSIONS.include?(extension)
end