Click here to Skip to main content
15,885,546 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.4K   9.5K   147  
A wrapper class library for the DirectMusic MIDI.
/*
 ___  _   __   _   _   __     ____  __   _   _  ______ ____    __   _
|   \| | /  \ | \ | | /  \   / __/ /  \ | \ | |/_   _/|  _ \  /  \ | |
|  _/| || <> ||  \| ||    | | |   |    ||  \| |  | |  | | | ||    || |
| |  | || || || |\  ||    | | |___|    || |\  |  | |  | |/ / |    || |__
|_|  |_||_|| ||_| |_| \__/   \___/ \__/ |_| |_|  |_|  |_|\_\  \__/ |____|


////////////////////////////////////////////////////////////////////////
  
  This library 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 library 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
  
  USING THE PIANO CONTROL STATICALLY
  
	1.- Place a custom control in your dialog with the resource editor

	2.- Fills the properties of the custom control. Fill the class name edit box with the
		value: CPianoClass

	3.- Add a CPiano member variable in the public part of your main CWnd derived class 

	4.- Modify the DoDataExchange method of you CWnd derived class, for example:
		
		CMidiStationDlg::DoDataExchange(CDataExchange* pDX)
		{
			CDialog::DoDataExchange(pDX);
			//{{AFX_DATA_MAP(CMidiStationDlg)
			// NOTE: the ClassWizard will add DDX and DDV calls here
			//}}AFX_DATA_MAP
			DDX_Control(pDX, IDC_PIANOCTRL, m_PianoCtrl);
		}

		Note: Place the DDX_Control function outside the AFX_DATA_MAP area.

	5.- Derive a class publicly from the CReciver class 
	
	6.- Instantiate a member variable of CReceiver derived class into your main CWnd derived class

	7.- Initialize the object in the corresponding derived CWnd OnInit() member function
 
  USING THE PIANO CONTROL DINAMICALLY

	1.- Add a CPiano class member variable to your CWnd derived class. This variable can be a pointer, 
		in this case you will need to allocate memory for it calling the operator new() and then you
		must delete the allocated object after ending the application

    2.- Call the member function Create of the CPiano class with the following values:
			CPiano::Create(nothing,nothing,dw_style,position and size,ID)
			Use WS_VISIBLE in dw_style always
	3.- Call the initialization functions of the CPiano object
    

  CPIANO CONTROL MEMBER FUNCTIONS SPECIFICATION
  
    1.- BOOL Initialize(int numOctaves, int scale)
		
		Pourpose: Initializes the piano control
		Parameters:
			- numOctaves: Number of required octaves
			- scale: A constan value respresenting the scale
		
		Returns:
			FALSE: Memory allocation failed
			TRUE: The object was created successfully

	 2.-virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, 
		const RECT& rect, CWnd* pParentWnd, UINT nID,CCreateContext* pContext = NULL)
			
		Pourpose: Creates the piano control window object	  
		Parameters:
			-lpszClassName: The name of the class for registration purposes
			-lpszWindowName: Name of the window for registration purposes
			-dwStyle: Style of the window
			-rect: Size and position of the control
			-pParentWnd: The pointer to the parent window
			-nID: Identification of the control
			-pContext: Creation context, can be NULL
	
		Returns:
			FALSE: The creation of the window failed
			TRUE: The window was created successfully
			
	 3.- void SetReceiver(CReceiver &Receiver)

		Pourpose: Sets the notification receiver
		Parameters:
			-Receiver: The receiver object

	 4.- void SetOctavesRange(int nLowLimit)
		
		Pourpose: Sets the range of the octaves from the lowest limit
		Parameters:
			- nLowLimit: The lowest range

	 5.- void BlitPiano(CDC *pDC)
		Pourpose: Blits the complete piano control
		Parameters: 
			-pDC: The parent window DC pointer

	 6.- void DrawPianoKeyFromNote(unsigned char Note)
		Pourpose: Draws a piano key as pressed with the corresponding color-on color
		Parameters: 
			-Note: The note, must fall in the octave range
	 7.- void ReleasePianoKeyFromNote(unsigned char Note)
		Pourpose: Draws a piano key as non-pressed with the respective color-off color
		Parameters:
			-Note: The note, must fall in the octave range
	 8.- unsigned char IsKeyPressed(CPoint point)
		Pourpose: Indicates if a key is pressed under the coordinates of the point parameter 
		Parameters: 
			-point: The point object

		Returns: The value of the note representing the key	
	 9.- BOOL IsDifferentLast_Current(unsigned char Note)
		Pourpose: Indicates if the current pressed key is different from the last one
		Parameters:
			-Note: Current Note
		Returns:
			TRUE: They are different
			FALSE: They are the same
	10.- SetColors(COLORREF KeyBlackDownColor,COLORREF KeyBlackUpColor,COLORREF KeyWhiteDownColor,
		 COLORREF KeyWhiteUpColor,COLORREF BackgroundColor)
		 Pourpose: Sets the color of the differents keys of the piano control
		 Parameters: 
			-KeyBlackDownColor: The color of the black key when pressed
			-KeyBlackUpColor: The color of the black key when released
			-KeyWhiteDownColor: The color of the white key when pressed
			-KeyWhiteUpColor: The color of thw white key when released
			-BackgroundColor: The octave background color	


  CRECEIVER MEMBER FUNCTIONS SPECIFICATION

	1.- virtual void IsNoteOn(unsigned char Note)=0;
		Pourpose: Pure virtual member function indicating that a key has generated a note-on
		Parameters:
			-Note: The value of the note pressed in the piano control

    2.- virtual void IsNoteOff(unsigned char Note)=0;
		Pourpose: Pure virtual member function indicating that a key has generated a note-off	
			  -Note: The value of the note relased in the piano control

///////////////////////////////////////////////////////////////////////
Version: 0.5
Module : Piano.h
Purpose: Defines the header for the CPiano control
Created: CJP / 02-05-2003
History: CJP / 03-25-2003 

*/

#if !defined(AFX_PIANO_H__8D977786_65E4_4833_B4E4_C560CEB4B6E1__INCLUDED_)
#define AFX_PIANO_H__8D977786_65E4_4833_B4E4_C560CEB4B6E1__INCLUDED_

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

#include "Object.h"
#include "Octave.h"


/////////////////////////////////////////////////////////////////////////////
// CPiano class
class CReceiver
{
public:	
	virtual void IsNoteOn(unsigned char Note)=0;
	virtual void IsNoteOff(unsigned char Note)=0;
	virtual ~CReceiver() {}
};


/////////////////////////////////////////////////////////////////////////////
// CPiano class


class CPiano : public CWnd,public CObj
{
	COctave *pOctaves; // Pointer to the octave array
	CReceiver *m_Receiver; // The receiver object
	BOOL m_bMouseCaptured; // Indicates that the mouse is captured in this CWnd
	unsigned char m_nLastNote,m_nCurrentNote; // Last and current pressed keys
protected:
	void RegisterWindowClass(); // Register the window class
public:
	CPiano();			   // Constructor	
	~CPiano();			   // Destructor	
	BOOL Initialize(int numOctaves, int scale); // Initialize keyboard
	void SetReceiver(CReceiver &Receiver);
	void SetOctavesRange(int nLowLimit);  // Sets the upper and the lower limit of the piano keys
	void BlitPiano(CDC *pDC); // Blits the piano when the OnPaint message is generated
	void DrawPianoKeyFromNote(unsigned char Note); // Draws the key 
	void ReleasePianoKeyFromNote(unsigned char Note); // Release the key 
	unsigned char IsKeyPressed(CPoint point); // Detects if the key is pressed in this coordinates
	BOOL IsDifferentLast_Current(unsigned char Note); // Detects if the last pressed key is different from the current one
	void SetColors(COLORREF KeyBlackDownColor,COLORREF KeyBlackUpColor,COLORREF KeyWhiteDownColor,COLORREF KeyWhiteUpColor,COLORREF BackgroundColor); // Sets the piano keyboard colors
	// Attributes
public:
	int m_nLoNote,m_nHiNote; // Note range of the piano
	int m_numOctaves; // Number of octaves
	int m_nOctaveWidth; // Size of the octave
	int m_nOctaveHeight;
				  
// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CPiano)
	public:
	virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID,CCreateContext* pContext = NULL);
	//}}AFX_VIRTUAL

	// Generated message map functions
protected:
	//{{AFX_MSG(CPiano)
	afx_msg void OnPaint();
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
	afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

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

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