www.pudn.com > AudioTest.rar > OWaveProc.cpp


#include "stdlib.h" 
#include "stdio.h" 
 
typedef struct OWAVEFORMAT 
{ 
    unsigned short        wFormatTag;         /* format type */ 
    unsigned short        nChannels;          /* number of channels (i.e. mono, stereo...) */ 
    unsigned long       nSamplesPerSec;     /* sample rate */ 
    unsigned long       nAvgBytesPerSec;    /* for buffer estimation */ 
    unsigned short        nBlockAlign;        /* block size of data */ 
    unsigned short        wBitsPerSample;     /* number of bits per sample of mono data */ 
    unsigned short        cbSize;             /* the count in bytes of the size of */ 
				    /* extra information (after cbSize) */ 
}; 
 
void WriteToWaveFile(unsigned char* pbuf, unsigned long len)  
{ 
	//if(filename==NULL) 
	char filename[]="/usr/src/tmp/test.wav"; 
	int fd; 
	OWAVEFORMAT waveform; 
	waveform.wFormatTag=1;//WAVE_FORMAT_PCM; 
	waveform.nChannels= 2;//aChans[m_cInChans.GetCurSel()]; 
	waveform.wBitsPerSample=8;//aBytes[m_cInBytes.GetCurSel()]; 
	waveform.nBlockAlign=waveform.nChannels*waveform.wBitsPerSample/8; 
	waveform.nSamplesPerSec= 8000;//aSPS[m_ctrlInSPS.GetCurSel()]; 
	waveform.nAvgBytesPerSec= waveform.nSamplesPerSec*waveform.nBlockAlign; 
	waveform.cbSize=0; 
 
	fd=open(filename, O_APPEND|O_RDWR); 
	if(fd==-1) 
	{ 
		perror("open file error.return\n"); 
		return; 
	} 
		 
	unsigned long m_WaveHeaderSize = 38;//4/*RIFF*/+4/*TOTAL_BYBTES*/+4/*WAVE*/+4/*FMT*/+4/*WAVEFORMAT LEN*/+sizeof(WAVEFORMAT)  ; 
	unsigned long m_WaveFormatSize = 18;//sizeof(WAVEFORMATEX); 
	m_file.SeekToBegin(); 
	write(fd, "RIFF",4);//4 
	unsigned int Sec=(len + m_WaveHeaderSize); 
	write(fd, &Sec,sizeof(Sec));//4 
	write(fd, "WAVE",4);//4 
	write(fd, "fmt ",4);//4 
	write(fd, &m_WaveFormatSize,sizeof(m_WaveFormatSize));//4 
	write(fd, &waveform.wFormatTag,sizeof(waveform.wFormatTag));//2 
	write(fd, &waveform.nChannels,sizeof(waveform.nChannels));//2 
	write(fd, &waveform.nSamplesPerSec,sizeof(waveform.nSamplesPerSec));//4 
	write(fd, &waveform.nAvgBytesPerSec,sizeof(waveform.nAvgBytesPerSec));//4 
	write(fd, &waveform.nBlockAlign,sizeof(waveform.nBlockAlign));//2 
	write(fd, &waveform.wBitsPerSample,sizeof(waveform.wBitsPerSample));//2 
	write(fd, &waveform.cbSize,sizeof(waveform.cbSize));//2 
	write(fd, "data",4);//4 
	write(fd, &len,4);//4 
/**///不能用一下这句替换上述waveform格式信息的写入	write(fd, &waveform, sizeof(waveform)); 
	write(fd, pbuf, len); 
	close(fd); 
	 
}