Android audioRecord - apply gain with variation

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++)
{ // 16bit sample size                      
  short curSample = getShort(buffer[i*2], buffer[i*2+1]);
  if(rGain != 1){
  //apply gain
  curSample *= rGain;
  //convert back from short sample that was "gained" to byte data
  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++)
                    { // 16bit sample size                      
                        short curSample = getShort(buffer[i*2], buffer[i*2+1]);
                        if(rGain != 1){
                            //apply gain
                            curSample *= rGain;
                            if (curSample>32767) {curSample=32767;}
                            if (curSample<-32768) {curSample=-32768;}
                            //convert back from short sample that was "gained" to byte data
                            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.

+3
3

. -32768..32767, s:

short curSample = getShort(buffer[i*2], buffer[i*2+1]);

, 1, "" short:

curSample *= rGain;

, , 32767 * 1.5 49150, - "" -16386, short.

,

if (curSample>32767) {curSample=32767;}
if (curSample<-32768) {curSample=-32768;}

, curSample 32767 , -32768.

, int:

short curSample = getShort(buffer[i*2], buffer[i*2+1]);
int temp = curSample * rGain;
if (temp>=32767)
    curSample=32767;
else if (temp<=-32768)
    curSample=-32768;
else
    curSample=(short)temp;
+1

... VU-... ...

final int numFrames = getNumOfFrames(source.length);
62                          final int bytesPerSample = bitsPerSamples / 8;
63                          final int emptySpace=64-bitsPerSamples;
64                          int byteIndex=0;
65                          int byteIndex2 = 0;
66                  
67                  
68                          int temp = 0;
69                          int mLeftTemp = 0;
70                          int mRightTemp = 0;
71                          int a=0;
72                          int x = 0;
73                          
74                          for(int frameIndex=0; frameIndex<numFrames; frameIndex++){
75                                  for(int c=0; c<nChannels; c++){
76                                          if(rGain != 1){
77                                                  // gain
78                                                  long accumulator=0;
79                                                  for(int b=0; b<bytesPerSample; b++){
80                                                          accumulator+=((long)(source[byteIndex++]&0xFF))<<(b*8+emptySpace);
81                                                  }
82                                                  double sample = ((double)accumulator/(double)Long.MAX_VALUE);
83                                                  sample *= rGain;                                
84                                          
85                                                  int intValue = (int)((double)sample*(double)Integer.MAX_VALUE);                         
86                                                  for(int i=0; i<bytesPerSample; i++){
87                                                          source[i+byteIndex2]=(byte)(intValue >>> ((i+2)*8) & 0xff);
88                                                  }
89                                                  byteIndex2 += bytesPerSample;   
90                                          }
91                                          
92                                          //average
93                                          if(bytesPerSample == 2){
94                                                  x = frameIndex*nChannels*bytesPerSample+(c*bytesPerSample);
95                                                  a = Math.abs((short)(((data[x+1] & 0xFF) << 8) | (data[x] & 0xFF)));
96                                          }else{
97                                                  a = Math.abs(data[frameIndex*nChannels +c]);
98                                          }
99                                          
100                                         temp += a;
101                                         mLeftTemp += (c==0)? a : 0;
102                                         mRightTemp += (c==1)? a : 0;
103                                         }//end for(channel)
104                         }//end for(frameIndex)
105                         
106                         mAverage = temp / (data.length / bytesPerSample);
107 //                      System.out.println("result 1 is: "+mAverage);
108 //                      System.out.println("result 2 is: "+calculateAverageValue());
109                         
110                         mLeftChannelAverage = mLeftTemp / (data.length/bytesPerSample/nChannels);
111                         mRightChannelAverage = mRightTemp / (data.length/bytesPerSample/nChannels);
112                         Amplitude ampl = new Amplitude(mAverage, mLeftChannelAverage, mRightChannelAverage);
113                         AmplitudePollAPI.getInstance().onAmplitudeReached(ampl);
+2

10 , (, ). , . 10 , - :

gain(t) = gain_old + (gain_new - gain_old) * 0.5 * (1 - cos(π * (t - t0) / (t1 - t0)))

t0, t1 - , .

+1

All Articles