简体   繁体   English

播放时更改音频信号的增益会导致失真

[英]Changing gain of audio signal while it's playing causes artifacts

I am playing back audio files in a program, and in the audio rendering callbacks, I apply a gain multiplier to the input signal and add it to the output buffer. 我正在播放程序中的音频文件,并且在音频渲染回调中,将增益乘法器应用于输入信号,并将其添加到输出缓冲区。 Here's some pseudo code to illustrate my actions: 这是一些伪代码来说明我的操作:

void audioCallback(AudioOutputBuffer* ao, AudioInput* ai, int startSample, int numSamples){
    for (int i=startSample; i<numSamples+startSample; i++){
        ao[i] = ai[i]*gain;
    }
}

Basically I just multiply the data by some multiplier. 基本上,我只是将数据乘以某个乘数。 In this case, gain is a float member that is being adjusted via a GUI callback. 在这种情况下, gain是通过GUI回调进行调整的float成员。 If I adjust this value while the audio is still playing, I can hear that the audio is getting softer or louder when I move the slider, but I hear lots of little pops and clicks. 如果在音频仍在播放时调整此值,则可以听到在移动滑块时音频变得更柔和或更大声,但是听到很多小小的啪嗒声。

Not really sure what the deal is. 不太确定这笔交易是什么。 I know about interpolation, and I do that if the audio is pitch shifted, but I'm not sure if I need to do any extra interpolation or something if the gain is being adjusted in real time before the audio file is finished playing. 我知道内插,如果音频被音高偏移,我会这样做,但是我不确定是否需要做任何额外的内插或在音频文件完成播放之前实时调整增益的事情。

If I adjust the slider before the audio start playing, the gain is set properly and I get no clicks. 如果在音频开始播放之前调整滑块,则增益设置正确,并且没有点击声。

Am I missing something here? 我在这里想念什么吗? How else is gain implemented but a multiplier on the input signal? 除了输入信号的乘法器外,如何实现增益?

Question: how does the multiplication operator know which operand is the audio signal and which one is the gain? 问题:乘法运算符如何知道哪个操作数是音频信号,哪个是增益? Answer: it doesn't. 答:不是。 They're both audio signals , and anything audible in either one will be audible in the output. 它们都是音频信号 ,任何一个声音都可以在输出中听到。

A flat, unchanging signal doesn't produce any audible sounds. 平坦,不变的信号不会产生任何可听见的声音。 As long as the gain remains constant, it won't introduce any sound of its own. 只要增益保持恒定,它就不会引入任何声音。

A signal that changes abruptly will be very audible, it sounds like a click, containing lots of high frequencies. 突然变化的信号将非常容易听见,听起来像是喀哒声,其中包含许多高频。

As you've determined on your own, one way to reduce the high frequency content and thus the audibility is to stretch out the change over a number of samples, using a constant slope. 正如您自己确定的那样,减少高频内容并降低可听度的一种方法是使用恒定的斜率将变化扩展到多个样本上。 This would certainly suffice in an application where you have lots of time to make the gain change. 在您有很多时间进行增益更改的应用程序中,这当然足够了。

Another way would be to run a low-pass filter on the gain signal and use that as the input to the multiplication. 另一种方法是对增益信号运行一个低通滤波器,并将用作乘法的输入。

I fixed it by changing the gain in increments of the amount changed. 我通过以改变量的增量来改变增益来修复它。 For instance, if the gain multiplier was set to 1.0, then changed to 0.8, that's a difference of 0.2 gain. 例如,如果将增益乘数设置为1.0,然后更改为0.8,则相差0.2增益。 For each sample in the callback, add the difference / numSamples to the previous volume to create a slurring or gradual gain change. 对于回调中的每个样本,将差值/ numSamples添加到上一个音量以创建调音或渐进式增益更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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