Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

MCI Midi class

, 14 Jul 2003
A tiny and very easy to use C++ class to play *.WAV, *.MID and *.RMI files
mididemo_appl.zip
Midi_Wav_Demo.exe
Marimba.mid
Birthday3.mid
Birthday1.mid
Birthday2.mid
Ring.wav
mididemo_src.zip
res
WINMM.LIB
MidiDemo.ico
MidiDemo.clw
MidiDemo.dsp
MidiDemo.dsw
/****************************************************************************
                 cSound.cpp  by  Elm� (elmue.de.vu)
****************************************************************************/


#include "stdafx.h"
#include "cSound.h"

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

cSound::cSound()
{
}

cSound::~cSound()
{
}

/****************************************
       Plays Wav or Midi file
****************************************/

DWORD cSound::PlaySoundFile(char *p_s8File)
{
	// It is important to check if the file exists !
	// On Windows NT PlaySound() returns TRUE even if the file does not exist !
	// Then PlaySound() makes the PC speaker beep !!!
	
	// mciSendString("open...") also gives an absolutely stupid error message
	// if the file does not exist !

	DWORD u32_Attr = ::GetFileAttributes(p_s8File);
	if (u32_Attr == 0xFFFFFFFF || (u32_Attr & FILE_ATTRIBUTE_DIRECTORY)) 
		return ERROR_FILE_NOT_FOUND;


	// Get file extension
	char *p_s8Ext = strrchr(p_s8File, '.');

	if (!p_s8Ext)
		return ERR_INVALID_FILETYPE;

	if (stricmp(p_s8Ext, ".wav") == 0)
	{
		StopSoundFile();
		
		// PlaySound() is very primitive: no Error Code available
		if (!PlaySound(p_s8File, 0, SND_FILENAME | SND_ASYNC))
			return ERR_PLAY_WAV;

		return 0;
	}
	

	DWORD u32_Err;
	if (!stricmp(p_s8Ext, ".mid") || !stricmp(p_s8Ext, ".midi") || !stricmp(p_s8Ext, ".rmi"))
	{
		StopSoundFile();

		static char s8_LastFile[MAX_PATH] = "";

		// the mciSendString("open...") command is slow (on Windows NT, 2000 and XP) 
		// so we call it only if necessary
		if (strcmp(s8_LastFile, p_s8File) != 0)
		{
			strcpy(s8_LastFile, p_s8File);

			mciSendString("close all", 0, 0, 0);

			char s8_Buf[300];
			sprintf(s8_Buf, "open \"%s\" type sequencer alias MidiDemo", p_s8File);

			if (u32_Err=mciSendString(s8_Buf, 0, 0, 0))
				return u32_Err;
		}

		if (u32_Err=mciSendString("play MidiDemo from 0", 0, 0, 0))
		{
			// replace stupid error messages
			if (u32_Err == 2) u32_Err = MCIERR_SEQ_NOMIDIPRESENT;
			return u32_Err;
		}

		return 0;
	}

	return ERR_INVALID_FILETYPE;
}



/**************************************************
       Stops the currently playing Wav and Midi
***************************************************/

void cSound::StopSoundFile()
{
	PlaySound(0, 0, SND_PURGE); // Stop Wav

    mciSendString("stop MidiDemo", 0, 0, 0); // Stop Midi
}

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

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