Solved

playing midi files using MCI interface

Posted on 1998-07-25
4
357 Views
Last Modified: 2008-02-01
Can anyone provide me with some code to play a midi file using the windows MCI interface?

I've been looking through the VC4 help files, and it all looks really complicated...

any code would be appreciated.

thanks
steb
0
Comment
Question by:steb
4 Comments
 
LVL 1

Expert Comment

by:timop
ID: 1168610
Here is some sample code:

void PlayMidi(LPCTSTR lpszFileName)
{
  MCI_OPEN_PARMS mciOpenParms;
  MCI_PLAY_PARMS mciPlayParms;
  MCIDEVICEID wDeviceID;

  mciOpenParms.lpstrDeviceType = "sequencer";
  mciOpenParms.lpstrElementName = lpszFileName;
  if ( mciSendCommand( 0, MCI_OPEN, MCI_OPEN_TYPE |  MCI_OPEN_ELEMENT, (DWORD)(LPVOID) &mciOpenParms) )
    // error
    return;

  wDeviceID = mciOpenParms.wDeviceID;
      
  mciPlayParms.dwCallback = NULL;
  if ( mciSendCommand( wDeviceID, MCI_PLAY, MCI_WAIT, (DWORD)(LPVOID)&mciPlayParms) )
    // error
    return;
            
  mciSendCommand( wDeviceID, MCI_CLOSE, 0, NULL);
}

0
 

Author Comment

by:steb
ID: 1168611
thanks for your help, but I forgot to mention the midi file needs
to be played in the background, inside quake2...
at the minute, it freezes my system.

anymore help would be much appreciated.

thanks
steb
0
 
LVL 2

Expert Comment

by:rayb
ID: 1168612
I don't think that you'll be able to do this, as Quake and Quake 2 both obtain exclusive locks on the sound hardware at game startup.  Now, if you had access to ID Software's source code, then we might be able to do something.

0
 
LVL 23

Accepted Solution

by:
chensu earned 50 total points
ID: 1168613
Don't use the MCI_WAIT flag. The following code is copied from the Platform SDK documentation. Close the device (mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL)) when you receive the MM_MCINOTIFY message.

Playing a MIDI File
The following example opens a MIDI sequencer device, verifies that the MIDI mapper was selected as the output port, plays the MIDI file specified by the lpszMIDIFileName parameter, and closes the device after playback is complete.

// Plays a specified MIDI file by using MCI_OPEN and MCI_PLAY. Returns
// as soon as playback begins. The window procedure function for the
// specified window will be notified when playback is complete.
// Returns 0L on success; otherwise, it returns an MCI error code.
DWORD playMIDIFile(HWND hWndNotify, LPSTR lpszMIDIFileName)
{
    UINT wDeviceID;
    DWORD dwReturn;
    MCI_OPEN_PARMS mciOpenParms;
    MCI_PLAY_PARMS mciPlayParms;
    MCI_STATUS_PARMS mciStatusParms;
    MCI_SEQ_SET_PARMS mciSeqSetParms;

    // Open the device by specifying the device and filename.
    // MCI will attempt to choose the MIDI mapper as the output port.
    mciOpenParms.lpstrDeviceType = "sequencer";
    mciOpenParms.lpstrElementName = lpszMIDIFileName;
    if (dwReturn = mciSendCommand(NULL, MCI_OPEN,
        MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,
        (DWORD)(LPVOID) &mciOpenParms))
    {
        // Failed to open device. Don't close it; just return error.
        return (dwReturn);
    }

    // The device opened successfully; get the device ID.
    wDeviceID = mciOpenParms.wDeviceID;

    // Check if the output port is the MIDI mapper.
    mciStatusParms.dwItem = MCI_SEQ_STATUS_PORT;
    if (dwReturn = mciSendCommand(wDeviceID, MCI_STATUS,
        MCI_STATUS_ITEM, (DWORD)(LPVOID) &mciStatusParms))
    {
        mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
        return (dwReturn);
    }

    // The output port is not the MIDI mapper.
    // Ask if the user wants to continue.
    if (LOWORD(mciStatusParms.dwReturn) != MIDI_MAPPER)
    {
        if (MessageBox(hMainWnd,
            "The MIDI mapper is not available. Continue?",
            "", MB_YESNO) == IDNO)
        {
            // User does not want to continue. Not an error;
            // just close the device and return.
            mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
            return (0L);
        }
    }

    // Begin playback. The window procedure function for the parent
    // window will be notified with an MM_MCINOTIFY message when
    // playback is complete. At this time, the window procedure closes
    // the device.
    mciPlayParms.dwCallback = (DWORD) hWndNotify;
    if (dwReturn = mciSendCommand(wDeviceID, MCI_PLAY, MCI_NOTIFY,
        (DWORD)(LPVOID) &mciPlayParms))
    {
        mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
        return (dwReturn);
    }

    return (0L);
}

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now