Translation: lanyu, benna, outmanProofread: Sharyu
Do you remember the dramatic effect of music when sonick started diving? Sorry for those anxious memories, but this is a good example of dynamic background music. I will show you how to add music with seamless emotional changes based on game movements.
WordSurge- An example of increased intensity
This is how I used WordSurge in my last game. The more dangerous the players are, the more intense the music. Please watch the following video
Using Apple'sGarage BandCreate your own music
Before you do anything, you need some music. Open Garage Bandand and guide you to isteo. Note: ierstau is an internationally renowned DJ.
Create music with equal length
The goal is to create equal-length music sets so that the gaming sound engine can seamlessly switch between them. Length is not important, but must be equal. In the WordSurge game, I used 16 seconds of music, 6 in total.
Create each movement in sequence from weak to strong. After you finish, you should use more and more instruments in each music movement like below. Save your project.
Disable Standardization
Before you start to save your movements, tell the Garage Band not to adjust the volume. By default, the audio track volume to be output is adjusted to make it as loud as possible. However, this will change the responsiveness of this chapter to the entire song. You can disable standardization in the Garage Band advanced parameter selection menu.
Crop music and export
Highlight everything except your first movement and delete them temporarily. It looks like:
In the Share menu, select export songs to the hard disk.
It is appropriate to set The high quality to high quality.
Name the file 1. m4a.
Note that Garage Band exports an audio file a little longer than you think. It includes extra echo and fade-out effects, and you need to make your background sound seamless. Because when you play these audio tracks, they overlap with each other, so they must be synchronized, and more detailed descriptions will be given later.
Undo it or restore it to another part of your song before saving it. Repeat the cut and export processes for each movement. Finally, you will get a series of music names, such as 1. m4a, 2. m4a, and 3. m4a.
At the same time, returnXcode
I used cocos2d in this game, which made timing operations very simple. It also loads Steve Oldmeadow's CocosDenshion sound library.
Open your game project and drag your files into the Resource folder in your project.
Put the following code at the top of your code:
- # Import "SimpleAudioEngine. h"
Declare in the initialization method
Schedule Your background music manager to run every X seconds. X is the length of your music.
- [Self schedule: @ selector (backgroundMusicManager) interval: 16];
Pre-load your audio file to the memory
- [[SimpleAudioEngine sharedEngine] preloadEffect: @ "1. m4a"];
-
- [[SimpleAudioEngine sharedEngine] preloadEffect: @ "2. m4a"];
-
- [[SimpleAudioEngine sharedEngine] preloadEffect: @ "3. m4a"];
-
- [[SimpleAudioEngine sharedEngine] preloadEffect: @ "4. m4a"];
-
- [[SimpleAudioEngine sharedEngine] preloadEffect: @ "5. m4a"];
-
- [[SimpleAudioEngine sharedEngine] preloadEffect: @ "6. m4a"];
Finally, start the music manager to run it for the first time. Otherwise, the first running will only happen 16 seconds later.
- [Self backgroundMusicManager];
Your music manager Method
- -(Void) backgroundMusicManager {
-
- Float intensity;
-
- /* Use your own code to control the intensity of your music */
-
- If (intensity> = 0 & intensity <0.1666f ){
-
- LastBGSoundPlaying = [[SimpleAudioEngine sharedEngine] playEffect: @ "1. m4a" pitch: 1.0f pan: 0.0f gain: 0.35f];
-
- } Elseif (intensity >=0.1666f & intensity <0.3333f ){
-
- LastBGSoundPlaying = [[SimpleAudioEngine sharedEngine] playEffect: @ "2. m4a" pitch: 1.0f pan: 0.0f gain: 0.35f];
-
- } Elseif (intensity >=0.3333f & intensity <0.5f ){
-
- LastBGSoundPlaying = [[SimpleAudioEngine sharedEngine] playEffect: @ "3. m4a" pitch: 1.0f pan: 0.0f gain: 0.35f];
-
- } Elseif (intensity >=0.5f & intensity <0.6666f ){
-
- LastBGSoundPlaying = [[SimpleAudioEngine sharedEngine] playEffect: @ "4. m4a" pitch: 1.0f pan: 0.0f gain: 0.35f];
-
- } Elseif (intensity >=0.6666f & intensity <0.8333f ){
-
- LastBGSoundPlaying = [[SimpleAudioEngine sharedEngine] playEffect: @ "5. m4a" pitch: 1.0f pan: 0.0f gain: 0.35f];
-
- } Elseif (intensity> = 0.8333f ){
-
- LastBGSoundPlaying = [[SimpleAudioEngine sharedEngine] playEffect: @ "6. m4a" pitch: 1.0f pan: 0.0f gain: 0.35f];
-
- } Else {
-
- LastBGSoundPlaying = [[SimpleAudioEngine sharedEngine] playEffect: @ "1. m4a" pitch: 1.0f pan: 0.0f gain: 0.35f];
-
- }
-
- }
Do you remember why these audio tracks are a little longer than 16 seconds? Because they contain echo and fade-out effects. This is why I used playEffect instead of cocosDenshion to play background music. In this way, audio tracks can overlap in sequence at accurate time to achieve a good seamless stitching effect.
Before running this code, you need to write a new method to determine the current strength of your game. In wordSurge, I used a combination of the remaining player life value and the number of bubbles on the screen to determine. Using the player's life value is a reasonable choice, but your game is unique and you can make full use of your creativity here.
Tips
You need to pay attention to memory usage. If you encounter memory restrictions after pre-loading music, you may need to reduce the mixing to a single channel, or lower the music sampling rate (kHz), such as 22.050. make sure that cocosDenshion runs at this sampling rate through the setMixSampleRate method.
Complete optimization is required to ensure that all audio files use the same sampling rate.
Adjust the gain settings so that your background music can be well integrated with other sounds. In general, you need to make the background music quiet. If you hear a bang when playing a variety of sound effects, your background music gain will be too high.