From 8686aad50775b7a0c770ea501a57eb727d1d7dc9 Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Mon, 16 Dec 2019 21:31:03 -0500 Subject: [PATCH] Add audio volume tools --- .../Tests/GamecraftModdingAPIPluginTest.cs | 2 + GamecraftModdingAPI/Utility/AudioTools.cs | 82 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 GamecraftModdingAPI/Utility/AudioTools.cs diff --git a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs index cdafb76..7c0e7df 100644 --- a/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs +++ b/GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs @@ -42,6 +42,8 @@ namespace GamecraftModdingAPI.Tests //SteamInitPatch.ForcePassSteamCheck = true; // in case running in a VM //MinimumSpecsCheckPatch.ForcePassMinimumSpecCheck = true; + // disable background music + AudioTools.SetVolume(0.0f, "Music"); // debug/test handlers EventManager.AddEventHandler(new SimpleEventHandlerEngine(() => { Logging.Log("App Inited event!"); }, () => { }, diff --git a/GamecraftModdingAPI/Utility/AudioTools.cs b/GamecraftModdingAPI/Utility/AudioTools.cs new file mode 100644 index 0000000..4d4af0d --- /dev/null +++ b/GamecraftModdingAPI/Utility/AudioTools.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +using FMODUnity; +using FMOD.Studio; + +namespace GamecraftModdingAPI.Utility +{ + /// + /// Common operations on audio objects + /// + public static class AudioTools + { + /// + /// Retrieve the list of mixers (aka VCAs) + /// + /// The names of the mixers (without "vca:/") + public static string[] GetMixers() + { + Bank masterBank; + RuntimeManager.StudioSystem.getBank("bank:/Master Bank", out masterBank); + VCA[] masterVCAs; + int count; + masterBank.getVCAList(out masterVCAs); + masterBank.getVCACount(out count); + string[] result = new string[count]; + string path; + for (int i = 0; i < count; i++) + { + masterVCAs[i].getPath(out path); + result[i] = path.Replace("vca:/", ""); + } + return result; + } + + /// + /// Get the volume of an audio mixer + /// + /// The name of the mixer + /// The volume + public static float GetVolume(string mixer) + { + float volume, finalVolume; + RuntimeManager.GetVCA($"vca:/{mixer}").getVolume(out volume, out finalVolume); + return volume; + } + + /// + /// Get the volume of an audio mixer at output time (this is influenced by the mixer volume and master volume) + /// + /// The name of the mixer + /// The final volume + public static float GetVolumeOutput(string mixer) + { + float volume, finalVolume; + RuntimeManager.GetVCA($"vca:/{mixer}").getVolume(out volume, out finalVolume); + return finalVolume; + } + + /// + /// Set the volume of an audio mixer (like a VCA aka Voltage-Controlled Amplifier) + /// + /// The volume from 0.0 to 1.0 (1.0+ is valid too) + /// The name of the mixer, as retrieved from GetMixers() + public static void SetVolume(float volume, string mixer) + { + RuntimeManager.GetVCA($"vca:/{mixer}").setVolume(volume); + } + + /// + /// Set the volume for all future audio + /// + /// The volume from 0.0 to 1.0 (1.0+ is valid too) + public static void SetVolumeMaster(float volume) + { + RuntimeManager.GetBus("bus:/").setVolume(volume); + } + } +}