如下是源碼,大路貨,從網上找的.函數
可是,這個東西在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\n eg:pcm2wav pcm_file wav_file channels sample_rate bits\n"); 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\n"); 63 64 if((fp=fopen(src_file, "rb")) == NULL) //讀取文件 65 { 66 printf("open pcm file %s error\n", argv[1]); 67 return -1; 68 } 69 70 if((fpCpy=fopen(dst_file, "wb+")) == NULL) //爲轉換創建一個新文件 71 { 72 printf("create wav file error\n"); 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\n"); 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位的,就沒往這上邊想,看來真老了.ui
記住把操做函數裏的strcpy改掉都.編碼
下一篇---->WebRTC錄音(3)-錄音音質優化及編碼設置spa