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


// ReadWavFileFormatDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "audiotest.h" 
#include "ReadWavFileFormatDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CReadWavFileFormatDlg dialog 
 
 
CReadWavFileFormatDlg::CReadWavFileFormatDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CReadWavFileFormatDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CReadWavFileFormatDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
} 
 
 
void CReadWavFileFormatDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CReadWavFileFormatDlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CReadWavFileFormatDlg, CDialog) 
	//{{AFX_MSG_MAP(CReadWavFileFormatDlg) 
	ON_BN_CLICKED(IDC_OPEN_FILE, OnOpenFile) 
	ON_BN_CLICKED(IDC_DEL_HEAD, OnDelHead) 
	ON_BN_CLICKED(IDC_ADD_HEAD, OnAddHead) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CReadWavFileFormatDlg message handlers 
#define OWAV_FILE_DATA_BEGIN_POS 46 
#define OWAV_DATALEN_POS 42 
#define OWAV_FILE_LEN_POS 4 
#define OWAV_FILE_FormatTag_POS 20 
#define OWAV_FILE_CHANNEL_POS 22 
#define OWAV_FILE_SamplesPerSec_POS 24 
#define OWAV_FILE_AvgBytesPerSec_POS 28 
#define OWAV_FILE_BlockAlign_POS 32 
#define OWAV_FILE_BitsPerSample_POS 34 
#define OWAV_FILE_Size_POS 36 
#define OWAV_FILE_HEAD_LEN  38;//4/*RIFF*/+4/*TOTAL_BYBTES*/+4/*WAVE*/+4/*FMT*/+4/*WAVEFORMAT LEN*/+sizeof(WAVEFORMAT)  ; 
#define OWAV_FORMAT_LEN  18;//sizeof(WAVEFORMATEX); 
typedef struct OWAVEFORMAT 
{ 
	unsigned short        wFormatTag;         /* format type */ 
	unsigned short        wChannels;          /* number of channels (i.e. mono, stereo...) */ 
	unsigned long       dwSamplesPerSec;     /* sample rate */ 
	unsigned long       dwAvgBytesPerSec;    /* for buffer estimation */ 
	unsigned short        wBlockAlign;        /* block size of data */ 
	unsigned short        wBitsPerSample;     /* number of bits per sample of mono data */ 
	unsigned short        wFileSize;             /* the count in bytes of the size of */ 
	unsigned long		dwFileLen;//oliver 
	unsigned char		ucRiff[5]; 
				/* extra information (after cbSize) */ 
}; 
int& OGetFileLen(char* pathname) 
{ 
	FILE *pfile=NULL; 
	pfile=fopen(pathname, "rb"); 
	 
	static int len=0; 
	if(pfile==NULL) 
		return len; 
	 
	while(!feof(pfile)) 
	{ 
		fseek(pfile, len++, 0); 
	} 
	fclose(pfile); 
	return len; 
} 
int OGetWavFileFormat(char* pathname, OWAVEFORMAT& waveformat) 
{ 
	FILE *pfile=NULL; 
	pfile=fopen(pathname, "rb"); 
	 
	if(pfile==NULL) 
		return 0; 
 
	{ 
		fseek(pfile, 0, 0); 
		 
		fread(waveformat.ucRiff, 4, 1, pfile); 
		waveformat.ucRiff[4]='\0'; 
		 
		fseek(pfile, OWAV_FILE_LEN_POS,0); 
		fread(&(waveformat.dwFileLen), 4, 1, pfile); 
		 
		fseek(pfile,  OWAV_FILE_FormatTag_POS, 0); 
		fread(&(waveformat.wFormatTag), 2, 1, pfile); 
 
		fseek(pfile,  OWAV_FILE_CHANNEL_POS, 0); 
		fread(&(waveformat.wChannels), 2, 1, pfile); 
		 
		fseek(pfile,  OWAV_FILE_SamplesPerSec_POS, 0); 
		fread(&(waveformat.dwSamplesPerSec), 4, 1, pfile); 
 
		fseek(pfile,  OWAV_FILE_AvgBytesPerSec_POS, 0); 
		fread(&(waveformat.dwAvgBytesPerSec), 4, 1, pfile); 
 
		fseek(pfile,  OWAV_FILE_BlockAlign_POS, 0); 
		fread(&(waveformat.wBlockAlign), 2, 1, pfile); 
 
		fseek(pfile,  OWAV_FILE_BitsPerSample_POS, 0); 
		fread(&(waveformat.wBitsPerSample), 2, 1, pfile); 
		 
		fseek(pfile,  OWAV_FILE_Size_POS, 0); 
		fread(&(waveformat.wFileSize), 2, 1, pfile); 
	} 
	fclose(pfile); 
 
	return 1; 
} 
 
 
int ODelWavFileHead(char* pathname, char *phead) 
{ 
	OWAVEFORMAT waveformat; 
	if(!OGetWavFileFormat(pathname, waveformat)) 
	{ 
		return 0; 
	} 
		 
	FILE *pfile=NULL; 
	pfile=fopen(pathname, "rb"); 
	if(pfile==NULL) 
		return 0; 
	fseek(pfile, 0, 0); 
	fread(phead, OWAV_FILE_DATA_BEGIN_POS, /*OWAV_FILE_DATA_BEGIN_POS,*/ 1, pfile); 
	FILE *pdesfile; 
	pdesfile=fopen("c:\\delhead.wav","wb+"); 
	fseek(pfile,  OWAV_FILE_DATA_BEGIN_POS/*OWAVE_FILE_HEAD_LEN*/, 0); 
	unsigned long write_len=waveformat.dwFileLen-OWAV_FILE_DATA_BEGIN_POS; 
	char* write_buf=new char[write_len]; 
	fread(write_buf, write_len, 1, pfile); 
	fwrite(write_buf, write_len, 1, pdesfile); 
	delete[] write_buf; 
	fclose(pdesfile); 
 
	fclose(pfile); 
 
	return 1; 
} 
 
int OAddWavFileHead(char* pathname, char *phead) 
{ 
	FILE *pfile=NULL; 
	pfile=fopen(pathname, "rb"); 
	if(pfile==NULL) 
		return 0; 
	 
	FILE *pdesfile; 
	pdesfile=fopen("c:\\addhead.wav","wb+"); 
	 
	fwrite(phead, OWAV_FILE_DATA_BEGIN_POS, 1, pdesfile); 
 
	unsigned long write_len=1000*1024/*OGetFileLen(pathname)*/; 
	char* write_buf=new char[write_len]; 
	fread(write_buf, write_len, 1, pfile); 
	fseek(pdesfile, OWAV_FILE_DATA_BEGIN_POS/*OWAVE_FILE_HEAD_LEN*/, 0); 
	fwrite(write_buf, write_len, 1, pdesfile); 
	 
	 
	delete[] write_buf; 
	fclose(pdesfile); 
 
	fclose(pfile); 
 
	return 1; 
} 
 
void CReadWavFileFormatDlg::OnOpenFile()  
{ 
	/*CFileDialog(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs 
		LPCTSTR lpszDefExt = NULL, 
		LPCTSTR lpszFileName = NULL, 
		DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
		LPCTSTR lpszFilter = NULL, 
		CWnd* pParentWnd = NULL); 
		*/ 
	CString path_name; 
	CFileDialog dlg(true, ".wav", "test", OFN_READONLY, "wav file(*.wav)|*.wav|", this); 
	if(dlg.DoModal()==IDOK) 
	{ 
		path_name=dlg.GetPathName(); 
	 
		OWAVEFORMAT waveformat; 
		if(!OGetWavFileFormat(path_name.GetBuffer(path_name.GetLength()+1), waveformat)) 
		{ 
			AfxMessageBox("error, return"); 
			return; 
		} 
		 
		char buf[1000]; 
		sprintf(buf, "RIFF='%s';\nfilelen=%i;\nchannels=%i;\nSamplesPerSec=%i;\nwBitsPerSample=%i;\n",  
			waveformat.ucRiff,  
			waveformat.dwFileLen, 
			waveformat.wChannels, 
			waveformat.dwSamplesPerSec, 
			waveformat.wBitsPerSample); 
 
		AfxMessageBox(buf); 
 
	} 
	 
} 
 
char pwavhead[OWAV_FILE_DATA_BEGIN_POS]; 
 
void CReadWavFileFormatDlg::OnDelHead()  
{ 
	 
	CString path_name; 
	CFileDialog dlg(true, "wav", "test", OFN_READONLY, "wav file(*.wav)|*.wav|", this); 
	if(dlg.DoModal()==IDOK) 
	{ 
		path_name=dlg.GetPathName(); 
		ODelWavFileHead(path_name.GetBuffer(path_name.GetLength()+1), pwavhead); 
	} 
} 
 
void CReadWavFileFormatDlg::OnAddHead()  
{ 
	CString path_name; 
	CFileDialog dlg(true, "wav", "test", OFN_READONLY, "wav file(*.wav)|*.wav|", this); 
	if(dlg.DoModal()==IDOK) 
	{ 
		path_name=dlg.GetPathName(); 
		if(OAddWavFileHead(path_name.GetBuffer(path_name.GetLength()+1), pwavhead)) 
			AfxMessageBox("ok"); 
		else 
			AfxMessageBox("error"); 
	} 
}