Modify volume gain on audio sample buffer

二次信任 提交于 2019-12-04 07:27:26

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.

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* 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.

One more EDIT (+1s are inspiring me):

If you have STEREO, same stuff will work also, just count all the samples x2 i.e.

number of shorts = number of samples * 2.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!