TechbloxModdingAPI/GamecraftModdingAPI/Utility/Audio.cs

91 lines
1.8 KiB
C#
Raw Normal View History

2020-05-30 01:30:53 +00:00
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.getParameterByName(key, out float val, out float finalVal);
2020-05-30 01:30:53 +00:00
return val;
}
set => sound.setParameterByName(key, value);
2020-05-30 01:30:53 +00:00
}
public float this[PARAMETER_ID index]
2020-05-30 01:30:53 +00:00
{
get
{
sound.getParameterByID(index, out float val, out float finalVal);
2020-05-30 01:30:53 +00:00
return val;
}
set => sound.setParameterByID(index, value);
2020-05-30 01:30:53 +00:00
}
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();
}
}
}