Play on MIDI, WAV and CD under VC 5.0
Wuhan Petrochemical Design Institute Warrior Zhou Honghan
---- Join Music is the simplest in all methods to enhance application functions. Almost every computer game or multimedia program is a background with some MIDI or CD music. Music can make the user feel happy; playing proper music in a suitable occasion enables programmers and his VC programs.
The first part of MIDI playback
---- Musical Digital Interface (MIDI) is an agreement developed by some large companies (including company-producing electronic music synthesizers), which are later used by the computer industry and become a standard format of multimedia music files. The MIDI file is generally small, and the requirements for hardware devices are high.
---- One, 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) & 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
Part 2 WAV file playback
---- One, principle
---- Microsoft API provides three different ways to implement WAV play:
Playsound () function. It can play the sound of the WAVE format by single-line encoding. This function has two restrictions: the sound data must be fully loaded into physical memory; the data format must be supported by a certain audio drive. According to experience, Playsound () is suitable for files below 100K. MCI (The Media Control Interface), with the previous chapter playing the MIDI file, can play a file with more than 100K. Low-level WAVE audio equipment. Use these devices to run application files that fully control WAVE data. ---- Second, WAV file playback control
---- Since the same MCI is used, only different parts are listed in the same chapter.
---- 1. Open the device
---- Change MIDI's MCI_DEVTYPE_SEQUENCER to "Waveaudio"
---- 2. recording
MCI_Record_Parms RecordParms;
McISendCommand (M_WDeviceID, MCI_Record,
NULL, (DWORD) (LPVOID)
& Recordparms);
---- 3. Save recording
MCI_SAVE_PARMS SAVEPARMS;
SaveParms.lpFileName = (LPCSTR) FileName;
McISendCommand (M_WDeviceID, MCI_save,
MCI_save_file | mci_wait,
(DWORD) & SAVEPARMS;
The third part of the CD playback
---- The unique advantage of CD is that it is designed by the composer and produced by music manufacturers. Different computers play the MIDI file, the sound effect is different, but the sound effect of the CD is always the same. High-quality audio will have the effect of computer users to surprise you. We still use MCI playback CD, most of the playback control is the same as the first two parts, only list different parts.
---- 1. Open light
McISendCommand (M_WDeviceID, MCI_Set,
MCI_set_door_open, null;
---- 2. Guanwei
McISendCommand (M_WDeviceID, MCI_Set,
MCI_set_door_closed, null);
---- 3. Open the device
Change MIDI's MCI_DEVTYPE_SEQUENCER to MCI_DEVTYPE_CD_AUDIO
---- 4. Play
---- Specifies the starting point of the play must be converted by MCI_make_TMSF (Track, Minute, Second, Frame)
---- 5. Query the current information
MCI_status_current_track gets current tracks
MCI_STATUS_LENGTH gets a CD or specified track length
MCI_STATUS_MODE gets the current state of the drive
MCI_STATUS_NUMBER_OF_TRACks Get the number of CD tracks
MCI_status_position gets the location under the current format
MCI_STATUS_READY Check if the device is ready
MCI_STATUS_TIME_FORMAT gets the current time format
MCI_Status_Media_Present Check to confirm if the CD is in the drive
MCI_cda_status_type_track checks if a track is audio track
---- Note:
Use the MCI_STATUS_LENGTH parameter to query the CD and track length, return value by calling MCI_MSF_MINUTE (), MCI_MSF_Second () converted to minutes, seconds. MCI_status_position Parameter Return Value Call MCI_TMSF_TRACK (), MCI_TMSF_MINUTE (), MCI_TMSF_SECOND (), MCI_TMSF_FRAME to get the channel, minute, second, frame of the current location. ---- 6. jump
---- The goal of the jump must be converted by MCI_make_tmsf (track, minute, second, frame), which is best to separate the above three formats, or make a dynamic connection library. Add WinMM.Lib in Project-> Setting-> Link-> Object / library modules, including in the source program
.
---- MCI calls simple, powerful, can meet the basic needs of daily multimedia programming. However, MCI can only play a file at a time, using DirectSound technology to play more than eight WAV files while playing.