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.

91 line
1.8KB

  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 TechbloxModdingAPI.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.getParameterByName(key, out float val, out float finalVal);
  41. return val;
  42. }
  43. set => sound.setParameterByName(key, value);
  44. }
  45. public float this[PARAMETER_ID index]
  46. {
  47. get
  48. {
  49. sound.getParameterByID(index, out float val, out float finalVal);
  50. return val;
  51. }
  52. set => sound.setParameterByID(index, value);
  53. }
  54. public float3 Position
  55. {
  56. get
  57. {
  58. sound.get3DAttributes(out FMOD.ATTRIBUTES_3D attr);
  59. return new float3(attr.position.x, attr.position.y, attr.position.z);
  60. }
  61. set => sound.set3DAttributes(RuntimeUtils.To3DAttributes(value));
  62. }
  63. public void Play()
  64. {
  65. sound.start();
  66. }
  67. public void Stop(FMOD.Studio.STOP_MODE mode = FMOD.Studio.STOP_MODE.IMMEDIATE)
  68. {
  69. sound.stop(mode);
  70. }
  71. ~Audio() // Use the wannabe C++ destructor to destroy the C++ object
  72. {
  73. sound.release();
  74. }
  75. }
  76. }