using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("MicrophoneNoiseMeters")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MicrophoneNoiseMeters")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("144e7fb8-77ca-4372-9282-50e4fd907238")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace MicrophoneNoiseMeters
{
public static class MyPluginInfo
{
public const string PLUGIN_GUID = "MicrophoneNoiseMeters";
public const string PLUGIN_NAME = "MicrophoneNoiseMeters";
public const string PLUGIN_VERSION = "1.0.0";
}
[BepInPlugin("MicrophoneNoiseMeters", "MicrophoneNoiseMeters", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
public static AudioClip Clip;
public static string MicrophoneDevice;
public static int _sampleWindow = 128;
public static ConfigEntry<float> LoudnessMultiplier;
public static ConfigEntry<float> MicrophoneThreshold;
public static ConfigEntry<int> MicrophoneIndex;
private void Awake()
{
((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin MicrophoneNoiseMeters is loaded!");
Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), (string)null);
LoudnessMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("MicrophoneNoiseMeters", "LoudnessMultiplier", 75f, "Floating-point Multiplier value for microphone output. Default value 75.0");
MicrophoneThreshold = ((BaseUnityPlugin)this).Config.Bind<float>("MicrophoneNoiseMeters", "MicrophoneThreshold", 0.5f, "Floating-point Threshold value for microphone output. Values must be larger than this to affect NoiseMeter. Default value 0.3");
MicrophoneIndex = ((BaseUnityPlugin)this).Config.Bind<int>("MicrophoneNoiseMeters", "MicrophoneIndex", 0, "Microphone device index to use. 0 is default audio input.");
}
public static float GetLoudnessFromMicrophone()
{
return GetLoudnessFromAudioClip(Clip, Microphone.GetPosition(MicrophoneDevice));
}
private static float GetLoudnessFromAudioClip(AudioClip clip, int clipPosition)
{
int num = clipPosition - _sampleWindow;
if (num < 0)
{
return 0f;
}
float[] array = new float[_sampleWindow];
clip.GetData(array, num);
float num2 = 0f;
float[] array2 = array;
foreach (float num3 in array2)
{
num2 += Mathf.Abs(num3);
}
return num2 / (float)_sampleWindow;
}
}
}
namespace MicrophoneNoiseMeters.Patches
{
[HarmonyPatch(typeof(NoiseSystem), "Start")]
internal class StartPatch
{
private static void Postfix(NoiseSystem __instance)
{
if (Microphone.devices.Length >= Plugin.MicrophoneIndex.Value + 1)
{
Plugin.MicrophoneDevice = Microphone.devices[Plugin.MicrophoneIndex.Value].ToString();
Plugin.Clip = Microphone.Start(Plugin.MicrophoneDevice, true, 20, AudioSettings.outputSampleRate);
}
}
}
[HarmonyPatch(typeof(NoiseSystem), "Update")]
internal class UpdatePatch
{
private static void Postfix(NoiseSystem __instance)
{
if (!((Object)(object)Plugin.Clip == (Object)null))
{
float num = Plugin.GetLoudnessFromMicrophone() * Plugin.LoudnessMultiplier.Value;
if (!(num < Plugin.MicrophoneThreshold.Value))
{
__instance.AddNoise(num);
}
}
}
}
}