From 98c7e624f82d6d9fae97b5dcd7313d298278bacb Mon Sep 17 00:00:00 2001 From: "NGnius (Graham)" Date: Fri, 29 May 2020 21:30:53 -0400 Subject: [PATCH] Add OOP Audio class --- GamecraftModdingAPI/Utility/Audio.cs | 106 +++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 GamecraftModdingAPI/Utility/Audio.cs diff --git a/GamecraftModdingAPI/Utility/Audio.cs b/GamecraftModdingAPI/Utility/Audio.cs new file mode 100644 index 0000000..9d227a8 --- /dev/null +++ b/GamecraftModdingAPI/Utility/Audio.cs @@ -0,0 +1,106 @@ +using System; +using System.Reflection; + +using FMOD.Studio; +using FMODUnity; +using RobocraftX.Common.Audio; +using Unity.Mathematics; + +namespace GamecraftModdingAPI.Utility +{ + public class Audio + { + private EventInstance sound; + + public Audio(string uri) : this(RuntimeManager.PathToGUID(uri)) + { + } + + public Audio(Guid uri) + { + sound = RuntimeManager.CreateInstance(uri); + } + + public static Audio Random() + { + System.Random potato = new System.Random(); + FieldInfo[] options = typeof(FMODAudioEvents).GetFields(); + Guid audio_uri; + while (true) + { + int index = potato.Next(0, options.Length); + try + { + audio_uri = (Guid)options[index].GetValue(null); + break; + } + catch (InvalidCastException) { } + } + return new Audio(audio_uri); + } + + public float this[string key] + { + get + { + sound.getParameterValue(key, out float val, out float finalVal); + return val; + } + + set => sound.setParameterValue(key, value); + } + + public float this[int index] + { + get + { + sound.getParameterValueByIndex(index, out float val, out float finalVal); + return val; + } + + set => sound.setParameterValueByIndex(index, value); + } + + public string[] Parameters + { + get + { + sound.getParameterCount(out int count); + string[] parameters = new string[count]; + for (int i = 0; i < count; i++) + { + sound.getParameterByIndex(i, out ParameterInstance param); + param.getDescription(out PARAMETER_DESCRIPTION desc); + parameters[i] = desc.name; + } + return parameters; + } + } + + public float3 Position + { + get + { + sound.get3DAttributes(out FMOD.ATTRIBUTES_3D attr); + return new float3(attr.position.x, attr.position.y, attr.position.z); + } + + set => sound.set3DAttributes(RuntimeUtils.To3DAttributes(value)); + } + + public void Play() + { + sound.start(); + } + + public void Stop(FMOD.Studio.STOP_MODE mode = FMOD.Studio.STOP_MODE.IMMEDIATE) + { + sound.stop(mode); + } + + ~Audio() // Use the wannabe C++ destructor to destroy the C++ object + { + sound.release(); + } + } +}