ClearBanks() public static méthode

public static ClearBanks ( ) : AKRESULT
Résultat AKRESULT
    public void Terminate()
    {
#if UNITY_EDITOR
        ClearInitializeState();

        if (!IsSoundEngineLoaded)
        {
            return;
        }
#endif

        if (!AkSoundEngine.IsInitialized())
        {
            return;
        }

        // Stop everything, and make sure the callback buffer is empty. We try emptying as much as possible, and wait 10 ms before retrying.
        // Callbacks can take a long time to be posted after the call to RenderAudio().
        AkSoundEngine.StopAll();
        AkSoundEngine.ClearBanks();
        AkSoundEngine.RenderAudio();
        var retry = 5;
        do
        {
            var numCB = 0;
            do
            {
                numCB = AkCallbackManager.PostCallbacks();

                // This is a WSA-friendly sleep
                using (System.Threading.EventWaitHandle tmpEvent = new System.Threading.ManualResetEvent(false))
                {
                    tmpEvent.WaitOne(System.TimeSpan.FromMilliseconds(1));
                }
            }while (numCB > 0);

            // This is a WSA-friendly sleep
            using (System.Threading.EventWaitHandle tmpEvent = new System.Threading.ManualResetEvent(false))
            {
                tmpEvent.WaitOne(System.TimeSpan.FromMilliseconds(10));
            }

            retry--;
        }while (retry > 0);

        AkSoundEngine.Term();

        // Make sure we have no callbacks left after Term. Some might be posted during termination.
        AkCallbackManager.PostCallbacks();

        AkCallbackManager.Term();
        AkBankManager.Reset();
    }
Exemple #2
0
    void Terminate()
    {
        if (ms_Instance == null || ms_Instance != this || !AkSoundEngine.IsInitialized())
        {
            return;             //Don't term twice
        }
        // Stop everything, and make sure the callback buffer is empty. We try emptying as much as possible, and wait 10 ms before retrying.
        // Callbacks can take a long time to be posted after the call to RenderAudio().
        AkCallbackManager.SetMonitoringCallback(0, null);
        AkSoundEngine.StopAll();
        AkSoundEngine.ClearBanks();
        AkSoundEngine.RenderAudio();
        int retry = 5;

        do
        {
            int numCB = 0;
            do
            {
                numCB = AkCallbackManager.PostCallbacks();

                // This is a WSA-friendly sleep
                using (EventWaitHandle tmpEvent = new ManualResetEvent(false))
                {
                    tmpEvent.WaitOne(System.TimeSpan.FromMilliseconds(1));
                }
            }while(numCB > 0);

            // This is a WSA-friendly sleep
            using (EventWaitHandle tmpEvent = new ManualResetEvent(false))
            {
                tmpEvent.WaitOne(System.TimeSpan.FromMilliseconds(10));
            }
            retry--;
        }while (retry > 0);

        AkSoundEngine.Term();

        // Make sure we have no callbacks left after Term. Some might be posted during termination.
        AkCallbackManager.PostCallbacks();
        ms_Instance = null;

        AkCallbackManager.Term();
        AkBankManager.Reset();
    }
    public void Init(AkInitializer akInitializer)
    {
        if (akInitializer == null)
        {
            UnityEngine.Debug.LogError("WwiseUnity: AkInitializer must not be null. Sound engine will not be initialized.");
            return;
        }

#if UNITY_EDITOR
        if (UnityEngine.Application.isPlaying && !IsTheSingleOwningInitializer(akInitializer))
        {
            UnityEngine.Debug.LogError("WwiseUnity: Sound engine is already initialized.");
            return;
        }

        var arguments = System.Environment.GetCommandLineArgs();
        if (System.Array.IndexOf(arguments, "-nographics") >= 0 &&
            System.Array.IndexOf(arguments, "-wwiseEnableWithNoGraphics") < 0)
        {
            return;
        }

        var isInitialized = false;
        try
        {
            isInitialized       = AkSoundEngine.IsInitialized();
            IsSoundEngineLoaded = true;
        }
        catch (System.DllNotFoundException)
        {
            IsSoundEngineLoaded = false;
            UnityEngine.Debug.LogWarning("WwiseUnity: AkSoundEngine is not loaded.");
            return;
        }
#else
        var isInitialized = AkSoundEngine.IsInitialized();
#endif

        engineLogging = akInitializer.engineLogging;

        AkLogger.Instance.Init();

        AKRESULT result;
        uint     BankID;
        if (isInitialized)
        {
#if UNITY_EDITOR
            if (UnityEngine.Application.isPlaying || UnityEditor.BuildPipeline.isBuildingPlayer)
            {
                AkSoundEngine.ClearBanks();
                AkBankManager.Reset();

                result = AkSoundEngine.LoadBank("Init.bnk", AkSoundEngine.AK_DEFAULT_POOL_ID, out BankID);
                if (result != AKRESULT.AK_Success)
                {
                    UnityEngine.Debug.LogError("WwiseUnity: Failed load Init.bnk with result: " + result);
                }
            }

            result = AkCallbackManager.Init(akInitializer.callbackManagerBufferSize * 1024);
            if (result != AKRESULT.AK_Success)
            {
                UnityEngine.Debug.LogError("WwiseUnity: Failed to initialize Callback Manager. Terminate sound engine.");
                AkSoundEngine.Term();
                return;
            }

            OnEnableEditorListener(akInitializer.gameObject);
            UnityEditor.EditorApplication.update += LateUpdate;
#else
            UnityEngine.Debug.LogError("WwiseUnity: Sound engine is already initialized.");
#endif
            return;
        }

#if UNITY_EDITOR
        if (UnityEditor.BuildPipeline.isBuildingPlayer)
        {
            return;
        }
#endif

        UnityEngine.Debug.Log("WwiseUnity: Initialize sound engine ...");
        var basePath = s_DefaultBasePath;
        language = akInitializer.language;

        //Use default properties for most SoundEngine subsystem.
        //The game programmer should modify these when needed.  See the Wwise SDK documentation for the initialization.
        //These settings may very well change for each target platform.
        var memSettings = new AkMemSettings();
        memSettings.uMaxNumPools = 20;

        var deviceSettings = new AkDeviceSettings();
        AkSoundEngine.GetDefaultDeviceSettings(deviceSettings);

        var streamingSettings = new AkStreamMgrSettings();
        streamingSettings.uMemorySize = (uint)akInitializer.streamingPoolSize * 1024;

        var initSettings = new AkInitSettings();
        AkSoundEngine.GetDefaultInitSettings(initSettings);
        initSettings.uDefaultPoolSize      = (uint)akInitializer.defaultPoolSize * 1024;
        initSettings.uMonitorPoolSize      = (uint)akInitializer.monitorPoolSize * 1024;
        initSettings.uMonitorQueuePoolSize = (uint)akInitializer.monitorQueuePoolSize * 1024;
#if (!UNITY_ANDROID && !PLATFORM_LUMIN && !UNITY_WSA) || UNITY_EDITOR // Exclude WSA. It only needs the name of the DLL, and no path.
        initSettings.szPluginDLLPath = System.IO.Path.Combine(UnityEngine.Application.dataPath,
                                                              "Plugins" + System.IO.Path.DirectorySeparatorChar);
#elif PLATFORM_LUMIN && !UNITY_EDITOR
        initSettings.szPluginDLLPath = UnityEngine.Application.dataPath.Replace("Data", "bin") + System.IO.Path.DirectorySeparatorChar;
#endif

        var platformSettings = new AkPlatformInitSettings();
        AkSoundEngine.GetDefaultPlatformInitSettings(platformSettings);
        platformSettings.uLEngineDefaultPoolSize           = (uint)akInitializer.lowerPoolSize * 1024;
        platformSettings.fLEngineDefaultPoolRatioThreshold = akInitializer.memoryCutoffThreshold;

        var musicSettings = new AkMusicSettings();
        AkSoundEngine.GetDefaultMusicSettings(musicSettings);

        var spatialAudioSettings = new AkSpatialAudioInitSettings();
        spatialAudioSettings.uPoolSize = (uint)akInitializer.spatialAudioPoolSize * 1024;
        spatialAudioSettings.uMaxSoundPropagationDepth = akInitializer.maxSoundPropagationDepth;
        spatialAudioSettings.uDiffractionFlags         = (uint)akInitializer.diffractionFlags;

#if UNITY_EDITOR
        AkSoundEngine.SetGameName(UnityEngine.Application.productName + " (Editor)");
#else
        AkSoundEngine.SetGameName(UnityEngine.Application.productName);
#endif

        result = AkSoundEngine.Init(memSettings, streamingSettings, deviceSettings, initSettings, platformSettings,
                                    musicSettings, spatialAudioSettings, (uint)akInitializer.preparePoolSize * 1024);

        if (result != AKRESULT.AK_Success)
        {
            UnityEngine.Debug.LogError("WwiseUnity: Failed to initialize the sound engine. Abort.");
            AkSoundEngine.Term();
            return;             //AkSoundEngine.Init should have logged more details.
        }

        var basePathToSet = AkBasePathGetter.GetSoundbankBasePath();
        if (string.IsNullOrEmpty(basePathToSet))
        {
            UnityEngine.Debug.LogError("WwiseUnity: Couldn't find soundbanks base path. Terminate sound engine.");
            AkSoundEngine.Term();
            return;
        }

        result = AkSoundEngine.SetBasePath(basePathToSet);
        if (result != AKRESULT.AK_Success)
        {
            UnityEngine.Debug.LogError("WwiseUnity: Failed to set soundbanks base path. Terminate sound engine.");
            AkSoundEngine.Term();
            return;
        }

#if !UNITY_SWITCH
        // Calling Application.persistentDataPath crashes Switch
        var decodedBankFullPath = GetDecodedBankFullPath();
        // AkSoundEngine.SetDecodedBankPath creates the folders for writing to (if they don't exist)
        AkSoundEngine.SetDecodedBankPath(decodedBankFullPath);
#endif

        AkSoundEngine.SetCurrentLanguage(language);

#if !UNITY_SWITCH
        // Calling Application.persistentDataPath crashes Switch
        // AkSoundEngine.AddBasePath is currently only implemented for iOS and Android; No-op for all other platforms.
        AkSoundEngine.AddBasePath(UnityEngine.Application.persistentDataPath + System.IO.Path.DirectorySeparatorChar);
        // Adding decoded bank path last to ensure that it is the first one used when writing decoded banks.
        AkSoundEngine.AddBasePath(decodedBankFullPath);
#endif

        result = AkCallbackManager.Init(akInitializer.callbackManagerBufferSize * 1024);
        if (result != AKRESULT.AK_Success)
        {
            UnityEngine.Debug.LogError("WwiseUnity: Failed to initialize Callback Manager. Terminate sound engine.");
            AkSoundEngine.Term();
            return;
        }

        AkBankManager.Reset();

        UnityEngine.Debug.Log("WwiseUnity: Sound engine initialized.");

        //Load the init bank right away.  Errors will be logged automatically.
        result = AkSoundEngine.LoadBank("Init.bnk", AkSoundEngine.AK_DEFAULT_POOL_ID, out BankID);

        if (result != AKRESULT.AK_Success)
        {
            UnityEngine.Debug.LogError("WwiseUnity: Failed load Init.bnk with result: " + result);
        }

#if UNITY_EDITOR
        OnEnableEditorListener(akInitializer.gameObject);
        UnityEditor.EditorApplication.update += LateUpdate;
#endif
    }
    public void Init(AkInitializer akInitializer)
    {
        engineLogging = akInitializer.engineLogging;

        AkLogger.Instance.Init();

        AKRESULT result;
        uint     BankID;

        if (AkSoundEngine.IsInitialized())
        {
#if UNITY_EDITOR
            if (Application.isPlaying)
            {
                AkSoundEngine.ClearBanks();
                AkBankManager.Reset();

                result = AkSoundEngine.LoadBank("Init.bnk", AkSoundEngine.AK_DEFAULT_POOL_ID, out BankID);
                if (result != AKRESULT.AK_Success)
                {
                    Debug.LogError("WwiseUnity: Failed load Init.bnk with result: " + result.ToString());
                }
            }

            result = AkCallbackManager.Init(akInitializer.callbackManagerBufferSize * 1024);
            if (result != AKRESULT.AK_Success)
            {
                Debug.LogError("WwiseUnity: Failed to initialize Callback Manager. Terminate sound engine.");
                AkSoundEngine.Term();
                return;
            }

            EditorApplication.update += LateUpdate;
#endif
            return;
        }

#if UNITY_EDITOR
        if (BuildPipeline.isBuildingPlayer)
        {
            return;
        }
#endif

        Debug.Log("WwiseUnity: Initialize sound engine ...");
        basePath = akInitializer.basePath;
        language = akInitializer.language;

        //Use default properties for most SoundEngine subsystem.
        //The game programmer should modify these when needed.  See the Wwise SDK documentation for the initialization.
        //These settings may very well change for each target platform.
        AkMemSettings memSettings = new AkMemSettings();
        memSettings.uMaxNumPools = 20;

        AkDeviceSettings deviceSettings = new AkDeviceSettings();
        AkSoundEngine.GetDefaultDeviceSettings(deviceSettings);

        AkStreamMgrSettings streamingSettings = new AkStreamMgrSettings();
        streamingSettings.uMemorySize = (uint)akInitializer.streamingPoolSize * 1024;

        AkInitSettings initSettings = new AkInitSettings();
        AkSoundEngine.GetDefaultInitSettings(initSettings);
        initSettings.uDefaultPoolSize      = (uint)akInitializer.defaultPoolSize * 1024;
        initSettings.uMonitorPoolSize      = (uint)akInitializer.monitorPoolSize * 1024;
        initSettings.uMonitorQueuePoolSize = (uint)akInitializer.monitorQueuePoolSize * 1024;
#if (!UNITY_ANDROID && !UNITY_WSA) || UNITY_EDITOR // Exclude WSA. It only needs the name of the DLL, and no path.
        initSettings.szPluginDLLPath = Path.Combine(Application.dataPath, "Plugins" + Path.DirectorySeparatorChar);
#endif

        AkPlatformInitSettings platformSettings = new AkPlatformInitSettings();
        AkSoundEngine.GetDefaultPlatformInitSettings(platformSettings);
        platformSettings.uLEngineDefaultPoolSize           = (uint)akInitializer.lowerPoolSize * 1024;
        platformSettings.fLEngineDefaultPoolRatioThreshold = akInitializer.memoryCutoffThreshold;

        AkMusicSettings musicSettings = new AkMusicSettings();
        AkSoundEngine.GetDefaultMusicSettings(musicSettings);

        AkSpatialAudioInitSettings spatialAudioSettings = new AkSpatialAudioInitSettings();
        spatialAudioSettings.uPoolSize = (uint)akInitializer.spatialAudioPoolSize * 1024;
        spatialAudioSettings.uMaxSoundPropagationDepth = akInitializer.maxSoundPropagationDepth;
        spatialAudioSettings.uDiffractionFlags         = (uint)akInitializer.diffractionFlags;

#if UNITY_EDITOR
        AkSoundEngine.SetGameName(Application.productName + " (Editor)");
#else
        AkSoundEngine.SetGameName(Application.productName);
#endif

        result = AkSoundEngine.Init(memSettings, streamingSettings, deviceSettings, initSettings, platformSettings, musicSettings, spatialAudioSettings, (uint)akInitializer.preparePoolSize * 1024);

        if (result != AKRESULT.AK_Success)
        {
            Debug.LogError("WwiseUnity: Failed to initialize the sound engine. Abort.");
            AkSoundEngine.Term();
            return;             //AkSoundEngine.Init should have logged more details.
        }

        string basePathToSet = AkBasePathGetter.GetSoundbankBasePath();
        if (string.IsNullOrEmpty(basePathToSet))
        {
            Debug.LogError("WwiseUnity: Couldn't find soundbanks base path. Terminate sound engine.");
            AkSoundEngine.Term();
            return;
        }

        result = AkSoundEngine.SetBasePath(basePathToSet);
        if (result != AKRESULT.AK_Success)
        {
            Debug.LogError("WwiseUnity: Failed to set soundbanks base path. Terminate sound engine.");
            AkSoundEngine.Term();
            return;
        }

#if !UNITY_SWITCH
        // Calling Application.persistentDataPath crashes Switch
        string decodedBankFullPath = GetDecodedBankFullPath();
        // AkSoundEngine.SetDecodedBankPath creates the folders for writing to (if they don't exist)
        AkSoundEngine.SetDecodedBankPath(decodedBankFullPath);
#endif

        AkSoundEngine.SetCurrentLanguage(language);

#if !UNITY_SWITCH
        // Calling Application.persistentDataPath crashes Switch
        // AkSoundEngine.AddBasePath is currently only implemented for iOS and Android; No-op for all other platforms.
        AkSoundEngine.AddBasePath(Application.persistentDataPath + Path.DirectorySeparatorChar);
        // Adding decoded bank path last to ensure that it is the first one used when writing decoded banks.
        AkSoundEngine.AddBasePath(decodedBankFullPath);
#endif

        result = AkCallbackManager.Init(akInitializer.callbackManagerBufferSize * 1024);
        if (result != AKRESULT.AK_Success)
        {
            Debug.LogError("WwiseUnity: Failed to initialize Callback Manager. Terminate sound engine.");
            AkSoundEngine.Term();
            return;
        }

        AkBankManager.Reset();

        Debug.Log("WwiseUnity: Sound engine initialized.");

        //Load the init bank right away.  Errors will be logged automatically.
        result = AkSoundEngine.LoadBank("Init.bnk", AkSoundEngine.AK_DEFAULT_POOL_ID, out BankID);
        if (result != AKRESULT.AK_Success)
        {
            Debug.LogError("WwiseUnity: Failed load Init.bnk with result: " + result.ToString());
        }

#if UNITY_EDITOR
#if UNITY_2017_2_OR_NEWER
        EditorApplication.pauseStateChanged += OnPauseStateChanged;
#else
        EditorApplication.playmodeStateChanged += OnEditorPlaymodeStateChanged;
#endif
        EditorApplication.update += LateUpdate;
#endif
    }