简体   繁体   中英

How to find highest volume level of a WAV file using C++

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM