www.pudn.com > Audio1.rar > Audio1Dlg.cpp


// Audio1Dlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Audio1.h" 
#include "Audio1Dlg.h" 
#include "Vfw.h" 
#include "Mmsystem.h" 
#include "BoostedCommittee.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CAudio1Dlg dialog 
 
CAudio1Dlg::CAudio1Dlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CAudio1Dlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CAudio1Dlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CAudio1Dlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAudio1Dlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAudio1Dlg, CDialog) 
	//{{AFX_MSG_MAP(CAudio1Dlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_Begin, OnBegin) 
	ON_BN_CLICKED(IDC_initial, Oninitial) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CAudio1Dlg message handlers 
 
BOOL CAudio1Dlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX < 0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CAudio1Dlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CAudio1Dlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
                                   
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CAudio1Dlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CAudio1Dlg::OnBegin()  
{ 
	// TODO: Add your control notification handler code here 
   int  circle; 
    
   circle=0; 
   HPSTR    lpData; 
   float _huge *    lpdata; 
	 //for(circle=0;circle<60;circle++) 
   while(circle<10) 
	 {	if(MCIWndCanRecord(g_hWndMCIWnd))    //判断是否可以录音 
	{  
		MCIWndRecord(g_hWndMCIWnd);    //录音 
	} 
	else 
	{ 
		AfxMessageBox("error #01");     
	} 
	Sleep(2000); 
	MCIWndStop(g_hWndMCIWnd); //1*(2-1) s后停止录音 
	CString m_Path; 
	//CFileDialog wav(FALSE,NULL,NULL,OFN_ALLOWMULTISELECT,"wav File (*.wav)|*.wav"); 
//	if(wav.DoModal() == IDOK) 
//	{ 
	m_Path ="D:\\c4.wav";//wav.GetPathName(); 
//	} 
//	else 
//		 return; 
 
	if(MCIWndCanSave(g_hWndMCIWnd)) 
	{ 
		 MCIWndSave(g_hWndMCIWnd, "a");  
		 CopyFile("a",m_Path,FALSE); 
	} 
	else 
		 AfxMessageBox("error Can`t save");//保存结束 
   // AfxMessageBox(m_Path); 
     CString pString= m_Path;//wav.GetPathName();  
                                                                  
	MMCKINFO mmckinfoParent;  
	MMCKINFO mmckinfoSubChunk;  
	DWORD    dwFmtSize; 
	HMMIO    m_hmmio; //音频文件句柄  
	DWORD    m_WaveLong;  
//	HPSTR    lpData; //音频数据 
	//float _huge *    lpData; 
//	float _huge *    lpdata; 
	DWORD m_dwDataOffset;  
	DWORD m_dwDataSize;  
	LPSTR    szFileName = (LPSTR)LPCTSTR(pString); 
    m_hmmio = mmioOpen(szFileName, NULL, MMIO_READWRITE); 
	if (!m_hmmio) 
	{ 
		MessageBox("WAV文件打开失败!"); 
		return; 
	} 
	//检查打开文件是否是声音文件  
	mmckinfoParent.fccType = mmioFOURCC('W','A','V','E');  
	if(mmioDescend(m_hmmio, (LPMMCKINFO)&mmckinfoParent, NULL, MMIO_FINDRIFF))  
	{  
	//NOT WAVE FILE AND QU99v  
	}  
 
	mmckinfoSubChunk.ckid = mmioFOURCC('f','m','t',' ');  
	if(mmioDescend(m_hmmio, &mmckinfoSubChunk, &mmckinfoParent, MMIO_FINDCHUNK))  
	{  
	//Can't find 'fmt' chunk  
	}  
 
 
	//获得 'fmt '块的大小,申请内存  
	dwFmtSize = mmckinfoSubChunk.cksize ;  
	WAVEFORMATEX m_PCMWaveFmtRecord; 
 
	if((unsigned long)mmioRead(m_hmmio, (LPSTR)&m_PCMWaveFmtRecord, dwFmtSize) != dwFmtSize)  
	{  
	//failed to read format chunk  
	}  
	 
	CString nchannels; 
	//CString  nSamplesPerSec; 
	//float  fs; 
 
    nchannels.Format("%d",m_PCMWaveFmtRecord.nChannels); 
      //nSamplesPerSec.Format("%d",m_PCMWaveFmtRecord.nSamplesPerSec); 
	  //fs=atof(nSamplesPerSec); 
      //MessageBox(fs); 
    //MessageBox(nchannels); 
	 
	//离开fmt块 
	mmioAscend(m_hmmio, &mmckinfoSubChunk, 0);  
 
    //nBitsPerSpl = lpFormat->wBitsPerSample; 
 
	//寻找 'data' 块  
	mmckinfoSubChunk.ckid = mmioFOURCC('d','a','t','a');  
	if(mmioDescend(m_hmmio, &mmckinfoSubChunk, &mmckinfoParent, MMIO_FINDCHUNK))  
	{  
	//Can't find 'data' chunk  
	}  
                                                     
	//获得 'data'块的大小  
	m_dwDataSize = mmckinfoSubChunk.cksize; 
 
	m_dwDataOffset = mmckinfoSubChunk.dwDataOffset;  
 
	if(m_dwDataSize==0L)  
	{  
		//no data in the 'data' chunk                                    
	}  
	 
	//为音频数据分配内存  
	lpData = new char[m_dwDataSize];  
	//lpData = new float[m_dwDataSize];  
	lpdata=new float[(11025/256)*13]; 
//float *lpdata; 
	if(!lpData)  
	{  
	    //faile  
	} 
	 
	if(mmioSeek(m_hmmio, m_dwDataOffset, SEEK_SET)<0)  
	{  
	//Failed to read the data chunk  
	}  
	 
	m_WaveLong = mmioRead(m_hmmio, lpData, m_dwDataSize);  
	//CString  ss; 
	//ss.Format("%d",m_dwDataSize); 
//MessageBox(ss); 
	if(m_WaveLong<0)  
	{  
	//Failed to read the data chunk  
	}  
 
	CString s; 
 
    int  i; 
    int j; 
	j=0; 
	for(i=0;i<11025/256;i++) 
	{float dataR[256],dataI[256],mel[13]; 
	for(j=0;j<256;j++)   {dataR[j]=lpData[i*256+j+11025*circle];dataI[j]=0;} 
		//for(j=0;j<13;j++)    mel[j]=lpdata[i*13+j]; 
        CAudio1Dlg::mfcc(dataR,dataI,mel,11000,256); 
        for(j=0;j<13;j++)    lpdata[j+i*13]=mel[j];} 
 
	for(i=0;i<13;i++) 
	{s.Format("%f", lpdata[i+13]);} 
//s.Format("%d", m_WaveLong); 
//s.Format("%d", m_dwDataSize); 
	//MessageBox("read \n" + s);} 
    //float i; 
	i=lpData[3]; 
//MessageBox("read \n" + i); 
	int size; 
	size=m_dwDataSize; 
	//保存到文件 
	//CFile file; 
	//file.Open("D:\\a.txt",CFile::modeCreate|CFile::modeReadWrite); 
	//file.Write(lpData,m_dwDataSize); 
	//file.Close(); 
	 
//	CString temp; 
//	int i = lpData[10]; 
 
//	temp.Format("%d",i); 
//	MessageBox(temp); 
	int num=1; 
	double Sample[143];  for(j=0;j<6;j++)  {    
   for(i=0;i<143;i++)    Sample[i]=lpdata[i+2*78]; 
	CBoostedCommittee Boost; 
	//Opening file saved in MATLAB 
  FILE *fid = fopen("D:\\C program\\audio2\\Audio1\\RealBoost13.txt","r"); 
    //Loading classifier 
  Boost.LoadFromFile(fid); 
 
  //Using classifier, the signum of output is the predicted class {-1,+1} 
  double Result = Boost.Predict(Sample); 
	//printf("Result is %f \n", Result); 
//	CString   re; 
   
	if(Result<0) 
		//	PlaySound( "D:\\gun1.wav",0,0); 
 
		num=num+1; 
	//	MessageBox("abnormal!");   
	}	 
	if(num>1)    
		MessageBox("abnormal!");   
	else MessageBox("normal!"); 
		//PlaySound( "D:\\gun1.wav",0,0); 
		 
	 
	//	break;} 
	  // re.Format("%f", Result); 
//MessageBox("read \n" + re); 
	circle++; 
} 
	delete lpData; 
	delete lpdata;	 
 
} 
 
void CAudio1Dlg::Oninitial()  
{ 
	// TODO: Add your control notification handler code here 
    MCIWndClose(g_hWndMCIWnd); 
	g_hWndMCIWnd = MCIWndCreate(this->m_hWnd,::AfxGetApp()->m_hInstance,WS_CAPTION,NULL);   
	MCIWndNew(g_hWndMCIWnd,"waveaudio");   
} 
 
 
//mfcc function for N points 
#include 
#include 
#define pi 3.14159 
float CAudio1Dlg::mfcc(float dataR[],float dataI[],float mel[13],float f,int N)// 
{//log energy of s[N] 
    int i,k,j,L,m; 
	//float E; 
    mel[0]=0; 
    for(i=0;i0) 
   {b=b*2;i--;}/*b=pow(2,L-1)*/ 
    for(j=0;j0) /*p=pow(2,log10(N)/log10(2)-L)*j*/ 
{p=p*2; i--;} 
p=p*j; 
for(k=j;klower[i]&&j*f/N