Downloadable
See How to use it? for more information.
What does the project do?
This audio project is based on Windows XAudio2 library. It provides a fast and easy way to add music to your game.
How to use it?
There are two parts of this project.
Audio Builder
Just like MeshBuilder and EffectBuilder, it asks for a Lua file and outputs the binary file that will be used for the Audio static library.
In order for the builder to work properly, you need to create a folder called Audio in the Content folder and make sure the actual audio file is also in this folder.
An example .audio file would be something like this:
-- background.audio
return{
audio = "Test.mp3",
loop = false,
autoplay = true,
volume = 1.0,
pitch = 1.0,
effects = {
reverb = true,
-- [0, 100]
density = 50,
-- [0.1, inf]
decayTime = 2,
-- [1, 100] in feet
roomSize = 100,
-- [0, 100]
wetDryMix = 100,
-- [0, 300] in ms
reflectionDelay = 4,
-- [0, 30]
positionLeft = 6,
-- [0, 30]
positionRight = 6,
},
}
*The reverb effect is optional. If you want to turn it off, just assign false to reverb variable.
Make sure you also add this to your AssetBuildFunctions.lua.
NewAssetTypeInfo( "audio",
{
ConvertSourceRelativePathToBuiltRelativePath = function( i_sourceRelativePath )
-- Change the source file extension to the binary version
local relativeDirectory, file = i_sourceRelativePath:match( "(.-)([^/\\]+)$" )
local fileName, extensionWithPeriod = file:match( "([^%.]+)(.*)" )
-- The line below just puts the original pieces back together,
-- but you could change this to customize the way that you build assets
-- (you could, for example, use a different extension for binary shaders)
return relativeDirectory .. fileName .. ".baudio"
end,
GetBuilderRelativePath = function()
return "AudioBuilder.exe"
end,
}
)
Don’t forget to add asset paths to AssetsToBuild.lua.
Audio Library
Most information you’ll need is in Audio.h and cMusic.h.
Audio.h
There are only two interfaces.
Initialize(): Should be called when initializing the game. You can also put it in Appilication class.
CleanUp(): Should be called when exiting the game.
cMusic.h:
Load(const char* p_MusicPath, cMusic*& o_Music): Should be called when initializing a binary audio file. The first parameter is the binary file path. The second parameter is the output cMusic object.
Play(bool p_FromStart = false, bool p_IsLoop = false): Music is played from start and is only played once by default. Make sure you pass in right parameters if you want it to act differently.
Stop(): Stop the music.
ExitLoop()
SetVolume(float p_Volume)/GetVolume()
SetPitchRatio(float p_Ratio)/GetPitchRatio(): Pay attention to this function. Since pitch is changed by changing frequency, music will become faster.
*To release a cMusic object, simply call delete.
What have I Learned? What have I done?
There aren’t many tutorials online and usually I can only look for answers in Microsoft documentations. I made hundreds of mistakes at first, such as using smart pointers everywhere or releasing something before using it, but gradually, I’m more used to it. It’s quite fun to learn about how actually every audio component works. Because XAudio2 used to be widely used in Xbox 360, I feel these interfaces are really suitable for a game. Effect is the most interesting part. Though I didn’t have time to learn more about it, I felt that it could allow you to do a lot of cool things that a professional software would allow you to do.
In the beginning, I forgot that windows was also a platform so I didn’t separate platform-specific code from other code, which was corrected later. This really should happen especially because XAudio2 is a window library. Thank you John!
I noticed almost everyone(including me) chose to create human-readable files. I think they are especially useful in this project, since they are clear and easy to understand.
If you have any questions about this project, don’t hesitate to ping me on slack!
Comments