First, principle
---- Although Microsoft supports MIDI files, Visual C or MFC does not create any components to implement this support, but the Microsoft API provides three different ways to implement MIDI play:
MCI (The Media Control Interface). This is the most basic method, this paper will be discussed in detail.
Flow buffer. This format allows the application to assign a buffer for the MIDI data. The stream buffer will be useful when needed to accurately control MIDI playback.
Low-level MIDI device. This method can be used in applications that need to fully control MIDI data.
---- MCI can be done by MCISendCommand () and McISendstring (), this article uses only the mciSendCommand () function.
---- Prototype: DWORD MCISENDCOMMAND (uint WDeviceID, Uint Wimentage, DWord Dwparam1, DWORD DWPARAM2);
Parameters: WDEVICEID: Device ID accepting messages
Wimentage: MCI command message
DWPARAM1: Signature of the command
DWPARAM2: Pointer for the parameter block used
---- Return to the value: call success, return zero; otherwise, return the low words in the double word to store an error message.
Two MIDI playback control
---- 1. Open the device
MCI_open_parms openparms;
OpenPARMS.LPSTRDEVICEPE =
(LPCSTR) MCI_DevType_sequence; // MIDI type
OpenPARMS.LPSTRELEMENTNAME = (LPCSTR) FILENAME;
OpenPARMS.WDEVICEID = 0;
McISendCommand (NULL, MCI_Open,
MCI_Wait | MCI_Open_Type |
MCI_open_type_id | mci_open_element,
(DWORD) (LPVOID) & OpenPARMS)
---- MCI device ID indicates which device is opened, and when the MCI_Open command is sent, this value returns in the parameter block - should be saved.
---- 2. Shut down device
McISendCommand (M_WDeviceID, MCI_close, NULL, NULL);
---- 3. Play
MCI_Play_Parms Playparms;
Playparms.dwfrom = 0;
/ / Specify where to play from?
MCISENDCOMMAND (M_WDeviceID, MCI_Play,
MCI_FROM, (DWORD) (LPVOID)
& PlayParms)));
---- 4. time out
MCI_Play_Parms Playparms;
McISendCommand (M_WDeviceID, MCI_Pause, 0,
(DWORD) & PlayParms;
---- 5. stop
MCISENDCOMMAND (M_WDeviceID, MCI_Stop, Null, NULL);
---- 6. jump
* Jump to any place
MCI_seek_parms seekparms;
Seekparms.dwto = (nminute * 60 nsecond) * 1000; // Target time of jump, time unit is millisecond
MCISendCommand (M_WDeviceID, MCI_seek, MCI_to
| MCI_Wait, (DWORD) (LPVOID)
& Seekparms);
* Jump to the file header
MCISENDCOMMAND (M_WDeviceID, MCI_seek,
MCI_Seek_to_Start, NULL);
* Jump to the end of the file
MCISENDCOMMAND (M_WDeviceID, MCI_seek,
MCI_seek_to_end, null;
---- 7. Query the current information
MCI_STATUS_PARMMS Statusparms;
Statusparms.dwItem = MCI_SEQ_STATUS_DIVTYPE;
MCISendCommand (M_WDeviceID, MCI_Status,
MCI_Wait | MCI_Status_Item,
(DWORD) & statusparms);
The return information is stored in statusparms.dwreturn.
MCI_Status logo
MCI_STATUS_LENGTH gets the file length
MCI_STATUS_MODE gets the current state of file playback
MCI_STATUS_POSITION Get the current location of file playback
MCI_STATUS_TIME_FORMAT Gets Current Time Format
MCI_SEQ_STATUS_DIVTYPE Judgment file is a PPQN type or a SMPTE type
MCI_seq_status_tempo gains the current playback speed, PQRN type,
This value is a beat / division, the SMPTE type, this value is / second
---- 8. Set time format and playback speed
MCI_set_parms setparms;
SetParms.dwtimeFormat = MCI_FORMAT_MILLISECONDS;
// Set the time unit to milliseconds
MCISendCommand (M_WDeviceID,
MCI_set, MCI_SET_TIME_FORMAT,
(Dword) (lpvoid) & setParms);
MCI_SEQ_SET_TEMPO Sets the playback speed,
PQRN type, this value is a beat / division,
SMPTE type, this value is / second
MCI command
Send a message, WM_NOTIFY, for example:
MCISendCommand (M_WDeviceID, MCI_Play, MCI_notify, (DWORD) Void) & PlayP);
When the music is played, the program will receive a WM_NOTIFY message.