Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / MFC

MCI Midi class

Rate me:
Please Sign up or sign in to vote.
3.33/5 (12 votes)
14 Jul 20034 min read 104.4K   4K   42  
A tiny and very easy to use C++ class to play *.WAV, *.MID and *.RMI files
cSound by Elm� 
http://elmue.de.vu
http://kickme.to/elmue
elmue@gmx.de


************************************************************************

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. 
	(MCIERR_SEQ_NOMIDIPRESENT instead of MCIERR_SEQ_PORT_INUSE)
	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 http://www.codeproject.com
(It requires DirectX 8)

_________________________




Elm�
http://elmue.de.vu
http://kickme.to/elmue

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


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions