TechbloxModdingAPI/GamecraftModdingAPI/Utility/Audio.cs
NorbiPeti a7f6a16231 Update to Gamecraft 2020.12.16.14.19 and custom block stuff
- Fixed the crash on second time start
- Tweaked more stuff about the block

Breaking changes coming from FMOD 2.0:
- Audio[int index] changed to Audio[PARAMETER_ID index]
- Audio.Parameters removed
2020-12-17 02:34:36 +01:00

90 lines
1.8 KiB
C#

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);
return val;
}
set => sound.setParameterByName(key, value);
}
public float this[PARAMETER_ID index]
{
get
{
sound.getParameterByID(index, out float val, out float finalVal);
return val;
}
set => sound.setParameterByID(index, value);
}
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();
}
}
}