Importing and Triggering Custom Sounds

Originally Written by Stephen Olender
Using Custom Sounds in Visual3d.net

To create custom sounds in Visual3D.netThe first step is to have the right kind of sound file, in the right place. To use files as “sounds” (as opposed to music, which we’re pretty sure has different architecture), the file MUST be in .wav format. I’m not sure about file size to be honest, but I’ve been able to input sounds as large as 1.45 MB (sounds that are used repeatedly, such as the rocket-firing sound in Scripted CTF) without any problems whatsoever.

 

Once you have your WAV file, you’ll need to put it in the correct folder. Open your Visual3d.net folder (default install is directly in C:/). From there you’ll head into Demo Assets, and then into Sounds. Drop your WAV into the Sounds folder.

 

Code-side, there isn’t much to be done, either; at least, not for a simple swap-in. The specific file I worked with was, as I mentioned, Rocket.cs. This file can be found by going into Visual3d.net > Tech Demos > Tech Demos.csproj . Inside this project, you’ll find the Rocket.cs file under Demo Assets > Actors > Custom > Missiles > Rocket.cs. On line 84 through line 88 of Rocket.cs, you should see this code:

 

protected override Sound CreateLaunchSound()

{

return Sound.Create(

World.Audio.CreateSoundConfig(IdNames.New("FireMissileSound"), "missile", "wav", -1200, 1f, 1, 100, 4000));

}

 

This is an override that refers to a public virtual (called SoundConfig) in the file Visual3d.Audio.AudioSystem.cs, which is metadata. From my understanding of the way it works, “FireMissileSound” in this example is the ID name of the sound being called, “missile” is the name of the actual sound file being called, and “wav” is the extension of the file. The rest of the numbers are variables that affect different traits of the sound, but aren’t really important to this tutorial. The outline for the function as defined in AudioSystem is as follows:

 

public virtual SoundConfig CreateSoundConfig(string idName, string fileBaseName, string fileExtension, int volume, float speed, short numberOfExecutions, float minDistance, float maxDistance);

 

The idName and fileBaseName were the only two arguments I had to alter once the file was in the proper folder. In Rocket.cs, line 87, I changed “FireMissileSound” to an idName more appropriate to my custom sound, and then switched “missile” with the actual name of my custom sound file. I didn’t touch any of the values after that, but you can if you want. It shouldn’t affect the ability of the engine to play your sound unless you put in a value not compatible with the data type, or turn the volume down so low you can’t hear it.

 

Once you’ve made those two code changes, save everything, click “Build Solution” in the Build menu, and once the build is completed (hopefully successfully), run Vis3d Beta. Launch the Tech Demos world, choose Scripted CTF (only because there are constantly rockets firing in that demo), and check to see if your change took place.

 

You should be able to do this with any actor that makes a sound, and test it in any demo where the sound takes place. This was just a specific example to show the basic mechanics of the fix. It’s cheap, yes, but it’s quick and easy, and it works.

 

Hope this helps.