在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
wav 文件格式参考资料:http://www.sonicspot.com/guide/wavefiles.html 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文件.
代码
1 #include <stdio.h>
2 #include <stdlib.h> 3 4 #define SIZE 1000*1024 5 6 typedef struct twav_header 7 { 8 // RIFF WAVE chunk, 12 bytes 9 unsigned char RIFF[4]; 10 unsigned int dwFilesize; 11 unsigned char WAVE[4]; 12 13 // Format chunk, 24 bytes 14 unsigned char fmt[4]; 15 unsigned int dwChunksize; 16 unsigned short wCompress; 17 unsigned short wChannelnum; 18 unsigned int dwSamplerate; 19 unsigned int dwBytepersec; 20 unsigned short wBlockAlign; 21 unsigned short wBitsPerSample; 22 23 // Data chunk, 8 bytes 24 unsigned char data[4]; 25 unsigned int dwDataSize; 26 }wav_header; 27 28 char riff[4] = {'R','I','F','F'}; 29 char wav[4] = {'W','A','V','E'}; 30 char fmt[4] = {'f','m','t', 0x20}; 31 char data[4] = {'d','a','t','a'}; 32 33 int main() 34 { 35 wav_header wavhead; 36 FILE *fp; 37 FILE *fpout; 38 char infile[10], outfile[10]; 39 unsigned char * pBuffer; 40 unsigned short * pwBuffer; 41 unsigned short * pw11Buffer; 42 unsigned short * pwnormBuffer; // Normalized data buffer 43 int i,cnt,channelno; 44 45 pBuffer = (unsigned char *)malloc( SIZE *sizeof(unsigned char) ); 46 memset(pBuffer, 0, SIZE); 47 48 pwBuffer = (unsigned short *)malloc( (SIZE/2 )*sizeof(unsigned short) ); 49 memset(pBuffer, 0, (SIZE/2 )*sizeof(unsigned short)); 50 51 pw11Buffer = (unsigned short *)malloc( (SIZE/12)*sizeof(unsigned short) ); 52 memset(pw11Buffer, 0, (SIZE/12)*sizeof(unsigned short)); 53 54 pwnormBuffer = (unsigned short *)malloc( (SIZE/12)*sizeof(unsigned short) ); 55 memset(pwnormBuffer, 0, (SIZE/12)*sizeof(unsigned short)); 56 57 puts("Input file name:"); 58 gets(infile); 59 puts("Output file name:"); 60 gets(outfile); 61 puts("Channel no(1-12):"); 62 scanf("%d", &channelno); 63 64 if( (fp=fopen(infile, "rb")) == NULL ) 65 { 66 printf("Cannot open file\n"); 67 return 0; 68 } 69 fseek(fp,0L,SEEK_SET); 70 fread(pBuffer,1,SIZE,fp); 71 72 // Convert 8 bit to 16 bit 73 for(i=0; i<SIZE/2; i++) 74 pwBuffer[i] = pBuffer[2*i]*256 + pBuffer[2*i+1]; 75 76 // Extract channel channelno data 77 cnt = 0; 78 for(i=0; i<SIZE/2; i++) 79 if( (pwBuffer[i-1] == 0x5A5A) && (pwBuffer[i] == 0x5A5A) ) 80 pw11Buffer[cnt++] = pwBuffer[i + channelno]; 81 82 // Normalize the data, (data-32768) 83 for(i=0; i<cnt; i++) 84 pwnormBuffer[i] = pw11Buffer[i] - 32768; 85 86 //for(i=0; i<10; i++) 87 // printf("%4X %4X\n", pw11Buffer[i], pwnormBuffer[i]); 88 89 // Initialize the wav file header 90 memcpy(wavhead.RIFF, riff, 4); 91 wavhead.dwFilesize = cnt*2 + sizeof(wav_header) - 8; 92 memcpy(wavhead.WAVE, wav , 4); 93 memcpy(wavhead.fmt, fmt , 4); 94 wavhead.dwChunksize = 0x10; 95 wavhead.wCompress = 1; 96 wavhead.wChannelnum = 1; 97 wavhead.dwSamplerate = 1000; 98 wavhead.dwBytepersec = 2*1000; 99 wavhead.wBlockAlign = 2; 100 wavhead.wBitsPerSample = 16; 101 memcpy(wavhead.data, data, 4); 102 wavhead.dwDataSize = cnt*2; 103 104 // Write the data into DAT file 105 if( (fpout=fopen(outfile, "wb")) == NULL ) 106 { 107 printf("Cannot open file\n"); 108 return 0; 109 } 110 111 fwrite(&wavhead, sizeof(wav_header), 1, fpout); 112 fwrite(pwnormBuffer, sizeof(unsigned short), cnt, fpout); 113 114 free(pBuffer); free(pwBuffer); free(pw11Buffer); 115 system("pause"); 116 } 117
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论