// SoundRecogniseDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "SoundRecognise.h"
#include "SoundRecogniseDlg.h"
#include "wavesame.h"
#pragma comment(lib,"WaveSame.lib")
#pragma comment(lib,"libmx.lib")
#pragma comment(lib,"libmatlb.lib")
#pragma comment(lib,"libmat.lib")
#pragma comment(lib,"libmmfile.lib")
#include <comutil.h>
#pragma comment(lib,"comsuppw.lib")
#include <Mmsystem.h>
#pragma comment(lib,"Winmm.lib")
#include <fstream>
#include <string>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CSoundRecogniseDlg 对话框
CSoundRecogniseDlg::CSoundRecogniseDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSoundRecogniseDlg::IDD, pParent)
, m_SOUNDFILE(_T(""))
, m_MESSAGE(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSoundRecogniseDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_SOUNDFILE, m_SOUNDFILE);
DDX_Text(pDX, IDC_MESSAGE, m_MESSAGE);
}
BEGIN_MESSAGE_MAP(CSoundRecogniseDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_RECOGNISE, &CSoundRecogniseDlg::OnBnClickedRecognise)
ON_WM_DROPFILES()
ON_WM_CLOSE()
END_MESSAGE_MAP()
// CSoundRecogniseDlg 消息处理程序
BOOL CSoundRecogniseDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
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);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
WaveSameInitialize();
filein=false;
m_SOUNDFILE=_T("未导入文件");
UpdateData(FALSE);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CSoundRecogniseDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CSoundRecogniseDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CSoundRecogniseDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CSoundRecogniseDlg::OnBnClickedRecognise()
{
// TODO: 在此添加控件通知处理程序代码
if(!filein)
{
MessageBox(_T("请先导入文件!"));
return;
}
int temp;
ifstream in;
string line;
in.open(_T("sound/map.txt"));
filenum=0;
while(in)
{
line="";
in>>temp;
getline(in,line);
if(line.length()>0)
{
filename[filenum++]=line.c_str();
}
}
double maxSame=-1;
CString strSame=_T("");
double numFind;
CString strFind;
for(int index=0;index<filenum;index++)
{
strFind.Format(_T("sound/%d.wav"),index);
m_MESSAGE.Format(_T("识别中,请稍候...:\t%s"),strFind);
UpdateData(FALSE);
numFind=GetSame(filepath,strFind);
if(numFind>maxSame)
{
maxSame=numFind;
strSame=filename[index];
}
}
m_MESSAGE.Format(_T("完成!识别结果:\t%s"),strSame);
UpdateData(FALSE);
}
void CSoundRecogniseDlg::OnDropFiles(HDROP hDropInfo)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
const int fileCount = DragQueryFile(hDropInfo, (UINT)-1, NULL, 0);
ASSERT(fileCount >= 1);
TCHAR fileName[MAX_PATH] = { 0 };
for (int i = 0; i < fileCount; ++i)
{
DragQueryFile(hDropInfo, i, fileName, MAX_PATH);
filepath=fileName;
m_SOUNDFILE=_T("已打开文件:")+filepath;
filein=true;
UpdateData(FALSE);
}
::DragFinish (hDropInfo);
sndPlaySound(filepath,SND_ASYNC);
}
double CSoundRecogniseDlg::GetSame(CString filepath1, CString filepath2)
{
BSTR bstrText1=SysAllocString(filepath1.GetBuffer());
char* f1=_com_util::ConvertBSTRToString(bstrText1);
BSTR bstrText2=SysAllocString(filepath2.GetBuffer());
char* f2=_com_util::ConvertBSTRToString(bstrText2);
mxArray *file1=mclGetUninitializedArray();
mxArray *file2=mclGetUninitializedArray();
mxArray *result=mclGetUninitializedArray();
mlfAssign(&file1,mxCreateString(f1));
mlfAssign(&file2,mxCreateString(f2));
mlfAssign(&result,mlfWave_same(file1,file2));
double *md=mxGetPr(result);
double res=md[0];
mxDestroyArray(file1);
mxDestroyArray(file2);
mxDestroyArray(result);
return res;
}
void CSoundRecogniseDlg::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
WaveSameTerminate();
CDialog::OnClose();
}