📄 heartdlg.cpp
字号:
// heartDlg.cpp : implementation file
//
#include "stdafx.h"
#include "heart.h"
#include "heartDlg.h"
#include "Mmsystem.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define MAX_SIZE 80
MCIDEVICEID m_wDeviceID;
CRect m_rcWave; //波形图所在的矩形
CDC* dctemp;
CDC * m_pdc;
RECT rect;
CBitmap bitmap;
CBitmap *pOldBitmap;
CDC m_dcBuffer; //用于存放波形图像的内存DC
CDC m_dcLine; //用于存放波形频谱图像的内存DC
USHORT m_nMaxValue; //用于存入波形中的最大值
USHORT m_nMaxValueFreq; //用于存入波形频谱中的最大值
float m_fTime; //用于存入声音文件的时间长度
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CHeartDlg dialog
CHeartDlg::CHeartDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHeartDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHeartDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CHeartDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHeartDlg)
DDX_Control(pDX, IDC_FileName, FileName);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CHeartDlg, CDialog)
//{{AFX_MSG_MAP(CHeartDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_proc, OnBUTTONproc)
ON_BN_CLICKED(IDC_BUTTON1, OnButton_stop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHeartDlg message handlers
BOOL CHeartDlg::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
GetDlgItem(IDC_BUTTON_proc)->EnableWindow(FALSE);
GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CHeartDlg::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 CHeartDlg::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 CHeartDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CHeartDlg::OnOK()
{
//Invalidate();
CFileDialog wavdlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
TEXT("*.wav *.pcm | *.wav;*.pcm||"));
wavdlg.DoModal();
FileName.SetWindowText(wavdlg.GetPathName());
int len;
len = FileName.GetWindowTextLength();
if(!len)
{
MessageBox( "please choose a file or a directoy!", "waring", MB_OK );
return;
}
FileName.GetWindowText(heartsoundname);
HMMIO file1;//定义HMMIO文件句柄;
file1=mmioOpen(heartsoundname.GetBuffer(MAX_SIZE),NULL,MMIO_READWRITE);//以读写模式打开所给的WAVE文件;
if(file1==NULL)
{
MessageBox("WAVE文件打开失败!");
return;
}
//MessageBox("WAVE文件打开成功!");
char style[4];//定义一个四字节的数据,用来存放文件的类型;
char riff[4];
int filesize[2];
mmioRead(file1,riff,4);
mmioSeek(file1,4,SEEK_SET);
mmioRead(file1,(char*)filesize,8);
mmioSeek(file1,8,SEEK_SET);//定位到WAVE文件的类型位置
mmioRead(file1,style,4);
if(style[0]!='W'||style[1]!='A'||style[2]!='V'||style[3]!='E')//判断该文件是否为"WAVE"文件格式
{
MessageBox("该文件不是WAVE格式的文件!");
return;
}
PCMWAVEFORMAT format; //定义PCMWAVEFORMAT结构对象,用来判断WAVE文件格式;
mmioSeek(file1,20,SEEK_SET);
//对打开的文件进行定位,此时指向WAVE文件的PCMWAVEFORMAT结构的数据;
mmioRead(file1,(char*)&format,sizeof(PCMWAVEFORMAT));//获取该结构的数据;
if(format.wf.nChannels!=1)//判断是否是立体声声音;
{
//MessageBox("该声音文件不是单通道文件");
// return 0;
}
short int sample;
sample = format.wBitsPerSample;
mmioSeek(file1,24+sizeof(PCMWAVEFORMAT),SEEK_SET);
//获取WAVE文件的声音数据的大小;
long size[2];
mmioRead(file1,(char*)size,4);
short *pData;
pData = NULL;
if(size[0]>100000)
{
MessageBox("the wave is too big!");
}
pData= new short[size[0]/format.wf.nBlockAlign];//根据数据的大小申请缓冲区;
mmioSeek(file1,28+sizeof(PCMWAVEFORMAT),SEEK_SET);//对文件重新定位;
mmioRead(file1,(char *)pData,size[0]*sizeof(char));//读取声音数据;
mmioClose(file1, MMIO_FHOPEN);//关闭WAVE文件;
GetDlgItem(IDC_BUTTON_proc)->EnableWindow(TRUE);
GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
int points = size[0]/format.wf.nBlockAlign;
if (m_pdc!=NULL)
m_pdc->DeleteDC();
pDraw = GetDlgItem(IDC_PICTURE);
pDraw->GetClientRect(&rect); // 波形显示窗口尺寸
rect.bottom -= rect.top; // 调整为逻辑坐标
rect.right -= rect.left;
rect.top = 0;
rect.left = 0;
m_pdc = pDraw->GetDC();
int high = rect.bottom - rect.top;
int wide = rect.right - rect.left;
m_pdc->FillSolidRect(rect.left,rect.top,wide,high,RGB(0,0,0));
bitmap.CreateCompatibleBitmap(m_pdc,rect.right,rect.bottom);
m_dcBuffer.CreateCompatibleDC(m_pdc);
pOldBitmap=m_dcBuffer.SelectObject(&bitmap);
int PaintMiddle = (rect.top+rect.bottom)/2; // 中线
CPen oldpen(PS_SOLID, 1, RGB(0,255,0));
CDC* middle = pDraw->GetDC();
CPen middlepen(PS_SOLID, 2, RGB(255,0,0));
SelectObject(*middle,middlepen);
middle->MoveTo(rect.left,PaintMiddle);
middle->LineTo(rect.right,PaintMiddle);
CDC*data = pDraw->GetDC();
SelectObject(*data, oldpen);
data->MoveTo(rect.left,PaintMiddle);
for(int i = 0;i<points;i++)
{
data->LineTo(wide*i/points,pData[i]/high+rect.bottom/2);
}
middle->DeleteDC();
data->DeleteDC();
m_dcBuffer.DeleteDC();
bitmap.DeleteObject();
//delete []pData;
/*BROWSEINFO bi; //BROWSEINFO结构体
TCHAR Buffer[512]="";
TCHAR FullPath[512]="";
bi.hwndOwner = m_hWnd; //m_hWnd你的程序主窗口
bi.pidlRoot = NULL;
bi.pszDisplayName = Buffer; //返回选择的目录名的缓冲区
bi.lpszTitle = "请选择文件夹"; //弹出的窗口的文字提示
bi.ulFlags = BIF_RETURNONLYFSDIRS|BIF_EDITBOX|BIF_BROWSEFORCOMPUTER ; //只返回目录。其他标志看MSDN
bi.lpfn = NULL; //回调函数,有时很有用
bi.lParam = 0;
bi.iImage = 0;
ITEMIDLIST* pidl = ::SHBrowseForFolder (&bi); //显示弹出窗口,ITEMIDLIST很重要
if(::SHGetPathFromIDList (pidl,FullPath)) //在ITEMIDLIST中得到目录名的整个路径
{
//成功
MessageBox(FullPath);
}
else
{
//失败
MessageBox("no selecte file path.");
} // TODO: Add extra validation here
*/
// CDialog::OnOK();
}
int CHeartDlg::wav_process(char *wav_file)
{
return 0;
}
int CHeartDlg::play_wav(char *wav_file)
{
sndPlaySound(wav_file,SND_ASYNC);//只能播放100k以内的wav文件
/*播放wav文件
MCI_OPEN_PARMS mciOpenParms;
MCI_PLAY_PARMS PlayParms;
mciOpenParms.dwCallback = 0;
mciOpenParms.lpstrElementName = wav_file;//要播放的文件
mciOpenParms.wDeviceID = 0;
mciOpenParms.lpstrDeviceType = "waveaudio";
mciOpenParms.lpstrAlias = " ";
PlayParms.dwCallback = (DWORD)(this->m_hWnd);
PlayParms.dwTo = 0; //播放至什么位置
PlayParms.dwFrom = 0;//0代表从头开始播放
mciSendCommand(NULL,MCI_OPEN,MCI_OPEN_TYPE|MCI_OPEN_ELEMENT,(DWORD)(LPVOID)&mciOpenParms);//打开音频设备;
mciSendCommand(mciOpenParms.wDeviceID,MCI_PLAY,MCI_WAIT,(DWORD)(LPVOID)&PlayParms);//播放WAVE声音文件;
m_wDeviceID = mciOpenParms.wDeviceID; //将设备ID保存起来
*/
// return pData;
/*
char fname[512];
HANDLE PlayerHandle;
DWORD dwPlayerID;
FILE* fpWav;
strcpy(fname,wav_file);
//Get the length of wave file
fpWav=fopen(fname,"rb");
if (fpWav==NULL)
{
MessageBox("Can't open the wavfile:",fname,MB_OK);
return -1;
}
fseek(fpWav,0,SEEK_END);
//allWaveLen=ftell(fpWav)/sizeof(short);
fclose(fpWav);
//start play
*/
return 0;
}
void CHeartDlg::InitDC(CDC &dc,BOOL bWave)
{
/* if(dc.m_hDC!=NULL)
{
dc.DeleteDC();
}
//初始化DC并把它变成合适的大小(通过把一个指定大小的位图选入来实现)
CBitmap bmpTemp;
dc.CreateCompatibleDC(GetDC());
bmpTemp.CreateCompatibleBitmap(GetDC(),m_rcWave.Width(),m_rcWave.Height());
dc.SelectObject(&bmpTemp);
bmpTemp.DeleteObject();
//设定背景色并画出一个框,这样看着有点立体感
CBrush brBack;
if (bWave)
brBack.CreateSolidBrush(RGB(255,255,255)); ///波形背景刷
///brBack.CreateSolidBrush(RGB(255,255,255));
else
brBack.CreateSolidBrush(RGB(0,0,0)); ///频谱背景刷
///brBack.CreateSolidBrush(RGB(0,220,200));
dc.SelectObject(&brBack);
dc.Rectangle(0,0,m_rcWave.Width(),m_rcWave.Height());
brBack.DeleteObject();
dc.Draw3dRect(1,1,m_rcWave.Width()-1,m_rcWave.Height()-1,RGB(0,0,0),RGB(255,255,255));
dc.SetTextColor(RGB(0,0,0));
dc.SetBkMode(TRANSPARENT);
*/
}
void CHeartDlg::OnBUTTONproc()
{
// TODO: Add your control notification handler code here
//wav_process(heartsoundname.GetBuffer(MAX_SIZE));
play_wav(heartsoundname.GetBuffer(MAX_SIZE));
}
void CHeartDlg::OnButton_stop()
{
// TODO: Add your control notification handler code here
//mciSendCommand(m_wDeviceID,MCI_CLOSE,NULL,NULL);//关闭音频设备;
sndPlaySound(NULL,SND_ASYNC);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -