In the previous part, we’ve integrated FMOD and Unity, and created a small project with a looping piece of music. In this part of the course, we will start our larger project – we will learn how to create horizontal adaptivity for a simple game.
4. Horizontal Adaptivity
In the previous chapter, you’ve learned how to integrate FMOD and Unity projects and how to add music to your game by using a simple component attached to an object in the game. In this chapter, we will turn this object off, and add a bit more interactive music to our game. We will control this music using simple C# scripts.
Another term for interactive music is adaptive music – a music that adapts to things happening on the screen. There are two basic “concepts” for adapting music: horizontal and vertical adaptivity. The horizontal adaptivity will be described in this chapter, while another chapter will discuss the vertical adaptivity.
To achieve our goals, we’ll discuss using scripts, game objects and triggers in Unity itself; parameters in FMOD; and we will also mention thing or two about things to keep in mind while composing music.
Horizontal and Vertical Adaptivity
When music is adapting horizontally, it means that the music transitions from one part to another in time – for example, we play the part between 10 and 20 seconds, and then we transition to a part between 30 and 40 seconds of the track. We create our parameters and music in events so that we can change the regions being played by using scripts in the game itself.
Vertical adaptivity is different – we play the very same piece of our music, let’s say between 10 and 20 seconds of it, but based on the things happening in the game, we change the layers of the music being played, for example by adding intensive drums, or taking away some elements of the music when they are not needed.
Each of these options has its ups and downs. The biggest problem is often the clean and seamless transitions between different pieces of music.
Both the horizontal and vertical adaptivity can be used in our game using FMOD. In this chapter, we will focus on the horizontal adaptivity.
Composing Comes First
Because our music will be interactive, it’s during the composing phase when you have to think it through. You have to decide which part of the composition will be looping, which part of the track will be used as transition parts between loops and so on.
I plan for my game – my example scene – to allow my player to walk between two areas of the scene, and to change the music between these two areas. Thus, when composing my music I’m already taking this into account and I want music to be more intense in one of the areas of the game. The simplest solution I can come up with is to compose the track in a simple AAA form, in which each following section of my music gets more intense. So this is a track I’ve created – omitting the transitions between sections entirely.
The track starts with a simple intro and then repeats the sections, adding new instruments, and finishing the track with an outro. At the same time, there are clear regions that are going to loop. This way the track can be used in the game, but it can also be put on the official soundtrack.
When composing, I took a single bar an copied some of its instrument, then I used it as a transition piece between the loops. I simply copied the drums. This way, by taking this single bar from my music, I can place it in FMOD event to act as a transition region between loops, and use a crossfade transition to make everything sound smooth.
The transition piece is then exported as a separate audio file. Sometimes you have to export different parts of the track as separate files to have greater control over the music in FMOD project.
Generally, it’s difficult to explain all of this – you simply need to plan ahead and think which parts of the music should loop, which parts should act as a transition pieces and so on. We plan this, because we never know how long the music should play. The player may get stuck in some part of the game, and the music must play constantly before switching to another loop. After the player moves to another part of the game, the music should change to another loop and so on. And it’s always useful to come up with transition pieces and stringers that are generally a part of your music, but can also be exported as separate files to be used as crossfade transitions.
I’ve exported the entire music piece as a single track, but I also copied a single bar of the rhythmic elements as transition elements, and some cymbals effects as yet another separate files. Figure 4.3 shows you a list of all the files.
After composing and exporting the piece, you can go back to FMOD. Let’s stay in the same project from the previous chapter. Open the Audio Bin and put your new files into it – you can use your own files, or the files from Horizontal_Layering folder I made for this book. You’ll find six files in this folder.
Now go to the Events tab and create a new 2D event. Call it HorizontalMusic. Create three audio tracks in it and call them:
- Main Track
Now go to the Assets tab and drag the fmod-kurs.ogg onto the Main Track. This is the main music piece that we will work with in this chapter. Play it, and you’ll notice it has a simple AAA form.
Take a look at the top of your editor, next to the playback controls. Make sure you switch to Beats, not the Time. The Beats button should be highlighted in gray. Thanks to this you will switch the timeline to beats display.
Now right-click on the logic tracks area and from the menu, choose Add Tempo Marker.
This will add a new marker on the logic tracks region, as figure 4.7 shows.
By using tempo markers in various places of the track, you can change both the tempo and the meter of the track. 120 BPM and 4/4 are the default values. Double-click the marker to type new values and adjust them to the track. In my case, I’m leaving the default values, because music I have composed is actually composed in 4/4 with 120 BPM.
We have to add the tempo marker so that FMOD can correctly divide the track to bars. Earlier, we have switched the timeline to display beats, not seconds, and this new beat-based timeline is controlled by this very marker.
Take a look at the timeline at figure 4.8.
Try to change the tempo marker, and type 6/8, for example. Look at the changes.
The timeline changes. OK, now go back to 4/4, because this is how my music has been composed.
If you have listened to my track, you already know it has at least four sections – I planned for three of them to loop. Create a new region on the logic tracks area – remember, Add Loop Region. Create this region between bars 13 and 21, like on figure 4.10.
Make sure the loop is well adjusted to bars. To do so, use the mouse wheel while holding the Alt key to zoom in.
Play the loop now, and you’ll hear it works. Now right-click on the loop and select Copy from the menu. Next, right-click on the logic tracks region and choose Paste from the menu. Place this pasted loop on bars 21 do 29. Repeat this one more time, and place the third loop between bars 29 and 37. You’ll create three loop regions this way. Each of this loops will be played in a different area of the scene in the game.
To create the mechanism for playing different parts of our music in accordance with the area of the game in which the player walks, we have to create some markers and transition regions, along with the conditional logic. It’s quite easy to do so, though.
In creating interactive music FMOD and C# themselves aren’t too complicated. The most difficult is usually the logic during planning and composing music. You simply have to plan how things will work in the game and how the music itself should look, to work well in the game.
Right-click the logic tracks region and from the menu choose Add Destination Marker.
Double-click the marker and change its name to “A”. Next, click and hold left mouse button to drag the marker one step down, and then place it at the beginning of the first loop. You will notice that the logic tracks region gets bigger this way.
Repeat this process two more times, and create B and C markers. Place them at the beginning of the second and third loop.
These simple markers mark places, to which playback should jump when specific conditions are met. We will create these conditions soon, using special field in the dock. But markers aren’t everything. Now we have to create transition regions.
Transition regions in FMOD
Open the right-click menu on the logic tracks area, and choose Add Transition Region. This way you can create a transition region, which is area of the track, from which music jumps to a different places if we tell it so.
Now move this new region below the first loop and expand it so that it fits the length of the loop. Next, right-click on this green region and choose Set Destination To | B.
This way you will create a region of the track that points towards B marker. If you try to play the first loop now (the A section), you’ll notice that we jump to the second loop right away. This is normal behaviour, because we haven’t set the logic of first loop’s behaviour. We’ll do this soon. First we need to create a few more transition regions. These regions must cover all possibilities in the game – after all, the player can do various things, like moving between different areas of the game, so the game mechanics must allow all possible changes in music to happen.
Right-click on the logic tracks area and choose Add Transition Region To | C. This is a different way to create transition regions, a bit faster. Place this region in the A loop, below the transition to B loop.
A good practice is to organize transition regions and generally logic elements on different levels. For example, we can place all transitions to the same loop at the same level. This way, it’s much easier to manage such logic tracks.
Create more transition regions, by creating them and placing under proper loops.
- B to C
- C to A
- B to A
- C to B
Figure 4.19 shows how all these regions should look now. At this point, no loop will actually play, because each region tries to change playback to a different loop. To fix this, we need to add conditional logic.
Conditional Logic in FMOD
Let’s do some conditional logic now. This logic will be controlled by a parameter. Click the plus button, +, next to Timeline. Then, choose New Parameter.
As you can see on figure 4.20, I already created one parameter, called Intensity. Don’t worry about it, you’ll create a different parameter now. But now that after creating a couple of parameters, this is where you will see a list of them.
Create your own parameter and call it Adaptive_Level. Set it’s value range between 1 and 5.
At the top of the interface you will notice a knob with a name of the newly created parameter. If you use it, you will notice that its values are within the range defined earlier, between 1 and 5.
Parameters are a powerful FMOD element that allow you to control music and sound in the game using C# scripts. Notice that creating this new parameter also created a new tab next to the Timeline. Don’t worry about it for now. Make sure you switch the editor view back to the Timeline tab.
Now we have everything we need to create the logic for our transitions. Select the To B transition in the A loop. At the bottom of the editor, in the dock, you will see a panel called Conditions.
There, click Add Condition. From the menu that appears, choose Add Parameter Condition | Adaptive_Level. In this submenu you will always see a list of all the parameters you’ve created for each project. OK, now let’s get to work.
Let’s say that we want for the first loop of our music to play all the time, unless things change in the game. We can say that the adaptive level of our music is between 1 and 2. But if the player decide to enter a different zone in our game, we want to play the second loop. Let it be the adaptive level of 2 to 3. FMOD needs specific values to work, so for the Adaptive_Level parameters for the To B transition, set values 2.00 and 2.99. You can drag the green sliders (with these little arrows) both ways to set these values, or you can double-click the digits and simply type the values. Take a look at figure 4.24.
Basically, all of this says that if the Adaptive_Level value in the game (set through a C# script) is between 2.00 and 2.99, then the condition is true and this particular transition region will switch the playback to the target loop.
Now, choose the To C transition in the A loop, and here, too, create an Adaptive_Level parameter condition, then set the values to 3.00 and 3.99. This way, once these conditions are met, the music will jump to the third loop.
Because all the conditions for this first loop are set, the loop itself can finally be played – it will work, because by default our Adaptive_Level’s knob is set to 1. Thus, the conditions will not be met and the first loop will play indefinitely.
But before we can test all the other loops, we need to set conditions in them, as well. Here are the conditions you need for each other loop.
- Section / loop B: transition to C – 3.00 to 3.99
- Section / loop B: transition to A – 1.00 to 1.99
- Section / loop C: transition to B – 2.00 to 2.99
- Section / loop C: transition to A – 1.00 to 1.99
This way you set all the conditions for all the regions – the logic is done. Now, play the very first loop and wait until it loops to check if it works. Then, by still playing the track, start turning the Adaptive_Level’s knob. Set its values to somewhere between 2.00 and 2.99 and you will see that our music jumps to the second loop. Then, turn the knob to values between 3.00 and 3.99, and our music will jump to the third loop. Keep turning the knob, changing its values, and you should see everything works.
You probably noticed already that if you play the track from the very beginning, music will keep playing until it reaches the first loop, and there it will loop indefinitely, unless specific conditions are met.
But right now, the transitions between the loops are not perfect – they’re sudden and instant. Later on we will deal with this issue and make everything smooth, but for now, let’s go to Unity itself and see how our music works in an actual game.
Add the HorizontalMusic event to the Master Bank, by clicking the right mouse button on this event in the Events tab, and selecting Assign to Bank | Master Bank. Finally, build the project, File | Build, and save everything. Now we can go to Unity.
Let’s finish for today – in the next part, we’ll continue with horizontal adaptivity.
Don't forget to become a fan on Facebook and subscribe to new posts via RSS or via email.