Class: Wavify::Core::SampleBuffer::View
- Inherits:
-
Object
- Object
- Wavify::Core::SampleBuffer::View
- Includes:
- Enumerable, Enumerable[Numeric]
- Defined in:
- lib/wavify/core/sample_buffer.rb,
sig/core.rbs
Overview
Lazy no-copy view over a contiguous frame range of a sample buffer.
Instance Attribute Summary collapse
-
#duration ⇒ Duration
readonly
Returns the value of attribute duration.
-
#format ⇒ Format
readonly
Returns the value of attribute format.
Instance Method Summary collapse
-
#convert(new_format, **options) ⇒ SampleBuffer
Converts the materialized view to another format.
-
#each {|sample, arg0| ... } ⇒ Enumerator, Array<Numeric>
Enumerates interleaved sample values in the view range.
- #frame_view ⇒ FrameView
-
#initialize(samples, format, start_frame: 0, frame_count: nil) ⇒ View
constructor
A new instance of View.
-
#length ⇒ Integer
(also: #size)
Number of interleaved samples in the view.
-
#sample_frame_count ⇒ Integer
Number of sample frames in the view.
-
#samples ⇒ Array<Numeric>
Materializes the view's interleaved samples.
-
#slice(start_frame, frame_length) ⇒ View
Returns another lazy view relative to this view.
-
#to_sample_buffer ⇒ SampleBuffer
Materializes the view as an immutable sample buffer.
Constructor Details
#initialize(samples, format, start_frame: 0, frame_count: nil) ⇒ View
Returns a new instance of View.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/wavify/core/sample_buffer.rb', line 111 def initialize(samples, format, start_frame: 0, frame_count: nil) raise InvalidParameterError, "samples must be an Array" unless samples.is_a?(Array) raise InvalidParameterError, "format must be Core::Format" unless format.is_a?(Format) raise InvalidParameterError, "start_frame must be a non-negative Integer" unless start_frame.is_a?(Integer) && start_frame >= 0 max_frames = samples.length / format.channels @samples = samples @format = format @start_frame = [start_frame, max_frames].min requested_count = frame_count.nil? ? max_frames - @start_frame : frame_count raise InvalidParameterError, "frame_count must be a non-negative Integer" unless requested_count.is_a?(Integer) && requested_count >= 0 @frame_count = [requested_count, max_frames - @start_frame].min @duration = Duration.from_samples(@frame_count, format.sample_rate) end |
Instance Attribute Details
#duration ⇒ Duration (readonly)
Returns the value of attribute duration.
109 110 111 |
# File 'lib/wavify/core/sample_buffer.rb', line 109 def duration @duration end |
#format ⇒ Format (readonly)
Returns the value of attribute format.
109 110 111 |
# File 'lib/wavify/core/sample_buffer.rb', line 109 def format @format end |
Instance Method Details
#convert(new_format, **options) ⇒ SampleBuffer
Converts the materialized view to another format.
190 191 192 |
# File 'lib/wavify/core/sample_buffer.rb', line 190 def convert(new_format, **) to_sample_buffer.convert(new_format, **) end |
#each ⇒ View #each ⇒ Enumerator[Numeric, View]
Enumerates interleaved sample values in the view range.
131 132 133 134 135 136 |
# File 'lib/wavify/core/sample_buffer.rb', line 131 def each return enum_for(:each) unless block_given? start_index = @start_frame * @format.channels length.times { |offset| yield @samples.fetch(start_index + offset) } end |
#frame_view ⇒ FrameView
159 160 161 |
# File 'lib/wavify/core/sample_buffer.rb', line 159 def frame_view FrameView.new(@samples, @format.channels, start_frame: @start_frame, frame_count: @frame_count) end |
#length ⇒ Integer Also known as: size
Returns number of interleaved samples in the view.
147 148 149 |
# File 'lib/wavify/core/sample_buffer.rb', line 147 def length @frame_count * @format.channels end |
#sample_frame_count ⇒ Integer
Returns number of sample frames in the view.
154 155 156 |
# File 'lib/wavify/core/sample_buffer.rb', line 154 def sample_frame_count @frame_count end |
#samples ⇒ Array<Numeric>
Materializes the view's interleaved samples.
141 142 143 144 |
# File 'lib/wavify/core/sample_buffer.rb', line 141 def samples start_index = @start_frame * @format.channels @samples.slice(start_index, length).freeze end |
#slice(start_frame, frame_length) ⇒ View
Returns another lazy view relative to this view.
168 169 170 171 172 173 174 175 176 177 |
# File 'lib/wavify/core/sample_buffer.rb', line 168 def slice(start_frame, frame_length) unless start_frame.is_a?(Integer) && start_frame >= 0 raise InvalidParameterError, "start_frame must be a non-negative Integer: #{start_frame.inspect}" end unless frame_length.is_a?(Integer) && frame_length >= 0 raise InvalidParameterError, "frame_length must be a non-negative Integer: #{frame_length.inspect}" end self.class.new(@samples, @format, start_frame: @start_frame + start_frame, frame_count: frame_length) end |
#to_sample_buffer ⇒ SampleBuffer
Materializes the view as an immutable sample buffer.
182 183 184 |
# File 'lib/wavify/core/sample_buffer.rb', line 182 def to_sample_buffer SampleBuffer.new(samples, @format) end |