Search code examples
iosaudiosampling

How to calculate number of samples from length of audio file?


Given an audio file of duration say 10s, how do I find out the no of samples betweeen 2s and 8s?


Solution

  • if it's LPCM (e.g. not compressed), then use the sample rate.

    in pseudocode:

    double sampleRate = audioFile.getSampleRate();
    // you may also need to account for channel count here
    size_t sampleCount = sampleRate * (8-2);
    

    -- where (8-2) represents "betweeen 2s and 8s"