Class: PulseAnalysis::File

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pulse-analysis/file.rb

Overview

An audio file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_or_path) ⇒ File

Returns a new instance of File.

Parameters:

  • file_or_path (::File, String)


13
14
15
16
17
18
# 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

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



8
9
10
# File 'lib/pulse-analysis/file.rb', line 8

def file
  @file
end

#num_channelsObject (readonly)

Returns the value of attribute num_channels.



8
9
10
# File 'lib/pulse-analysis/file.rb', line 8

def num_channels
  @num_channels
end

#sizeObject (readonly)

Returns the value of attribute size.



8
9
10
# File 'lib/pulse-analysis/file.rb', line 8

def size
  @size
end

Instance Method Details

#read(options = {}) ⇒ Array<Array<Float>>, Array<Float>

Read the audio file into memory

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :logger (IO)

Returns:

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

    File data



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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_rateInteger

The sample rate of the audio file

Returns:

  • (Integer)


22
23
24
# File 'lib/pulse-analysis/file.rb', line 22

def sample_rate
  @sample_rate ||= @sound.info.samplerate
end