Browse Source

Add audio volume tools

tags/v0.1.1.0
NGnius (Graham) 5 years ago
parent
commit
8686aad507
2 changed files with 84 additions and 0 deletions
  1. +2
    -0
      GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs
  2. +82
    -0
      GamecraftModdingAPI/Utility/AudioTools.cs

+ 2
- 0
GamecraftModdingAPI/Tests/GamecraftModdingAPIPluginTest.cs View File

@@ -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!"); }, () => { },


+ 82
- 0
GamecraftModdingAPI/Utility/AudioTools.cs View File

@@ -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
{
/// <summary>
/// Common operations on audio objects
/// </summary>
public static class AudioTools
{
/// <summary>
/// Retrieve the list of mixers (aka VCAs)
/// </summary>
/// <returns>The names of the mixers (without "vca:/")</returns>
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;
}

/// <summary>
/// Get the volume of an audio mixer
/// </summary>
/// <param name="mixer">The name of the mixer</param>
/// <returns>The volume</returns>
public static float GetVolume(string mixer)
{
float volume, finalVolume;
RuntimeManager.GetVCA($"vca:/{mixer}").getVolume(out volume, out finalVolume);
return volume;
}

/// <summary>
/// Get the volume of an audio mixer at output time (this is influenced by the mixer volume and master volume)
/// </summary>
/// <param name="mixer">The name of the mixer</param>
/// <returns>The final volume</returns>
public static float GetVolumeOutput(string mixer)
{
float volume, finalVolume;
RuntimeManager.GetVCA($"vca:/{mixer}").getVolume(out volume, out finalVolume);
return finalVolume;
}

/// <summary>
/// Set the volume of an audio mixer (like a VCA aka Voltage-Controlled Amplifier)
/// </summary>
/// <param name="volume">The volume from 0.0 to 1.0 (1.0+ is valid too)</param>
/// <param name="mixer">The name of the mixer, as retrieved from GetMixers()</param>
public static void SetVolume(float volume, string mixer)
{
RuntimeManager.GetVCA($"vca:/{mixer}").setVolume(volume);
}

/// <summary>
/// Set the volume for all future audio
/// </summary>
/// <param name="volume">The volume from 0.0 to 1.0 (1.0+ is valid too)</param>
public static void SetVolumeMaster(float volume)
{
RuntimeManager.GetBus("bus:/").setVolume(volume);
}
}
}

Loading…
Cancel
Save