以编程方式转换 WAV

发布于 2024-09-02 01:33:35 字数 246 浏览 8 评论 0原文

我正在用 C++ 编写一个文件压缩器实用程序,我希望支持 PCM WAV 文件,但是我想将其保留在 PCM 编码中,只需将其转换为较低的采样率,并将其从立体声更改为单声道(如果适用)以产生较低的采样率。文件大小。

我理解 WAV 文件头,但是我对实际声音数据的工作原理没有经验或了解。所以我的问题是,以编程方式操作 WAV 文件中的“数据”子块以将其转换为另一个采样率并更改通道号是否相对容易,或者我最好使用现有的库来实现它?如果是的话,那该怎么办呢?提前致谢。

I'm writing a file compressor utility in C++ that I want support for PCM WAV files, however I want to keep it in PCM encoding and just convert it to a lower sample rate and change it from stereo to mono if applicable to yield a lower file size.

I understand the WAV file header, however I have no experience or knowledge of how the actual sound data works. So my question is, would it be relatively easy to programmatically manipulate the "data" sub-chunk in a WAV file to convert it to another sample rate and change the channel number, or would I be much better off using an existing library for it? If it is, then how would it be done? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森罗 2024-09-09 01:33:35

PCM仅仅意味着在等距时间点对原始信号的值进行采样。

对于立体声,这些值有两个序列。要将它们转换为单声道,您只需对两个序列进行分段平均即可。

以较低采样率对信号进行重新采样有点棘手——您必须从信号中滤除高频,以防止产生混叠(虚假低频信号)。

PCM merely means that the value of the original signal is sampled at equidistant points in time.

For stereo, there are two sequences of these values. To convert them to mono, you merely take piecewise average of the two sequences.

Resampling the signal at lower sampling rate is a little bit more tricky -- you have to filter out high frequencies from the signal so as to prevent alias (spurious low-frequency signal) from being created.

生生漫 2024-09-09 01:33:35

我同意阿瓦卡和尼科的观点,但我想补充一点解释。降低 PCM 音频的采样率并非易事,除非满足以下两点:

  1. 您的信号仅包含低于新采样率 1/2 的重要频率 (奈奎斯特速率)。在这种情况下,您不需要抗混叠滤波器。

  2. 您正在按整数值进行下采样。在这种情况下,按 N 进行下采样只需保留第 N 个样本并丢弃其余样本。

如果这些都是真的,您可以定期删除样本来进行下采样。然而,如果您处理的是合成信号以外的任何信号,那么它们可能都不是真的。

为了解决问题一,您必须使用低通滤波器对音频样本进行滤波,以确保生成的信号仅包含高达新采样率 1/2 的频率内容。如果不这样做,高频将无法准确表示,并且会混叠回可以正确表示的频率,从而导致严重失真。查看此维基百科文章的临界频率部分,了解混叠的说明。具体来说,请参见图 7,它显示了 3 个不同的信号,由于采样率太低,仅通过样本无法区分这些信号。

解决第二个问题可以通过多种方式来完成。有时分两步执行:上采样,然后下采样,从而实现采样率的合理变化。也可以使用插值或其他技术来完成。基本上,必须解决的问题是新信号的样本与原始信号的样本不及时对齐。

正如您所看到的,重新采样音频可能非常复杂,因此我会采纳 nico 的建议并使用现有的库。正确选择滤波器步骤需要您学习大量有关信号处理和频率分析的知识。您不必成为专家,但这需要一些时间。

I agree with avakar and nico, but I'd like to add a little more explanation. Lowering the sample rate of PCM audio is not trivial unless two things are true:

  1. Your signal only contains significant frequencies lower than 1/2 the new sampling rate (Nyquist rate). In this case you do not need an anti-aliasing filter.

  2. You are downsampling by an integer value. In this case, downampling by N just requires keeping every Nth sample and dropping the rest.

If these are true, you can just drop samples at a regular interval to downsample. However, they are both probably not true if you're dealing with anything other than a synthetic signal.

To address problem one, you will have to filter the audio samples with a low-pass filter to make sure the resulting signal only contains frequency content up to 1/2 the new sampling rate. If this is not done, high frequencies will not be accurately represented and will alias back into the frequencies that can be properly represented, causing major distortion. Check out the critical frequency section of this wikipedia article for an explanation of aliasing. Specifically, see figure 7 that shows 3 different signals that are indistinguishable by just the samples because the sampling rate is too low.

Addressing problem two can be done in multiple ways. Sometimes it is performed in two steps: an upsample followed by a downsample, therefore achieving rational change in the sampling rate. It may also be done using interpolation or other techniques. Basically the problem that must be solved is that the samples of the new signal do not line up in time with samples of the original signal.

As you can see, resampling audio can be quite involved, so I would take nico's advice and use an existing library. Getting the filter step right will require you to learn a lot about signal processing and frequency analysis. You won't have to be an expert, but it will take some time.

寂寞美少年 2024-09-09 01:33:35

我认为确实没有必要重新发明轮子(除非您想为了个人学习而这样做)。
例如,您可以尝试使用 libsnd

I don't think there's really the need of reinventing the wheel (unless you want to do it for your personal learning).
For instance you can try to use libsnd

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文