Class: Wavify::Core::SampleBuffer::View

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(samples, format, start_frame: 0, frame_count: nil) ⇒ View

Returns a new instance of View.

Parameters:

  • samples (Array[Numeric])
  • format (Format)
  • start_frame: (Integer) (defaults to: 0)
  • frame_count: (Integer, nil) (defaults to: nil)

Raises:



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

#durationDuration (readonly)

Returns the value of attribute duration.

Returns:



109
110
111
# File 'lib/wavify/core/sample_buffer.rb', line 109

def duration
  @duration
end

#formatFormat (readonly)

Returns the value of attribute format.

Returns:



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.

Parameters:

  • new_format (Format)
  • options (Object)

Returns:



190
191
192
# File 'lib/wavify/core/sample_buffer.rb', line 190

def convert(new_format, **options)
  to_sample_buffer.convert(new_format, **options)
end

#eachView #eachEnumerator[Numeric, View]

Enumerates interleaved sample values in the view range.

Overloads:

  • #eachView

    Returns:

  • #eachEnumerator[Numeric, View]

    Returns:

    • (Enumerator[Numeric, View])

Yields:

Yield Parameters:

  • sample (Numeric)
  • arg0 (Numeric)

Yield Returns:

  • (void)

Returns:

  • (Enumerator, Array<Numeric>)


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_viewFrameView

Returns:



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

#lengthInteger Also known as: size

Returns number of interleaved samples in the view.

Returns:

  • (Integer)

    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_countInteger

Returns number of sample frames in the view.

Returns:

  • (Integer)

    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

#samplesArray<Numeric>

Materializes the view's interleaved samples.

Returns:

  • (Array<Numeric>)


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.

Parameters:

  • start_frame (Integer)
  • frame_length (Integer)

Returns:



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_bufferSampleBuffer

Materializes the view as an immutable sample buffer.

Returns:



182
183
184
# File 'lib/wavify/core/sample_buffer.rb', line 182

def to_sample_buffer
  SampleBuffer.new(samples, @format)
end