Click here to Skip to main content
11,802,762 members (73,076 online)
Click here to Skip to main content
Add your own
alternative version

MCI Midi class

, 14 Jul 2003 88.9K 3.6K 42
A tiny and very easy to use C++ class to play *.WAV, *.MID and *.RMI files
cSound by Elm�


cSound is a really tiny and very easy to use C++ class to play *.WAV, *.MID and *.RMI files.
You can add it to your project without changes.
To play a Wave, Midi or Riff-Midi file you need only one function call !!!

This class uses MCI, it does NOT require DirectX.
It will run under Windows 95 / 98 / ME / NT / 2000 / XP....

It will play Midi via the Midi Mapper.
Which Midi device is used by the mapper depends on the settings 
in your Control Panel --> MultiMedia.
Try the differnet devices, they sound extremely different !!!!
I added some specific Midi files to check.

The Main Demo project is written in MFC but the cSound class is completely free of MFC.
It does NOT make sense to create more than one instance of this class !


Known Bugs in Windows NT, 2000 and XP :
1.) On Windows NT, 2000 and XP the first note of a Midi song is omitted in playing
    if the song does not begin with a rest of at least a quarter note !!
	(The Midi files added to this sourcecode all begin with a rest)

2.) On Windows NT, 2000 and XP the mciSendString("open...") command is extremely slow
3.) Only on Windows XP mciSendString("open...") gives a stupid error message 
    if the Midi device is occupied by another application. 
	mciGetErrorString() translates this to 
	"No Midi hardware available or no drivers installed" !!


If you search the internet for source code to play Midi you will find a lot
which require DircetX to be installed on the users computer.

Advantages of using DirectX to play Midi :
1.) If you select the DirectX Microsoft Synthesizer (default device) it will 
    be possible that multiple applications can play Midi at once.
2.) If you have a very CHEAP sound card, the Microsoft Synthesizer will sound better
    than the driver of your soundcard.
Disadvantages of using DirectX to play Midi:
1.) Every user of your software needs to have DirectX installed.
    On Windows 95 and NT he will always have to update Direct X.
    Users of Windows 98 and 2000 will also need to update DirectX if your
    application requires DirectX 7 or 8.
    The download of DirectX is unacceptable for modem users (> 12 Megabyte)
2.) The documentation of DirectX in the MSDN is poor, poor, poor !
    And if you even want to write for an older DirectX version (like 5 or 6) 
    to allow users of Windows 98 and 2000 to use your application without updating
    DirectX you will find NOTHING about that in the actual MSDN !
    Microsoft completely removed the older documentations !
3.) When your application initializes DirectX a dozen of DLLs is loaded
    which consume about 4 MegaByte of RAM.
    On a Pentium 100 this loading of DLLs takes 3 seconds.
4.) After playing a Midi file DirectX does NOT automatically free the used
    Midi device. This means that other applications (e.g. WinAmp) can NOT access it.
    Your software has to take care to remove the port after playing the file.
    The problem is that the sound is played asynchronously and you don't know 
    when DirectX will finish to play. To find out in advance the length of a Midi sound
    is very complicated because the tempo can change multiple times in a Midi file.
    So you could check with a timer in certain intervals if the sound is still playing
    (IDirectMusicPerformance::IsPlaying()) and than remove the port - but this is awkward !

5.) If you have a middle-class or a good soundcard (like Soundblaster live) 
    you will find that the Microsoft Synthesizer (default device) sounds awful ! 
    You have to insert extra code (IDirectMusic::EnumPort()), a combo box,
    code to store the user settings etc. to allow the user to choose a Midi device, 
    which sounds better. My cSound class does not need that because it uses 
    the device, which the user has selected in the Control Panel.


In the MSDN you find a sample code (search for "MidiPlyr")
which also plays Midi files without DirectX.

It uses the midiOutxxx() and midiStreamxxx() Interface of WinMM.Dll (This interface is used by WinAmp)
The sample code is EXTREMELY complex (written in pure C)

The bugs in Windows 2000 and XP described above do not affect MidiPlyr.


If you still want a DirectX Midi Player 
(which also supports 3D-Sound, special effects etc)
download cMidiMusic (C++) from
(It requires DirectX 8)



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.


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


About the Author

Web Developer
Germany Germany
No Biography provided

You may also be interested in...

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