Click here to Skip to main content
15,886,026 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

Note: I would appreciate very much if you could provide a brief description of your project, 
as well as any report on bugs or errors. This will help me improve the code for the benefit of your
application and the other users  
///////////////////////////////////////////////////////////////////////

Version: 1.9.0
Module : MidiStationDlg.h
Purpose: Defines the header implementation for the CMidiStationDlg class
Created: CJP / 4-10-2002
History: CJP / 3-24-2003 
	
	  // 11-10-2002 Update

	  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 CDirectMidi 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

	  // 6 - 10 - 2003 Update
	   
	  11. Added exception handling
	  
	  12. Included the piano keyboard control

	  13. Enhanced implicit multitask synchronization system
	  
      14. Simplified the class hierarchy 

	  15. Program structure grouped into new classes
	  
      16. Fixed bugs in calls to DirectMidi library

	  17. Program released under GNU (General Public License) terms	
	
*/

#if !defined(AFX_MIDISTATIONDLG_H__3BE1DA07_610B_11D6_B3BC_EC5E2C31CF36__INCLUDED_)
#define AFX_MIDISTATIONDLG_H__3BE1DA07_610B_11D6_B3BC_EC5E2C31CF36__INCLUDED_

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

// Some required headers

#include ".\\DirectMidi\\CDirectMidi.h" // The DirectMidi wrapper library
#include "MessagesList.h" // Messages list control
#include "AboutDlg.h"  // About dialog
#include "Record.h"	   // Class definition for record object
#include "Keyboard.h"  // Keyboard controller
#include "Piano\\Piano.h"

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

using namespace directmidi;

// Note receiver class

class CMIDIReceiver:public CReceiver
{
public:
	void IsNoteOn(unsigned char Note);
	void IsNoteOff(unsigned char Note);
};

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

class CMidiStationDlg : public CDialog
{

public:
	CMidiStationDlg(CWnd* pParent = NULL);	// standard constructor
	~CMidiStationDlg();
	
	void EnumPorts();  // Enumerate system ports
	void InitializeDirectMusic();  //Initialize directx/directmusic objects
	void InitializeKeyboard();     // Intialize the keyboard piano control
	void InitializeInstruments();  // Configure the GM instruments list
	void InitializeMessageList();  // Initializes the messages list control  
	void InitializeControls();     // Initializes the rest of the controls
	void InitializeEventHandler(); // Initializes the event handler and timer
	void CloseDown();			   // Close down the application	
	void PerformMidiMsgOut(unsigned char note,BOOL bOn); // Sends a midi message to the port
	void ReleaseAllKeys(); // Release all keys in the piano control
	void PrepareSoftwareSynth(); // Prepare a software synthesizer
	void StopRecord();           // Stops the recording
	BOOL InitializeLibrary();    // Initializes the directmidi library 
	static UINT ProcessMidiRecord(LPVOID lpParam); // Entry point for the playback thread
	BOOL  m_bAbandon,m_bExitPlayBack,m_bSwSynth;    
	DWORD m_nInPortSel,m_nOutPortSel;	// Selected ports
	HANDLE m_hEvent;					// The event
	BOOL  m_bActivated;					// Application activation flag	

	CPiano m_Piano;		      // The piano control	
	CMIDIReceiver m_Receiver; // The receiver object
	InstrumentState State;	  // Keyboard state enum variable	
	// Dialog Data
	//{{AFX_DATA(CMidiStationDlg)
	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;
	CMessagesList	m_MessagesList;
	CComboBox		m_InstList;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CMidiStationDlg)
	public:
	virtual BOOL PreTranslateMessage(MSG* pMsg);
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);
	virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
	//}}AFX_VIRTUAL



protected:
	HICON m_hIcon;
	// Generated message map functions
	//{{AFX_MSG(CMidiStationDlg)
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnButtonExpand();
	afx_msg void OnSelchangeInstruments();
	afx_msg void OnRecord();
	afx_msg void OnTimer(UINT nIDEvent);
	afx_msg void OnPlayBack();
	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()
};


/////////////////////////////////////////////////////////////////////////////
// CDirectInputPort class
// Derived from CInputport class

class CDirectInputPort:public CInputPort
{
public:
	void RecvMidiMsg(REFERENCE_TIME rt,DWORD dwChannel,DWORD dwBytesRead,BYTE *lpBuffer) {};
	void RecvMidiMsg(REFERENCE_TIME rt,DWORD dwChannel,DWORD dwMsg);
};



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

#endif // !defined(AFX_MIDISTATIONDLG_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