Search Tips Advanced Search |
| Delphi Graphics and Game Programming Exposed with DirectX 7.0 by John Ayres Wordware Publishing, Inc. ISBN: 1556226373 Pub Date: 12/01/99
Search this book:
Previous | Table of Contents | Next
### Music Music, like sound effects, adds depth to a game, giving it a feeling of completeness. Even more than sound effects, music adds mood to a game, and when used effectively can adjust the user’s frame of mind as effectively as a movie sound track. Most games feature music that changes in response to changing conditions within the game. An effective use of music would be to play something eerie and mysterious when exploring catacombs, change to a heart-pounding rock-and-roll piece during the heat of combat, and end with a euphoric anthem to signal victory or a melancholic hymn to signal defeat. The difficulty comes in writing such musical ballads; programmatically outputting the result is easy by comparison. The topic of musical output, while poorly documented, has been explored by a number of adventurous programmers, and as such there are several techniques for its implementation. One simple technique may be to simply digitize a musical piece and use the methods described above for its playback. Several other techniques involve digitized music compressed in various formats and then uncompressed on the fly as it is output to the audio device. However, for our purposes, we will examine only two common musical output techniques: MIDI and CD audio. Both of these techniques use Win32 MCI API commands, and as such do not make use of DirectX or DirectSound functionality.
| Tip: As stated at the beginning of the chapter, the DirectX 6.1 and above SDK contains the new DirectMusic component that brings the power of DirectX to MIDI output. The header files were not available during the production of this book, and thus we were unable to cover them in this chapter. However, the DirectMusic header files are available on the CD accompanying this book.
#### MCISendCommand All of the examples to follow make use of a very powerful Win32 API function called MCISendCommand. This is a high-level command in the Media Control Interface (MCI) subsystem of Windows. The MCISendCommand function is simple in its declaration but very complex in terms of what it can do. You can use the MCISendCommand function to take advantage of any native Windows multimedia functionality, including playback of WAV files, video files, and MIDI and CD music (as we’ll see below), and interaction with video recording and playback equipment. Our uses of this function will be limited solely to MIDI and CD audio output, but you should be aware that this is a very powerful and complex function that unfortunately is poorly documented. The MCISendCommand function is defined as:
    function mciSendCommand(

    mciId: MCIDEVICEID;             // the MCI device identifier

    uMessage: UINT;                 // the command message

    dwParam1,                       // command message flags

    dwParam2: DWORD                 // address of parameter structure

    ): MCIERROR;                    // returns an MCI error code
The first parameter to this function is the identifier of the MCI device to which the command should be sent. This is retrieved when the device is first opened. The second parameter is the actual command to be sent to the device. Each command instructs the device to perform a different task, and we’ll see several commands in the following examples. The third parameter is a series of flags that are dependent on the type of command sent, and indicate which members of the structure identified by the last parameter contain valid data. In particular, the MCI_WAIT flag is used often to indicate that the function should not return until the command has been carried out. The final parameter takes a pointer to a data structure, again dependent upon the type of command being sent. Some data structures must be initialized, while others are initialized by the function and contain pertinent data when it returns. For some reason, due to the way this function was declared, you must pass the structure’s address by typecasting it as a longint. This will be illustrated in the examples to follow. Notification MCISendCommand is asynchronous by nature, and will return immediately unless the MCI_WAIT flag was specified. In this event, you can usually specify the MCI_NOTIFY flag to indicate that the function should notify the application when it has completed. This is accomplished by sending the application an MM_MCINOTIFY message. It is a simple matter to define a handler for this message, and this technique is used in Listing 9-10. #### MIDI MIDI, or Musical Instrument Digital Interface, is a format for describing musical sound, much the same as the WAV format describes digitized sound. It is an effective means of providing music within a small footprint. Both software for creating MIDI files and freeware or shareware MIDI songs are readily available.
| MIDI (Musical Instrument Digital Interface): A digital format for describing music.
In order to implement MIDI music playback, several steps must be followed. With the exception of detecting available hardware, none of these steps are optional. Outputting MIDI music is implemented by performing these tasks: • Check for MIDI output hardware existence • Open the MIDI output device • Set the time format • Commence playback • Retrieve playback status • Close the device Check for MIDI Output Hardware Existence The target machine must have a device capable of outputting MIDI music. It would be very difficult these days to find a sound card that did not also include the ability to output MIDI music. Thus, it is safe to assume that any machine with a sound card is capable of outputting MIDI. However, the developer can use the function midiOutGetNumDevs to determine if there are indeed any devices that can output MIDI music. The midiOutGetNumDevs function takes no parameters and returns a simple integer. If this integer value is greater than zero, at least one hardware device has been found that is capable of MIDI music output. Open the MIDI Output Device The first real step in playing MIDI music is to open the “sequencer” device (the hardware device capable of outputting MIDI music) and the MIDI file that will be played. This requires a variable of the type TMCI_Open_Parms. This data structure describes the device being opened and the file that will be played on the device. The TMCI_Open_Parms data structure is defined as:
    TMCI_Open_Parms = record

      dwCallback: DWORD;                // a window handle

      wDeviceID: MCIDEVICEID;           // the device identifier

      lpstrDeviceType: PAnsiChar;       // the name of the device

      lpstrElementName: PAnsiChar;      // the name of the element

      lpstrAlias: PAnsiChar;            // a device alias

    end;
Previous | Table of Contents | Next

Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.