Question

I want to get the value of the highest volume level of a WAV-file by using C++ (library libsndfile)? Any suggestions on how to do it?

Was it helpful?

Solution

You can simply find the highest single sample value (Peak) among the absolute values of the samples in the sample buffer(s). This takes the general form:

t_sample PeakAmplitude(const t_sample* const buffer, const size_t& count) {
  t_sample highest(0);
  for (size_t idx(0); idx < count; ++idx) {
    // or fabs if fp
    highest = std::max(highest, abs(buffer[idx]));
  }
  return highest;
}

To get averages, you can use RMS functions. Illustration:

t_sample RMSAmplitude(const t_sample* const buffer, const size_t& count) {
  t_sample s2(0);
  for (size_t idx(0); idx < count; ++idx) {
    // mind your sample types and ranges
    s2 += buffer[idx] * buffer[idx];
  }
  return sqrt(s2 / static_cast<double>(count));
}

RMS calculations are closer to human perception than Peak.

To go even deeper into human perception, you can employ Weighing Filters.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top