A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.2KB

  1. using System;
  2. using System.Reflection;
  3. using FMOD.Studio;
  4. using FMODUnity;
  5. using RobocraftX.Common.Audio;
  6. using Unity.Mathematics;
  7. namespace GamecraftModdingAPI.Utility
  8. {
  9. public class Audio
  10. {
  11. private EventInstance sound;
  12. public Audio(string uri) : this(RuntimeManager.PathToGUID(uri))
  13. {
  14. }
  15. public Audio(Guid uri)
  16. {
  17. sound = RuntimeManager.CreateInstance(uri);
  18. }
  19. public static Audio Random()
  20. {
  21. System.Random potato = new System.Random();
  22. FieldInfo[] options = typeof(FMODAudioEvents).GetFields();
  23. Guid audio_uri;
  24. while (true)
  25. {
  26. int index = potato.Next(0, options.Length);
  27. try
  28. {
  29. audio_uri = (Guid)options[index].GetValue(null);
  30. break;
  31. }
  32. catch (InvalidCastException) { }
  33. }
  34. return new Audio(audio_uri);
  35. }
  36. public float this[string key]
  37. {
  38. get
  39. {
  40. sound.getParameterValue(key, out float val, out float finalVal);
  41. return val;
  42. }
  43. set => sound.setParameterValue(key, value);
  44. }
  45. public float this[int index]
  46. {
  47. get
  48. {
  49. sound.getParameterValueByIndex(index, out float val, out float finalVal);
  50. return val;
  51. }
  52. set => sound.setParameterValueByIndex(index, value);
  53. }
  54. public string[] Parameters
  55. {
  56. get
  57. {
  58. sound.getParameterCount(out int count);
  59. string[] parameters = new string[count];
  60. for (int i = 0; i < count; i++)
  61. {
  62. sound.getParameterByIndex(i, out ParameterInstance param);
  63. param.getDescription(out PARAMETER_DESCRIPTION desc);
  64. parameters[i] = desc.name;
  65. }
  66. return parameters;
  67. }
  68. }
  69. public float3 Position
  70. {
  71. get
  72. {
  73. sound.get3DAttributes(out FMOD.ATTRIBUTES_3D attr);
  74. return new float3(attr.position.x, attr.position.y, attr.position.z);
  75. }
  76. set => sound.set3DAttributes(RuntimeUtils.To3DAttributes(value));
  77. }
  78. public void Play()
  79. {
  80. sound.start();
  81. }
  82. public void Stop(FMOD.Studio.STOP_MODE mode = FMOD.Studio.STOP_MODE.IMMEDIATE)
  83. {
  84. sound.stop(mode);
  85. }
  86. ~Audio() // Use the wannabe C++ destructor to destroy the C++ object
  87. {
  88. sound.release();
  89. }
  90. }
  91. }