1

I am using libsndfile in C++ to read a WAV file. There are two points I don't understand:

  1. How can I get the "Bits per sample" value for the WAV file in question? I read the documentation at the website http://www.mega-nerd.com/libsndfile/api.html, but I didn't find a member for "Bits per sample" in the SF_INFO struct.
  2. Using the WAV file, how can I create the data to use to draw vectors for describing the sound data, read by function sf_readf_float() in library sndfile.h? Is there any method to do this?
4

1 回答 1

3

格式字段将为您提供 BPS。例如:SF_FORMAT_PCM_16。

sf_readf_float 会将样本转换为 -1.0 到 1.0 的范围,无论输入声音的 bps 是多少。您只需要注意音频通道即可。如果音频有 2 个通道并且您读取 4 个浮点数,您将拥有:

sample-1 of left channel
sample-1 of right channel
sample-2 of left channel
sample-2 of right channel

然后,要绘制该点,您必须将 [-1.0;1.0] 转换为视口高度。例如,如果视口位于 Y=20 且高度为 300px,则公式为:

PY = (int)(20.0 + (sample_value / 2.0 + 0.5) * 300.0);
于 2013-07-06T15:25:07.103 回答