Class: Awaaz::Utils::Soundread

Inherits:
Object
  • Object
show all
Defined in:
lib/awaaz/utils/soundread.rb

Overview

A helper that mimics librosa.load using libsndfile via FFI.

  • Always returns Float32 samples normalized in [-1.0, 1.0]

  • Preserves channel structure (returns shape ‘[channels, frames]`)

  • Returns ‘[data, channels, sr]` where:

    • ‘data` = Numo::SFloat array (2D, shape: channels x frames)

    • ‘channels` = Integer number of channels

    • ‘sr` = sample rate (Integer)

Examples:

reader = Awaaz::Utils::Soundread.new("audio.wav")
data, channels, sr = reader.read

Instance Method Summary collapse

Constructor Details

#initialize(filename, **resampling_options) ⇒ Soundread

Initializes a Soundread instance.

Parameters:

  • filename (String)

    Path to the audio file to read.

  • resampling_options (Hash)

    Optional resampling configuration.



26
27
28
29
# File 'lib/awaaz/utils/soundread.rb', line 26

def initialize(filename, **resampling_options)
  @filename = filename
  @resampling_options = resampling_options
end

Instance Method Details

#readArray<(Numo::SFloat, Integer, Integer)>

Reads the audio file, returning samples, number of channels, and sample rate.

Returns:

  • (Array<(Numo::SFloat, Integer, Integer)>)
    • data [Numo::SFloat] Audio samples, shape = ‘[channels, frames]`

    • channels [Integer] Number of channels

    • sr [Integer] Sample rate

Raises:

  • (ArgumentError)

    If the file cannot be opened.



41
42
43
44
45
46
47
48
49
50
# File 'lib/awaaz/utils/soundread.rb', line 41

def read
  info, sndfile = open_file
  frames, channels, sr = extract_info(info)

  buffer, read_frames = read_buffer(sndfile, frames, channels)
  close_file(sndfile)

  data = process_data(buffer, read_frames, channels)
  [resample(data, sr, channels), channels, sr]
end