-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSGBAudioSettings.cs
172 lines (142 loc) · 7.74 KB
/
SGBAudioSettings.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yukar.Engine;
using UnityEngine.Audio;
namespace BobboNet.SGB.IMod
{
public static class SGBAudioSettings
{
private const int MinVolume = 0;
private const int MaxVolume = 100;
private const int DefaultVolume = 50;
private static int volumeBGM = DefaultVolume; // The current background music volume (0 - 100)
private static int volumeSFX = DefaultVolume; // The current sound effect volume (0 - 100)
private static AudioMixerGroup mixerGroupBGM = null; // (optional) The current mixer group to route BGM through
private static AudioMixerGroup mixerGroupSFX = null; // (optional) The current mixer group to route SFX through
private static string mixerVolumeHandleNameBGM = null; // (optional) The name of the BGM's volume handle in it's mixer
private static string mixerVolumeHandleNameSFX = null; // (optional) The name of the SFX's volume handle in it's mixer
//
// Constructor
//
static SGBAudioSettings()
{
// Add debug buttons to overlay that allow BGM and SFX volume adjustment
IModOverlay.AddButtonAction("SGB BGM Volume +", () => SetVolumeBGMRaw(volumeBGM + 5));
IModOverlay.AddButtonAction("SGB BGM Volume -", () => SetVolumeBGMRaw(volumeBGM - 5));
IModOverlay.AddButtonAction("SGB SFX Volume +", () => SetVolumeSFXRaw(volumeSFX + 5));
IModOverlay.AddButtonAction("SGB SFX Volume -", () => SetVolumeSFXRaw(volumeSFX - 5));
}
//
// Mixer Methods
//
/// <summary>
/// Update what audio mixer group all SGB background music should be routed through.
/// </summary>
/// <param name="mixerGroup">The group to route all BGM through. If null, unroutes all BGM.</param>
/// <param name="volumeHandleName">The name of the mixer handle that controls this group's volume level. If null, volume level is ignored.</param>
public static void SetMixerGroupBGM(AudioMixerGroup mixerGroup, string volumeHandleName = null)
{
mixerGroupBGM = mixerGroup; // Update the internal mixer group...
mixerVolumeHandleNameBGM = volumeHandleName; // ...cache a reference to it's volume handle...
Audio.SetMixerGroupBGM(mixerGroup); // ...and apply it to SGB
Audio.SetMixerGroupBGS(mixerGroup); // ...and apply it to SGB
}
/// <summary>
/// Get the active audio mixer group that all SGB background music is being routed through.
/// </summary>
/// <returns>The current BGM mixer group. Is null if unrouted.</returns>
public static AudioMixerGroup GetMixerGroupBGM() => mixerGroupBGM;
/// <summary>
/// Update what audio mixer group all SGB sound effects should be routed through.
/// </summary>
/// <param name="mixerGroup">The group to route all SFX through. If null, unroutes all SFX.</param>
/// <param name="volumeHandleName">The name of the mixer handle that controls this group's volume level. If null, volume level is ignored.</param>
public static void SetMixerGroupSFX(AudioMixerGroup mixerGroup, string volumeHandleName = null)
{
mixerGroupSFX = mixerGroup; // Update the internal mixer group...
mixerVolumeHandleNameSFX = volumeHandleName; // ...cache a reference to it's volume handle...
}
/// <summary>
/// Get the active audio mixer group that all SGB sound effects are being routed through.
/// </summary>
/// <returns>The current SFX mixer group. Is null if unrouted.</returns>
public static AudioMixerGroup GetMixerGroupSFX() => mixerGroupSFX;
//
// Volume Methods
//
/// <summary>
/// Set the RAW volume for SGB's background music.
/// </summary>
/// <param name="volume">Volume as an integer, from 0 - 100 (inclusive).</param>
public static void SetVolumeBGMRaw(int volume)
{
volumeBGM = Mathf.Clamp(volume, MinVolume, MaxVolume);
SetMixerVolumeIfNecessary(mixerGroupBGM, mixerVolumeHandleNameBGM, volumeBGM);
UpdateVolumeState();
}
/// <summary>
/// Set the volume for SGB's background music.
/// </summary>
/// <param name="volume">Volume as a float, from 0.0 - 1.0 (inclusive).</param>
public static void SetVolumeBGM(float volume) => SetVolumeBGMRaw(ConvertVolumeToInt(volume));
/// <summary>
/// Set the volume for SGB's backround music using decibels.
/// </summary>
/// <param name="volumeDb">Volume as decibels.</param>
public static void SetVolumeBGMDecibels(float volumeDb) => SetVolumeBGMRaw(ConvertDecibelsToInt(volumeDb));
/// <summary>
/// Get the volume of SGB's background music.
/// </summary>
/// <returns>Volume as a float, from 0.0 - 1.0 (inclusive).</returns>
public static float GetVolumeBGM() => ConvertVolumeToFloat(volumeBGM);
/// <summary>
/// Get the RAW volume of SGB's background music.
/// </summary>
/// <returns>Volume as an integer, from 0 - 100 (inclusive).</returns>
public static int GetVolumeBGMRaw() => volumeBGM;
/// <summary>
/// Set the RAW volume for SGB's sound effects.
/// </summary>
/// <param name="volume">Volume as an integer, from 0 - 100 (inclusive).</param>
public static void SetVolumeSFXRaw(int volume)
{
volumeSFX = Mathf.Clamp(volume, MinVolume, MaxVolume);
SetMixerVolumeIfNecessary(mixerGroupSFX, mixerVolumeHandleNameSFX, volumeSFX);
UpdateVolumeState();
}
/// <summary>
/// Set the volume for SGB's sound effects.
/// </summary>
/// <param name="volume">Volume as a float, from 0.0 - 1.0 (inclusive).</param>
///
public static void SetVolumeSFX(float volume) => SetVolumeSFXRaw(ConvertVolumeToInt(volume));
/// <summary>
/// Set the volume for SGB's sound effects using decibels.
/// </summary>
/// <param name="volumeDb">Volume as decibels.</param>
public static void SetVolumeSFXDecibels(float volumeDb) => SetVolumeSFXRaw(ConvertDecibelsToInt(volumeDb));
/// <summary>
/// Get the volume of SGB's sound effects.
/// </summary>
/// <returns>Volume as a float, from 0.0 - 1.0 (inclusive).</returns>
public static float GetVolumeSFX() => ConvertVolumeToFloat(volumeSFX);
/// <summary>
/// Get the RAW volume of SGB's sound effects.
/// </summary>
/// <returns>Volume as an integer, from 0 - 100 (inclusive).</returns>
public static int GetVolumeSFXRaw() => volumeSFX;
//
// Private Methods
//
private static void UpdateVolumeState() => Audio.updateVolume(); // Update the volume in SGB
private static int ConvertVolumeToInt(float volumeAsFloat) => Mathf.Clamp(Mathf.RoundToInt(volumeAsFloat * 100), MinVolume, MaxVolume);
private static float ConvertVolumeToFloat(int volumeAsInt) => Mathf.Clamp(volumeAsInt / 100.0f, MinVolume, MaxVolume);
private static int ConvertDecibelsToInt(float volumeAsDb) => ConvertVolumeToInt(VolumeUtils.DbToVolumeFloat(volumeAsDb));
private static void SetMixerVolumeIfNecessary(AudioMixerGroup mixerGroup, string handleName, int rawVolume)
{
if (mixerGroup == null || handleName == null) return;
mixerGroup.audioMixer.SetFloat(handleName, VolumeUtils.VolumeFloatToDb(ConvertVolumeToFloat(rawVolume)));
}
}
}