There are a couple of ways you can use sound in the Armory game engine and we will look at all of them now. First let’s start by playing a simple sound effect, in this case, a .WAV file. You can also use .OGG and .MP4 platforms, depending on the platform you are targeting. WAV is an ideal format for playing sound effects, while OGG is better for longer audio files, such as music or dialogs. First, we need to get a resource into our game. In the Armory properties, click Project Folder.
This will open your local file system browser, such as Windows Explorer on Windows or Finder on Mac. Create a new folder called “Bundled” in this directory
Next, drop your .WAV file into this folder. If you need a source for .WAV files, try FreeSound.org if you wish to download some, or http://sfbgames.com/chiptone/ if you wish to make your own simple sound effects.
Let’s start with a Haxe example, create a node in the scene or use the default cube and attached the following trait/script to it:
package arm; class SoundPlayer extends iron.Trait { public function new() { super(); notifyOnUpdate(function() { if(iron.system.Input.getKeyboard().down("s")){ iron.data.Data.getSound("belch.wav",function(sound:kha.Sound){ iron.system.Audio.play(sound); }); } }); } }
The code is pretty straight forward, in the update callback of our trait, we check to see if the ‘S’ key is pressed if it is we call getSound(), which takes two parameters. The first is the name of the sound file to load, the second is the callback function to call when the loading is done. Once the file is loaded we play it will a call to Audio.play() passing our newly loaded audio file.
Now let’s perform the exact same task using nodes instead. Before we continue, however, we are going to have to make the sound effect available in Armory. First, start by hitting Space Bar and typing open sound:
A file choosing dialog will now appear, select the sound file you just added to the Bundled folder. Next in the Outliner, switch to Data-Blocks mode:
Now scroll down and Sounds, expand it, and select your newly added sound, click the text box to the right of Fake User.
Open the Node Editor, switch to game mode and create the following Node tree called “SoundPlayer”:
Now make sure that you create a trait, of type Nodes and attach your newly created node:
The final way you can play sound in Armory is by using the Speaker object in Blender. Select Add->Speaker in 3D View.
Now select the Speaker Icon in the Properties panel, drop down the Music note dialog and select the wav file.
Now create the following node tree:
In the Play Speaker node, be sure to select the newly created Speaker in Blender. The nice thing about using the Speaker node is you have detailed control over the way the audio is played such as the volume: