Class: Wavify::Core::SampleBuffer::FrameView
- Inherits:
-
Object
- Object
- Wavify::Core::SampleBuffer::FrameView
- Includes:
- Enumerable, Enumerable[Array[Numeric]]
- Defined in:
- lib/wavify/core/sample_buffer.rb,
sig/core.rbs
Overview
Lazy no-copy view over interleaved samples as sample frames.
Instance Attribute Summary collapse
-
#channels ⇒ Integer
readonly
Returns the value of attribute channels.
-
#frame_count ⇒ Integer
readonly
Returns the value of attribute frame_count.
Instance Method Summary collapse
- #[](index) ⇒ Array[Numeric]?
- #each {|arg0| ... } ⇒ Object
-
#initialize(samples, channels, start_frame: 0, frame_count: nil) ⇒ FrameView
constructor
A new instance of FrameView.
- #length ⇒ Integer (also: #size)
- #slice(start_frame, frame_length) ⇒ FrameView
Constructor Details
#initialize(samples, channels, start_frame: 0, frame_count: nil) ⇒ FrameView
Returns a new instance of FrameView.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/wavify/core/sample_buffer.rb', line 56 def initialize(samples, channels, start_frame: 0, frame_count: nil) raise InvalidParameterError, "samples must be an Array" unless samples.is_a?(Array) raise InvalidParameterError, "channels must be a positive Integer" unless channels.is_a?(Integer) && channels.positive? raise InvalidParameterError, "start_frame must be a non-negative Integer" unless start_frame.is_a?(Integer) && start_frame >= 0 max_frames = samples.length / channels @samples = samples @channels = channels @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 end |
Instance Attribute Details
#channels ⇒ Integer (readonly)
Returns the value of attribute channels.
54 55 56 |
# File 'lib/wavify/core/sample_buffer.rb', line 54 def channels @channels end |
#frame_count ⇒ Integer (readonly)
Returns the value of attribute frame_count.
54 55 56 |
# File 'lib/wavify/core/sample_buffer.rb', line 54 def frame_count @frame_count end |
Instance Method Details
#[](index) ⇒ Array[Numeric]?
77 78 79 80 81 82 83 84 85 |
# File 'lib/wavify/core/sample_buffer.rb', line 77 def [](index) raise InvalidParameterError, "frame index must be an Integer" unless index.is_a?(Integer) normalized = index.negative? ? @frame_count + index : index return nil unless normalized.between?(0, @frame_count - 1) sample_index = (@start_frame + normalized) * @channels @samples.slice(sample_index, @channels).dup end |
#each ⇒ FrameView #each ⇒ Enumerator[Array[Numeric], FrameView]
71 72 73 74 75 |
# File 'lib/wavify/core/sample_buffer.rb', line 71 def each return enum_for(:each) unless block_given? @frame_count.times { |index| yield self[index] } end |
#length ⇒ Integer Also known as: size
98 99 100 |
# File 'lib/wavify/core/sample_buffer.rb', line 98 def length @frame_count end |
#slice(start_frame, frame_length) ⇒ FrameView
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/wavify/core/sample_buffer.rb', line 87 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, @channels, start_frame: @start_frame + start_frame, frame_count: frame_length) end |