Categories Menu

Posted by on Feb 12, 2016 | 0 comments

Dive Into Code! – Part 1 of 3

A vacant dock relaxes in the grand view of the Rainbow Park mountain range as a peaceful Alta Lake flickers below.

Photo: Leonard Paul

This article is a guest contribution by Leonard Paul, president of the School of Video Game Audio. He has worked on over twenty AAA and indie games such as ‘Need for Speed: Hot Pursuit 2,’ ‘NHL11,’ ‘Vessel’ and ‘Retro City Rampage’ as a technical sound designer and composer, and he has also composed for documentaries like ‘The Corporation’ and the upcoming ‘Beep: A Documentary History of Game Sound.’ You can visit his School of Video Game Audio website or can follow him at @SchoolGameAudio.

 

Ready for the plunge? Or maybe just a toe first? There has never been a better time to “dive” into audio coding, but instead of jumping in and hoping to swim right away, we’re just going to get our feet wet with this first article in the series.

This article is a gentle introduction to the fun world of game audio programming using C++ with FMOD Studio under OS X. You might be familiar with audio middleware implementation, or even complex effects chains and intricate modular synthesizer patches, but the thought of C++ code can still seem a bit daunting. It can be hard to figure out where to start with game audio coding, especially since the software and technology changes every few years. But just like learning a new language, even a few phrases can have amazing outcomes. C++ is currently the language used in many games, and the tools to learn how to code have never been more accessible. Each tool used in this article is entirely free to download and use, and with FMOD Studio being free for commercial indie releases, the skills you learn here can be used directly when working on games. It definitely isn’t necessary to know how to code when working in game audio, but it’s a lot more fun when you understand how a game plays back your sounds, and it can help you learn how to have more creative control. Now let’s get to it!

 

Step 1: Download and install FMOD Studio and API

First, you’ll need to create an account on FMOD.org to access the FMOD API downloads. Click “Register” at the top right of the FMOD homepage to arrive at the registration page: http://www.fmod.org/profile/register/. Once you’re logged in, the page will show you’re logged in at the top right.

Next, download the “FMOD Studio Authoring Tool” for Macintosh from the FMOD Download page: http://www.fmod.org/download/. This authoring tool will be your main way of changing the implementation of the game audio.

If using VoiceOver, (V O+F) V O search for 'Macintosh' and the .dmg download link will be the next item.

Next, download the “FMOD Studio Programmer’s API and Low Level Programmer API” for Macintosh from further down the same Download page. The API you’re downloading basically allows the code from the game to talk to the game audio middleware. When an action happens in the game, such as a footstep, the game sends out an event to the audio system. The API tells the game audio code what to do, such as play a footstep sound.

Same as last time. If using VoiceOver, (V O+F) V O search for 'Macintosh' and the .dmg download link will be the next item.

Install the Authoring Tool and then the API from the downloaded .dmg files. When the installer windows open, the FMOD Studio tool should go in your Applications folder and the FMOD Programmers API should go in your Home folder inside an “FMOD Studio” folder that you will make. If you don’t know where your Home folder is, you can locate it in the Finder with the Menu Bar by going to Go→Go to folder… and then entering tilde (“~”) in the window. You can also select Go→Home (Shift + Command + H) from the Finder. Your Home folder should now appear and you can create a new “FMOD Studio” folder in it (with a space between FMOD and Studio).

Step 2: Install Xcode 

Download Xcode using the link: https://developer.apple.com/xcode/download/. Likely it’s best to skip over the Beta version and download the current stable version of Xcode from the App Store by clicking “View in the Mac App Store >”.

If using VoiceOver, V O search for 'View in the Mac App Store' and select the link

Depending on when you download it, the version of Xcode might be different, but Xcode 6 and higher should work with the example. Install Xcode by opening the .dmg and following the instructions inside.

Step 3: Add custom simple_event.cpp main file and open Xcode project

Now, locate the Xcode project (don’t open it just yet). It should be found in:

~/FMOD Studio/FMOD Programmers API/api/studio/examples/xcode32/simple_event.xcodeproj

You can either locate your Home folder and follow the directories after the tilde “~” or open the folder directly by the Finder using Go→Go to folder… and then entering the path above. Make a backup of simple_event.xcodeproj called simple_event-original.xcodeproj or something similar.

Next, download the custom simple_event.cpp main program file from the School page: http://School.VideoGameAudio.com/simple_event.cpp.

Copy this file over the existing one at:

~/FMOD Studio/FMOD Programmers API/api/studio/examples/simple_event.cpp

Then click to open the simple_event.xcodeproj located in the ‘xcode32’ folder:

~/FMOD Studio/FMOD Programmers API/api/studio/examples/xcode32

Everything should open in Xcode. If you click on simple_event.cpp in Xcode’s left panel (simple_event→Sources folder→simple_event.cpp), then you’ll see a screen similar to the following (notice the blue highlighted folder icon. This means we are in the Project navigator/folder view.):

If using VoiceOver, simple_event.cpp can be found under Xcode's Project navigator. Expand simple_event and under the Sources folder, simple_event.cpp will be the second item after a collapsed common folder.

Step 4: Run project and interact with it

Run project through the Menu Bar with Product→Run or by pressing ⌘R. Ignore the yellow warnings, hopefully everything should work :).

Interact with program by clicking on the “1” at the bottom of the black screen (not by pressing the “1” key on the keyboard). You should hear the UI Cancel sound being triggered. What is happening is that the button is telling the FMOD Studio middleware game audio engine to trigger the event that has been set up using the FMOD Studio tool. The game audio engine receives the event and plays the appropriate sound.

Hit the “More” button to close the program.

Step 5: Modify the program and sounds

Now, open the main program file by locating the simple_event.cpp file in the left panel of the Xcode in folder view.

Find “My” (⌘F) in the file, and change the line:

Common_Draw(“My Simple Event Example.”);

to

Common_Draw(“An Amazing Yet Simple Event Example.”);

or whatever you’d like, maybe add your name to it!

Recompile and run the new code using ⌘R. Hopefully the title has changed to what you want. You’re now a C++ game programmer!

But we really want to be a game audio programmer, so search for “UI/Cancel” and change the line so the “Cancel” sound is now the “Okay” sound:

ERRCHECK( system->getEvent(“event:/UI/Cancel”, &cancelDescription) );

to

ERRCHECK( system->getEvent(“event:/UI/Okay”, &cancelDescription) );

Compile (⌘R) the new code and you should be able to hear the sound is higher and shorter than last time when you trigger it with the “1” button.

You’re now a C++ game audio programmer! :) Well, the game isn’t that interesting since you’re only pressing buttons, but you are able to change the sound depending on your input in exactly the same way a game would. Another cool thing is you can share your program with almost anyone who has a Mac. All you need to do right-click simple_event.app in the Xcode left panel (found under simple_event→Products folder) and select “Show in Finder.” If you click on the .app file here, it will do exactly the same thing as running the code inside Xcode. If you send them the file then they’ll be able to check out your code!

Summary:

Just to recap, we were able to navigate Xcode, compile a C++ program and modify the code to play a specific event. As you can see, learning C++ code is just like learning a regular language, but it’s a language that computers understand. There is a lot more to learn about coding and game implementation, but I hope this article was an easy introduction to technical game audio. If you are interested in learning more, make sure you check out Part 2 and 3 of this article when they are released. Part 2 will explain a bit more about how this example works and give you other fun stuff to try out. Part 3 will get into the details of actual game events to help you understand how a real game works. But until then, use this code and dive in!

Another view of the Rainbow Park mountain range where the snow caps reflect in the lake below.

Photo: Leonard Paul

0 Comments

Trackbacks/Pingbacks

  1. Dive Into Code – Part 2 of 3 | Designing Sound - […] see how the code works in the Xcode debugger. This tutorial uses the project and C++ files from Part 1 of this series,…

Post a Reply

Your email address will not be published. Required fields are marked *