以下是源码,大路货,从网上找的.
但是,这个东西在MacOS上是有问题的,原因在最后,都是泪啊.
1 #include <stdio.h> 2 #include <string.h> 3 4 //wav头的结构如下所示: 5 typedef struct { 6 char fccID[4]; 7 unsigned long dwSize; 8 char fccType[4]; 9 }HEADER; 10 11 typedef struct { 12 char fccID[4]; 13 unsigned long dwSize; 14 unsigned short wFormatTag; 15 unsigned short wChannels; 16 unsigned long dwSamplesPerSec; 17 unsigned long dwAvgBytesPerSec; 18 unsigned short wBlockAlign; 19 unsigned short uiBitsPerSample; 20 }FMT; 21 22 typedef struct { 23 char fccID[4]; 24 unsigned long dwSize; 25 }DATA; 26 27 void show_usage() 28 { 29 printf("PCM 2 WAV usage eg:pcm2wav pcm_file wav_file channels sample_rate bits "); 30 } 31 32 int main(int argc, char **argv) 33 { 34 char src_file[128] = {0}; 35 char dst_file[128] = {0}; 36 int channels = 1; 37 int bits = 16; 38 int sample_rate = 22050; 39 40 //以下是为了建立.wav头而准备的变量 41 HEADER pcmHEADER; 42 FMT pcmFMT; 43 DATA pcmDATA; 44 45 unsigned short m_pcmData; 46 FILE *fp,*fpCpy; 47 48 if (argc < 5) 49 { 50 show_usage(); 51 return -1; 52 } 53 sscanf(argv[1], "%s", src_file); 54 sscanf(argv[2], "%s", dst_file); 55 sscanf(argv[3], "%d", &channels); 56 sscanf(argv[4], "%d", &sample_rate); 57 if (argc>6) 58 { 59 sscanf(argv[5], "%d", &bits); 60 } 61 62 printf("parameter analyse succeess "); 63 64 if((fp=fopen(src_file, "rb")) == NULL) //读取文件 65 { 66 printf("open pcm file %s error ", argv[1]); 67 return -1; 68 } 69 70 if((fpCpy=fopen(dst_file, "wb+")) == NULL) //为转换建立一个新文件 71 { 72 printf("create wav file error "); 73 return -1; 74 } 75 76 //以下是创建wav头的HEADER;但.dwsize未定,因为不知道Data的长度。 77 strcpy(pcmHEADER.fccID,"RIFF"); 78 strcpy(pcmHEADER.fccType,"WAVE"); 79 fseek(fpCpy,sizeof(HEADER),1); //跳过HEADER的长度,以便下面继续写入wav文件的数据; 80 81 //以上是创建wav头的HEADER; 82 if(ferror(fpCpy)) 83 { 84 printf("error "); 85 } 86 //以下是创建wav头的FMT; 87 pcmFMT.dwSamplesPerSec=sample_rate; 88 pcmFMT.dwAvgBytesPerSec=pcmFMT.dwSamplesPerSec*sizeof(m_pcmData); 89 pcmFMT.uiBitsPerSample=bits; 90 91 strcpy(pcmFMT.fccID,"fmt "); 92 pcmFMT.dwSize=16; 93 pcmFMT.wBlockAlign=2; 94 pcmFMT.wChannels=channels; 95 pcmFMT.wFormatTag=1; 96 //以上是创建wav头的FMT; 97 98 fwrite(&pcmFMT,sizeof(FMT),1,fpCpy); //将FMT写入.wav文件; 99 //以下是创建wav头的DATA; 但由于DATA.dwsize未知所以不能写入.wav文件 100 strcpy(pcmDATA.fccID,"data"); 101 102 pcmDATA.dwSize=0; //给pcmDATA.dwsize 0以便于下面给它赋值 103 fseek(fpCpy,sizeof(DATA),1); //跳过DATA的长度,以便以后再写入wav头的DATA; 104 fread(&m_pcmData,sizeof(unsigned short),1,fp); //从.pcm中读入数据 105 106 while(!feof(fp)) //在.pcm文件结束前将他的数据转化并赋给.wav; 107 { 108 109 pcmDATA.dwSize+=2; //计算数据的长度;每读入一个数据,长度就加一; 110 111 fwrite(&m_pcmData,sizeof(unsigned short),1,fpCpy); //将数据写入.wav文件; 112 fread(&m_pcmData,sizeof(unsigned short),1,fp); //从.pcm中读入数据 113 } 114 115 fclose(fp); //关闭文件 116 pcmHEADER.dwSize=44+pcmDATA.dwSize; //根据pcmDATA.dwsize得出pcmHEADER.dwsize的值 117 118 rewind(fpCpy); //将fpCpy变为.wav的头,以便于写入HEADER和DATA; 119 fwrite(&pcmHEADER,sizeof(HEADER),1,fpCpy); //写入HEADER 120 fseek(fpCpy,sizeof(FMT),1); //跳过FMT,因为FMT已经写入 121 fwrite(&pcmDATA,sizeof(DATA),1,fpCpy); //写入DATA; 122 123 fclose(fpCpy); //关闭文件 124 125 return 0; 126 }
说下被卡了几天的原因:64位.MacOS是64位的,结果我的Win7是64位的,另外找的一个CentOS也是64位的,就没往这上边想,看来真老了.
记住把操作函数里的strcpy改掉都.
下一篇---->WebRTC录音(3)-录音音质优化及编码设置