一、高变低
例如16k转换为8k,就是将16k降为8k,也就是每采集的16k两次数据变为1次,采用两个数据求平均除以2
二、低变高
例如 8k变为16k升频。将8k的每个数据变为两个,也就是后一个数据减去前一个数据除以2,加上前一个数据
/* @breaf 用于pcm音频采集率的转换
@param bit[in],输入文件和输出文件的采样位数
@param sourcefile[in],源文件名
@param sou_rate[in],源文件的采集率
@param disfile[out],目标文件名
@param dis_rate[in],目标文件的采集率
@return 0成功,-1出错
*/
void pcm_rate_trans(int bit,char * sourcefile,int sou_rate,char *disfile,int dis_rate )
{
FILE *fp_sou;
FILE *fp_dis;
fopen_s(&fp_sou, sourcefile, "rb+");
fopen_s(&fp_dis,disfile, "wb+");
if (fp_sou==NULL|| fp_dis==NULL)
{
printf("error open!\n");
}
int rate_len = sou_rate / dis_rate;
//减小采样率
if (rate_len > 0)
{
short tempRead = 0;
short tempSum = 0;
int flag = 0;
while (!feof(fp_sou))
{
if (fread(&tempRead, bit/8, 1, fp_sou) > 0)
{
tempSum = tempSum + tempRead; // 求和
flag++;
if (flag == rate_len)
{
flag = 0;
tempSum = tempSum / rate_len;
fwrite(&tempSum,bit/8, 1, fp_dis);
tempSum = 0;
}
}
}
}
//增大采样率
else
{
rate_len = dis_rate/ sou_rate ;
int tempRead1;
int tempRead2;
int tempSum;
int tempWrite;
int flag;
while (!feof(fp_sou))
{
if (fread(&tempRead1,bit/8, 1, fp_sou) >0&& fread(&tempRead2, bit / 8, 1, fp_sou) >0)
{
// tempSum = *((short *)&tempRead +1)-*(short *)&tempRead;//波距离
tempSum = tempRead2 - tempRead1;
flag = rate_len;
tempWrite =tempRead1;
do
{
tempWrite += tempSum / rate_len;
fwrite(&tempWrite, bit/8, 1, fp_dis);
} while (--flag!=0);
fseek(fp_sou, -1L*bit/8, SEEK_CUR);
}
}
}
fclose(fp_sou);
fclose(fp_dis);
}