using System;
using UnityEngine;
using System.Collections.Generic;
using Wwise.Wooduan.Components;
using Wwise.Wooduan.Config;
using Wwise.Wooduan.Tools;
namespace Wwise.Wooduan.Core
{
public static class AudioManager
{
#region Event
///
/// Original method from AkSoundEngine, but with console logging.
///
public static uint PostEvent(string eventName, GameObject emitter = null, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName))
return AkSoundEngine.AK_INVALID_PLAYING_ID;
var playingID = AkSoundEngine.PostEvent(eventName, emitter);
ValidateEvent(playingID, eventName, emitter, trigger);
return playingID;
}
///
/// Original method from AkSoundEngine, but with console logging.
///
public static uint PostEvent(string eventName, GameObject emitter, AudioTriggerSource trigger, uint callbackType, AkCallbackManager.EventCallback callbackFunction, object cookie)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName))
return AkSoundEngine.AK_INVALID_PLAYING_ID;
var playingID = AkSoundEngine.PostEvent(eventName, emitter, callbackType, callbackFunction, cookie);
ValidateEvent(playingID, eventName, emitter, trigger);
return playingID;
}
///
/// Stop a playing event with optional fade out.
///
public static void StopEvent(string eventName, GameObject emitter = null, float fadeOutTime = 0f, AudioTriggerSource source = AudioTriggerSource.Code)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName)) return;
ExecuteActionOnEvent(eventName, emitter, AkActionOnEventType.AkActionOnEventType_Stop, AudioAction.StopEvent, fadeOutTime, source);
}
///
/// Stop all events playing on an emitter.
///
public static void StopAllEvents(GameObject emitter = null)
{
if (!AkSoundEngine.IsInitialized()) return;
AkSoundEngine.StopAll(emitter);
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, AudioObjectType.SFX, AudioTriggerSource.Code, AudioAction.StopEvent, "Stop All", emitter);
}
///
/// Jump to a specific time of a playing audio event.
///
public static void SeekOnEvent(string eventName, GameObject emitter, int timeMs, bool snapToMarker = false)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName))
return;
var result = AkSoundEngine.SeekOnEvent(eventName, emitter, timeMs, snapToMarker);
AkWooduanHelper.DebugToConsole(result == AKRESULT.AK_Success ? AudioLogVerbosity.Notification : AudioLogVerbosity.Error, GetEventType(eventName), AudioTriggerSource.Code, AudioAction.Seek, eventName, emitter, timeMs.ToString());
}
///
/// Jump to a specific time of a playingID.
///
public static void SeekOnEvent(uint playingID, int timeMs, bool snapToMarker = false)
{
if (!AkSoundEngine.IsInitialized() || playingID == 0)
return;
var result = AkSoundEngine.Seek(playingID, timeMs, snapToMarker);
AkWooduanHelper.DebugToConsole(result == AKRESULT.AK_Success ? AudioLogVerbosity.Notification : AudioLogVerbosity.Error, AudioObjectType.SFX, AudioTriggerSource.Code, AudioAction.Seek, playingID.ToString(), null, timeMs.ToString());
}
///
/// Pause a playing event with optional fade out.
///
public static void PauseEvent(string eventName, GameObject emitter = null, float fadeOutTime = 0f)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName))
return;
ExecuteActionOnEvent(eventName, emitter, AkActionOnEventType.AkActionOnEventType_Pause, AudioAction.Pause, fadeOutTime);
}
///
/// Resume a playing event with optional fade in.
///
public static void ResumeEvent(string eventName, GameObject emitter = null, float fadeInTime = 0f)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName))
return;
ExecuteActionOnEvent(eventName, emitter, AkActionOnEventType.AkActionOnEventType_Resume, AudioAction.Resume, fadeInTime);
}
// creating profiler log
internal static void ValidateEvent(uint playingID, string eventName, GameObject emitter, AudioTriggerSource trigger, string message = "")
{
var eventType = GetEventType(eventName);
if (playingID != 0)
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, eventType, trigger, AudioAction.PostEvent, eventName, emitter, message);
else
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Error, eventType, trigger, AudioAction.PostEvent, eventName, emitter, "Event not found");
}
private static AudioObjectType GetEventType(string eventName)
{
var eventType = AudioObjectType.SFX;
if (eventName.StartsWith("MU"))
eventType = AudioObjectType.Music;
else if (eventName.StartsWith("VO"))
eventType = AudioObjectType.Voice;
return eventType;
}
#endregion Event
#region Sound
///
/// Post a simple sound effect event.
///
public static uint PlaySound(string eventName, GameObject emitter = null, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName))
return AkSoundEngine.AK_INVALID_PLAYING_ID;
var playingID = AkSoundEngine.PostEvent(eventName, emitter);
ValidateEvent(playingID, eventName, emitter, trigger);
return playingID;
}
///
/// Play a sound effect with callback.
///
public static uint PlaySound(string eventName, AkCallbackType callbackType, Action callbackAction, GameObject emitter = null)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName))
return AkSoundEngine.AK_INVALID_PLAYING_ID;
var playingID = AkSoundEngine.PostEvent(eventName, emitter, (uint)callbackType, CallbackSimple, callbackAction);
ValidateEvent(playingID, eventName, emitter, AudioTriggerSource.Code);
return playingID;
}
///
/// Play a sound effect with multiple callbacks.
///
public static uint PlaySound(string eventName, AkCallbackType callbackTypes, Action callbackAction, GameObject emitter = null)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName))
return AkSoundEngine.AK_INVALID_PLAYING_ID;
var playingID = AkSoundEngine.PostEvent(eventName, emitter, (uint)callbackTypes, CallbackWithInfo, callbackAction);
ValidateEvent(playingID, eventName, emitter, AudioTriggerSource.Code);
return playingID;
}
///
/// Play a sound effect at location using sound object pool.
///
public static uint PlaySoundAtLocation(string eventName, Vector3 location, PooledAkGameObj emitter = null, bool useSpatialAudio = false)
{
if (!emitter)
emitter = EmitterManager.GetIdleEmitter();
emitter.transform.position = location;
emitter.isRoomAware = useSpatialAudio;
emitter.EventName = eventName;
emitter.InitPosition();
var playingID = AkSoundEngine.PostEvent(eventName, emitter.gameObject, (uint)AkCallbackType.AK_EndOfEvent, EmitterManager.OnPooledEventEnd, emitter);
ValidateEvent(playingID, eventName, emitter.gameObject, AudioTriggerSource.Code, location.ToString());
return playingID;
}
#endregion Sound
#region Music
private static string _lastPlayedMusic;
private static string _currentPlayingMusic;
private static uint _currentMusicPlayingID;
///
/// Switch back to the last music played.
///
public static uint PlayLastMusic(GameObject source = null, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
return PlayMusic(_lastPlayedMusic, source, trigger);
}
///
/// Post a background music event, ignore if same event is already playing.
///
public static uint PlayMusic(string eventName, GameObject emitter = null, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName))
return AkSoundEngine.AK_INVALID_PLAYING_ID;
if (eventName != _currentPlayingMusic)
{
var playingID = AkSoundEngine.PostEvent(eventName, emitter);
ValidateMusic(playingID, eventName, emitter, trigger);
}
else
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Warning, AudioObjectType.Music, trigger, AudioAction.PostEvent, eventName, emitter, "Music already playing");
return _currentMusicPlayingID;
}
///
/// Stop the background music currently playing.
///
public static void StopMusic(GameObject emitter = null, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
if (!AkSoundEngine.IsInitialized()) return;
//AkSoundEngine.PostEvent("Music_Stop", emitter);
_lastPlayedMusic = _currentPlayingMusic;
_currentPlayingMusic = string.Empty;
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, AudioObjectType.Music, trigger, AudioAction.StopEvent, "Global Music Stop", emitter);
}
///
/// Play background music with callback.
///
public static uint PlayMusic(string eventName, GameObject emitter, AkCallbackType callbackType, Action callbackAction)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName) || eventName == _currentPlayingMusic)
return AkSoundEngine.AK_INVALID_PLAYING_ID;
var playingID = AkSoundEngine.PostEvent(eventName, emitter, (uint) callbackType, CallbackSimple, callbackAction);
ValidateMusic(playingID, eventName, emitter);
return playingID;
}
///
/// Play background music with multiple callbacks.
///
public static uint PlayMusic(string eventName, GameObject emitter, AkCallbackType callbackTypes, Action callbackAction)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName) || eventName == _currentPlayingMusic)
return AkSoundEngine.AK_INVALID_PLAYING_ID;
var playingID = AkSoundEngine.PostEvent(eventName, emitter, (uint)callbackTypes, CallbackWithInfo, callbackAction);
ValidateMusic(playingID, eventName, emitter);
return playingID;
}
// update music play history and create profiler log
private static void ValidateMusic(uint playingID, string eventName, GameObject emitter = null, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
if (playingID != 0)
{
_lastPlayedMusic = _currentPlayingMusic;
_currentPlayingMusic = eventName;
_currentMusicPlayingID = playingID;
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, AudioObjectType.Music, trigger, AudioAction.PostEvent, eventName, emitter);
}
else
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Error, AudioObjectType.Music, trigger, AudioAction.PostEvent, eventName, emitter, "Event not found");
}
#endregion Music
#region Voice
private static string _currentPlayingVoice = "";
///
/// Post a voice dialog event.
///
public static uint PlayVoice(string eventName, GameObject speaker = null, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName))
return AkSoundEngine.AK_INVALID_PLAYING_ID;
var playingID = AkSoundEngine.PostEvent(eventName, speaker);
ValidateVoice(playingID, eventName, speaker, trigger);
return playingID;
}
///
/// Post a voice dialog event with external source.
///
public static uint PlayVoice(string eventName, GameObject speaker, string externalSourceName, int randomCount = 1)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName) || string.IsNullOrEmpty(externalSourceName))
return AkSoundEngine.AK_INVALID_PLAYING_ID;
var externalSourceArray = new AkExternalSourceInfoArray(1);
var externalSourceInfo = new AkExternalSourceInfo();
if (randomCount > 1)
{
var Selection = UnityEngine.Random.Range(1, randomCount + 1);
externalSourceName += (Selection < 10 ? "_0" : "_") + Selection;
}
externalSourceInfo.iExternalSrcCookie = AkSoundEngine.GetIDFromString("External_Source");
externalSourceInfo.szFile = externalSourceName;
externalSourceInfo.idCodec = AkSoundEngine.AKCODECID_VORBIS;
externalSourceArray[0] = externalSourceInfo;
var playingID = AkSoundEngine.PostEvent(eventName, speaker, 0, null, null, 1, externalSourceArray);
ValidateVoice(playingID, eventName, speaker, AudioTriggerSource.Code);
return playingID;
}
///
/// Play voice with callback
///
public static uint PlayVoice(string eventName, AkCallbackType callbackType, Action callbackAction)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName))
return AkSoundEngine.AK_INVALID_PLAYING_ID;
var playingID = AkSoundEngine.PostEvent(eventName, null, (uint)callbackType, CallbackSimple, callbackAction);
ValidateVoice(playingID, eventName);
return playingID;
}
///
/// Play voice with multiple callbacks
///
public static uint PlayVoice(string eventName, AkCallbackType callbackTypes, Action callbackAction)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName))
return AkSoundEngine.AK_INVALID_PLAYING_ID;
var playingID = AkSoundEngine.PostEvent(eventName, null, (uint)callbackTypes, CallbackWithInfo, callbackAction);
ValidateVoice(playingID, eventName);
return playingID;
}
// creating profiler log
private static void ValidateVoice(uint playingID, string eventName, GameObject speaker = null, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
if (playingID != 0)
{
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, AudioObjectType.Voice, trigger, AudioAction.PostEvent, eventName, speaker);
_currentPlayingVoice = eventName;
}
else
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Error, AudioObjectType.Voice, trigger, AudioAction.PostEvent, eventName, speaker, "Voice Event not found");
}
// pass pause/resume/stop command to Wwise
private static void ExecuteActionOnEvent(string eventName, GameObject emitter, AkActionOnEventType akAction, AudioAction action, float fadeTime, AudioTriggerSource source = AudioTriggerSource.Code)
{
fadeTime = Mathf.Clamp(fadeTime * 1000, 0, 10000);
var result = AkSoundEngine.ExecuteActionOnEvent(eventName, akAction, emitter, (int) fadeTime, AkCurveInterpolation.AkCurveInterpolation_Linear);
var eventType = GetEventType(eventName);
if (result == AKRESULT.AK_Success)
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, eventType, source, action, eventName, emitter);
else
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Error, eventType, source, action, eventName, emitter, result.ToString());
}
#endregion Voice
#region GameSync
///
/// Set a global Wwise state in the format of [stateGroupName / stateName]
///
public static void SetState(string stateInfo, GameObject source = null, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(stateInfo)) return;
var nameSplit = stateInfo.Split('/');
if (nameSplit.Length != 2)
{
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Error, AudioObjectType.State, trigger, AudioAction.SetValue, stateInfo, source, "Invalid State format");
return;
}
var groupName = nameSplit[0].Trim();
var stateName = nameSplit[1].Trim();
SetState(groupName, stateName, source, trigger);
}
///
/// Set a global Wwise state.
///
public static void SetState(string stateGroup, string state, GameObject source = null, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(stateGroup) || string.IsNullOrEmpty(state))
return;
if (EmitterManager.CanUpdateState(stateGroup, state))
{
AkSoundEngine.SetState(stateGroup, state);
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, AudioObjectType.State, trigger, AudioAction.SetValue, stateGroup + " / " + state, source);
}
}
///
/// Check if a state group is set at a certain state.
///
public static bool IsStateAt(string stateGroup, string assertedState)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(stateGroup) || string.IsNullOrEmpty(assertedState))
return false;
var checkId = AkSoundEngine.GetIDFromString(assertedState);
uint currentId;
AkSoundEngine.GetState(stateGroup, out currentId);
return checkId == currentId;
}
///
/// Get current global state value of a state group.
///
public static string GetState(string stateGroup)
{
return EmitterManager.GetState(stateGroup);
}
///
/// Set a Wwise switch on a game object in the format of [switchGroupName / switchName]
///
public static void SetSwitch(string switchInfo, GameObject emitter = null, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(switchInfo))
return;
var nameSplit = switchInfo.Split('/');
if (nameSplit.Length != 2)
{
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Error, AudioObjectType.Switch, trigger, AudioAction.SetValue, switchInfo, emitter, "Invalid Switch format");
return;
}
var groupName = nameSplit[0].Trim();
var switchName = nameSplit[1].Trim();
SetSwitch(groupName, switchName, emitter);
}
///
/// Set a Wwise switch on a game object.
///
public static void SetSwitch(string switchGroup, string switchName, GameObject emitter = null, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(switchGroup) || string.IsNullOrEmpty(switchName))
return;
if (EmitterManager.CanUpdateSwitch(emitter, switchGroup, switchName))
{
AkSoundEngine.SetSwitch(switchGroup, switchName, emitter);
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, AudioObjectType.Switch, trigger, AudioAction.SetValue, switchGroup + " / " + switchName, emitter);
}
}
///
/// Check if a switch group is set at a certain switch on an emitter.
///
public static bool IsSwitcheAt(string switchGroup, string assertedSwitch, GameObject emitter)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(switchGroup) || string.IsNullOrEmpty(assertedSwitch))
return false;
var checkId = AkSoundEngine.GetIDFromString(assertedSwitch);
uint currentId;
AkSoundEngine.GetSwitch(switchGroup, emitter, out currentId);
return checkId == currentId;
}
///
/// Get current switch value of a switch group on an emitter.
///
public static string GetSwitch(string switchGroup, GameObject emitter)
{
return EmitterManager.GetSwitch(emitter, switchGroup);
}
///
/// Set a Wwise RTPC value on a game object.
///
public static void SetRTPCValue(string RTPCName, float value, GameObject emitter = null, float updateTolerance = 0.01f, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(RTPCName))
return;
var result = AKRESULT.AK_Success;
if (EmitterManager.CanUpdateRTPC(emitter, RTPCName, value, updateTolerance))
{
if (!emitter || !AkSoundEngine.IsGameObjectRegistered(emitter))
result = AkSoundEngine.SetRTPCValue(RTPCName, value);
else
result = AkSoundEngine.SetRTPCValue(RTPCName, value, emitter);
if (result == AKRESULT.AK_Success)
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, AudioObjectType.RTPC, trigger,
AudioAction.SetValue, RTPCName, emitter, "Set to " + value.ToString("0.000"));
else
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Error, AudioObjectType.RTPC, trigger,
AudioAction.SetValue, RTPCName, emitter, result.ToString());
}
}
///
/// Get current RTPC value of an emitter or global.
///
public static float GetRTPCValue(string RTPCName, GameObject emitter = null)
{
return EmitterManager.GetRTPC(emitter, RTPCName);
}
///
/// Post a Wwise trigger on a game object.
///
public static void PostTrigger(string triggerName, GameObject emitter = null, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(triggerName))
return;
var result = AkSoundEngine.PostTrigger(triggerName, emitter);
if (result == AKRESULT.AK_Success)
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, AudioObjectType.Trigger, trigger, AudioAction.SetValue, triggerName, emitter);
else
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Error, AudioObjectType.Trigger, trigger, AudioAction.SetValue, triggerName, emitter, result.ToString());
}
#endregion GameSync
#region SoundBank
///
/// Load a sound bank synchronously with optional finish callback.
///
public static AKRESULT LoadBank(string bankName, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(bankName))
return AKRESULT.AK_Fail;
return AkBankManager.LoadBank(bankName, false, false, trigger);
}
public static void LoadBankAsync(string bankName, AkCallbackManager.BankCallback loadFinishedCallback = null)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(bankName))
return;
AkBankManager.LoadBankAsync(bankName, loadFinishedCallback);
}
///
/// Unload a sound bank with optional finish callback.
///
public static void UnloadBank(string bankName, bool ignoreRefCount = true, AudioTriggerSource trigger = AudioTriggerSource.Code)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(bankName))
return;
AkBankManager.UnloadBank(bankName, ignoreRefCount, trigger);
}
///
/// Load a sound file package.
///
public static void LoadPackage(string packageName)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(packageName))
return;
PCKManager.LoadPackage(packageName);
}
#endregion
#region ExternalAudioInput
///
/// Turn off audio temporarily for purposes like playing video.
///
public static void MuteAudio()
{
if (!AkSoundEngine.IsInitialized()) return;
if (SoundEnabled)
AkSoundEngine.PostEvent("Sound_Off", null);
if (VoiceEnabled)
AkSoundEngine.PostEvent("Voice_Off", null);
if (MusicEnabled)
AkSoundEngine.PostEvent("Music_Off", null);
}
///
/// Put audio back for conditions like video finishes.
///
public static void UnmuteAudio()
{
if (!AkSoundEngine.IsInitialized()) return;
if (SoundEnabled)
AkSoundEngine.PostEvent("Sound_On", null);
if (VoiceEnabled)
AkSoundEngine.PostEvent("Voice_On", null);
if (MusicEnabled)
AkSoundEngine.PostEvent("Music_On", null);
}
///
/// Pause audio system temporarily for purposes like playing video.
///
public static void PauseAudio()
{
if (!AkSoundEngine.IsInitialized()) return;
AkSoundEngine.Suspend();
}
///
/// Resume audio system temporarily for conditions like video finishes.
///
public static void ResumeAudio()
{
if (!AkSoundEngine.IsInitialized()) return;
AkSoundEngine.WakeupFromSuspend();
}
///
/// Duck main audio when player voice chat starts.
///
public static void StartVoicePlay()
{
if (!AkSoundEngine.IsInitialized()) return;
AkSoundEngine.PostEvent("Voice_Play_Start", null);
}
///
/// Resume main audio when player voice chat ends.
///
public static void EndVoicePlay()
{
if (!AkSoundEngine.IsInitialized()) return;
AkSoundEngine.PostEvent("Voice_Play_End", null);
}
///
/// Mute game music when player plays custom music.
///
public static void StartCustomMusic()
{
if (!AkSoundEngine.IsInitialized()) return;
AkSoundEngine.MuteBackgroundMusic(true);
}
///
/// Unmute game music when player stops custom music.
///
public static void EndCustomMusic()
{
if (!AkSoundEngine.IsInitialized()) return;
AkSoundEngine.MuteBackgroundMusic(false);
}
#endregion SoundBank
#region Bus
public static void SetBusConfig(string busName, AkChannelConfig config)
{
AkSoundEngine.SetBusConfig(busName, config);
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, AudioObjectType.Bus, AudioTriggerSource.Code, AudioAction.SetValue, busName, null, config.uNumChannels.ToString());
}
public static void SetBusEffect(string busName, string effectShareSetName, uint effectSlotIndex)
{
var shareSetID = AkSoundEngine.GetIDFromString(effectShareSetName);
AkSoundEngine.SetBusEffect(busName, effectSlotIndex, shareSetID);
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, AudioObjectType.Bus, AudioTriggerSource.Code, AudioAction.SetValue, busName, null, effectShareSetName);
}
public static void ClearBusEffect(string busName, uint effectSlotIndex)
{
AkSoundEngine.SetBusEffect(busName, effectSlotIndex, AkSoundEngine.AK_INVALID_UNIQUE_ID);
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, AudioObjectType.Bus, AudioTriggerSource.Code, AudioAction.SetValue, busName, null, "Clear Bus Effect");
}
#endregion Bus
#region Preference
///
/// Completely disable Wwise and all audio components to test performance without audio.
///
public static bool DisableWwise;
///
/// Turn on or off sound effects.
///
public static bool SoundEnabled
{
get
{
return PlayerPrefs.GetInt("AUDIO_SOUND", 1) == 1;
}
set
{
if (SoundEnabled == value) return;
var evt = value ? "Sound_On" : "Sound_Off";
AkSoundEngine.PostEvent(evt, null);
PlayerPrefs.SetInt("AUDIO_SOUND", value ? 1: 0);
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, AudioObjectType.SFX, AudioTriggerSource.Code, value ? AudioAction.Activate : AudioAction.Deactivate, evt);
}
}
///
/// Turn on or off voice dialogs.
///
public static bool VoiceEnabled
{
get
{
return PlayerPrefs.GetInt("AUDIO_VOICE", 1) == 1;
}
set
{
if (!AkSoundEngine.IsInitialized() || VoiceEnabled == value) return;
var evt = value ? "Voice_On" : "Voice_Off";
AkSoundEngine.PostEvent(evt, null);
PlayerPrefs.SetInt("AUDIO_VOICE", value ? 1: 0);
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, AudioObjectType.Voice, AudioTriggerSource.Code, value ? AudioAction.Activate : AudioAction.Deactivate, evt);
}
}
///
/// Turn on or off background music.
///
public static bool MusicEnabled
{
get
{
return PlayerPrefs.GetInt("AUDIO_MUSIC", 1) == 1;
}
set
{
if (!AkSoundEngine.IsInitialized() || MusicEnabled == value) return;
var evt = value ? "Music_On" : "Music_Off";
AkSoundEngine.PostEvent(evt, null);
PlayerPrefs.SetInt("AUDIO_MUSIC", value ? 1: 0);
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, AudioObjectType.Music, AudioTriggerSource.Code, value ? AudioAction.Activate : AudioAction.Deactivate, evt);
}
}
///
/// Change sound effect bus volume.
///
public static float SoundVolume
{
get
{
return PlayerPrefs.GetFloat("SOUND_VOLUME", 100f);
}
set
{
if (!AkSoundEngine.IsInitialized()) return;
AkSoundEngine.SetRTPCValue("Sound_Volume", value);
PlayerPrefs.SetFloat("SOUND_VOLUME", value);
}
}
///
/// Change voice dialog bus volume.
///
public static float VoiceVolume
{
get
{
return PlayerPrefs.GetFloat("VOICE_VOLUME", 100f);
}
set
{
if (!AkSoundEngine.IsInitialized()) return;
AkSoundEngine.SetRTPCValue("Voice_Volume", value);
PlayerPrefs.SetFloat("VOICE_VOLUME", value);
}
}
///
/// Change music bus volume.
///
public static float MusicVolume
{
get
{
return PlayerPrefs.GetFloat("MUSIC_VOLUME", 100f);
}
set
{
if (!AkSoundEngine.IsInitialized()) return;
AkSoundEngine.SetRTPCValue("Music_Volume", value);
PlayerPrefs.SetFloat("MUSIC_VOLUME", value);
}
}
///
/// Change language of voice dialog.
///
public static VoiceLanguage VoiceLanguage
{
get
{
var i = PlayerPrefs.GetInt("VOICE_LANGUAGE", 1);
return (VoiceLanguage)i;
}
set
{
if (!AkSoundEngine.IsInitialized())
return;
PlayerPrefs.SetInt("VOICE_LANGUAGE", (int)value);
AkSoundEngine.SetCurrentLanguage(value.ToString());
AkBankManager.ReloadLocalizedVoiceBanks();
AkWooduanHelper.DebugToConsole(AudioLogVerbosity.Notification, AudioObjectType.Language, AudioTriggerSource.Code, AudioAction.SetValue, value.ToString());
}
}
#endregion Preference
#region Callback & Query
// post a simple Wwise callback without any information
private static void CallbackSimple(object method, AkCallbackType in_type, AkCallbackInfo in_info)
{
var callback = (Action)method;
if (callback != null)
callback();
}
// post a Wwise callback with information included
private static void CallbackWithInfo(object method, AkCallbackType in_type, AkCallbackInfo in_info)
{
var callback = (Action)method;
if (callback != null)
callback(in_type, in_info);
}
public static bool IsEventPlayingOnGameObject(string eventName, GameObject emitter = null)
{
if (!AkSoundEngine.IsInitialized() || string.IsNullOrEmpty(eventName))
return false;
var playingIDs = new uint[10];
uint numOfEvents = 10;
AkSoundEngine.GetPlayingIDsFromGameObject(emitter, ref numOfEvents, playingIDs);
if (playingIDs.Length == 0)
return false;
var eventID = AkSoundEngine.GetIDFromString(eventName);
foreach (var playingID in playingIDs)
{
if (AkSoundEngine.GetEventIDFromPlayingID(playingID) == eventID)
return true;
}
return false;
}
#endregion Callback & Query
}
}