The principle of increasing or decreasing PCM audio data is primarily to multiply the sampled data by a number or divide by a number, but pay attention to overflow handling. Implementation of the following C language implementation
#define OLD_FILE_PATH "FILE.PCM" #define Vol_file_path "VOL.PCM" int volume_adjust (short * in_buf, short * out_buf, FL
Oat in_vol) {int i, TMP;
In_vol[0] float = in_vol-98;
if ( -98<vol && vol<0) Vol. = 1/(vol* (-1));
else if (0<=vol && vol<=1) vol. = 1;
* Else if (1<=vol && vol<=2) vol. = Vol;
*/Else if (vol<=-98) vol. = 0; else if (vol>=2) vol. = 40; This value can be adjusted to your actual situation to adjust TMP = (*in_buf) *vol;
The above all About Vol's judgment, actually is for here *in_buf times a multiple, you may according to own need to modify//The following code is mainly for overflow to judge if (tmp > 32767) tmp = 32767;
else if (TMP < -32768) TMP =-32768;
*OUT_BUF = tmp;
return 0;
} void Pcm_volume_control (int volume) {short s16in = 0;
Short s16out = 0;
int size = 0;
FILE *FP = fopen (Old_file_path, "rb+");
FILE *fp_vol = fopen (Vol_file_path, "wb+"); while (!feof (FP)) {size = Fread (&s16in, 2, 1, FP);
if (size>0) {volume_adjust (&s16in, &s16out, volume);
Fwrite (&s16out, 2, 1, fp_vol);
} fclose (FP);
Fclose (Fp_vol);
int main (void) {Pcm_volume_control (100);
return 0; }
In the above program, Pcm_volume_control (100) in the main function is set to 100 to allow it to go into the last else statement in the Volume_adjust function, and the final magnified data is to multiply it by 40. This value can be adjusted according to your own needs.
vol. = n;
TMP = (*in_buf) *vol;
After the above algorithm amplification, you can use the Audacity tool to view, amplified PCM data, as shown in the following figure, the above waveform is the original data, the following waveform is after volume amplification of the data
reference materials
1.PCM Sound Data parsing
2. Audio resampling and PCM volume adjustment final code
3.PCM Volume adjustment