NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/Audio/AudioManager.cs

397 lines
11 KiB
C#
Raw Normal View History

2024-01-19 17:25:06 +08:00
using System;
using Cysharp.Threading.Tasks;
using PhxhSDK;
2023-08-22 19:08:45 +08:00
using UnityEngine;
using UnityEngine.Audio;
2025-03-10 13:12:05 +08:00
// public enum EPlayType
// {
// Music,
// Sound,
// Speak
// }
2023-08-22 19:08:45 +08:00
public class AudioManager: Singlenton<AudioManager>, IStart, IInitable, IUpdatable, IAudioPlay
2023-08-22 19:08:45 +08:00
{
2025-03-10 13:12:05 +08:00
private AudioUnityManager _audioMgrObj = new();
//private AudioCriwareManager _audioMgrOldObj = new();
2023-08-22 19:08:45 +08:00
public GameObject initGameObject;
2024-01-19 17:25:06 +08:00
2024-04-28 17:03:37 +08:00
public static bool DisableAudio = false;
2024-04-28 16:18:27 +08:00
2024-07-01 17:23:05 +08:00
private bool isPlayingAlbum = false;
public bool IsPlayingAlbum
{
get
{
return isPlayingAlbum;
}
set
{
isPlayingAlbum = value;
}
}
public bool IsPlayingBGM { get; set; }
2024-07-01 17:23:05 +08:00
2025-03-10 13:12:05 +08:00
public AudioUnityManager AudioMgrObj
2024-03-26 12:22:47 +08:00
{
get { return _audioMgrObj; }
}
//public AudioMixer masterMixer => _audioMgrObj.masterMixer;
2025-03-10 13:12:05 +08:00
// public AudioCriwareManager AudioOldMgrObj
// {
// get { return _audioMgrOldObj; }
// }
2024-03-26 12:22:47 +08:00
2024-01-19 17:25:06 +08:00
private void AfterSwitchCamera(bool isRegister = true)
{
if (isRegister)
{
CameraManager.Instance.AfterSwitchMainCamera += _audioMgrObj.AfterSwitchMainCamera;
}
else
{
CameraManager.Instance.AfterSwitchMainCamera -= _audioMgrObj.AfterSwitchMainCamera;
}
}
2023-08-22 19:08:45 +08:00
public void Init()
{
_audioMgrObj.Init();
2024-01-19 17:25:06 +08:00
AfterSwitchCamera();
2023-08-22 19:08:45 +08:00
}
2023-08-22 19:08:45 +08:00
public void Release()
{
_audioMgrObj.Release();
2024-01-19 17:25:06 +08:00
AfterSwitchCamera(false);
2023-10-19 15:00:19 +08:00
}
public void Start()
2023-08-22 19:08:45 +08:00
{
_audioMgrObj.Start();
initGameObject = initGameObject;
2023-08-22 19:08:45 +08:00
}
public void Update(float dt)
2023-08-22 19:08:45 +08:00
{
_audioMgrObj.Update(dt);
2023-08-22 19:08:45 +08:00
}
public float GlobalVolume
2023-08-22 19:08:45 +08:00
{
get { return _audioMgrObj.GlobalVolume; }
set { _audioMgrObj.GlobalVolume = value; }
2023-08-22 19:08:45 +08:00
}
public float GlobalMusicVolume
2023-08-22 19:08:45 +08:00
{
get { return _audioMgrObj.GlobalMusicVolume; }
set { _audioMgrObj.GlobalMusicVolume = value; }
2023-08-22 19:08:45 +08:00
}
public float GlobalSoundsVolume
2023-08-22 19:08:45 +08:00
{
get { return _audioMgrObj.GlobalSoundsVolume; }
set { _audioMgrObj.GlobalSoundsVolume = value; }
2023-08-22 19:08:45 +08:00
}
2023-12-13 15:14:52 +08:00
public bool GlobalIsSpeakOpen
{
get { return _audioMgrObj.GlobalIsOpenSpeak; }
set { _audioMgrObj.GlobalIsOpenSpeak = value; }
}
2024-04-28 14:24:10 +08:00
public float GlobalSpeakVolume
{
get { return _audioMgrObj.GlobalSpeakVolume; }
set { _audioMgrObj.GlobalSpeakVolume = value; }
}
2023-10-19 15:00:19 +08:00
2023-08-22 19:08:45 +08:00
public void SetMusicEnable(bool isEnable)
{
GlobalMusicVolume = isEnable ? 1 : 0;
}
public void SetSoundEnable(bool isEnable)
{
GlobalSoundsVolume = isEnable ? 1 : 0;
}
2025-03-10 13:12:05 +08:00
// public CriAtomExAcb GetAcb(string cueSheetName)
// {
// return _audioMgrObj.GetAcb(cueSheetName);
// }
public async UniTask<float> GetClipLength(string key)
2023-08-22 19:08:45 +08:00
{
2025-03-10 13:12:05 +08:00
return await _audioMgrObj.GetClipLength(key);
2023-10-19 15:00:19 +08:00
}
#region 流式播放
public int PlayAudio(string key, AudioCriManager.EPlayType type = AudioCriManager.EPlayType.Sound,
GameObject posObj = null, bool loop = false, int priority = AudioConstant.DefaultPriority, bool isFade = false)
2023-10-19 15:00:19 +08:00
{
2024-10-17 18:38:07 +08:00
if (type == AudioCriManager.EPlayType.Music)
{
//如果使用单个audio播放music可能会顶掉原先播放的专辑音乐
//在这里将专辑音乐标记为false
IsPlayingAlbum = false;
IsPlayingBGM = false;
2024-10-17 18:38:07 +08:00
}
return _audioMgrObj.PlayAudio(key, type, posObj, loop, priority, isFade);
2023-10-19 15:00:19 +08:00
}
2024-07-25 17:43:14 +08:00
2024-10-10 18:55:19 +08:00
public int PlayAudio(string key, Vector3 pos, AudioCriManager.EPlayType type = AudioCriManager.EPlayType.Sound, bool loop = false,
int priority = AudioConstant.DefaultPriority, bool isFade = false)
2024-07-25 17:43:14 +08:00
{
2024-10-17 18:38:07 +08:00
if (type == AudioCriManager.EPlayType.Music)
{
//如果使用单个audio播放music可能会顶掉原先播放的专辑音乐
//在这里将专辑音乐标记为false
IsPlayingAlbum = false;
IsPlayingBGM = false;
2024-10-17 18:38:07 +08:00
}
return _audioMgrObj.PlayAudio(key, type, pos, loop, priority, isFade);
2024-07-25 17:43:14 +08:00
}
#endregion
2024-07-25 16:23:17 +08:00
public async UniTask<int> PlayAudioOld(string key, AudioCriManager.EPlayType type = AudioCriManager.EPlayType.Sound, GameObject posObj = null)
{
// if (DisableAudio) return 0;
// return await _audioMgrObj.PlayAudio(key, type, posObj);
2024-10-28 16:26:20 +08:00
DebugUtil.Log("更换流式播放后Key值需要修改为String详情参考AudioSourceConfig");
2024-07-25 16:23:17 +08:00
return -1;
}
2023-10-19 15:00:19 +08:00
2024-07-25 16:23:17 +08:00
public async UniTask<int> PlayAudioOld(string key, Vector3 pos, AudioCriManager.EPlayType type = AudioCriManager.EPlayType.Sound)
{
2024-10-28 16:26:20 +08:00
DebugUtil.Log("更换流式播放后Key值需要修改为String详情参考AudioSourceConfig");
2024-07-25 16:23:17 +08:00
return -1;
// if (DisableAudio) return 0;
// return await _audioMgrObj.PlayAudio(key, type, pos);
}
public async UniTask<int> PlayAudioOld(int id, AudioCriManager.EPlayType type = AudioCriManager.EPlayType.Sound, GameObject posObj = null)
2023-10-19 15:00:19 +08:00
{
// if (DisableAudio) return 0;
// return await _audioMgrOldObj.PlayAudio(id, type, posObj);
2024-10-28 16:26:20 +08:00
DebugUtil.Log("更换流式播放后Key值需要修改为String详情参考AudioSourceConfig");
return -1;
2023-10-24 18:53:34 +08:00
}
public async UniTask<int> PlayAudioOld(int id, Vector3 pos, AudioCriManager.EPlayType type = AudioCriManager.EPlayType.Sound)
2023-10-19 15:00:19 +08:00
{
// if (DisableAudio) return 0;
// DebugUtil.Log("Test");
// return await _audioMgrOldObj.PlayAudio(id, type, pos);
2024-10-28 16:26:20 +08:00
DebugUtil.Log("更换流式播放后Key值需要修改为String详情参考AudioSourceConfig");
return -1;
2023-10-19 15:00:19 +08:00
}
2024-03-26 12:22:47 +08:00
2024-09-10 16:21:12 +08:00
public void StopByID(int id)
{
if (DisableAudio)
return;
2024-09-10 16:21:12 +08:00
_audioMgrObj.StopByID(id);
}
2025-03-04 17:31:03 +08:00
public void StopWithFadeByID(int id, float fadeOutTime = 1f)
{
if (DisableAudio) return;
_audioMgrObj.StopWithFadeByID(id, fadeOutTime);
}
2024-09-10 16:21:12 +08:00
2024-04-24 12:59:23 +08:00
public void StopMusicById(int id)
{
2024-04-28 16:18:27 +08:00
if (DisableAudio) return;
2024-04-24 12:59:23 +08:00
_audioMgrObj.StopMusicById(id);
}
2024-09-10 16:21:12 +08:00
public void StopSoundById(int id)
{
if (DisableAudio) return;
_audioMgrObj.StopSoundByID(id);
}
2024-07-01 17:23:05 +08:00
public void PauseBackgroundMusic()
{
//TODO 暂停背景音乐
_audioMgrObj?.StopAllMusic();
}
/// <summary>
/// 暂停所有音效
/// </summary>
public void PauseAllSound()
{
_audioMgrObj?.PauseAllSound();
}
public void ResumeAllSound()
{
_audioMgrObj?.ResumeAllSound();
}
2024-07-01 17:23:05 +08:00
public void ResumeBackgroundMusic()
{
//TODO 恢复背景音乐
_audioMgrObj?.ResumeAllMusic();
}
2025-03-10 13:12:05 +08:00
// /// <summary>
// /// 注册音频分析器的对音频挂接的监听
// /// </summary>
// /// <returns></returns>
// public CriAtomExOutputAnalyzer RegisterAnalyzer()//CriAtomExOutputAnalyzer criAtomExOutputAnalyzer
// {
// if (DisableAudio) return null;
// return _audioMgrObj.RegisterAnalyzer();//criAtomExOutputAnalyzer
// }
// /// <summary>
// /// 取消音频分析器对挂接的监听
// /// </summary>
// public void UnregisterAnalyzer()//CriAtomExOutputAnalyzer criAtomExOutputAnalyzer
// {
// if (DisableAudio) return;
// _audioMgrObj.UnregisterAnalyzer();//criAtomExOutputAnalyzer
// }
// public CriAtomExOutputAnalyzer GetAnalyzer()
// {
// return _audioMgrObj.analyzer;
// }
2024-04-11 19:15:37 +08:00
/// <summary>
/// 停止语音
/// </summary>
public void StopAllSpeaks()
{
_audioMgrObj?.StopAllSpeaks();
}
public void StopAllMusic()
{
2024-10-17 16:24:24 +08:00
IsPlayingAlbum = false;
IsPlayingBGM = false;
2024-10-17 16:24:24 +08:00
_audioMgrObj?.StopAllMusic();
}
2024-04-11 19:15:37 +08:00
public void StopAll()
{
2024-10-17 16:24:24 +08:00
IsPlayingAlbum = false;
IsPlayingBGM = false;
2024-04-11 19:15:37 +08:00
_audioMgrObj.StopAll();
}
2024-04-23 19:42:31 +08:00
// public Audio GetAudioByID(AudioType audioType, int id)
// {
// if (DisableAudio) return null;
// return _audioMgrObj.GetAudioByID(audioType, id);
// }
2025-04-16 17:58:35 +08:00
public void GetSpectrumData(AudioCriManager.EPlayType audioType, int id, float[] spectrumsShow, float[] spectrumsData)
{
AudioInst audioInst = _audioMgrObj.GetAudioByID(audioType, id);
if (null == audioInst)
{
for (int i = 0; i < spectrumsShow.Length; ++i)
{
spectrumsShow[i] = 0f;
}
return;
}
audioInst.GetSpectrumData(spectrumsData);
int n = spectrumsData.Length / spectrumsShow.Length;
for (int i = 0; i < spectrumsShow.Length; ++i)
{
float value = 0f;
for (int j = 0; j < n; ++j)
{
value += spectrumsData[i * n + j];
}
value = Mathf.Log10(Mathf.Log(i + 2f, 2f) * value * (i * i + 1) * 9 + 1);
spectrumsShow[i] = value;
}
}
/// <summary>
/// 通过ID和类型获取播放对象
/// </summary>
/// <param name="audioType"></param>
/// <param name="id"></param>
/// <returns></returns>
public AudioInst GetAudioByID(AudioCriManager.EPlayType audioType, int id)
2024-04-23 19:42:31 +08:00
{
2024-04-28 16:18:27 +08:00
if (DisableAudio) return null;
2024-04-23 19:42:31 +08:00
return _audioMgrObj.GetAudioByID(audioType, id);
}
2024-08-01 14:03:20 +08:00
public AudioInst GetStoryAudioInst(AudioBase.AudioType audioType = AudioBase.AudioType.Sound)
2024-08-01 14:03:20 +08:00
{
return _audioMgrObj.GetStoryAudioInst(audioType);
2024-08-01 14:03:20 +08:00
}
public void ReleaseStoryAudios()
{
_audioMgrObj.ReleaseStoryAudios();
}
public void PlayByCueName(int audioID, string cueName, bool isFade = false)
2024-08-01 14:03:20 +08:00
{
2025-03-04 17:31:03 +08:00
_audioMgrObj.PlayByCueName(audioID, cueName, isFade);
}
public void PlayByCueName(int audioID, string cueName, float fadeInTime)
{
_audioMgrObj.PlayByCueName(audioID, cueName, true, fadeInTime);
2024-08-01 14:03:20 +08:00
}
2025-03-05 13:40:25 +08:00
public void PlayByCueName(int audioID, string cueName, float fadeInTime, float targetVolume)
{
_audioMgrObj.PlayByCueName(audioID, cueName, fadeInTime, targetVolume);
}
2025-03-04 17:31:03 +08:00
public int PlayByCueName(string cueName, bool isFade = false, float fadeInTime = 4f)
{
return _audioMgrObj.PlayAudio(cueName, AudioCriManager.EPlayType.Music, null, true, AudioConstant.DefaultPriority,
isFade, fadeInTime);
}
public void ReleaseAllAudios()
{
_audioMgrObj.ReleaseAllAudios();
}
2024-09-10 16:21:12 +08:00
/// <summary>
/// 通过播放ID获取音频对象
/// </summary>
/// <param name="playID"></param>
/// <returns></returns>
public AudioInst GetSoundByPlayID(long playID)
{
return _audioMgrObj.GetSoundByPlayID(playID);
}
2025-03-10 13:12:05 +08:00
// public long GetCueLength(string cueSheet, string cueName)
// {
// return _audioMgrObj.GetCueLength(cueSheet, cueName);
// }
2024-10-16 16:03:13 +08:00
public void InitLimit(int c0, int c1, int c2)
{
AudioMgrObj.InitLimit(c0, c1, c2);
}
2025-04-02 15:00:32 +08:00
public void SetGlobalListenerFollow(bool isActive)
{
2025-04-02 15:00:32 +08:00
if(isActive)
AudioMgrObj.EnterFollowState();
else
AudioMgrObj.ExitFollowState();
}
}