I want to apply gain to my records (16 bit PCM). For this, I have the following code:
for (int i=0; i<buffer.length/2; i++)
{
short curSample = getShort(buffer[i*2], buffer[i*2+1]);
if(rGain != 1){
curSample *= rGain;
byte[] a = getByteFromShort(curSample);
buffer[i*2] = a[0];
buffer[i*2 + 1] = a[1];
}
If you apply this (multiplying each sample by the number of fractions), I stop playing during playback (hearing, like an old walkie-talkie). Is there any formula for changing the gain for each sample? I assume that for the range of samples there are several maxValue and minValue (I think [-32768, +32767]), and using these values in some formula, I can get the gain variation coefficient for the current sample.
// EDIT: added
if (curSample>32767) {curSample=32767;}
if (curSample<-32768) {curSample=-32768;}
full method
aRecorder.read(buffer, 0, buffer.length);
for (int i=0; i<buffer.length/2; i++)
{
short curSample = getShort(buffer[i*2], buffer[i*2+1]);
if(rGain != 1){
curSample *= rGain;
if (curSample>32767) {curSample=32767;}
if (curSample<-32768) {curSample=-32768;}
byte[] a = getByteFromShort(curSample);
buffer[i*2] = a[0];
buffer[i*2 + 1] = a[1];
}
But still he hears the odd (noise + stops like an old walkie-talkie).
Any help would be appreciated
Thank.