Jump to content
  • entries
    51
  • comments
    106
  • views
    28,233

Loading Sounds with JSON


reepblue

2,174 views

 Share

Loading sounds in Leadwerks has always been straight forward. A sound file is loaded from the disk, and with the Source class emits the sound in 3D space. The sound entity also has a play function, but it's only really good for UI sounds. There is also Entity::EmitSound() which will play the sound at the entity's location. (You can also throw in a Source, but it'll auto release the object when it's done.)

While this is OK for small games, larger games in which sounds may change might mean you have to open your class, and adjust the sounds accordingly. What if you use the sound in multiple places and you're happy with the volume and pitch settings from an earlier implementation? You could just redefine the source in a different actor, but why should you?

A solution I came up with comes from SoundScripts from the Source Engine. With that engine, you had to define each sound as a SoundScript entry. This allowed you to define a sound once, and it allowed for other sound settings such as multiple sounds per entry. I thought this over, and with JSON, we can easily create a similar system for Leadwerks 4 and the new engine.

I first started with a dummy script so I can figure out how I wanted the end result to be.

{
    "soundData":
    {
        "Error":
        {
            "file": "Sound/error.wav",
            "volume": 1.0,
            "pitch": 1.0,
            "range": 0.25
        },

        "RandomSound":
        {
            "files":
            {
                "file1": "Sound/Test/tone1.wav",
                "file2": "Sound/Test/tone2.wav",
                "file3": "Sound/Test/tone3.wav"
            },
            
            "volume": 1.0,
            "pitch": 1.0,
            "range": 0.25
        }        
    }
}

In this script, we have two sound entries. We have an error sound (Which is suppose to be the fall back sound for an invalid sound entry) and we have a sound entry that holds multiple files. We want a simple, straight forward. entry like "Error" to work, while also supporting something "RandomSound" which can be used for something like footstep sounds.

The script is streamed and stored into multiple structs in a std::map at the application start. We use the key for the name, and the value is the struct.

typedef struct
{
	std::string files[128];
	char filecount; 
	float volume;
	float pitch;
	float range;
	bool loopmode;

} sounddata_t;

std::map<std::string, sounddata_t> scriptedsounds;

Also notice that we don't store any pointers, just information. To do the next bit, I decided to derive off of the engine's Source class and call it "Speaker". The Speaker class allows us to load sounds via the script entry, and support multiple sounds.

You create one like this, and you have all the functionalities with the Source as before, but a few differences.

	// Speaker:
	auto speaker = CreateSpeaker("RandomSound");

When you use Play() with the speaker class and if the sound entry has a "files" table array, it'll pick a sound at random. You can also use PlayIndex() to play the sound entry in the array. I also added a SetSourceEntity() function which will create a pivot, parent to the target entity. From there, the Play function will always play from the pivot's position. This is a good alternative to Entity::EmitSound(), as you don't need to Copy/Instance the Source before calling the function as that function releases the Source as mentioned earlier. Just play the speaker, and you'll be fine! You can also change the sound entry at anytime by calling SetSoundEntry(const std::string pSoundEntryName); The creation of the Speaker class will start the JSON phrasing. If it has already been done, it will not do it again.

Having sounds being loaded and stored like this opens up a lot of possibles. One thing I plan on implementing is a volume modifier which will adjust the volume based on the games volume setting.Right now, it uses the defined volume setting. It's also a part of another system I have in the works.

  • Like 2
 Share

6 Comments


Recommended Comments

2 hours ago, Josh said:

"Speaker" is definitely a better name for the class than "Source". I'm taking that.

Good. But now I need to find a new name for my class. ? 

Maybe you can modify EmitSound to have a parameter so it'll do the copying of the Speaker for you. IDK I recall that "feature" making me upset using Leadwerks as I didn't expect it to auto release my objects 

Link to comment
5 minutes ago, reepblue said:

Maybe you can modify EmitSound to have a parameter so it'll do the copying of the Speaker for you. IDK I recall that "feature" making me upset using Leadwerks as I didn't expect it to auto release my objects 

That's the wonderful thing about smart pointers, problems like this go away.

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...