Class: WaveFile::Buffer
- Inherits:
-
Object
- Object
- WaveFile::Buffer
- Defined in:
- lib/wavefile/buffer.rb
Overview
Represents a collection of samples in a certain format (e.g. 16-bit mono). Reader returns sample data contained in Buffers, and Writer expects incoming sample data to be contained in a Buffer as well.
Contains methods to convert the sample data in the buffer to a different format.
Instance Attribute Summary collapse
-
#samples ⇒ Object
readonly
Returns the sample data contained in the Buffer as an Array.
Instance Method Summary collapse
-
#bits_per_sample ⇒ Object
Returns the bits per sample of the buffer’s sample data.
-
#channels ⇒ Object
Returns the number of channels the buffer’s sample data has.
-
#convert(new_format) ⇒ Object
Creates a new Buffer containing the sample data of this Buffer, but converted to a different format.
-
#convert!(new_format) ⇒ Object
Converts the sample data contained in the Buffer to a new format.
-
#initialize(samples, format) ⇒ Buffer
constructor
Creates a new Buffer.
-
#sample_rate ⇒ Object
Returns the sample rate of the buffer’s sample data.
Constructor Details
#initialize(samples, format) ⇒ Buffer
Creates a new Buffer.
samples - An array of samples. If the Format has 1 channel (i.e. is mono), this
should be a flat array of samples such as [0.5, 0.4, -0.3, ...]. If the
Format has 2 or more channels the array should include a sub-array for
each sample frame. For example, [[0.5, 0.2], [0.1, 0.6], [-0.2, 0.4], ...]
for a stereo file.
format - A Format instance which describes the sample format of the sample array.
Note that the sample array is not compared with the format to make sure
they match - you are on the honor system to make sure they do. If they
don't match, unexpected things will happen.
Examples
samples = ([0.5] * 50) + ([-0.5] * 50) # A 440Hz mono square wave
buffer = Buffer.new(samples, Format.new(:mono, :float, 44100)
samples = ([0.5, 0.5] * 50) + ([-0.5, -0.5] * 50) # A 440Hz stereo square wave
buffer = Buffer.new(samples, Format.new(2, :float, 44100)
Returns a constructed Buffer.
38 39 40 41 |
# File 'lib/wavefile/buffer.rb', line 38 def initialize(samples, format) @samples = samples @format = format end |
Instance Attribute Details
#samples ⇒ Object (readonly)
Returns the sample data contained in the Buffer as an Array. If the Format has 1 channel, the Array will be a flat list of samples. If the Format has 2 or more channels, the Array will include sub arrays for each sample frame, with a sample for each channel.
Examples
samples = mono_buffer.samples
# => [-0.5, 0.3, 0.2, -0.9, ...]
samples = stereo_buffer.samples
# => [[-0.2, 0.5], [0.1, 0.2], [-0.4, 0.7], [0.1, 0.2], ...]
samples = three_channel_buffer.samples
# => [[0.3, 0.5, 0.2], [-0.1, 0.2, -0.9], [0.2, 0.3, -0.4], [0.1, 0.2, -0.8], ...]
111 112 113 |
# File 'lib/wavefile/buffer.rb', line 111 def samples @samples end |
Instance Method Details
#bits_per_sample ⇒ Object
Returns the bits per sample of the buffer’s sample data
86 87 88 |
# File 'lib/wavefile/buffer.rb', line 86 def bits_per_sample @format.bits_per_sample end |
#channels ⇒ Object
Returns the number of channels the buffer’s sample data has
80 81 82 |
# File 'lib/wavefile/buffer.rb', line 80 def channels @format.channels end |
#convert(new_format) ⇒ Object
Creates a new Buffer containing the sample data of this Buffer, but converted to a different format.
new_format - The format that the sample data should be converted to
Examples
new_format = Format.new(:mono, :pcm_16, 44100)
new_buffer = old_buffer.convert(new_format)
Returns a new Buffer; the existing Buffer is unmodified.
55 56 57 58 |
# File 'lib/wavefile/buffer.rb', line 55 def convert(new_format) new_samples = convert_buffer(@samples.dup, @format, new_format) Buffer.new(new_samples, new_format) end |
#convert!(new_format) ⇒ Object
Converts the sample data contained in the Buffer to a new format. The sample data is converted in place, so the existing Buffer is modified.
new_format - The format that the sample data should be converted to
Examples
new_format = Format.new(:mono, :pcm_16, 44100)
old_buffer.convert!(new_format)
Returns self.
72 73 74 75 76 |
# File 'lib/wavefile/buffer.rb', line 72 def convert!(new_format) @samples = convert_buffer(@samples, @format, new_format) @format = new_format self end |
#sample_rate ⇒ Object
Returns the sample rate of the buffer’s sample data
92 93 94 |
# File 'lib/wavefile/buffer.rb', line 92 def sample_rate @format.sample_rate end |