readSamplesFromFile method

  1. @experimental
Future<Float32List> readSamplesFromFile(
  1. String completeFileName,
  2. int numSamplesNeeded, {
  3. double startTime = 0,
  4. double endTime = -1,
  5. bool average = false,
})

Read numSamplesNeeded audio data from a file equally spaced in time. The returned Float32List is not guaranteed to be numSamplesNeeded long. Each value in the returned Float32List is in the range -1.0 to 1.0 (but not guaranteed). Their values are the average of audio data from the previous index sample if average is true. NOTE: this is not available on Web. Use readSamplesFromMem instead.

completeFileName the complete path to the audio file. numSamplesNeeded is not guaranteed to be the same length as the returned Float32List. This could happen if the endTime is greater than the audio lenght. startTime in seconds. Defaults to 0. endTime in seconds. Defaults to -1. If -1, the audio will be read until the end of the file. average if true, the returned Float32List will be filled with the average of the samples from the previous index sample. Defaults to false. When true it does not affect performance much.

Here a representation of the range startTime to endTime in the audio with numSamplesNeeded=10:

0 1 2 3 4 5 6 7 8 9 |------|------|------|------|------|------|------|------|------| ------- with average=true all the samples are the average of the samples from 2 to 3 and it is stored in the returned Float32List at index 3. - with average=false the value returned at index 3 is the value got at 3.

Throws SoLoudReadSamplesNoBackendCppException if an error occurred while initializing the backend to read samples. Throws SoLoudReadSamplesFailedToGetDataFormatCppException if an error occurred while reading the decoder data format. Throws SoLoudReadSamplesFailedToSeekPcmCppException if an error occurred when seeking audio data. Throws SoLoudReadSamplesFailedToReadPcmFramesCppException if an error occurred when reading PCM frames.

See also readSamplesFromMem.

Implementation

@experimental
Future<Float32List> readSamplesFromFile(
  String completeFileName,
  int numSamplesNeeded, {
  double startTime = 0,
  double endTime = -1,
  bool average = false,
}) async {
  assert(
    endTime == -1 || endTime > startTime,
    '[endTime] must be greater than [startTime].',
  );
  assert(startTime >= 0, '[startTime] must be greater than or equal to 0.');
  final samples = await compute(_readSamplesFromFile, {
    'completeFileName': completeFileName,
    'numSamplesNeeded': numSamplesNeeded,
    'startTime': startTime,
    'endTime': endTime,
    'average': average,
  });

  return samples;
}