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.
81 lines
2.5 KiB
81 lines
2.5 KiB
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class AudioManager : MonoBehaviour
|
|
{
|
|
private const int MaxChannels = 128; // Should match MAX_CHANNELS in sound.h
|
|
|
|
private FMOD.System fmodSystem;
|
|
public FMOD.System FmodSystem => fmodSystem;
|
|
|
|
private static AudioManager instance;
|
|
public static AudioManager Instance
|
|
{
|
|
get
|
|
{
|
|
if (instance == null)
|
|
{
|
|
instance = FindObjectOfType<AudioManager>();
|
|
if (instance == null)
|
|
{
|
|
var gameObject = new GameObject("FMOD Audio Manager");
|
|
instance = gameObject.AddComponent<AudioManager>();
|
|
}
|
|
}
|
|
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
void Awake()
|
|
{
|
|
FMOD.RESULT result = FMOD.Factory.System_Create(out fmodSystem);
|
|
CheckFmodResult(result, "FMOD.Factory.System_Create");
|
|
|
|
result = fmodSystem.getVersion(out uint version);
|
|
CheckFmodResult(result, "FMOD.System.getVersion");
|
|
|
|
Debug.LogFormat("[FMOD] Initialized FMOD System version {0:X1}.{1:X2}.{2:X2}",
|
|
(version >> 16) & 0xFF, (version >> 8) & 0xFF, version & 0xFF);
|
|
|
|
result = fmodSystem.getNumDrivers(out int numDrivers);
|
|
CheckFmodResult(result, "FMOD.System.getNumDrivers");
|
|
|
|
result = fmodSystem.setOutput(numDrivers > 0 ? FMOD.OUTPUTTYPE.AUTODETECT : FMOD.OUTPUTTYPE.NOSOUND);
|
|
CheckFmodResult(result, "FMOD.System.setOutput");
|
|
|
|
result = fmodSystem.getOutput(out FMOD.OUTPUTTYPE output);
|
|
CheckFmodResult(result, "FMOD.System.getOutput");
|
|
|
|
Debug.Log($"[FMOD] Using output type: {output}");
|
|
|
|
result = fmodSystem.init(MaxChannels, FMOD.INITFLAGS.NORMAL, IntPtr.Zero);
|
|
CheckFmodResult(result, "FMOD.System.init");
|
|
}
|
|
|
|
private void CheckFmodResult(FMOD.RESULT result, string cause)
|
|
{
|
|
if (result != FMOD.RESULT.OK)
|
|
{
|
|
if (fmodSystem.hasHandle())
|
|
{
|
|
fmodSystem.close();
|
|
fmodSystem.release();
|
|
fmodSystem.clearHandle();
|
|
}
|
|
throw new Exception($"[FMOD] Initialization failed : {cause} : {result.ToString()} : {FMOD.Error.String(result)}");
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (fmodSystem.hasHandle())
|
|
{
|
|
fmodSystem.close();
|
|
fmodSystem.release();
|
|
fmodSystem.clearHandle();
|
|
}
|
|
}
|
|
}
|