📄 mididlg.cpp
字号:
// midiDlg.cpp : implementation file
//
#include "stdafx.h"
#include "midi.h"
#include "midiDlg.h"
#include "fileopen.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CFile file1;
CString File1Name;
int T_i;
long tickTM;
int oldCom;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CMidiDlg dialog
CMidiDlg::CMidiDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMidiDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMidiDlg)
m_edit1 = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMidiDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMidiDlg)
DDX_Text(pDX, IDC_EDIT1, m_edit1);
DDX_Control(pDX, IDC_MSCOMM1, m_MyComm);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMidiDlg, CDialog)
//{{AFX_MSG_MAP(CMidiDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMidiDlg message handlers
BOOL CMidiDlg::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
UpdateData(TRUE);
if ( m_MyComm.GetPortOpen() ) m_MyComm.SetPortOpen(FALSE);
m_MyComm.SetCommPort(1);//设定串口 COM1
m_MyComm.SetSettings("9600,S,8,1");
m_MyComm.SetInputMode(1);
m_MyComm.SetRThreshold(1);
m_MyComm.SetSThreshold(0);
m_MyComm.SetInputLen(0);
m_MyComm.SetPortOpen(TRUE);
m_MyComm.GetInput();
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMidiDlg::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 CMidiDlg::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 CMidiDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMidiDlg::OnButton1()
{
// TODO: Add your control notification handler code here
// TODO: Add your control notification handler code here
CFileOpen dlg;
if(dlg.DoModal()==IDOK)
{
m_edit1=dlg.GetPathName();
if(m_edit1.GetLength())
{
OFSTRUCT of;
if(OpenFile(m_edit1, &of, OF_EXIST) != HFILE_ERROR)
{
UpdateData(FALSE);
File1Name=m_edit1;
}
else
{
CString tmp;
tmp.Format("%s文件不存在!",m_edit1);
MessageBox(tmp);
}
}
}
file1.Open(File1Name,CFile::modeRead);
}
void CMidiDlg::DelayMS(long Delay)
{
// Delay=Delay;
do
{
Sleep(tickTM);
}while(Delay--);
}
long CMidiDlg::ReadTimer()
{
BYTE buf[255];
UINT nbyte;
BYTE tim[4];
long tm;
nbyte=file1.Read(buf,1);
if(nbyte == 0)
{
return 0;
}
else
{
if(buf[0]<=0x7f)
{
tm = buf[0];
return tm;
}
else
{
tim[0] = buf[0];
nbyte=file1.Read(buf,1);
if(buf[0]<=0x7f)
{
tim[1] = buf[0];
tim[0] = tim[0] & 0x7f;
if(tim[0]&0x1==1)
{
tim[1] = tim[1] | 0x80;
tim[0] >>= 1;
tm = tim[0]*256+tim[1];
}
else
{
tim[0] >>= 1;
tm = tim[0]*256+tim[1];
}
}
else
{
tim[1] = buf[0];
nbyte=file1.Read(buf,1);
if(buf[0]<=0x7f)
{
tim[2] = buf[0];
tim[0] &= 0x7f;
tim[1] &= 0x7f;
if(tim[1]&0x1==1)
{
tim[2] |= 0x80;
}
tim[1] >>= 1;
if(tim[0]&0x1==1)
{
tim[1] |= 0x40;
}
if(tim[0]&0x2==2)
{
tim[1] |= 0x80;
}
tim[0] >>= 2;
tm = tim[0]*0x10000 + tim[1]*0x100 + tim[2];
}
else
{
tim[2] = buf[0];
nbyte=file1.Read(buf,1);
tim[3] = buf[0];
tim[0] &= 0x7f;
tim[1] &= 0x7f;
tim[2] &= 0x7f;
if(tim[2]&0x1==1)
{
tim[3] |= 0x80;
}
tim[2] >>= 1;
if(tim[1]&0x1==1)
{
tim[2] |= 0x40;
}
if(tim[1]&0x2==2)
{
tim[2] |= 0x80;
}
tim[1] >>= 2;
if(tim[0]&0x1 == 1)
{
tim[1] |= 0x20;
}
if(tim[0]&0x2 == 2)
{
tim[1] |= 0x40;
}
if(tim[0]&0x4 == 4)
{
tim[1] |= 0x80;
}
tim[0] >>= 3;
tm = tim[0]*0x1000000 + tim[1]*0x10000 + tim[2]*0x100 + tim[3];
}
}
return tm;
}
}
}
void CMidiDlg::OnOK()
{
// TODO: Add extra validation here
BYTE buf[250];
UINT nbyte,sudu,len;
CByteArray sendsbuf;
CString tmp;
long timer;
oldCom = 0;
do
{
nbyte=file1.Read(buf,1);
if(nbyte == 0) break;
// add chu li
if(buf[0] == 0x4d)
{
nbyte=file1.Read(buf,3);
if(nbyte == 0) break;
if(buf[0]==0x54 && buf[1]==0x68 && buf[2]==0x64)
{
nbyte = file1.Read(buf,10);
sudu = buf[8] * 256 + buf[9];
}
else if(buf[0]==0x54 && buf[1]==0x72 && buf[2]==0x6b)
{
nbyte = file1.Read(buf,4);
do
{
timer = ReadTimer();
if(timer!=0)
{
// delay
DelayMS(timer);
}
nbyte = file1.Read(buf,1);
if(buf[0]>=0x80)
{
if(buf[0] == 0x0ff)
{
oldCom = 0x0ff;
nbyte = file1.Read(buf,1);
if(buf[0] == 0x51)
{
file1.Read(buf,4);
tickTM = (buf[1]*0x10000+buf[2]*0x100+buf[3])/sudu;
tickTM = tickTM/1000;
}
else if(buf[0] == 0x2f)
{
nbyte = file1.Read(buf,1);
MessageBox("MIDI END");
}
else
{
nbyte = file1.Read(buf,1);
len = buf[0];
file1.Read(buf,len);
}
}
else if(buf[0] == 0x0f0)
{
oldCom = 0x0f0;
nbyte = file1.Read(buf,1);
len = buf[0];
file1.Read(buf,len);
}
else if(buf[0] == 0x0f7)
{
oldCom = 0x0f7;
nbyte = file1.Read(buf,1);
len = buf[0];
file1.Read(buf,len);
}
else if((buf[0] & 0xf0) == 0x90)
{
oldCom = 0x90;
file1.Read(buf,2);
sendsbuf.SetSize(3);
sendsbuf[0] = 0x90;
sendsbuf[1] = buf[0];
sendsbuf[2] = buf[1];
m_MyComm.SetOutput(COleVariant(sendsbuf));
}
else if((buf[0] & 0xf0) == 0x80)
{
oldCom = 0x80;
file1.Read(buf,2);
sendsbuf.SetSize(3);
sendsbuf[0] = 0x80;
sendsbuf[1] = buf[0];
sendsbuf[2] = buf[1];
m_MyComm.SetOutput(COleVariant(sendsbuf));
}
else if((buf[0] & 0xf0) == 0x0a0)
{
oldCom = 0x0a0;
file1.Read(buf,2);
sendsbuf.SetSize(3);
sendsbuf[0] = 0x90;
sendsbuf[1] = buf[0];
sendsbuf[2] = buf[1];
m_MyComm.SetOutput(COleVariant(sendsbuf));
}
else if((buf[0] & 0xf0) == 0x0b0)
{
oldCom = 0x0b0;
file1.Read(buf,2);
}
else if((buf[0] & 0xf0) == 0x0c0)
{
oldCom = 0x0c0;
file1.Read(buf,1);
}
else if((buf[0] & 0xf0) == 0x0d0)
{
oldCom = 0x0d0;
file1.Read(buf,1);
}
else if((buf[0] & 0xf0) == 0x0e0)
{
oldCom = 0x0e0;
file1.Read(buf,2);
}
}
else
{
if(oldCom == 0x90)
{
sendsbuf.SetSize(3);
sendsbuf[0] = 0x90;
sendsbuf[1] = buf[0];
file1.Read(buf,1);
sendsbuf[2] = buf[0];
m_MyComm.SetOutput(COleVariant(sendsbuf));
}
else if(oldCom == 0x80)
{
sendsbuf.SetSize(3);
sendsbuf[0] = 0x80;
sendsbuf[1] = buf[0];
file1.Read(buf,1);
sendsbuf[2] = buf[0];
m_MyComm.SetOutput(COleVariant(sendsbuf));
}
else if(oldCom == 0x0a0)
{
oldCom = 0x0a0;
file1.Read(buf,1);
}
else if(oldCom == 0x0b0)
{
oldCom = 0x0b0;
file1.Read(buf,1);
}
else if(oldCom == 0x0c0)
{
}
else if(oldCom == 0x0d0)
{
}
else if(oldCom == 0x0e0)
{
oldCom = 0x0e0;
file1.Read(buf,1);
}
}
}while(nbyte!=0);
}
}
}while(nbyte!=0);
MessageBox("MIDI END");
// CDialog::OnOK();
}
void CMidiDlg::OnButton2()
{
// TODO: Add your control notification handler code here
SetTimer(1,1000,NULL);
T_i = 20;
}
void CMidiDlg::OnButton3()
{
// TODO: Add your control notification handler code here
CString str;
CByteArray sendsbuf;
UpdateData(TRUE);
KillTimer(1);
KillTimer(2);
sendsbuf.SetSize(3);
sendsbuf[0] = 0x90;
sendsbuf[1] = T_i;
sendsbuf[2] = 0;
m_MyComm.SetOutput(COleVariant(sendsbuf));
str.Format("STOP T_i = %xH",T_i);
MessageBox(str);
UpdateData(FALSE);
}
void CMidiDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CString str;
CByteArray sendsbuf;
UpdateData(TRUE);
if(nIDEvent == 1)
{
KillTimer(1);
// MessageBox("Stop 1 and Start 2");
// START SEND PIANO KEY 90 NN VV
T_i++;
if(T_i>108)
{
T_i = 21;
}
sendsbuf.SetSize(3);
sendsbuf[0] = 0x90;
sendsbuf[1] = T_i;
sendsbuf[2] = 100;
m_MyComm.SetOutput(COleVariant(sendsbuf));
// str.Format("OPEN T_i = %xH",T_i);
// MessageBox(str);
SetTimer(2,1000,NULL);
}
else if(nIDEvent == 2)
{
KillTimer(2);
// MessageBox("Stop 2 AND Start 1");
// STOP SEND PIANO KEY 90 NN 00
sendsbuf.SetSize(3);
sendsbuf[0] = 0x90;
sendsbuf[1] = T_i;
sendsbuf[2] = 0;
m_MyComm.SetOutput(COleVariant(sendsbuf));
// str.Format("STOP T_i = %xH",T_i);
// MessageBox(str);
SetTimer(1,1000,NULL);
}
UpdateData(FALSE);
CDialog::OnTimer(nIDEvent);
}
BEGIN_EVENTSINK_MAP(CMidiDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CMidiDlg)
ON_EVENT(CMidiDlg, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CMidiDlg::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -