Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of MoreCompanyVoiceFix v1.0.0
MoreCompanyVoiceFix.dll
Decompiled 31 minutes agousing System; using System.Reflection; using System.Runtime.CompilerServices; using BepInEx; using BepInEx.Logging; using GameNetcodeStuff; using HarmonyLib; using UnityEngine; using UnityEngine.Audio; [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: AssemblyVersion("0.0.0.0")] [BepInPlugin("flamsey.morecompany.voicefix", "MoreCompany Voice Effects Fix", "1.0.0")] [BepInDependency(/*Could not decode attribute arguments.*/)] public class MoreCompanyVoiceFix : BaseUnityPlugin { internal static ManualLogSource Log; private void Awake() { //IL_0010: Unknown result type (might be due to invalid IL or missing references) //IL_0016: Expected O, but got Unknown Log = ((BaseUnityPlugin)this).Logger; GameObject val = new GameObject("MoreCompanyVoiceFixRunner"); Object.DontDestroyOnLoad((Object)(object)val); ((Object)val).hideFlags = (HideFlags)61; val.AddComponent<VoiceFixRunner>(); Log.LogInfo((object)"MoreCompany Voice Effects Fix loaded."); } } public class VoiceFixRunner : MonoBehaviour { private const int VanillaVoiceChannels = 4; private static readonly FieldInfo pitchOffsetsField = AccessTools.Field(typeof(SoundManager), "pitchOffsets"); private AudioMixer cachedMixer; private AudioMixerGroup master; private AudioMixerGroup[] vanillaGroups; private bool loggedSetup; private void LateUpdate() { try { Tick(); } catch (Exception ex) { MoreCompanyVoiceFix.Log.LogError((object)ex); ((Behaviour)this).enabled = false; } } private void Tick() { SoundManager instance = SoundManager.Instance; StartOfRound instance2 = StartOfRound.Instance; if ((Object)(object)instance == (Object)null || (Object)(object)instance2 == (Object)null || (Object)(object)instance.diageticMixer == (Object)null || instance2.allPlayerScripts == null || instance2.allPlayerScripts.Length <= 4) { return; } if ((Object)(object)cachedMixer != (Object)(object)instance.diageticMixer) { cachedMixer = instance.diageticMixer; AudioMixerGroup[] array = cachedMixer.FindMatchingGroups("Master"); master = ((array == null || array.Length <= 0) ? null : array[0]); vanillaGroups = (AudioMixerGroup[])(object)new AudioMixerGroup[4]; for (int i = 0; i < 4; i++) { AudioMixerGroup[] array2 = cachedMixer.FindMatchingGroups("Master/VoicePlayer" + i); vanillaGroups[i] = ((array2 == null || array2.Length <= 0) ? null : array2[0]); } loggedSetup = false; } if ((Object)(object)master == (Object)null) { return; } AudioMixerGroup[] playerVoiceMixers = instance.playerVoiceMixers; if (playerVoiceMixers != null) { for (int j = 4; j < playerVoiceMixers.Length; j++) { if ((Object)(object)playerVoiceMixers[j] != (Object)(object)master) { playerVoiceMixers[j] = master; } } } bool flag = true; if (instance.mixerSnapshots != null && instance.currentMixerSnapshotID >= 0 && instance.currentMixerSnapshotID < instance.mixerSnapshots.Length && (Object)(object)instance.mixerSnapshots[instance.currentMixerSnapshotID] != (Object)null) { flag = ((Object)instance.mixerSnapshots[instance.currentMixerSnapshotID]).name == "Default"; } float[] array3 = ((!(pitchOffsetsField != null)) ? null : (pitchOffsetsField.GetValue(instance) as float[])); PlayerControllerB val = ((!((Object)(object)GameNetworkManager.Instance != (Object)null)) ? null : GameNetworkManager.Instance.localPlayerController); for (int k = 4; k < instance2.allPlayerScripts.Length; k++) { PlayerControllerB val2 = instance2.allPlayerScripts[k]; if ((Object)(object)val2 == (Object)null || (Object)(object)val2 == (Object)(object)val) { continue; } AudioSource currentVoiceChatAudioSource = val2.currentVoiceChatAudioSource; if (!((Object)(object)currentVoiceChatAudioSource == (Object)null)) { if ((Object)(object)currentVoiceChatAudioSource.outputAudioMixerGroup == (Object)null || IsVanillaVoiceGroup(currentVoiceChatAudioSource.outputAudioMixerGroup)) { currentVoiceChatAudioSource.outputAudioMixerGroup = master; } ExtraPlayerVoiceDSP extraPlayerVoiceDSP = ((Component)currentVoiceChatAudioSource).GetComponent<ExtraPlayerVoiceDSP>(); if ((Object)(object)extraPlayerVoiceDSP == (Object)null) { extraPlayerVoiceDSP = ((Component)currentVoiceChatAudioSource).gameObject.AddComponent<ExtraPlayerVoiceDSP>(); } float num = ((instance.playerVoicePitches == null || k >= instance.playerVoicePitches.Length) ? 1f : instance.playerVoicePitches[k]); if (array3 != null && k < array3.Length && array3[k] > 0f) { num += array3[k] * Mathf.Abs((float)val2.health / 100f - 1f); } float num2 = ((instance.playerVoiceVolumes == null || k >= instance.playerVoiceVolumes.Length) ? 0f : (16f * instance.playerVoiceVolumes[k])); if (val2.isPlayerDead || !val2.isPlayerControlled) { num = 1f; } extraPlayerVoiceDSP.targetPitch = Mathf.Clamp(num, 0.5f, 2f); extraPlayerVoiceDSP.overlap = ((!flag) ? 4 : 6); extraPlayerVoiceDSP.compThresholdDb = ((!flag) ? (-0.8f) : (-11.7f)); extraPlayerVoiceDSP.targetGain = Mathf.Pow(10f, Mathf.Clamp(num2, -80f, 20f) / 20f); } } if (!loggedSetup) { loggedSetup = true; MoreCompanyVoiceFix.Log.LogInfo((object)("Voice effects enabled for " + (instance2.allPlayerScripts.Length - 4) + " extra player slots.")); } } private bool IsVanillaVoiceGroup(AudioMixerGroup g) { if (vanillaGroups == null) { return false; } for (int i = 0; i < vanillaGroups.Length; i++) { if ((Object)(object)vanillaGroups[i] == (Object)(object)g) { return true; } } return false; } } public class ExtraPlayerVoiceDSP : MonoBehaviour { public volatile float targetPitch = 1f; public volatile float targetGain = 1f; public volatile float compThresholdDb = -11.7f; public volatile int overlap = 6; private const float CompAttackMs = 17.4f; private const float CompReleaseMs = 35f; private const float CompMakeupDb = 0f; private PhaseVocoderPitchShifter[] shifters; private VoiceCompressor[] comps; private float sampleRate; private float curGain = 1f; private int silentSamples; private void Awake() { sampleRate = ((AudioSettings.outputSampleRate <= 0) ? 48000 : AudioSettings.outputSampleRate); } private void OnAudioFilterRead(float[] data, int channels) { if (channels <= 0) { return; } int num = overlap; if (shifters == null || shifters.Length != channels) { shifters = new PhaseVocoderPitchShifter[channels]; comps = new VoiceCompressor[channels]; for (int i = 0; i < channels; i++) { shifters[i] = new PhaseVocoderPitchShifter(1024, num, sampleRate); comps[i] = new VoiceCompressor(sampleRate); } } for (int j = 0; j < channels; j++) { if (shifters[j].Overlap != num) { shifters[j].SetOverlap(num); } } float pitch = targetPitch; float num2 = targetGain; float thresholdDb = compThresholdDb; int num3 = data.Length / channels; bool flag = true; for (int k = 0; k < data.Length; k++) { if (data[k] > 1E-05f || data[k] < -1E-05f) { flag = false; break; } } if (flag) { silentSamples += num3; if (silentSamples > 4096) { curGain = num2; return; } } else { silentSamples = 0; } for (int l = 0; l < num3; l++) { curGain += (num2 - curGain) * 0.001f; for (int m = 0; m < channels; m++) { int num4 = l * channels + m; float x = data[num4] * curGain; x = comps[m].Process(x, thresholdDb, 17.4f, 35f, 0f); data[num4] = shifters[m].Process(x, pitch); } } } } public sealed class PhaseVocoderPitchShifter { private readonly int N; private readonly int half; private int osamp; private int hop; private int latency; private int rover; private readonly float sampleRate; private readonly float[] inFifo; private readonly float[] outFifo; private readonly float[] accum; private readonly float[] window; private readonly float[] lastPhase; private readonly float[] sumPhase; private readonly float[] anaMag; private readonly float[] anaFreq; private readonly float[] synMag; private readonly float[] synFreq; private readonly float[] re; private readonly float[] im; private readonly float[] cosT; private readonly float[] sinT; private readonly int[] rev; public int Overlap { [CompilerGenerated] get { return osamp; } } public PhaseVocoderPitchShifter(int fftSize, int overlap, float sampleRate) { N = fftSize; half = N / 2; this.sampleRate = sampleRate; inFifo = new float[N]; outFifo = new float[N]; accum = new float[2 * N]; window = new float[N]; lastPhase = new float[half + 1]; sumPhase = new float[half + 1]; anaMag = new float[half + 1]; anaFreq = new float[half + 1]; synMag = new float[half + 1]; synFreq = new float[half + 1]; re = new float[N]; im = new float[N]; for (int i = 0; i < N; i++) { window[i] = 0.5f - 0.5f * (float)Math.Cos(Math.PI * 2.0 * (double)i / (double)N); } cosT = new float[half]; sinT = new float[half]; for (int j = 0; j < half; j++) { cosT[j] = (float)Math.Cos(Math.PI * 2.0 * (double)j / (double)N); sinT[j] = (float)Math.Sin(Math.PI * 2.0 * (double)j / (double)N); } rev = new int[N]; int k; for (k = 0; 1 << k < N; k++) { } for (int l = 0; l < N; l++) { int num = 0; for (int m = 0; m < k; m++) { if ((l & (1 << m)) != 0) { num |= 1 << k - 1 - m; } } rev[l] = num; } SetOverlap(overlap); } public void SetOverlap(int overlap) { if (overlap < 1) { overlap = 1; } if (overlap != osamp) { osamp = overlap; hop = N / osamp; latency = N - hop; rover = latency; Array.Clear(inFifo, 0, N); Array.Clear(outFifo, 0, N); Array.Clear(accum, 0, accum.Length); Array.Clear(lastPhase, 0, lastPhase.Length); Array.Clear(sumPhase, 0, sumPhase.Length); } } public float Process(float x, float pitch) { inFifo[rover] = x; float result = outFifo[rover - latency]; rover++; if (rover >= N) { rover = latency; Frame(pitch); } return result; } private void Frame(float pitch) { double num = sampleRate / (float)N; double num2 = Math.PI * 2.0 * (double)hop / (double)N; for (int i = 0; i < N; i++) { re[i] = inFifo[i] * window[i]; im[i] = 0f; } Fft(inverse: false); for (int j = 0; j <= half; j++) { double num3 = re[j]; double num4 = im[j]; double num5 = 2.0 * Math.Sqrt(num3 * num3 + num4 * num4); double num6 = Math.Atan2(num4, num3); double num7 = num6 - (double)lastPhase[j]; lastPhase[j] = (float)num6; num7 -= (double)j * num2; num7 -= Math.PI * 2.0 * Math.Round(num7 / (Math.PI * 2.0)); num7 = (double)osamp * num7 / (Math.PI * 2.0); anaMag[j] = (float)num5; anaFreq[j] = (float)(((double)j + num7) * num); } Array.Clear(synMag, 0, synMag.Length); Array.Clear(synFreq, 0, synFreq.Length); for (int k = 0; k <= half; k++) { int num8 = (int)((float)k * pitch); if (num8 <= half) { synMag[num8] += anaMag[k]; synFreq[num8] = anaFreq[k] * pitch; } } for (int l = 0; l <= half; l++) { double num9 = (double)synFreq[l] / num - (double)l; num9 = Math.PI * 2.0 * num9 / (double)osamp + (double)l * num2; double num10 = (double)sumPhase[l] + num9; num10 -= Math.PI * 2.0 * Math.Floor(num10 / (Math.PI * 2.0)); sumPhase[l] = (float)num10; re[l] = (float)((double)synMag[l] * Math.Cos(num10)); im[l] = (float)((double)synMag[l] * Math.Sin(num10)); } for (int m = half + 1; m < N; m++) { re[m] = 0f; im[m] = 0f; } Fft(inverse: true); float num11 = 4f / (3f * (float)half * (float)osamp); for (int n = 0; n < N; n++) { accum[n] += window[n] * re[n] * num11; } Array.Copy(accum, 0, outFifo, 0, hop); Array.Copy(accum, hop, accum, 0, N); Array.Clear(accum, N, hop); Array.Copy(inFifo, hop, inFifo, 0, latency); } private void Fft(bool inverse) { for (int i = 0; i < N; i++) { int num = rev[i]; if (num > i) { float num2 = re[i]; re[i] = re[num]; re[num] = num2; num2 = im[i]; im[i] = im[num]; im[num] = num2; } } for (int num3 = 2; num3 <= N; num3 <<= 1) { int num4 = num3 >> 1; int num5 = N / num3; for (int j = 0; j < N; j += num3) { for (int k = 0; k < num4; k++) { float num6 = cosT[k * num5]; float num7 = ((!inverse) ? (0f - sinT[k * num5]) : sinT[k * num5]); int num8 = j + k; int num9 = num8 + num4; float num10 = re[num9] * num6 - im[num9] * num7; float num11 = re[num9] * num7 + im[num9] * num6; re[num9] = re[num8] - num10; im[num9] = im[num8] - num11; re[num8] += num10; im[num8] += num11; } } } } } public sealed class VoiceCompressor { private float env; private float gainDb; private readonly float sr; public VoiceCompressor(float sampleRate) { sr = sampleRate; } public float Process(float x, float thresholdDb, float attackMs, float releaseMs, float makeupDb) { float num = (float)Math.Exp(-1.0 / ((double)(sr * attackMs) * 0.001)); float num2 = (float)Math.Exp(-1.0 / ((double)(sr * releaseMs) * 0.001)); float num3 = Math.Abs(x); env = ((!(num3 > env)) ? (num2 * env + (1f - num2) * num3) : (num * env + (1f - num) * num3)); float num4 = ((!(env > 1E-06f)) ? (-120f) : (20f * (float)Math.Log10(env))); float num5 = num4 - thresholdDb; float num6 = ((!(num5 > 0f)) ? 0f : (0f - num5)); gainDb = num6; return x * (float)Math.Pow(10.0, (double)(gainDb + makeupDb) / 20.0); } }