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

时间:2011-11-22 12:44:02

标签: c++ audio wav volume sample-data

我想通过使用C ++(库libsndfile)获取WAV文件的最高音量级别的值?有关如何做的任何建议吗?

1 个答案:

答案 0 :(得分:6)

您可以在样本缓冲区中的样本的绝对值中找到最高的单个样本值(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计算比人类感知更接近峰值。

为了更深入地了解人类的感知,你可以使用Weighing Filters