Advertisement
TorutheRedFox

FMOD Ex Unity script

Jan 2nd, 2022
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.97 KB | None | 0 0
  1. using FMOD;
  2. using UnityEngine;
  3.  
  4. public class fmodlowleveltest : MonoBehaviour
  5. {
  6.  
  7.     [Range(1, 6)] public int stereoPairs = 6;
  8.  
  9.     [Range(0.0f, 1.0f)] public float baseVolume = 0.75f;
  10.  
  11.     [Range(0.0f, 1.0f)] public float channel1Volume = 1.0f;
  12.     [Range(0.0f, 1.0f)] public float channel2Volume;
  13.     [Range(0.0f, 1.0f)] public float channel3Volume;
  14.     [Range(0.0f, 1.0f)] public float channel4Volume;
  15.     [Range(0.0f, 1.0f)] public float channel5Volume;
  16.     [Range(0.0f, 1.0f)] public float channel6Volume;
  17.  
  18.     private FMOD.System SystemEX;
  19.     private Sound baseSound;
  20.     private Sound baseSound02;
  21.     private DSP dsp01;
  22.     private Sound sound01;
  23.     private Channel channel01;
  24.     private DSP dsp02;
  25.     private Sound sound02;
  26.     private Channel channel02;
  27.     private ChannelGroup channelGroup;
  28.     bool hasInitializedMainSound = false;
  29.     bool hasInitializedSubSounds = false;
  30.     bool hasInitializedSound2 = false;
  31.     public string bank = "i_pod.fsb";
  32.     public uint loopStart = 0;
  33.     public bool reloadSound = false;
  34.     private bool hasLoadedAtLeastOnce;
  35.  
  36.     public static bool ERRCHECK(FMOD.RESULT result)
  37.     {
  38.         if (result != FMOD.RESULT.OK)
  39.         {
  40.             Debug.LogError("FMOD Error (" + result.ToString() + "): " + FMOD.Error.String(result));
  41.         }
  42.        
  43.         return (result == FMOD.RESULT.OK);
  44.     }
  45.  
  46.     // Start is called before the first frame update
  47.     void LoadTrack()
  48.     {
  49.         RESULT result;
  50.  
  51.         FMODUnity.RuntimeManager.StudioSystem.getCoreSystem(out SystemEX);
  52.  
  53.         Debug.Log("set up parameters");
  54.  
  55.         result = SystemEX.createStream(Application.streamingAssetsPath + "/" + bank, MODE.LOOP_NORMAL | MODE._2D | MODE.CREATESTREAM | MODE.NONBLOCKING, out baseSound);
  56.         ERRCHECK(result);
  57.         if (stereoPairs > 3)
  58.         {
  59.             result = SystemEX.createStream(Application.streamingAssetsPath + "/" + bank, MODE.LOOP_NORMAL | MODE._2D | MODE.CREATESTREAM | MODE.NONBLOCKING, out baseSound02);
  60.             ERRCHECK(result);
  61.         }
  62.     }
  63.  
  64.     void setVolumes(float track1, float track2, float track3, float track4, float track5, float track6)
  65.     {
  66.         float[] vol = new float[6] { track1 * baseVolume, track2 * baseVolume, track3 * baseVolume,
  67.         track4 * baseVolume, track5 * baseVolume, track6 * baseVolume};
  68.  
  69.         //float[] matrix01 = { vol1, 0, vol2, 0, vol3, 0, 0, vol1, 0, vol2, 0, vol3 };
  70.         float[] matrix01_1 = new float[System.Math.Min(stereoPairs, 3) * 2];
  71.         float[] matrix01_2 = new float[System.Math.Min(stereoPairs, 3) * 2];
  72.         for (int i=0; i < System.Math.Min(stereoPairs, 3); i++)
  73.         {
  74.             matrix01_1[i * 2] = vol[i];
  75.             matrix01_2[i * 2] = 0;
  76.             matrix01_1[(i * 2)+1] = 0;
  77.             matrix01_2[(i * 2) + 1] = vol[i];
  78.         }
  79.         float[] matrix01 = new float[matrix01_1.Length + matrix01_2.Length];
  80.         matrix01_1.CopyTo(matrix01, 0);
  81.         matrix01_2.CopyTo(matrix01, matrix01_1.Length);
  82.        
  83.         channel01.setMixMatrix(matrix01, 2, 6, System.Math.Min(stereoPairs, 3) * 2);
  84.         if (stereoPairs > 3)
  85.         {
  86.             float[] matrix02_1 = new float[(stereoPairs - 3) * 2];
  87.             float[] matrix02_2 = new float[(stereoPairs - 3) * 2];
  88.             for (int i = 0; i < (stereoPairs - 3); i++)
  89.             {
  90.                 matrix01_1[i * 2] = vol[i+3];
  91.                 matrix01_2[i * 2] = 0;
  92.                 matrix01_1[(i * 2) + 1] = 0;
  93.                 matrix01_2[(i * 2) + 1] = vol[i + 3];
  94.             }
  95.             float[] matrix02 = new float[matrix01_1.Length + matrix01_2.Length];
  96.             matrix02_1.CopyTo(matrix02, 0);
  97.             matrix02_2.CopyTo(matrix02, matrix02_1.Length);
  98.             channel02.setMixMatrix(matrix02, 2, 6, (stereoPairs - 3) * 2);
  99.         }
  100.     }
  101.  
  102.     // Update is called once per frame
  103.     void Update()
  104.     {
  105.         RESULT result;
  106.         if (hasLoadedAtLeastOnce)
  107.         {
  108.             OPENSTATE openstate;
  109.             result = baseSound.getOpenState(out openstate, out _, out _, out _);
  110.             ERRCHECK(result);
  111.  
  112.             if (openstate == FMOD.OPENSTATE.READY && !hasInitializedMainSound)
  113.             {
  114.                 hasInitializedMainSound = true;
  115.                 result = baseSound.getSubSound(0, out sound01);
  116.                 ERRCHECK(result);
  117.                 Debug.Log("get sound 1");
  118.             }
  119.  
  120.             OPENSTATE openstate01 = 0;
  121.             if (stereoPairs > 3)
  122.             {
  123.                 result = baseSound02.getOpenState(out openstate01, out _, out _, out _);
  124.                 ERRCHECK(result);
  125.             }
  126.  
  127.             if (openstate01 == FMOD.OPENSTATE.READY && !hasInitializedSound2 && stereoPairs > 3)
  128.             {
  129.                 hasInitializedSound2 = true;
  130.                 result = baseSound02.getSubSound(1, out sound02);
  131.                 ERRCHECK(result);
  132.                 Debug.Log("get sound 2");
  133.             }
  134.  
  135.             OPENSTATE openstate02 = 0;
  136.             if (stereoPairs > 3)
  137.             {
  138.                 result = sound02.getOpenState(out openstate02, out _, out _, out _);
  139.                 ERRCHECK(result);
  140.             }
  141.  
  142.             if (openstate01 == FMOD.OPENSTATE.READY && (openstate02 == FMOD.OPENSTATE.READY || stereoPairs <= 3) && !hasInitializedSubSounds)
  143.             {
  144.                 hasInitializedSubSounds = true;
  145.                 //baseSound.release();
  146.  
  147.                 ERRCHECK(result);
  148.                 result = FMODUnity.RuntimeManager.GetBus("bus:/master/music").getChannelGroup(out channelGroup);
  149.                 ERRCHECK(result);
  150.  
  151.                 Debug.Log("get channel group");
  152.  
  153.                 uint sound01loopend;
  154.                 sound01.getLoopPoints(out _, TIMEUNIT.MS, out sound01loopend, TIMEUNIT.PCM);
  155.                 sound01.setLoopPoints(loopStart, TIMEUNIT.MS, sound01loopend, TIMEUNIT.PCM);
  156.  
  157.                 if (stereoPairs > 3)
  158.                 {
  159.                     uint sound02loopend;
  160.                     sound02.getLoopPoints(out _, TIMEUNIT.MS, out sound02loopend, TIMEUNIT.PCM);
  161.                     sound02.setLoopPoints(loopStart, TIMEUNIT.MS, sound02loopend, TIMEUNIT.PCM);
  162.                     SystemEX.playSound(sound01, channelGroup, false, out channel01);
  163.                     SystemEX.playSound(sound02, channelGroup, false, out channel02);
  164.                 }
  165.                 else
  166.                 {
  167.                     SystemEX.playSound(sound01, channelGroup, false, out channel01);
  168.                 }
  169.                 setVolumes(channel1Volume, channel2Volume, channel3Volume, channel4Volume, channel5Volume, channel6Volume);
  170.                 string name01;
  171.                 sound01.getName(out name01, 256);
  172.                 if (stereoPairs > 3)
  173.                 {
  174.                     string name02;
  175.                     sound02.getName(out name02, 256);
  176.                     Debug.LogFormat("Now playing {0} and {1}", name01, name02);
  177.                 }
  178.                 else
  179.                 {
  180.                     Debug.LogFormat("Now playing {0}", name01);
  181.                 }
  182.             }
  183.         }
  184.         if (reloadSound)
  185.         {
  186.             reloadSound = false;
  187.             ReloadSound(true);
  188.         }
  189.         if (hasInitializedSubSounds)
  190.             setVolumes(channel1Volume, channel2Volume, channel3Volume, channel4Volume, channel5Volume, channel6Volume);
  191.     }
  192.  
  193.     void OnDisable()
  194.     {
  195.         ReloadSound(false);
  196.     }
  197.  
  198.     void ReloadSound(bool runAwake)
  199.     {
  200.         if (hasLoadedAtLeastOnce)
  201.         {
  202.             OnDestroy();
  203.             hasInitializedMainSound = false;
  204.             hasInitializedSound2 = false;
  205.             hasInitializedSubSounds = false;
  206.         }
  207.         else
  208.         {
  209.             hasLoadedAtLeastOnce = true;
  210.         }
  211.         if (runAwake)
  212.             LoadTrack();
  213.     }
  214.  
  215.     void OnDestroy()
  216.     {
  217.         dsp01.release();
  218.         dsp02.release();
  219.         dsp01.release();
  220.         dsp02.release();
  221.         sound01.release();
  222.         sound02.release();
  223.         baseSound.release();
  224.         baseSound02.release();
  225.     }
  226. }
  227.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement