在asr平台实现pcm格式录制的文件增加wav头,发现根据网上给的方法添加wav头的wav文件播放异常,播放没有声音!
后来发现问题出现在下面几个地方的问题:
1、samplerate和bitrate转换不正确;
int bit_rate = sample_rate * channels * bits;//(sample_rate * channels * bits)/8;
2、文件总大小填的不正确;
pcm数据大小+44 - 8 = PCM数据大小+36
int file_size = len + 44;
//04~07 header[4] = (char) ((file_size -8) & 0xff); header[5] = (char) (((file_size -8) >> 8) & 0xff); header[6] = (char) (((file_size -8) >> 16) &0xff); header[7] = (char) (((file_size -8) >> 24) &0xff);
3、每秒字节数:
header[34] = (char) (sample_bit<<3) & 0xff;
header[35] = (sample_bit<<3)>> 8;
static int pcmAddWavHeader(FILE_ID fp, int channels, int bits, int sample_rate, int len) { if (NULL == fp) { printf("Input file ptr is null.\n"); return -1; } int file_size = len + 44; // int channel = channels; // int bit_rate = sample_rate * channels * bits;//(sample_rate * channels * bits)/8; // int sample_bit = bits ; // int data_size = len; //pcm char header[44] = "\0"; //wav memset(header, 0x00, 44); //00~03 header[0] ='R'; header[1] ='I'; header[2] ='F'; header[3] ='F'; //04~07 header[4] = (char) ((file_size -8) & 0xff); header[5] = (char) (((file_size -8) >> 8) & 0xff); header[6] = (char) (((file_size -8) >> 16) &0xff); header[7] = (char) (((file_size -8) >> 24) &0xff); //3:08~11 header[8] ='W'; header[9] ='A'; header[10] ='V'; header[11] ='E'; //4:12~15 header[12] ='f'; header[13] ='m'; header[14] ='t'; header[15] =' '; //5:16~19 header[16] =16; header[17] =0; header[18] =0; header[19] =0; //6:20~21 header[20] =1; header[21] =0; //7:22~23 header[22] = (char) channel; header[23] =0; //8:24~27 header[24] = (char) (sample_rate &0xff); header[25] = (char) ((sample_rate >>8) & 0xff); header[26] = (char) ((sample_rate >>16) & 0xff); header[27] = (char) ((sample_rate >>24) & 0xff); //9:28~31 header[28] = (char) (bit_rate &0xff); header[29] = (char) ((bit_rate >>8) & 0xff); header[30] = (char) ((bit_rate >>16) & 0xff); header[31] = (char) ((bit_rate >>24) & 0xff); //10:32~33 header[32] = (char) (channel* sample_bit / 8); header[33] =0; //11:34~35 header[34] = (char) sample_bit; header[35] =0; //12:36~39 header[36] ='d'; header[37] ='a'; header[38] ='t'; header[39] ='a'; //13:40~43 header[40] = (char) (data_size &0xff); header[41] = (char) ((data_size >>8) & 0xff); header[42] = (char) ((data_size >>16) & 0xff); header[43] = (char) ((data_size >>24) & 0xff); FDI_fseek(fp, 0, SEEK_SET); FDI_fwrite(header, 44, 1, fp); } static void checkPCMHeader(int file_number){ FILE_ID fd_old = FDI_fopen(record_name , "rb"); FDI_fseek(fd_old, 0L, SEEK_END); ¨ int file_size = FDI_ftell(fd_old); FDI_fseek(fd_old, 0L, SEEK_SET); char *mempcm = (char *)malloc(file_size); if (mempcm == NULL){ DAL_AUDIO_DBG(checkPCMHeader,"mempcm malloc failed, fd:%d\r\n",file_size); FDI_fclose(fd_old); return; } memset(mempcm, 0x00, file_size); int ret = FDI_fread(mempcm, file_size, 1, fd_old); if (ret < 0){ DAL_AUDIO_DBG(checkPCMHeader,"FDI_fread failed, ret:%d\r\n",ret); FDI_fclose(fd_old); free(mempcm); return; } DAL_AUDIO_DBG(checkPCMHeader,"FDI_fread ok, ret:%d, file_size:%d",ret, file_size); FDI_fclose(fd_old); FILE_ID fd = FDI_fopen(record_name, "wb"); if (fd <= 0){ DAL_AUDIO_DBG(checkPCMHeader,"FDI_fopen failed, fd:%d\r\n",fd); FDI_fclose(fd); free(mempcm); return; } //write wav header pcmAddWavHeader(fd, 1, 2, 8000, file_size); FDI_fseek(fd, 44, SEEK_SET); FDI_fwrite(mempcm, file_size, 1, fd); FDI_fclose(fd); free(mempcm); }
-------------------广告线---------------
项目、合作,欢迎勾搭,邮箱:promall@qq.com
本文为呱牛笔记原创文章,转载无需和我联系,但请注明来自呱牛笔记 ,it3q.com