Click here to Skip to main content
13,090,730 members (43,937 online)
Click here to Skip to main content

Stats

92.6K views
3.7K downloads
42 bookmarked
Posted 14 Jul 2003

MCI Midi class

, 14 Jul 2003
A tiny and very easy to use C++ class to play *.WAV, *.MID and *.RMI files
Midi_Wav_Demo.exe
Marimba.mid
Birthday3.mid
Birthday1.mid
Birthday2.mid
Ring.wav
res
WINMM.LIB
MidiDemo.ico
MidiDemo.clw
MidiDemo.dsp
MidiDemo.dsw
// MidiDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MidiDemo.h"
#include "MidiDemoDlg.h"

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




// ****************** MFC Stuff *************************



CMidiDemoDlg::CMidiDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMidiDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMidiDemoDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMidiDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMidiDemoDlg)
	DDX_Control(pDX, IDC_COMBO_PATH, mCtrl_ComboPath);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMidiDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CMidiDemoDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_PLAY, OnButtonPlay)
	ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


void CMidiDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this);

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CMidiDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


// ****************** Start of own code *************************


BOOL CMidiDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);

	CString s_CurDir;
	GetCurrentDirectory(MAX_PATH, s_CurDir.GetBuffer(MAX_PATH));
	s_CurDir.ReleaseBuffer();

	// The files have to be in the program directory !
	// Otherwise you have to specify the full path !
	mCtrl_ComboPath.InsertString(0, s_CurDir + "\\Marimba.mid");
	mCtrl_ComboPath.InsertString(1, s_CurDir + "\\Birthday1.mid");
	mCtrl_ComboPath.InsertString(2, s_CurDir + "\\Birthday2.mid");
	mCtrl_ComboPath.InsertString(3, s_CurDir + "\\Birthday3.mid");
	mCtrl_ComboPath.InsertString(4, s_CurDir + "\\Ring.wav");
	mCtrl_ComboPath.SetCurSel(0);

	return TRUE;
}


void CMidiDemoDlg::OnButtonPlay() 
{
	// get Path
	char s8_Buf[MAX_PATH];
	mCtrl_ComboPath.GetWindowText(s8_Buf, sizeof(s8_Buf));

	DWORD u32_Err;
	if (u32_Err=i_Sound.PlaySoundFile(s8_Buf))
	{
			 // errors defined in cSound.h
		     if (u32_Err == ERR_INVALID_FILETYPE)     strcpy(s8_Buf, "This filetype is not supported !");
		else if (u32_Err == ERR_PLAY_WAV)             strcpy(s8_Buf, "Windows could not play the Wav file !");
		else if (u32_Err == MCIERR_SEQ_NOMIDIPRESENT) strcpy(s8_Buf, "There is no Midi device installed or it is used by another application!");
		else
		{
			// translate errors from WinError.h
			if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, u32_Err, 0, s8_Buf, sizeof(s8_Buf), 0))
			{
				// translate errors from MMsystem.h
				if (!mciGetErrorString(u32_Err, s8_Buf, sizeof(s8_Buf)))
				{
					sprintf(s8_Buf, "Error %d", u32_Err);
				}
			}
		}

		MessageBox(s8_Buf, "Error", MB_ICONSTOP);
	}
}


void CMidiDemoDlg::OnButtonStop()
{
	i_Sound.StopSoundFile();
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

AXELMUELLER
Web Developer
Germany Germany
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 15 Jul 2003
Article Copyright 2003 by AXELMUELLER
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid