Click here to Skip to main content
15,891,933 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 612.3K   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
  
///////////////////////////////////////////////////////////////////////

Module : CCollection.cpp
Purpose: Code implementation for CCollection class
Created: CJP / 02-08-2002
History: CJP / 03-25-2003 
	
	Update: 09/20/2002

	1. Improved class destructors

	2. Check member variables for NULL values before performing an operation
		
	3. Restructured the class system
	
	4. Better method to enumerate and select MIDI ports
	
	5. Adapted the external thread to a pure virtual function

	6. SysEx reception and sending enabled

	7. Added flexible conversion functions

	8. Find out how to activate the Microsoft software synth. 

	9. Added DLS support

  Update: 03/25/2003

	10. Added exception handling

    11. Fixed bugs with channel groups
  
    12. Redesigned class hierarchy
	
    13. DirectMidi wrapped in the directmidi namespace
  
    14. UNICODE/MBCS support for instruments and ports

	15. Added DELAY effect to software synthesizer ports
	
	16. Project released under GNU (General Public License) terms		 
	
*/

#include "CDirectMidi.h"

using namespace directmidi;

// Class constructor

CCollection::CCollection()
{
	m_pCollection = NULL;
}
	
// Enumerate instruments stored in a collection given the index in the collection
// returns a pointer to an INSTRUMENTINFO

HRESULT CCollection::EnumInstrument(DWORD dwIndex,LPINSTRUMENTINFO InstInfo)
{
	HRESULT hr = DM_FAILED;
    DWORD dwPatch;
	TCHAR strMembrFunc[] = _T("CCollection::EnumIntrument");
	WCHAR wszInstName[MAX_PATH];
	
	if (m_pCollection && InstInfo)
	{	
		hr = m_pCollection->EnumInstrument(dwIndex,&dwPatch,wszInstName,MAX_PATH);
		if ((hr == E_FAIL) || (hr == E_OUTOFMEMORY) || (hr == E_POINTER)) throw CDMusicException(strMembrFunc,hr,__LINE__);
		
		#ifdef _UNICODE
			_tcscpy(InstInfo->szInstName,wszInstName);
		#else
			wcstombs(InstInfo->szInstName,wszInstName,MAX_PATH);
		#endif

		InstInfo->dwPatchInCollection = dwPatch; // Patch in the collection	
	
	} else throw CDMusicException(strMembrFunc,hr,__LINE__);
	
	
	return hr;
}


// Get the instrument given a pointer to an INSTRUMENTINFO structure

HRESULT CCollection::GetInstrument(CInstrument &pInstrument,LPINSTRUMENTINFO InstInfo)
{
	HRESULT hr = DM_FAILED;
	TCHAR strMembrFunc[] = _T("CCollection::GetIntrument/structure");

	if (m_pCollection && InstInfo)
	{
		
		SAFE_RELEASE(pInstrument.m_pInstrument); // Release the old reference to the interface
		
		// Gets the instrument given the patch in the collection
		
		if (FAILED(hr = m_pCollection->GetInstrument(InstInfo->dwPatchInCollection,&pInstrument.m_pInstrument))) 
			 throw CDMusicException(strMembrFunc,hr,__LINE__);
		
		// Fills with information the class public member variables

		pInstrument.m_dwPatchInCollection = InstInfo->dwPatchInCollection;
		_tcscpy(pInstrument.m_strInstName,InstInfo->szInstName);
	
	} else throw CDMusicException(strMembrFunc,hr,__LINE__);
	
	return S_OK;
}

// Get the instrument given the index in the collection

HRESULT CCollection::GetInstrument(CInstrument &pInstrument,INT nIndex)
{
	HRESULT hr = DM_FAILED;
	TCHAR strMembrFunc[] = _T("CCollection::GetIntrument/index");

	INSTRUMENTINFO InstInfo;

	if (m_pCollection)
	{
		SAFE_RELEASE(pInstrument.m_pInstrument); // Release the old reference to the interface
		
		// Gets the instrument associated with the index
		
		if (FAILED(hr = EnumInstrument(nIndex,&InstInfo))) 
			throw CDMusicException(strMembrFunc,hr,__LINE__); 
		
		// Gets the instrument given the patch in the collection
		
		if (FAILED(hr = m_pCollection->GetInstrument(InstInfo.dwPatchInCollection,&pInstrument.m_pInstrument))) 
			throw CDMusicException(strMembrFunc,hr,__LINE__); 
		
		// Fills with information the class public member variables

		pInstrument.m_dwPatchInCollection = InstInfo.dwPatchInCollection;
		
		_tcscpy(pInstrument.m_strInstName,InstInfo.szInstName);
	
	} else throw CDMusicException(strMembrFunc,hr,__LINE__);
	
	return S_OK;
}

// The class destructor

CCollection::~CCollection()
{
	SAFE_RELEASE(m_pCollection);
}

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