Click here to Skip to main content
15,888,816 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 610.9K   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
  
///////////////////////////////////////////////////////////////////////
Version: 0.5
Module : Octave.cpp
Purpose: Code implementation for the COctave class
Date Format: Day/Month/Year
Created: CJP / 05-02-2003
History: CJP / 25-03-2003 

*/


#include "stdafx.h"
#include "Octave.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif



// Class constructor

COctave::COctave()
{
	BackgroundColor = RGB(200,200,200); // Default background color
}

// Initializes the octave, given the coordinates and scale 

void COctave::Initialize(CWnd *pWnd,int px,int py,int scale,int nOctaveWidth,int nOctaveHeight)
{
	m_nPosX = px; // Member variables inherited from CObj
	m_nPosY = py;
	m_nWidth = nOctaveWidth; // Size of the octave object
	m_nHeight = nOctaveHeight;
	m_pDC = pWnd->GetDC();			// Gets the target window DC
	m_memDC.CreateCompatibleDC(m_pDC);	// Creates the memory DC
	m_bmp.CreateCompatibleBitmap(m_pDC,m_nWidth,m_nHeight);
	m_memDC.SelectObject(&m_bmp); // Selects the bitmap into the memory DC
	
	// Paints the memory DC with the dialog brush
	
	CBrush bkbrush;
	bkbrush.CreateSolidBrush(BackgroundColor);
	m_memDC.SelectObject(bkbrush);
	m_memDC.PatBlt(0,0,m_nWidth,m_nHeight,PATCOPY);
	
	// Calculates shifts and key positions

	int WHITE_X_SHIFT = scale*WHITE_KEY_UNITS_WIDTH;
	int WHITE_X_SHIFT_LEFT = (WHITE_X_SHIFT - WHITE_X_SHIFT/2)/2;
	int WHITE_X_SHIFT_RIGHT = WHITE_X_SHIFT - WHITE_X_SHIFT_LEFT;	
	int DELTA = static_cast<int>(scale/3);

	// Initializes the keys of the octave in a given x,y inside 
	// the memory DC
	
	m_key[0].Initialize(&m_memDC,DELTA,0,'A',scale);
	m_key[1].Initialize(&m_memDC,WHITE_X_SHIFT_RIGHT+DELTA*2,0,'D',scale);
	m_key[2].Initialize(&m_memDC,WHITE_X_SHIFT+DELTA*3,0,'B',scale);
	m_key[3].Initialize(&m_memDC,WHITE_X_SHIFT+WHITE_X_SHIFT_RIGHT+DELTA*4,0,'D',scale);
	m_key[4].Initialize(&m_memDC,WHITE_X_SHIFT*2+DELTA*5,0,'C',scale);
	m_key[5].Initialize(&m_memDC,WHITE_X_SHIFT*3+DELTA*7,0,'A',scale);
	m_key[6].Initialize(&m_memDC,WHITE_X_SHIFT*3+WHITE_X_SHIFT_RIGHT+DELTA*8,0,'D',scale);
	m_key[7].Initialize(&m_memDC,WHITE_X_SHIFT*4+DELTA*9,0,'B',scale);
	m_key[8].Initialize(&m_memDC,WHITE_X_SHIFT*4+WHITE_X_SHIFT_RIGHT+DELTA*10,0,'D',scale);
	m_key[9].Initialize(&m_memDC,WHITE_X_SHIFT*5+DELTA*11,0,'B',scale);
	m_key[10].Initialize(&m_memDC,WHITE_X_SHIFT*5+WHITE_X_SHIFT_RIGHT+DELTA*12,0,'D',scale);
	m_key[11].Initialize(&m_memDC,WHITE_X_SHIFT*6+DELTA*13,0,'C',scale);

}

// Change the note value of the octave keys from a range

void COctave::SetRange(unsigned char LoRange)
{
	for (int i = 0;i <= 11;i++)
		m_key[i].SetNote(LoRange + i);
	
}

// Member function called when all keys must be drawn as normal state

void COctave::Draw()
{
	for(int i = 0;i<=11;i++)
		m_key[i].DrawasNormal();
}

// Blit the entire octave

void COctave::Blit(CDC *pDC)
{
	pDC->BitBlt(m_nPosX,m_nPosY,m_nWidth,m_nHeight,&m_memDC,0,0,SRCCOPY);
}

// Blit a specific key of the octave

void COctave::BlitKey(int numKey,CWnd *pWnd)
{
	m_key[numKey].Blit(m_nPosX,m_nPosY,pWnd);
}

// Returns information of the key whose octave has been pressed

unsigned char COctave::IsKeyPressed(int x,int y)
{
	int i = 0;
	unsigned char note_value;
	while ((i<12) && ((note_value = m_key[i].IsPressed(x-m_nPosX,y-m_nPosY))==NO_KEY)) i++;
	return note_value;
}

// This member function is called when a key of the octave
// has to be drawn as pressed

void COctave::DrawOnKey(int numKey,CWnd *pWnd)
{
	m_key[numKey].DrawOnClick();
	BlitKey(numKey,pWnd);
}

// This member function is called when a key of the octave
// has to be drawn as normal (released)

void COctave::ReleaseKey(int numKey,CWnd *pWnd)
{
	m_key[numKey].DrawasNormal();
	BlitKey(numKey,pWnd);
}

void COctave::SetBackgroundColor(COLORREF BackColor)
{
	BackgroundColor = BackColor; 
}

void COctave::SetKeyColors(COLORREF KeyBlackDownColor,COLORREF KeyBlackUpColor,COLORREF KeyWhiteDownColor,COLORREF KeyWhiteUpColor)
{
	for(int i=0;i<12;i++)
		m_key[i].SetColors(KeyBlackDownColor,KeyBlackUpColor,KeyWhiteDownColor,KeyWhiteUpColor);
}

// Destructor (nothing to release-destroy)

COctave::~COctave()
{
}

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