C programming-讀取DAT資料,轉成wav格式檔案

來源:互聯網
上載者:User

DAT資料格式 5A 5A 5A 5A xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx ( 幀頭 ) 幀頭後面依次為第一塊AD通道1資料(2 byte),第二塊AD通道1資料(2 byte),第一塊AD通道2資料(2 byte),第二塊AD通道2資料(2 byte),.。。。。。。。。第一塊AD通道6資料(2 byte),第二塊AD通道6資料(2 byte)。我提取的是第一塊AD通道6資料。然後進行轉成16bit,然後-32768變成正負訊號,因為AD採集的是0-5V訊號,減去32768將其變成-2.5V~+2.5V。然後根據wav檔案結構將其寫入wav檔案。

http://download.csdn.net/source/2206332 代碼和dat檔案。

#include <stdio.h><br />#include <stdlib.h></p><p>#define SIZE 1300*1024<br />#define SPS 8000</p><p>typedef struct twav_header<br />{<br /> // RIFF WAVE chunk, 12 bytes<br /> unsigned char RIFF[4];<br /> unsigned int dwFilesize;<br /> unsigned char WAVE[4];</p><p> // Format chunk, 24 bytes<br /> unsigned char fmt[4];<br /> unsigned int dwChunksize;<br /> unsigned short wCompress;<br /> unsigned short wChannelnum;<br /> unsigned int dwSamplerate;<br /> unsigned int dwBytepersec;<br /> unsigned short wBlockAlign;<br /> unsigned short wBitsPerSample;</p><p> // Data chunk, 8 bytes<br /> unsigned char data[4];<br /> unsigned int dwDataSize;<br />}wav_header;</p><p>char riff[4] = {'R','I','F','F'};<br />char wav[4] = {'W','A','V','E'};<br />char fmt[4] = {'f','m','t', 0x20};<br />char data[4] = {'d','a','t','a'};</p><p>int main()<br />{<br /> wav_header wavhead;<br /> FILE *fp;<br /> FILE *fpoutwav;<br /> FILE *fpoutdat;<br /> char infile[20], outfilewav[20], outfiledat[20];<br /> unsigned char * pBuffer;<br /> unsigned short * pwBuffer;<br /> unsigned short * pwNoBuffer;<br /> unsigned short * pwnormBuffer; // Normalized data buffer<br /> int i,cnt,channelno;</p><p> pBuffer = (unsigned char *)malloc( SIZE *sizeof(unsigned char) );<br /> memset(pBuffer, 0, SIZE);</p><p> pwBuffer = (unsigned short *)malloc( (SIZE/2 )*sizeof(unsigned short) );<br /> memset(pBuffer, 0, (SIZE/2 )*sizeof(unsigned short));</p><p> pwNoBuffer = (unsigned short *)malloc( (SIZE/12)*sizeof(unsigned short) );<br /> memset(pwNoBuffer, 0, (SIZE/12)*sizeof(unsigned short));</p><p> pwnormBuffer = (unsigned short *)malloc( (SIZE/12)*sizeof(unsigned short) );<br /> memset(pwnormBuffer, 0, (SIZE/12)*sizeof(unsigned short));</p><p> puts("Input file name(dat):");<br /> gets(infile);<br /> puts("Output file name(wav):");<br /> gets(outfilewav);<br /> puts("Output file name(dat):");<br /> gets(outfiledat);<br /> puts("Channel no(1-12):");<br /> scanf("%d", &channelno);</p><p> if( (fp=fopen(infile, "rb")) == NULL )<br /> {<br /> printf("Cannot open file:%s/n",infile);<br /> system("pause");<br /> return 0;<br /> }<br /> fseek(fp,0L,SEEK_SET);<br /> fread(pBuffer,1,SIZE,fp);</p><p> for(i=0; i<100; i++)<br /> printf("%X ",*(pBuffer+i));</p><p> // Extract channel channelno data<br /> cnt = 0;<br /> for(i=0; i<SIZE-36; i++)<br /> if( (pBuffer[i] == 0xFF) && (pBuffer[i+1] == 0x5A) && (pBuffer[i+2] == 0x5A) && (pBuffer[i+3] == 0x5A) )<br /> pwNoBuffer[cnt++] = pBuffer[i + 4 + 2*(channelno-1)]*256 + pBuffer[i + 4 + 2*(channelno-1) + 1];</p><p> for(i=0; i<5; i++)<br /> printf("/n%X %d",*(pwNoBuffer+i),cnt);</p><p> // Normalize the data, (data-32768)<br /> for(i=0; i<cnt; i++)<br /> pwnormBuffer[i] = pwNoBuffer[i] - 32768;</p><p> // Initialize the wav file header<br /> memcpy(wavhead.RIFF, riff, 4);<br /> wavhead.dwFilesize = cnt*2 + sizeof(wav_header) - 8;<br /> memcpy(wavhead.WAVE, wav , 4);<br /> memcpy(wavhead.fmt, fmt , 4);<br /> wavhead.dwChunksize = 0x10;<br /> wavhead.wCompress = 1;<br /> wavhead.wChannelnum = 1;<br /> wavhead.dwSamplerate = SPS;<br /> wavhead.dwBytepersec = 2*SPS;<br /> wavhead.wBlockAlign = 2;<br /> wavhead.wBitsPerSample = 16;<br /> memcpy(wavhead.data, data, 4);<br /> wavhead.dwDataSize = cnt*2;</p><p> // Write the data into Wav file<br /> if( (fpoutwav=fopen(outfilewav, "wb")) == NULL )<br /> {<br /> printf("Cannot open file:%s/n",outfilewav);<br /> system("pause");<br /> return 0;<br /> }</p><p> fwrite(&wavhead, sizeof(wav_header), 1, fpoutwav);<br /> fwrite(pwnormBuffer, sizeof(unsigned short), cnt, fpoutwav);</p><p> // Write the data into DAT file<br /> if( (fpoutdat=fopen(outfiledat, "wb")) == NULL )<br /> {<br /> printf("Cannot open file:%s/n",outfiledat);<br /> system("pause");<br /> return 0;<br /> }</p><p> fwrite(pwNoBuffer, sizeof(unsigned short), cnt, fpoutdat);</p><p> fclose(fp);fclose(fpoutwav);fclose(fpoutdat);<br /> free(pBuffer); free(pwBuffer); free(pwNoBuffer);<br /> system("pause");<br /> return 0;<br />}<br />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.