Click here to Skip to main content
15,886,110 members
Articles / Artificial Intelligence

Developing MIDI applications with DirectMusic

Rate me:
Please Sign up or sign in to vote.
4.91/5 (45 votes)
11 Apr 2008LGPL325 min read 609.5K   9.5K   147  
A wrapper class library for the DirectMusic MIDI.
/*
 __  __  _  ____  _   _________ __  ______ _  ____  __  _
|  \/  || ||  _ \| | (  /__  _//  \/_   _/| |/    \|  \| | 
| |\/| || || |_| | |  \ \ | | | <> | | |  | || <> || |\  | 
|_|  |_||_||____/|_| /___)|_| |_||_| |_|  |_|\____/|_| \_| 
                                           
////////////////////////////////////////////////////////////////////////
  
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.
 
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.
 
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 
Copyright (c) 2002-2003 by Carlos Jim�nez de Parga  
All rights reserved.
For any suggestion or failure report, please contact me by e-mail: 
cjimenez@servitel.es

Module : KeybDlg.h
Purpose: Define header implementation for the CKeybDlg class
Created: CJP / 04-10-2002
History: CJP / 03-25-2003 
	
	  Updated: 04/10/2002

	  1. Solved problems with Midi port selection
	  
	  2. Improved built-in keyboard
		
	  3. Supervised the multithread-safe application	

	  4. Improved the interactive interface 
	  
	  5. Application restructured to the new DirectMidi wrapper
	  
	  6. Added the Microsoft Software Synthesizer

	  7. Added keyboard support 
	  
	  8. Fixed bugs in classes design 
	  
      9. Improved playback thread using Events
	  
	  10. Guide line on the list control
	  	
	Updated: 03/25/2003

	  11. Added exception handling

	  12. Fixed bugs in directmidi functions calls

	  13. Project released under GNU(General Public License) terms	

Copyright (c) 2002 by C. Jim�nez de Parga  
All rights reserved.
*/

#if !defined(AFX_KEYBDLG_H__3BE1DA07_610B_11D6_B3BC_EC5E2C31CF36__INCLUDED_)
#define AFX_KEYBDLG_H__3BE1DA07_610B_11D6_B3BC_EC5E2C31CF36__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

const int MAX_LIST_ITEMS = 1000;	// Maximum list items 
const BYTE MAX_OCTAVES = 4; 
const BYTE MAX_KEYS = 24;
const BYTE ALLNOTES_OFF = 0xB0; // Turn off all current playing notes

// Some required headers
#include <afxmt.h>
#include ".\\DirectMidi\\CDirectMidi.h" // The DirectMidi wrapper library
#include "Link.h"	// Class for URL hyperlink


using namespace directmidi;

enum KeybState { RECORDING, STOPPED , PLAYING }; // Keyboard state

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD  =  IDD_ABOUTBOX };
	CStatic	m_ImageLogo;
	CLink	m_Link;
	CStatic	m_Image;
	//}}AFX_DATA
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnStaticUrl();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////
// CKeybDlg dialog

class CKeybDlg : public CDialog
{
// Construction
public:
	CKeybDlg(CWnd* pParent = NULL);	// standard constructor
	void EnumPorts();
	void InitializeDirectMusic();
	void InitializeKeyboard();
	void InitializeInstruments();
	void InitializeMessageList();
	void InitializeControls();
	void InitializeEventHandler();
	
	void StoresInformation(LVITEM &lvi,int nSubItem,CString strCad);
	void CloseDown();

	void RecvNote(unsigned char note);
	void ReleaseNote(unsigned char note);
	void SendMidiMsg(unsigned char note,BOOL bOn);
	void StopRecord();
	void ReleaseAllKeys();
	void PrepareSoftwareSynth();
	void KeyMessage(int nCode,BOOL NoteType,int nOctave);
	void OnKeyRecv(char KeyCode,BOOL NoteType);
	static UINT ProcessMidiRecord(LPVOID lpParam);
	BOOL AddListCtrl(unsigned char note,unsigned char velocity);
	BOOL InitializeLibrary();

	CString GetEngValue(unsigned char note);
	CString GetEspValue(unsigned char note);
	
	BOOL m_bAbandon,m_bExitPlayBack,m_bSwSynth,m_bActive;
	DWORD nInPortSel,nOutPortSel;
	UCHAR nLoNote,nHiNote;
	HANDLE m_hEvent;
	// Dialog Data
	//{{AFX_DATA(CKeybDlg)
	enum { IDD = IDD_KEYB_DIALOG };
	CStatic			m_Image;
	CComboBox		m_NoteRange;
	CProgressCtrl	m_MemProgress;
	CButton			m_Stop;
	CButton			m_Rec;
	CButton			m_Play;
	CComboBox		m_OutPortList;
	CComboBox		m_InPortList;
	CListCtrl		m_MessagesList;
	CComboBox		m_InstList;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CKeybDlg)
	public:
	virtual BOOL PreTranslateMessage(MSG* pMsg);
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);
	//}}AFX_VIRTUAL

// Implementation
protected:
	HICON m_hIcon;
	// Generated message map functions
	//{{AFX_MSG(CKeybDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnButtonExpand();
	afx_msg void OnSelchangeInstruments();
	afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
	afx_msg void OnRec();
	afx_msg void OnTimer(UINT nIDEvent);
	afx_msg void OnPlay();
	afx_msg void OnStop();
	afx_msg void OnClose();
	afx_msg void OnClear();
	afx_msg void OnSelchangeOutputPorts();
	afx_msg void OnSelchangeInputPorts();
	afx_msg void OnSelchangeNoteRange();
	afx_msg void OnHelp();
	afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
	afx_msg void OnInitMenu(CMenu* pMenu);
	afx_msg void OnMove(int x, int y);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

// Derivated class from CInputport

class CDirectInputPort:public CInputPort
{
	CKeybDlg* m_pKeybDlg;
public:
	void RecvMidiMsg(REFERENCE_TIME rt,DWORD dwChannel,DWORD dwBytesRead,BYTE *lpBuffer) {};
	void RecvMidiMsg(REFERENCE_TIME rt,DWORD dwChannel,DWORD dwMsg);
	void SetDlgReference(CKeybDlg *pKeybDlg) { m_pKeybDlg = pKeybDlg; } // Pointer to the main dialog object
};




//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_KEYBDLG_H__3BE1DA07_610B_11D6_B3BC_EC5E2C31CF36__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer
Spain Spain
I obtained my PhD degree in Computer Graphics at the National Distance Education University (UNED) in October 2019. I also hold a Ms. degree in Software Engineering and Computer Systems and a Bs. degree in Computer Science from the National Distance Education University (UNED).
I have been employed as a C++ software developer in several companies since year 2000.
I currently work as a Tutor-Professor of Symbolic Logic, Discrete Math and Java Object-Oriented Programming at UNED-Cartagena (Spain) since 2015.

Comments and Discussions