loadOffset method

void loadOffset(
  1. List<double> newData,
  2. int offset,
  3. int size
)

Loads a slice of the float array to the ring buffer. If the float array is longer than ring buffer's capacity, samples with lower indicies in the array will be ignored.

Implementation

void loadOffset(List<double> newData, int offset, int size) {
  checkArgument(
    offset + size <= newData.length,
    message:
    "Index out of range. offset ($offset) + size ($size) should <= newData.length (${newData
        .length})",
  );
  // If buffer can't hold all the data, only keep the most recent data of size buffer.length
  if (size > _buffer.length) {
    offset = size - _buffer.length;
    size = _buffer.length;
  }
  if (_nextIndex + size < _buffer.length) {
    // No need to wrap nextIndex, just copy newData[offset:offset + size]
    // to buffer[nextIndex:nextIndex+size]
    List.copyRange(_buffer, _nextIndex, newData, offset, offset + size);
  } else {
    // Need to wrap nextIndex, perform copy in two chunks.
    int firstChunkSize = _buffer.length - _nextIndex;
    // First copy newData[offset:offset+firstChunkSize] to buffer[nextIndex:buffer.length]
    List.copyRange(
        _buffer, _nextIndex, newData, offset, offset + firstChunkSize);
    // Then copy newData[offset+firstChunkSize:offset+size] to buffer[0:size-firstChunkSize]
    List.copyRange(
        _buffer, 0, newData, offset + firstChunkSize, offset + size);
  }

  _nextIndex = (_nextIndex + size) % _buffer.length;
}