Firelight Technologies FMOD Studio API
Programmer Sounds
FMOD Studio events can include programmer sound modules that are controlled at runtime. There are a few different ways of hooking them up.
Programmer Sounds via Audio Tables
Note: With this approach, you don't need to do any programming at all!
Setting up audio tables
Audio tables are directories of sounds that are loaded up into a bank file. You can use audio tables to control localized sounds. To create an audio table, create a new bank, assign an audio table to it, and then select the directory which will contain all the sounds you want in the bank.
The "key" for each entry it just the file name without any extension. You can create multiple banks each pointing to a different directory for each localized audio you need.
Loading audio tables
By default, the UE4 integration loads all banks at startup. To avoid that, you can use the advanced setting "Skip Bank Load Name" and provide a prefix, such as "Lang_". In the above project, it means the normal banks will be loaded at startup, but Lang_EN.bank and Lang_JP.bank will be skipped.
Then you can load up one of those banks using blueprint function "Load Bank".
Choosing the audio entry to play
Create an event with a programmer sound module on it. If the module has a name, then if nothing else is assigned then that sound will be used. For example, if the sound designer sets the module name as "Welcome", then the audio table entry "Welcome" will be used by default.
To select at runtime what audio entry to use, set the "Programmer Sound Name" field in the FMOD audio component.
Or you can assign the name via blueprint.
The name has to be one of the audio asset entries of a loaded audio table, or it won't find the sound to play.
Be careful to set the name before you play the audio component. If the name is assigned after the event has started, it may not play the right sound.
Programmer Sounds by Path
Note: With this approach, you can easily play any media file for your event.
You can set up a programmer sound to point directly to a file. To do this, set the FMOD audio component's "Programmer Sound Name" to the path to the .wav or .ogg file that you want to load. If this path is relative, it will be looked up relative to the content directory.
If you do this, you'll need to make sure that directory with the media files is added to "Directories to Package" in the packaging settings, otherwise it will work in the editor, but not when packaged into the final game.
Programmer Sounds via API
Note: With this approach, you have the most flexibility for programmers.
If you are writing code, you can programmatically set the FMOD Sound to use by calling the FMOD audio component function SetProgrammerSound. Here is an example of setting the sound from code:
void AExampleGameMode::InitAudio(UFMODAudioComponent* AudioComponent)
{
if (AudioComponent)
{
FMOD::Studio::System* System = IFMODStudioModule::Get().GetStudioSystem(EFMODSystemContext::Runtime);
FMOD::System* LowLevelSystem = nullptr;
System->getLowLevelSystem(&LowLevelSystem);
// Create sound in memory
static const int EXAMPLE_SOUND_LEN = 4096;
float ExampleData[EXAMPLE_SOUND_LEN];
for (int i=0; i<EXAMPLE_SOUND_LEN; ++i)
{
ExampleData[i] = FMath::Sin((float)i);
}
FMOD_CREATESOUNDEXINFO SoundInfo = {0};
SoundInfo.cbsize = sizeof(SoundInfo);
SoundInfo.format = FMOD_SOUND_FORMAT_PCMFLOAT;
SoundInfo.defaultfrequency = 12000;
SoundInfo.numchannels = 1;
SoundInfo.length = sizeof(float) * EXAMPLE_SOUND_LEN;
FMOD::Sound* Sound = nullptr;
if (LowLevelSystem->createSound(reinterpret_cast<const char*>(ExampleData), FMOD_OPENMEMORY | FMOD_OPENRAW | FMOD_LOOP_OFF, &SoundInfo, &Sound) == FMOD_OK)
{
AudioComponent->SetProgrammerSound(Sound);
// Note: Need to remember to release the sound, *after* the audio component has finished using it.
}
}
}
Troubleshooting
When setting the programmer sound via Blueprint or code, you will need to make sure that you set it before you play the event. If the audio component is already playing, then setting the name won't have any effect.
Also, when setting the name to an audio table entry, you will need to make sure the audio table bank is already loaded before the event starts.
The FMOD audio component only supports a single programmer sound per event. If you want to have an event that has multiple programmer sounds, each one playing a different sound, then you'll need to create the event directly via the FMOD API and provide your own callback. You can look at how the FMOD audio component programmer sound callback works and use that as a base for your own class.