Class: PulseAnalysis::File
- Inherits:
-
Object
- Object
- PulseAnalysis::File
- Extended by:
- Forwardable
- Defined in:
- lib/pulse-analysis/file.rb
Overview
An audio file
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#num_channels ⇒ Object
readonly
Returns the value of attribute num_channels.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#initialize(file_or_path) ⇒ File
constructor
A new instance of File.
-
#read(options = {}) ⇒ Array<Array<Float>>, Array<Float>
Read the audio file into memory.
-
#sample_rate ⇒ Integer
The sample rate of the audio file.
Constructor Details
#initialize(file_or_path) ⇒ File
Returns a new instance of File.
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
#file ⇒ Object (readonly)
Returns the value of attribute file.
8 9 10 |
# File 'lib/pulse-analysis/file.rb', line 8 def file @file end |
#num_channels ⇒ Object (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 |
#size ⇒ Object (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
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/pulse-analysis/file.rb', line 30 def read( = {}) if logger = [: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 ⇒ Integer
The sample rate of the audio file
22 23 24 |
# File 'lib/pulse-analysis/file.rb', line 22 def sample_rate @sample_rate ||= @sound.info.samplerate end |