C Programming-read dat data and convert it to a WAV file

Source: Internet
Author: User

Dat data format 5A 5A 5A xx (frame header) the frame header is followed by the first ad Channel 1 data (2 bytes), the second Ad Channel 1 data (2 bytes), and the first ad Channel 2 data (2 bytes ), the second AD Channel 2 data (2 bytes ),......... The first ad Channel 6 data (2 byte) and the second Ad Channel 6 data (2 byte ). I extracted the first ad Channel 6 data. Then convert it to 16 bit, and then-32768 to positive and negative signals, because ad collects 0-5 V signals, minus 32768 to convert it to-2.5 V ~ + 2.5 V. Then, write the wav file into the wav file according to the wav file structure.

Http://download.csdn.net/source/2206332 code and DAT files.

# 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 wblockal; <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; <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/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.