简体   繁体   English

修改音频样本缓冲区的音量增益

[英]Modify volume gain on audio sample buffer

I want to increase a volume on buffer with voice data. 我想用语音数据增加缓冲区的音量。 The point is I'm using DirectSound and I have one primary and one secondary buffer - all streams mixing is done by hand. 关键是我正在使用DirectSound,我有一个主缓冲区和一个辅助缓冲区 - 所有流混合都是手工完成的。 In a voice chat all participants can have independent volume levels. 在语音聊天中,所有参与者可以具有独立的音量级别。 I multiply each stream data by a value (gain) and sum it to one buffer. 我将每个流数据乘以一个值(增益)并将其加到一个缓冲区。 Everything works fine but when I try to multiply data by a value greater than 1.0f - I hear some clipping or what. 一切正常,但当我尝试将数据乘以大于1.0f的值时 - 我听到一些剪辑或什么。

I've tried using Audacity effect compressor but this doesn't help reducing the strange noise. 我尝试使用Audacity效果压缩器,但这无助于减少奇怪的噪音。

Probably I should modify gain in some other way? 可能我应该以其他方式改变收益? Or just use another post-processing algorithm? 或者只是使用另一种后处理算法?

UPDATE: Wow, I've just found out interesting thing! 更新:哇,我刚发现有趣的事情! I've dumped audio before increasing volume and right after that. 在增加音量之前,我已经放弃了音频。

Here is the pic 这是照片 剪辑的音频

Sorry for the quality - I think that's how the sound is supposed to appear (I've drawn red line myself). 对不起质量 - 我认为这应该是声音出现的方式(我自己画了红线)。 Really looks like values exceed the sample data type. 真的看起来像超过样本数据类型的值。 But I cannot understand WHY? 但我无法理解为什么? My samplebuffer is BYTE but I access it only via short pointer. 我的samplebuffer是BYTE,但我只通过短指针访问它。 It is signed but clipping happens even when *ptr is about 15-20 thousand. 它已签名但即使* ptr约为15-20万时也会发生剪辑。

For every sample - convert it to some larger data type - if you have 16 bit signed samples, they originally fit in SHORT - extract it from the stream, then cast to local double, then multiply, then CLIP, then cast back to SHORT. 对于每个样本 - 将其转换为更大的数据类型 - 如果您有16位签名样本,它们最初适合SHORT - 从流中提取它,然后转换为本地double,然后乘以,然后CLIP,然后转换回SHORT。

It MUST work that way... 它必须以这种方式工作......

I can even provide code sample if needed. 如果需要,我甚至可以提供代码示例。

EDIT: 编辑:

Your picture is exact evidence that you didn't expand to larger type before multiplication - you can't 'capture' clipping condition on SHORT because it will wrap automatically. 您的图片确实证明您在乘法之前没有扩展到更大的类型 - 您无法在SHORT上“捕获”剪切条件,因为它会自动换行。

short* sampleBuffer;
...
short sample=*sampleBuffer;
double dsample=(double)sample * gain;
if (dsample>32767.0) {dsample=32767.0;}
if (dsample<-32768.0) {dsample=-32768.0;}
*sampleBuffer=(short)dsample;
sampleBuffer++;

And one more EDIT: 还有一个编辑:

if you have several voices - first put them all to double - then GAIN each one - then add them - and CLIP them as the last step. 如果你有几个声音 - 首先将它们全部加倍 - 然后加入每个声音 - 然后添加它们 - 并将它们作为最后一步进行CLIP。

One more EDIT (+1s are inspiring me): 还有一个编辑(+ 1s鼓舞了我):

If you have STEREO, same stuff will work also, just count all the samples x2 ie 如果你有STEREO,同样的东西也会起作用,只计算所有的样本x2即

number of shorts = number of samples * 2 . number of shorts number of samples = number of samples * 2

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

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