Class: AudioPlayback::File
- Inherits:
-
Object
- Object
- AudioPlayback::File
- Defined in:
- lib/audio-playback/file.rb
Overview
An audio file
Instance Attribute Summary collapse
-
#num_channels ⇒ Object
readonly
Returns the value of attribute num_channels.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#sample_rate ⇒ Object
readonly
Returns the value of attribute sample_rate.
-
#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>
File data.
Constructor Details
#initialize(file_or_path) ⇒ File
Returns a new instance of File.
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_channels ⇒ Object (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 |
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'lib/audio-playback/file.rb', line 6 def path @path end |
#sample_rate ⇒ Object (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 |
#size ⇒ Object (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.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/audio-playback/file.rb', line 20 def read( = {}) if logger = [: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 |