如何使用c++找到WAV文件的最高音量级别

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

本文关键字:高音量 文件 何使用 c++ 找到 WAV      更新时间:2023-10-16

我想通过使用c++(库libsndfile)获得wav文件的最高音量级别的值?有什么建议吗?

您可以简单地在样本缓冲区(s)中样本的绝对值中找到最高的单个样本值(Peak)。它采用一般的形式:

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;
}

要获得平均值,可以使用RMS函数。说明:

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计算比Peak更接近人类感知。

要更深入地了解人类的感知,您可以使用称重过滤器。