Play game music and sound effects

Source: Internet
Author: User
Play game music and sound effects

09:30:55 | category:
Game world
| Tag:
| Font size
Subscription

Play game music and sound effects

In the Win32 environment, there are too many ways to play music sound effects, and one thing in common is: You don't need to spend a lot of effort to get what you need. Continuing the topic-based discussion, this issue focuses on playing music and sound effects.

□Game music

I believe that many people agree to the role of music in the game. In retrospect, the Chinese RPG classic "Legend of the legend of the fairy sword", the whole game will be inferior to the music, in particular, appropriate scenes can be used with appropriate music to integrate players into the plot. When you cry, when you laugh, it's probably just a cut. The remaining sound effects of RPG games are not particularly prominent. It is basically enough to know that there is a sword playing sound when hacking people. Therefore, it is usually not so important in terms of sound effects. Real-time Combat Games focus on the sound performance of the fight, a large volume of people, a mix of sounds, which involves the sound mixing part, we will also discuss below. After reading this article, you will learn when to use a program to express another life of the game: music and sound effects.

□Start with MIDI

Most of the music in early dos systems adopt the audio card specification. The sub-file named CMF is in this format. Of course, games usually won't let you see the real practice, however, the internal use of this format is mostly non-trivial. Windows games are dominated by CD publishers. In order to fully utilize the space, the game will use a large number of files in WAV format or directly burn the music into the audio track format. In particular, many games like to use the first data piece and the second music piece. If you do not play the game, you can also use it as a music CD, which is a matter of texture. Of course, I mean these music must be heard. If the music is mediocre, even if it is burned into a sound track, it cannot be changed.

In the window, considering the space size, the midi-format music file is definitely the best choice. A five-minute midi is worth 100,000 characters, the one-minute usage in the WAV format is measured in MB, which is almost dumb. Therefore, music and game music on websites are suitable for playing with Midi, while I personally pay attention to the music, as for the number of musical instruments used by a piece of music, I seldom pay attention to it. People's ears have a certain limit to listen to things. As long as there is no noise, they can work with beautiful music, it is generally acceptable.

□Program practices for playing MIDI videos

The main point of playing music in the game is loop playback, that is, after the playing is complete, you need to let him play from the beginning until the scene changes, or the game ends. Therefore, after the MIDI files are played, you must be able to notify the program to make appropriate processing. The playing of MIDI can be completed immediately with multimedia support from the window. You can even cut it directly from the help method and modify it a little bit, because this is quite a formula, the program code written by a Jun and B Jun will grow almost as long as possible. If you don't talk nonsense, just look at how simple the program is:

Class cmidi

{

Public:

DWORD play (hwnd, char * filename );

Void replay ();

Void stop ();

PRIVATE:

Uint wdeviceid; // The device code of the MCI Device

DWORD dwreturn;

Mci_open_parms mciopenparms;

Mci_play_parms mciplayparms;

Mci_status_parms mcistatusparms;

Mci_seq_set_parms mciseqsetparms;

};

It can also be packaged into a category, and the interface needs to be purified. intuitively, the first action is play, followed by replay ), the last thing is good post-job (STOP), not many, just three. Of course, you will think about whether a paused interface is required. No problem. It is not difficult, it takes three minutes to win.

After learning about the general appearance of a category, let's take a look at what the implementation is like, starting with cmidi: Play:

DWORD cmidi: Play (hwnd, char * midifile)

{

// Enable the Midi hardware device. We use the general setting value.

Mciopenparms. lpstrdevicetype = "sequencer ";

// This parameter indicates the name of The MIDI file to be played.

Mciopenparms. lpstrelementname = midifile;

// Use message to play MIDI instead of string

If (dwreturn = mcisendcommand (null, mci_open,

Mci_open_type | mci_open_element,

(DWORD) (lpvoid) & mciopenparms)

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 );

}

// For the purpose of repeated playback, our program must be able to receive

// Message of mm_mcinotify. The call method is to pass

// Send the wm_play message to the device and ask him to start playing the video.

Mciplayparms. dwcallback = (DWORD) hwnd;

If (dwreturn = mcisendcommand (wdeviceid, mci_play, mci_notify,

(DWORD) (lpvoid) & mciplayparms ))

{

Mcisendcommand (wdeviceid, mci_close, 0, null );

Return (dwreturn );

}

Return (0l );

};

There are two ways to play MIDI. The first is to use the string command hardware action, the second is to transmit messages, and the second is to use the second method. The reason is clear, only by transmitting messages can we know whether the music has been played.

Next let's take a look at cmidi: REPLAY:

Void cmidi: replay ()

{

Mcisendcommand (wdeviceid, mci_seek, mci_seek_to_start, null );

Mcisendcommand (wdeviceid, mci_play, mci_policy, (DWORD) (lpvoid) & mciplayparms );

}

It is incredible that the letter contains only two calls. The first call sends a message to the device, asking him to move the playing indicator of MIDI to the starting part, that is, mci_seek_to_start,

This is just like moving an archive index. Then the second command can be used to check whether the video can be played again. Don't forget mci_notify. When the next playback is complete, we still need to use a message to notify our program.

Finally, let's take a look at the cmidi: Stop () practice:

Void cmidi: Stop ()

{

Mcisendcommand (wdeviceid, mci_close, 0, null );

}

More and more simple, there is only one function call, the Message Parameter mci_close, is to end the playing of the whole music. After you finish playing the video, it's easy to play another piece of music. Call cmidi: Play () again.

The usage of the entire category is roughly like this: first, configure an actual cmidi object for the program, as long as the cmidi command is executed in the whole region, erhoumidi is a real object. Call Midi. Play (hwnd, "ff3celes. Mid") in the scenario initialization section. Enter the correct MIDI file name. Here I play three generations of Space fighters, just to demonstrate that this music is really great. In the message loop, we must define a message:

Case mm_mcinotify:

MIDI. Replay ();

Break;

After the music is played, we will receive the message mm_mcinotify in the loop. Call cmidi: replay () as we mentioned earlier. When the scenario changes, a new music or the program ends, it is the time to call cmidi: Stop. Because only one piece of music exists in a scenario at the same time, our category performs well, so don't worry.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.