Class: Wavify::Core::SampleBuffer::FrameView

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

Instance Method Summary collapse

Constructor Details

#initialize(samples, channels, start_frame: 0, frame_count: nil) ⇒ FrameView

Returns a new instance of FrameView.

Parameters:

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

Raises:



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

#channelsInteger (readonly)

Returns the value of attribute channels.

Returns:

  • (Integer)


54
55
56
# File 'lib/wavify/core/sample_buffer.rb', line 54

def channels
  @channels
end

#frame_countInteger (readonly)

Returns the value of attribute frame_count.

Returns:

  • (Integer)


54
55
56
# File 'lib/wavify/core/sample_buffer.rb', line 54

def frame_count
  @frame_count
end

Instance Method Details

#[](index) ⇒ Array[Numeric]?

Parameters:

  • index (Integer)

Returns:

  • (Array[Numeric], nil)

Raises:



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

#eachFrameView #eachEnumerator[Array[Numeric], FrameView]

Overloads:

Yields:

Yield Parameters:

  • arg0 (Array[Numeric])

Yield Returns:

  • (void)


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

#lengthInteger Also known as: size

Returns:

  • (Integer)


98
99
100
# File 'lib/wavify/core/sample_buffer.rb', line 98

def length
  @frame_count
end

#slice(start_frame, frame_length) ⇒ FrameView

Parameters:

  • start_frame (Integer)
  • frame_length (Integer)

Returns:



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