2

Currently my application reads audio files based on a while-realloc loop:

// Pseudocode
float data* = nullptr;
int size = 0;
AVFrame* frame;
while(readFrame(formatContext, frame))
{
    data = realloc(data, size + frame.nSamples);
    size += frame.nSamples;
    /* Read frame samples into data */
}

Is there a way to obtain the total number of samples in a stream at the beginning? I want to be able to create the array with new[] instead of malloc.

Henricus V.
  • 898
  • 1
  • 8
  • 29

1 Answers1

0

For reference, this was answered here: FFmpeg: How to estimate number of samples in audio stream?

I used the following in my code:

int total_samples = (int) ((format_context->duration / (float) AV_TIME_BASE) * SAMPLE_RATE * NUMBER_CHANNELS);

NOTE: my testing shows this calculation will be most likely be more than the actual number of samples found, so make sure you compensate for that in your code. I set all remaining "unset" samples to zero.