WAV文件的读写

wav文件比snd文件多了个头=======》可以说,wav文件去掉头,就是snd文件了

以下便是wav文件的读写代码:(按着结构体顺序解析即可)

#ifndef _WAVREAD_H
#define _WAVREAD_H

#ifdef   __cplusplus  
extern "C" {  
#endif  

#include "stdafx.h"
#include 

	typedef unsigned char uint8;
	typedef unsigned short int uint16;
	typedef unsigned int uint32;

	typedef struct WaveHeader 
	{ 
		uint8  riff[4];             //资源交换文件标志;
		uint32 size;               //从下个地址开始到文件结尾的字节数; 
		uint8  wave_flag[4];        //wave文件标识; 
		uint8  fmt[4];              //波形格式标识 ;
		uint32 fmt_len;            //过滤字节(一般为00000010H) ;
		uint16 tag;                //格式种类,值为1时,表示PCM线性编码 ;
		uint16 channels;           //通道数,单声道为1,双声道为2 ;
		uint32 samp_freq;          //采样频率 ;
		uint32 byte_rate;          //数据传输率 (每秒字节=采样频率×每个样本字节数) ;
		uint16 block_align;        //块对齐字节数 = channles * bit_samp / 8 ;
		uint16 bit_samp;           //bits per sample (又称量化位数) ;
	} wave_header_t; 


	typedef struct WaveStruct 
	{ 
		FILE *fp;                  //file pointer ;
		wave_header_t* pHeader;      //header; 
		uint8 data_flag[4];        //数据标识符 ;
		uint32 length;             //采样数据总数 ;
		uint32 *pData;             //data ;
	} wave_t; 

void wavread(const char * filename, wave_t *pWave);


#ifdef   __cplusplus  
 }  
#endif   /* end of __cplusplus */ 

#endif

//读文件

void wavread(const char * filename, wave_t *pWave)
{

   unsigned char temp = 0;
   uint32 byts;
   uint32 i=0,w=0,iLength;
   uint32 j;
   char iHigh;

    pWave->fp = fopen(filename,"rb");

   if(pWave->fp==NULL)                              // open file 
   {  
        printf("file open failure!\n");   
		return;
   }  
   if(4 != fread(&pWave->pHeader->riff, sizeof(uint8), 4, pWave->fp))           // RIFF chunk 
   {  
        printf("read riff error!\n");  
        return;  
   }  
    if(1 != fread(&(pWave->pHeader->size),  sizeof(uint32), 1, pWave->fp))         //SIZE : from here to file end 
    {  
        printf("read size error!\n");  
        return;  
    }  
    if(4 != fread(&pWave->pHeader->wave_flag, sizeof(uint8), 4, pWave->fp))      // wave file flag 
    {  
        printf("read wave_flag error!\n");  
        return;  
    }  
    if(4 != fread(&pWave->pHeader->fmt, sizeof(uint8), 4, pWave->fp))             //fmt chunk   
    {  
        printf("read fmt error!\n");  
        return;  
    }  
    if(1 != fread(&pWave->pHeader->fmt_len, sizeof(uint32), 1, pWave->fp))       //fmt length 
    {  
        printf("read fmt_len error!\n");  
        return;  
    }  
    if(1 != fread(&(pWave->pHeader->tag), sizeof(uint16), 1, pWave->fp))          // tag : PCM or not
    {  
        printf("read tag error!\n");  
        return;  
    }  
    if(1 != fread(&(pWave->pHeader->channels), sizeof(uint16), 1, pWave->fp))      //channels 
    {  
        printf("read channels error!\n");  
        return;  
    }  
    if(1 != fread(&(pWave->pHeader->samp_freq), sizeof(uint32), 1, pWave->fp))      // samp_freq 
    {  
        printf("read samp_freq error!\n");  
        return;  
    }  
    if(1 != fread(&(pWave->pHeader->byte_rate), sizeof(uint32), 1, pWave->fp))      // byte_rate : decode how many bytes per second  
    {                                                                       // byte_rate = samp_freq * bit_samp 
        printf("read byte_rate error!\n");  
        return;  
    }  
    if(1 != fread(&(pWave->pHeader->block_align), sizeof(uint16), 1, pWave->fp))       //quantize bytes for per samp point 
    {  
        printf("read byte_samp error!\n");  
        return;  
    }  
    if(1 != fread(&(pWave->pHeader->bit_samp), sizeof(uint16), 1, pWave->fp))        //quantize bits for per samp point
    {                                                                        // bit_samp = byte_samp * 8 
        printf("read bit_samp error!\n");  
        return;  
    }  

	do  	// jump to "data" for reading data   
    {  
        fread(&temp, sizeof(uint8), 1, pWave->fp);  
    }  
    while('d' != temp);  
    pWave->data_flag[0] = temp;  
	pWave->data_flag[4] = '\0';
	for (i = 1; i <= 3; i++)/* data chunk */  
	{	  
	  if(1 != fread(&pWave->data_flag[i], sizeof(uint8), 1, pWave->fp))
	  {
	     printf("read header data error!\n");  
        return;  
	  }
	} 
    if(1 != fread(&pWave->length, sizeof(uint32), 1, pWave->fp))                  //data length 
    {  
	   printf("read length error!\n");  
    }  
	

	byts=pWave->pHeader->bit_samp /8;	
	if(byts == 4 && pWave->pHeader->channels == 2) //32位PCM
	{
		uint32 idata;
	  pWave->pData = (uint32*) malloc(pWave->length * sizeof(uint32) *2);

	  iLength = (pWave->length)/(pWave->pHeader->bit_samp/8 * 2);
	  for (j = 0; j < iLength ; j++)
	  {
		 fread(&idata, 8, 1, pWave->fp);
		 pWave->pData[j] = idata;
	   }	    
	  pWave->length = iLength;
	}
	if(byts == 4 && pWave->pHeader->channels == 1) //32位PCM
	{
		uint32 idata;
		pWave->pData = (uint32*) malloc(pWave->length * sizeof(uint32));
		iLength= (pWave->length)/(pWave->pHeader->bit_samp/8);
		for (j = 0; j < iLength; j++)
		{
			fread(&idata, 4, 1, pWave->fp);
			pWave->pData[j] = idata; 
		}	    
		 pWave->length = iLength;
	}
	if (byts == 2 && pWave->pHeader->channels == 2 ) //16位双字节PCM
	{
		uint32 idata;
		pWave->pData = (uint32*) malloc(pWave->length * sizeof(uint32));
		iLength= (pWave->length)/(pWave->pHeader->bit_samp/8 *2);
	   for (j = 0; j < iLength; j++)   
	   {
		 fread(&idata,4, 1, pWave->fp);
		 (pWave->pData[j]) =idata;
	   }
	 }
	if (byts == 2 && pWave->pHeader->channels == 1 ) //16位双字节PCM		
	{	
		uint16 idata;
		pWave->pData = (uint32*) malloc(pWave->length * sizeof(uint32));
		
		for (j = 0; j < pWave->length; j++)   
		{
			fread(&idata, 2, 1, pWave->fp);    
			pWave->pData[j] =idata;	
		}
	}

     if (byts == 1 && pWave->pHeader->channels == 1 )//8位单字节PCM	
	 {
		 uint8 idata;
		 pWave->pData = (uint32*) malloc(pWave->length * sizeof(uint32)/4);
		 iLength = (pWave->length);
	   for (j = 0; j < iLength;j++)   
	   { 
	  	 fread(&iHigh, 1, 1, pWave->fp);
		 idata = (uint32)iHigh;
		 pWave->pData[j]= idata ;
	   }
	    pWave->length = iLength;
	 }
}



写文件:

       int idata;

	long  iByts, iLength;


	wave_t * pWaveOrigin;
	wave_t *pWaveOut;

	pWaveOrigin = (wave_t *)malloc(sizeof(wave_t));
	pWaveOrigin->pHeader = (wave_header_t *)malloc(sizeof(wave_header_t));

	pWaveOut = (wave_t *)malloc(sizeof(wave_t));
	

       fp1 = fopen("D:\\devoiceOut11.wav", "wb+");

	fwrite(pWaveOrigin->pHeader,sizeof(wave_header_t),1,fp1); //将头写入.wav文件;
	fwrite(&(pWaveOrigin->data_flag) ,sizeof(uint8),4,fp1); //写入.wav文件; 
	fwrite(&(pWaveOrigin->length),sizeof(uint32),1,fp1);
	
	if(pWaveOrigin->pHeader->channels == 2)   
	{
		iByts = pWaveOrigin->pHeader->bit_samp/8;
		iLength = pWaveOrigin->length/pWaveOrigin->pHeader->block_align;
	
		if (iByts == 2)
		{
			uint32 idata;
			while(pWaveOrigin->length > j)
			{
				for(i=0; i<160; i++)
				{
					if(j > (pWaveOrigin->length-1)) continue;
					idata = pWaveOrigin->pData[j++];
					readbuffL[i] = idata & 0x0000ffff;
					readbuffH[i] = (idata & 0Xffff0000)>>16;
				}
				WebRtcNsx_Process(pNsxInst, readbuffL, NULL, writebuffL, NULL);
				WebRtcNsx_Process(pNsxInst, readbuffH, NULL, writebuffH, NULL);
				for(i=0; i<160; i++)
				{
					if(j > (pWaveOrigin->length-1)) continue;
					fwrite(&writebuffL[i], 2, 1, fp1);
					fwrite(&writebuffH[i], 2, 1, fp1);
				}
			}
		}
		
		if (iByts == 1)
		{
			while(pWaveOrigin->length > j)
			{
				for(i=0; i<160; i++)
				{
					if(j > (pWaveOrigin->length-1)) continue;
					readbuff[i] = (short)pWaveOrigin->pData[j++];
				}
				WebRtcNsx_Process(pNsxInst, readbuff, NULL, writebuff, NULL);
				for(i=0; i<160; i++)
				{
					if(j > (pWaveOrigin->length-1)) continue;
					writebuff[i] = (char)writebuff[i];
					
				}
				fwrite(writebuff, 1, 160, fp1);
			}
		}
		
	}
	
	if(pWaveOrigin->pHeader->channels == 1)   
	{
		
		iByts = pWaveOrigin->pHeader->bit_samp/8;
		iLength = pWaveOrigin->length /( pWaveOrigin->pHeader->bit_samp/8);

		if (iByts == 2)
		{
			while(pWaveOrigin->length > j)
			{
				for(i=0; i<160; i++)
				{
					if(j > (pWaveOrigin->length-1)) continue;
					readbuff[i] = (short)(pWaveOrigin->pData[j++]);
				}
				WebRtcNsx_Process(pNsxInst, readbuff, NULL, writebuff, NULL);


				fwrite(writebuff, 2, 160, fp1);
			}

		}
	
		if (iByts == 1)
		{
			while(pWaveOrigin->length > j)
			{
				for(i=0; i<160; i++)
				{
					if(j > (pWaveOrigin->length-1)) continue;
					readbuff[i] = (short)pWaveOrigin->pData[j++];
				}
				WebRtcNsx_Process(pNsxInst, readbuff, NULL, writebuff, NULL);
				
				for(i=0; i<160; i++)
				{
					writebuff[i] = (char)writebuff[i];
				}
				fwrite(writebuff, 1, 160, fp1);

			}
		}
	}
	free(pWaveOrigin);
	free(pWaveOut);
	fclose(fp1);


你可能感兴趣的:(测试代码)