Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using FMOD;
- using UnityEngine;
- public class fmodlowleveltest : MonoBehaviour
- {
- [Range(1, 6)] public int stereoPairs = 6;
- [Range(0.0f, 1.0f)] public float baseVolume = 0.75f;
- [Range(0.0f, 1.0f)] public float channel1Volume = 1.0f;
- [Range(0.0f, 1.0f)] public float channel2Volume;
- [Range(0.0f, 1.0f)] public float channel3Volume;
- [Range(0.0f, 1.0f)] public float channel4Volume;
- [Range(0.0f, 1.0f)] public float channel5Volume;
- [Range(0.0f, 1.0f)] public float channel6Volume;
- private FMOD.System SystemEX;
- private Sound baseSound;
- private Sound baseSound02;
- private DSP dsp01;
- private Sound sound01;
- private Channel channel01;
- private DSP dsp02;
- private Sound sound02;
- private Channel channel02;
- private ChannelGroup channelGroup;
- bool hasInitializedMainSound = false;
- bool hasInitializedSubSounds = false;
- bool hasInitializedSound2 = false;
- public string bank = "i_pod.fsb";
- public uint loopStart = 0;
- public bool reloadSound = false;
- private bool hasLoadedAtLeastOnce;
- public static bool ERRCHECK(FMOD.RESULT result)
- {
- if (result != FMOD.RESULT.OK)
- {
- Debug.LogError("FMOD Error (" + result.ToString() + "): " + FMOD.Error.String(result));
- }
- return (result == FMOD.RESULT.OK);
- }
- // Start is called before the first frame update
- void LoadTrack()
- {
- RESULT result;
- FMODUnity.RuntimeManager.StudioSystem.getCoreSystem(out SystemEX);
- Debug.Log("set up parameters");
- result = SystemEX.createStream(Application.streamingAssetsPath + "/" + bank, MODE.LOOP_NORMAL | MODE._2D | MODE.CREATESTREAM | MODE.NONBLOCKING, out baseSound);
- ERRCHECK(result);
- if (stereoPairs > 3)
- {
- result = SystemEX.createStream(Application.streamingAssetsPath + "/" + bank, MODE.LOOP_NORMAL | MODE._2D | MODE.CREATESTREAM | MODE.NONBLOCKING, out baseSound02);
- ERRCHECK(result);
- }
- }
- void setVolumes(float track1, float track2, float track3, float track4, float track5, float track6)
- {
- float[] vol = new float[6] { track1 * baseVolume, track2 * baseVolume, track3 * baseVolume,
- track4 * baseVolume, track5 * baseVolume, track6 * baseVolume};
- //float[] matrix01 = { vol1, 0, vol2, 0, vol3, 0, 0, vol1, 0, vol2, 0, vol3 };
- float[] matrix01_1 = new float[System.Math.Min(stereoPairs, 3) * 2];
- float[] matrix01_2 = new float[System.Math.Min(stereoPairs, 3) * 2];
- for (int i=0; i < System.Math.Min(stereoPairs, 3); i++)
- {
- matrix01_1[i * 2] = vol[i];
- matrix01_2[i * 2] = 0;
- matrix01_1[(i * 2)+1] = 0;
- matrix01_2[(i * 2) + 1] = vol[i];
- }
- float[] matrix01 = new float[matrix01_1.Length + matrix01_2.Length];
- matrix01_1.CopyTo(matrix01, 0);
- matrix01_2.CopyTo(matrix01, matrix01_1.Length);
- channel01.setMixMatrix(matrix01, 2, 6, System.Math.Min(stereoPairs, 3) * 2);
- if (stereoPairs > 3)
- {
- float[] matrix02_1 = new float[(stereoPairs - 3) * 2];
- float[] matrix02_2 = new float[(stereoPairs - 3) * 2];
- for (int i = 0; i < (stereoPairs - 3); i++)
- {
- matrix01_1[i * 2] = vol[i+3];
- matrix01_2[i * 2] = 0;
- matrix01_1[(i * 2) + 1] = 0;
- matrix01_2[(i * 2) + 1] = vol[i + 3];
- }
- float[] matrix02 = new float[matrix01_1.Length + matrix01_2.Length];
- matrix02_1.CopyTo(matrix02, 0);
- matrix02_2.CopyTo(matrix02, matrix02_1.Length);
- channel02.setMixMatrix(matrix02, 2, 6, (stereoPairs - 3) * 2);
- }
- }
- // Update is called once per frame
- void Update()
- {
- RESULT result;
- if (hasLoadedAtLeastOnce)
- {
- OPENSTATE openstate;
- result = baseSound.getOpenState(out openstate, out _, out _, out _);
- ERRCHECK(result);
- if (openstate == FMOD.OPENSTATE.READY && !hasInitializedMainSound)
- {
- hasInitializedMainSound = true;
- result = baseSound.getSubSound(0, out sound01);
- ERRCHECK(result);
- Debug.Log("get sound 1");
- }
- OPENSTATE openstate01 = 0;
- if (stereoPairs > 3)
- {
- result = baseSound02.getOpenState(out openstate01, out _, out _, out _);
- ERRCHECK(result);
- }
- if (openstate01 == FMOD.OPENSTATE.READY && !hasInitializedSound2 && stereoPairs > 3)
- {
- hasInitializedSound2 = true;
- result = baseSound02.getSubSound(1, out sound02);
- ERRCHECK(result);
- Debug.Log("get sound 2");
- }
- OPENSTATE openstate02 = 0;
- if (stereoPairs > 3)
- {
- result = sound02.getOpenState(out openstate02, out _, out _, out _);
- ERRCHECK(result);
- }
- if (openstate01 == FMOD.OPENSTATE.READY && (openstate02 == FMOD.OPENSTATE.READY || stereoPairs <= 3) && !hasInitializedSubSounds)
- {
- hasInitializedSubSounds = true;
- //baseSound.release();
- ERRCHECK(result);
- result = FMODUnity.RuntimeManager.GetBus("bus:/master/music").getChannelGroup(out channelGroup);
- ERRCHECK(result);
- Debug.Log("get channel group");
- uint sound01loopend;
- sound01.getLoopPoints(out _, TIMEUNIT.MS, out sound01loopend, TIMEUNIT.PCM);
- sound01.setLoopPoints(loopStart, TIMEUNIT.MS, sound01loopend, TIMEUNIT.PCM);
- if (stereoPairs > 3)
- {
- uint sound02loopend;
- sound02.getLoopPoints(out _, TIMEUNIT.MS, out sound02loopend, TIMEUNIT.PCM);
- sound02.setLoopPoints(loopStart, TIMEUNIT.MS, sound02loopend, TIMEUNIT.PCM);
- SystemEX.playSound(sound01, channelGroup, false, out channel01);
- SystemEX.playSound(sound02, channelGroup, false, out channel02);
- }
- else
- {
- SystemEX.playSound(sound01, channelGroup, false, out channel01);
- }
- setVolumes(channel1Volume, channel2Volume, channel3Volume, channel4Volume, channel5Volume, channel6Volume);
- string name01;
- sound01.getName(out name01, 256);
- if (stereoPairs > 3)
- {
- string name02;
- sound02.getName(out name02, 256);
- Debug.LogFormat("Now playing {0} and {1}", name01, name02);
- }
- else
- {
- Debug.LogFormat("Now playing {0}", name01);
- }
- }
- }
- if (reloadSound)
- {
- reloadSound = false;
- ReloadSound(true);
- }
- if (hasInitializedSubSounds)
- setVolumes(channel1Volume, channel2Volume, channel3Volume, channel4Volume, channel5Volume, channel6Volume);
- }
- void OnDisable()
- {
- ReloadSound(false);
- }
- void ReloadSound(bool runAwake)
- {
- if (hasLoadedAtLeastOnce)
- {
- OnDestroy();
- hasInitializedMainSound = false;
- hasInitializedSound2 = false;
- hasInitializedSubSounds = false;
- }
- else
- {
- hasLoadedAtLeastOnce = true;
- }
- if (runAwake)
- LoadTrack();
- }
- void OnDestroy()
- {
- dsp01.release();
- dsp02.release();
- dsp01.release();
- dsp02.release();
- sound01.release();
- sound02.release();
- baseSound.release();
- baseSound02.release();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement