Class: AudioPlayback::File

Inherits:
Object
  • Object
show all
Defined in:
lib/audio-playback/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)


9
10
11
12
13
14
15
# File 'lib/audio-playback/file.rb', line 9

def initialize(file_or_path)
  @path = file_or_path.kind_of?(::File) ? file_or_path.path : file_or_path
  @file = RubyAudio::Sound.open(@path)
  @size = ::File.size(@path)
  @num_channels = @file.info.channels
  @sample_rate = @file.info.samplerate
end

Instance Attribute Details

#num_channelsObject (readonly)

Returns the value of attribute num_channels.



6
7
8
# File 'lib/audio-playback/file.rb', line 6

def num_channels
  @num_channels
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/audio-playback/file.rb', line 6

def path
  @path
end

#sample_rateObject (readonly)

Returns the value of attribute sample_rate.



6
7
8
# File 'lib/audio-playback/file.rb', line 6

def sample_rate
  @sample_rate
end

#sizeObject (readonly)

Returns the value of attribute size.



6
7
8
# File 'lib/audio-playback/file.rb', line 6

def size
  @size
end

Instance Method Details

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

Returns File data.

Parameters:

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

Options Hash (options):

  • :logger (IO)

Returns:

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

    File data



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/audio-playback/file.rb', line 20

def read(options = {})
  if logger = options[:logger]
    logger.puts("Reading audio file #{@path}")
  end
  buffer = RubyAudio::Buffer.float(@size, @num_channels)
  begin
    @file.seek(0)
    @file.read(buffer, @size)
    data = buffer.to_a
  rescue RubyAudio::Error
  end
  logger.puts("Finished reading audio file #{@path}") if logger
  data
end