// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using Microsoft.MixedReality.Toolkit.Editor; using System.Linq; using UnityEditor; using UnityEngine; namespace Microsoft.MixedReality.Toolkit.Utilities.Editor { /// /// Collection of utilities to manage the configured audio spatializer. /// public static class SpatializerUtilities { /// /// Returns the name of the currently selected spatializer plugin. /// public static string CurrentSpatializer => AudioSettings.GetSpatializerPluginName(); /// /// Returns the names of installed spatializer plugins. /// public static string[] InstalledSpatializers => AudioSettings.GetSpatializerPluginNames(); /// /// Checks to see if the audio spatializer is configured and/or whether or /// not the spatializer collection has changed. /// /// /// True if the selected spatializer is installed and no changes have been made to the collection of installed spatializers. /// False if the selected spatializer is no longer installed or the collection of installed spatializers has been changed. /// public static bool CheckSettings() { // Check to see if the count of installed spatializers has changed if (!CheckSpatializerCount()) { // A spatializer has been added or removed. return false; } string spatializerName = CurrentSpatializer; // Check to see if an audio spatializer is configured. if (string.IsNullOrWhiteSpace(spatializerName)) { // The user chose to not initialize a spatializer so we are set correctly return true; } string[] installedSpatializers = InstalledSpatializers; // Check to see if the configured spatializer is installed. if (!installedSpatializers.Contains(spatializerName)) { // The current spatializer has been uninstalled. return false; } // A spatializer is correctly configured. return true; } /// /// Saves the specified spatializer to the audio settings. /// public static void SaveSettings(string spatializer) { if (string.IsNullOrWhiteSpace(spatializer)) { Debug.Log("No spatializer was specified. The application will not support Spatial Sound."); } else if (!InstalledSpatializers.Contains(spatializer)) { Debug.LogError($"{spatializer} is not an installed spatializer."); return; } SerializedObject audioMgrSettings = MixedRealityOptimizeUtils.GetSettingsObject("AudioManager"); SerializedProperty spatializerPlugin = audioMgrSettings.FindProperty("m_SpatializerPlugin"); if (spatializerPlugin == null) { Debug.LogError("Unable to save the spatializer settings. The field could not be located into the Audio Manager settings object."); return; } AudioSettings.SetSpatializerPluginName(spatializer); spatializerPlugin.stringValue = spatializer; audioMgrSettings.ApplyModifiedProperties(); // Cache the count of installed spatializers MixedRealityProjectPreferences.AudioSpatializerCount = InstalledSpatializers.Length; } /// /// Compares the previous and current count of installed spatializer plugins. /// /// True if the count of installed spatializers is unchanged, false otherwise. private static bool CheckSpatializerCount() { int previousCount = MixedRealityProjectPreferences.AudioSpatializerCount; int currentCount = InstalledSpatializers.Length; return (previousCount == currentCount); } } }