本文介绍了将PCM16LE左声道音量减半的方法,PCM音频数据可以使用音频编辑软件导入查看,推荐免费开源的音频编辑软件Audacity。本文参考文献链接:https://blog.csdn.net/leixiaohua1020/article/details/50534316
PCM16LE简介
PCM16LE双声道数据中左声道和右声道的采样值是间隔存储的。本文中声音样值的采样频率一律是44100Hz,采样格式一律为16LE。“16”代表采样位数是16bit。由于1Byte=8bit,所以一个声道的一个采样值占用2Byte。“LE”代表Little Endian,代表2 Byte采样值的存储方式为高位存在高地址中。
下图为输入的双声道PCM数据的波形图。上面的波形图是左声道的图形,下面的波形图是右声道的波形。
函数代码
函数的代码如下所示:
int CTransPic::simplest_pcm16le_halfvalueleft(char* url){
ifstream infptr;
infptr.open(url, ios::in | ios::binary);
ofstream hflfptr;
hflfptr.open("G:/ffmpeg/simplest_mediadata_test/halfvalueleft.pcm", ios::out | ios::binary);
int cnt = 0;
unsigned char* halfbuf = (unsigned char*)malloc(4);
while (!infptr.eof())
{
short *samplenum = NULL;
infptr.read((char*)halfbuf, 4);
samplenum = (short*)halfbuf;
*samplenum = (*samplenum) / 2;
hflfptr.write((const char*)samplenum, 2);
hflfptr.write((const char*)halfbuf + 2, 2);
++cnt;
}
cout << cnt << endl;
free(halfbuf);
infptr.close();
hflfptr.close();
return 0;
}
测试用例
左声道音量减半后的双声道波形图如下:
注:本文参考文献链接https://blog.csdn.net/leixiaohua1020/article/details/50534316