class PulseAnalysis::File

An audio file

Attributes

file[R]
num_channels[R]
size[R]

Public Class Methods

new(file_or_path) click to toggle source

@param [::File, String] file_or_path

# File lib/pulse-analysis/file.rb, line 13
def initialize(file_or_path)
  @file = file_or_path.kind_of?(::File) ? file_or_path : ::File.new(file_or_path)
  @sound = RubyAudio::Sound.open(@file)
  @size = ::File.size(@file)
  @num_channels = @sound.info.channels
end

Public Instance Methods

read(options = {}) click to toggle source

Read the audio file into memory @param [Hash] options @option options [IO] :logger @return [Array<Array<Float>>, Array<Float>] File data

# File lib/pulse-analysis/file.rb, line 30
def read(options = {})
  if logger = options[:logger]
    logger.puts("Reading audio file #{@file}")
  end
  buffer = RubyAudio::Buffer.float(@size, @num_channels)
  begin
    @sound.seek(0)
    @sound.read(buffer, @size)
    data = buffer.to_a
  rescue RubyAudio::Error
  end
  logger.puts("Finished reading audio file #{@file}") if logger
  data
end
sample_rate() click to toggle source

The sample rate of the audio file @return [Integer]

# File lib/pulse-analysis/file.rb, line 22
def sample_rate
  @sample_rate ||= @sound.info.samplerate
end