private void InitializeTemporaryMeshCluster()
        {
            var meshes = SoundToolKitManager.Instance.ResourceContainer.AcousticMeshes;

            if (meshes.Any(x => x.MeshCluster == null))
            {
                m_temporaryCluster      = ScriptableObject.CreateInstance <SoundToolKitMeshCluster>();
                m_temporaryCluster.name = "Temporary Mesh Cluster";
                MeshClusters.Add(m_temporaryCluster);

                SoundToolKitDebug.Log("Temporary SoundToolKitMeshCluster created for the duration of the playtime " +
                                      "in order to load all of the SoundToolKitMeshes that have no MeshCluster assigned. \n" +
                                      "To make this permanent and suppress this message use the menu " +
                                      "Tools/SoundToolKit/Geometry/SynchronizeDefaultGeometry after exiting the PlayMode. \n" +
                                      "In order not to load specific SoundToolKitMeshes, assign them to " +
                                      "a MeshCluster that is not added to the SoundToolKitGeometry's MeshClusters field.");

                foreach (var mesh in meshes)
                {
                    if (mesh.MeshCluster == null)
                    {
                        mesh.MeshCluster = MeshClusters.Last();
                    }
                }
            }
        }
        private void Initialize()
        {
            m_stkTaskScheduler = new SoundToolKitTaskScheduler();

            if (StkAudioEngine == null)
            {
                InitializeAudioEngine();
            }

            InitializeAudioBridge();

            ResourceContainer = new ResourcesContainer();

            if (m_settingsInspector == null)
            {
                Settings = ScriptableObject.CreateInstance <SoundToolKitSettings>();
            }

            UpdateProperties();

            SoundToolKitDebug.SetLogStackTrace();

#if UNITY_EDITOR
#if !UNITY_5 && !UNITY_2017_1
            EditorApplication.pauseStateChanged += EditorApplication_pauseStateChanged;
#else
            EditorApplication.playmodeStateChanged += EditorApplication_playmodeStateChanged;
#endif
#endif
            SoundToolKitDebug.Log("Sound enabled.");

            // NOTE: On audioEngine start it will automatically play sound.
            Play();
        }
Beispiel #3
0
        private System.Func <float, float> GetPredefinedAttenuation()
        {
            switch (Attenuation)
            {
            case SoundAttenuation.Nothing:
                return((distance) => 1.0f);

            case SoundAttenuation.PointSource:
                return((distance) => SoundToolKit.Extensions.Attenuation.PointSource(distance, MinDistance));

            case SoundAttenuation.LineSource:
                return((distance) => SoundToolKit.Extensions.Attenuation.LineSource(distance, MinDistance));

            case SoundAttenuation.Linear:
                return((distance) => SoundToolKit.Extensions.Attenuation.Linear(distance, MinDistance, MaxDistance));

            case SoundAttenuation.Logarithmic:
                return((distance) => SoundToolKit.Extensions.Attenuation.Logarithmic(distance, MinDistance, MaxDistance));

            case SoundAttenuation.Inverse:
                return((distance) => SoundToolKit.Extensions.Attenuation.Inverse(distance, MinDistance, MaxDistance));

            case SoundAttenuation.ReverseLog:
                return((distance) => SoundToolKit.Extensions.Attenuation.ReverseLog(distance, MinDistance, MaxDistance));

            default:
                SoundToolKitDebug.LogError("Invalid attenuation type.");
                return((distance) => 0.0f);
            }
        }
Beispiel #4
0
        internal void CreateInternalModel()
        {
            List <SoundToolKitMesh> stkMeshes = new List <SoundToolKitMesh>();

            Mesh[] internalMeshes = new Mesh[AcousticMeshNames.Count];

            for (var i = 0; i < AcousticMeshNames.Count; ++i)
            {
                var mesh = SoundToolKitManager.Instance.ResourceContainer.AcousticMeshes.FirstOrDefault(x => x.CombinedName() == AcousticMeshNames[i]);
                if (mesh != null)
                {
                    stkMeshes.Add(mesh);
                    internalMeshes[i] = mesh.GetStkMesh();
                }
                else
                {
                    SoundToolKitDebug.LogError("Mesh " + AcousticMeshNames[i] + " not found while trying to create internal MeshCluster.");
                }
            }

            Model = SoundToolKitManager.Instance.StkAudioEngine.ResourcesFactory.CreateModel(internalMeshes);

            if (Serialized)
            {
                RestoreGeometry();
            }
            else
            {
                SetGeometry(stkMeshes);
            }
        }
Beispiel #5
0
        protected override void OnValidate()
        {
            // <STK.LITE>
            // Warning: Modifying this code may result in plugin instability and frequent crashes to desktop.
            // This is the SoundToolKit Lite version.
            // Internally only one Playback per source is created anyway, so circumventing this check will not result in a possibility
            // of creating more playbacks - the previous Playback will be overwritten.
            if (Playbacks != null && Playbacks.Count > 1)
            {
                Playbacks.RemoveRange(1, Playbacks.Count - 1);
                SoundToolKitDebug.Log("Only one Playback per Sound Source is available in the SoundToolKit Lite version. " +
                                      "Deleting illegal playbacks.");
                return;
            }
            // </STK.LITE>

            base.OnValidate();

            if (Playbacks != null)
            {
                Playbacks.ForEach(x =>
                {
                    x.OnValidate();
                    if (m_playbackComponent != null)
                    {
                        m_playbackComponent.SynchronizePlaybackSamples(x);
                    }
                });
            }
        }
        public void OnStkInitialized(SoundToolKitManager soundToolKitManager)
        {
            if (!Initialized)
            {
                SoundToolKitDebug.Assert(soundToolKitManager.StkAudioEngine != null, "Audio engine is not initialized. Logical error.");

                m_stkStatus = soundToolKitManager.StkAudioEngine.Control;

                SerializedLogs = SoundToolKitManager.Instance.UncacheLogs();
                SoundToolKitManager.Instance.OnLogAdded += OnLogEntryAdded;

                m_stkStatus.OnIndicatorAdded += (indicator, indicatorValue) =>
                {
                    SerializedStatusIndicators.Add(new SerializedStatusIndicator(indicator, indicatorValue));
                };

                m_stkStatus.OnIndicatorRemoved += (indicator) =>
                {
                    SerializedStatusIndicators.Remove(SerializedStatusIndicators.FirstOrDefault(
                                                          x => x.Name == indicator.Name));
                };

                SoundToolKitManager.Instance.ResourceContainer.Status = this;
                Initialized = true;
            }
        }
Beispiel #7
0
        internal void CreateInternalPlayback(SoundToolKitPlayback playback)
        {
            if (playback.SoundToolKitSample == null)
            {
                return;
            }

            if (m_source != null)
            {
                var resourcesFactory = SoundToolKitManager.Instance.StkAudioEngine.ResourcesFactory;
                var samplesBuffer    = playback.SoundToolKitSample.GetSamplesBuffer();

                SoundToolKitDebug.Assert(samplesBuffer != null, "SamplesBuffer is missing. Logical error.");
                SoundToolKitDebug.Assert(samplesBuffer.Length > 0, "SamplesBuffer doesn't have samples.");

                var playbackSample = resourcesFactory.CreateSampleStatic(samplesBuffer, playback.SoundToolKitSample.AudioClip.channels);

                if (playback.Looped)
                {
                    playback.Playback = resourcesFactory.CreatePlayback(playbackSample, m_source, new PlaybackParameters(0, 1, PlaybackParameters.Looped));
                }
                else
                {
                    playback.Playback = resourcesFactory.CreatePlayback(playbackSample, m_source);
                }

                playback.Playback.OnDispose += (sender, e) =>
                {
                    playbackSample.Dispose();
                };

                playback.Name = playback.SoundToolKitSample.name;
            }
        }
        public override void OnStkInitialized(SoundToolKitManager soundToolKitManager)
        {
            if (!Initialized)
            {
                // <STK.LITE>
                // Warning: Modifying this code may result in plugin instability and frequent crashes to desktop.
                // This is the SoundToolKit Lite version.
                // Internally only three SoundSources can be created simultaneously, so circumventing this check will not result
                // in a possibility of creating more sound sources - only more references to the same source.
                if (soundToolKitManager.ResourceContainer.AudioSources.Count >= StkLite.AvailableSources())
                {
                    SoundToolKitDebug.Log("Only three SoundSources are available simultaneously in the SoundToolKit " +
                                          "Lite version. Deleting the illegal SoundSource components at GameObject " + gameObject.name);
                    Destroy(this);
                    return;
                }
                // </STK.LITE>

                SoundToolKitDebug.Assert(soundToolKitManager.StkAudioEngine != null, "AudioEngine is not initialized.");

                var audioEngine = soundToolKitManager.StkAudioEngine;
                m_ambientSoundSource = audioEngine.ResourcesFactory.CreateAmbientSoundSource();

                UpdateSourceProperties();

                soundToolKitManager.ResourceContainer.Add(audioSource: this);

                m_playbackComponent = new PlaybackComponent(m_ambientSoundSource);

                m_playbackComponent.InitializeInternalPlaybacks(Playbacks);
                m_playbackComponent.PlayOnAwake(Playbacks, gameObject.activeSelf, isActiveAndEnabled);

                Initialized = true;
            }
        }
Beispiel #9
0
        public void OnStkInitialized(SoundToolKitManager soundToolKitManager)
        {
            if (!Initialized)
            {
                SoundToolKitDebug.Assert(soundToolKitManager.StkAudioEngine != null, "Audio engine is not initialized. Logical error.");

                m_acousticMesh      = soundToolKitManager.StkAudioEngine.ResourcesFactory.CreateMesh();
                m_acousticMesh.Name = gameObject.name;

                UpdateProperties();

                var mesh = GetComponent <MeshFilter>().mesh;
                if (mesh != null)
                {
                    Mesh = mesh;
                }
                else
                {
                    SoundToolKitDebug.LogWarning("UnityEngine.Mesh not present in this component. Cannot AutoRegister.");
                }

                soundToolKitManager.ResourceContainer.Add(acousticMesh: this);

                if (soundToolKitManager.ResourceContainer.Geometry != null && MeshCluster == null)
                {
                    MeshCluster = soundToolKitManager.ResourceContainer.Geometry.GetTemporaryMeshCluster();
                }

                Initialized = true;
            }
        }
        private void InitializeAudioEngine()
        {
            try
            {
                m_stkThreadPoolHandler = (taskData, task) =>
                {
                    return((ulong)m_stkTaskScheduler.Schedule(() => task(taskData)));
                };
                m_stkThreadPool = SoundToolKitBuilder.CreateCustomThreadPool(m_stkThreadPoolHandler);

                StkAudioEngine = SoundToolKitBuilder.CreateSoundToolKit(
                    new SoundToolKitBuilder.ThreadPoolRequest
                {
                    CustomThreadPool = m_stkThreadPool
                }, null, null);

                var resourcesDirectory = Path.Combine(Application.streamingAssetsPath, @"SoundToolKit\Resources\");

                var diffractionData = File.ReadAllBytes(resourcesDirectory + "diffraction_table_100_20.bin");
                var hrtfData        = File.ReadAllBytes(resourcesDirectory + "bqc_hrtf.bin");

                StkAudioEngine.SetDiffractionData(diffractionData);
                StkAudioEngine.AudioOutput.SetHrtfData(hrtfData);

                StkAudioEngine.AudioOutput.HrtfEnabled = true;
            }
            catch (Exception e)
            {
                SoundToolKitDebug.LogError("Message: " + e.Message + "\nStack Trace: " + e.InnerException);
            }
        }
Beispiel #11
0
 private void CheckMeshClusterSynchronization()
 {
     if (MeshCluster != null && !MeshCluster.AcousticMeshNames.Contains(CombinedName()))
     {
         SoundToolKitDebug.LogWarning("Mesh: " + name + " was assigned to MeshCluster: " +
                                      MeshCluster.name + " but was not found on that Cluster's MeshList.\n" +
                                      "Probably a desynchronization occured due to deleting Meshes from the scene.\n" +
                                      "Please make sure everything is assigned correctly for the scene geometry to work as intended.");
     }
 }
 public override void Stop()
 {
     if (m_streamingSample != null)
     {
         Playback.Stop();
     }
     else
     {
         SoundToolKitDebug.LogWarning("StkStreamingSource is not initialized in: " + gameObject.name);
     }
 }
Beispiel #13
0
        internal float[] GetSamplesBuffer()
        {
            SoundToolKitDebug.Assert(m_audioClip != null, "AudioClip is missing.");

            if (m_samplesBuffer == null || m_samplesBuffer.Length != m_audioClip.samples * m_audioClip.channels)
            {
                m_samplesBuffer = ConvertToArrayOfSamples(m_audioClip);
            }

            return(m_samplesBuffer);
        }
Beispiel #14
0
        internal void WriteData(string filename, byte[] geometryData)
        {
            Filename = Path.ChangeExtension(filename, "dat");
            var targetPath = DataFilePath();

            File.WriteAllBytes(targetPath, geometryData);

            SoundToolKitDebug.Log("Successful serialization of a MeshCluster. Geometry data saved to file: " + Filename);
#if UNITY_EDITOR
            UnityEditor.EditorUtility.SetDirty(this);
#endif
        }
Beispiel #15
0
 private bool PlaybackListNotEmpty(List <SoundToolKitPlayback> playbacks)
 {
     if (playbacks.Any())
     {
         return(true);
     }
     else
     {
         SoundToolKitDebug.LogWarning("Playback List for SoundToolKitspatialSource is empty. No Playback to Play.");
         return(false);
     }
 }
        private void ReInitialize()
        {
            SoundToolKitDebug.LogWarning("Manager became unusable. Trying to re-initialize.");

            //TODO: [Not Tested]: This should be tested when AudioEngine is not Good.
            m_audioBridge.Dispose();
            StkAudioEngine.Dispose();

            //Re-Initialize
            Initialize();

            m_reinitializeCounter++;
        }
Beispiel #17
0
        public void Remove(SoundToolKitPlayback playback)
        {
            SoundToolKitDebug.Assert(playback != null, "Playback is null");

            if (Playbacks.Contains(playback))
            {
                m_playbacks.Remove(playback);
            }
            else
            {
                SoundToolKitDebug.LogWarning("Playback wasn't registered.");
            }
        }
Beispiel #18
0
        public void Remove(SoundToolKitMesh acousticMesh)
        {
            SoundToolKitDebug.Assert(acousticMesh != null, "Mesh is null");

            if (AcousticMeshes.Contains(acousticMesh))
            {
                m_acousticMeshes.Remove(acousticMesh);
            }
            else
            {
                SoundToolKitDebug.LogWarning("Mesh wasn't registered.");
            }
        }
Beispiel #19
0
        public void Remove(SoundToolKitMaterial acousticMaterial)
        {
            SoundToolKitDebug.Assert(acousticMaterial != null, "Material is null");

            if (AcousticMaterials.Contains(acousticMaterial))
            {
                m_acousticMaterials.Remove(acousticMaterial);
            }
            else
            {
                SoundToolKitDebug.LogWarning("Material wasn't registered.");
            }
        }
Beispiel #20
0
        public void Add(SoundToolKitMaterial acousticMaterial)
        {
            SoundToolKitDebug.Assert(acousticMaterial != null, "Material is null");

            if (!AcousticMaterials.Contains(acousticMaterial))
            {
                m_acousticMaterials.Add(acousticMaterial);
            }
            else
            {
                SoundToolKitDebug.LogWarning("Material already registered.");
            }
        }
Beispiel #21
0
        public void Remove(SourceComponent audioSource)
        {
            SoundToolKitDebug.Assert(audioSource != null, "Source component is null");

            if (AudioSources.Contains(audioSource))
            {
                m_audioSources.Remove(audioSource);
            }
            else
            {
                SoundToolKitDebug.LogWarning("Source component wasn't registered.");
            }
        }
Beispiel #22
0
        public void OnStkInitialized(SoundToolKitManager soundToolKitManager)
        {
            if (!Initialized)
            {
                SoundToolKitDebug.Assert(soundToolKitManager.StkAudioEngine != null, "Audio engine is not initialized. Logical error.");

                UpdateDamping(DampingCoefficients);
                DampingCoefficients.OnCoefficientsChanged += UpdateDamping;

                SoundToolKitManager.Instance.ResourceContainer.SceneConfiguration = this;
                Initialized = true;
                UpdateProperties();
            }
        }
Beispiel #23
0
        public void Add(SoundToolKitMesh acousticMesh)
        {
            SoundToolKitDebug.Assert(acousticMesh != null, "Mesh is null");

            if (!AcousticMeshes.Contains(acousticMesh))
            {
                m_acousticMeshes.Add(acousticMesh);
                RaiseMeshesChanged();
            }
            else
            {
                SoundToolKitDebug.LogWarning("Mesh already registered.");
            }
        }
 internal void SerializeGeometry()
 {
     MeshClusters.ForEach(x =>
     {
         if (x != null && x != m_temporaryCluster)
         {
             x.Serialize();
         }
         else if (x == m_temporaryCluster)
         {
             SoundToolKitDebug.Log("Temporary Mesh Cluster can't be serialized.");
         }
     });
 }
Beispiel #25
0
        private int ClampIndex(int playbackIndex, int playbacksCount)
        {
            if (playbackIndex >= playbacksCount)
            {
                playbackIndex = playbacksCount - 1;
                SoundToolKitDebug.LogWarning("Index of the chosen Playback is too large. Clamping to the last element on the list.");
            }
            else if (playbackIndex < 0)
            {
                playbackIndex = 0;
                SoundToolKitDebug.LogWarning("Index of the chosen Playback is too small. Clamping to the first element on the list.");
            }

            return(playbackIndex);
        }
Beispiel #26
0
        public static void SubscribeOnIntialized(ISoundToolKitObserver soundToolKitObserver)
        {
            var soundToolKitManager = SoundToolKitManager.Instance;

            SoundToolKitDebug.Assert(soundToolKitManager != null, "No instance of Manager exists.");

            if (soundToolKitManager.Initialized)
            {
                soundToolKitObserver.OnStkInitialized(soundToolKitManager);
            }
            else
            {
                soundToolKitManager.OnInitialized += () => soundToolKitObserver.OnStkInitialized(soundToolKitManager);
            }
        }
Beispiel #27
0
        internal byte[] ReadData()
        {
            var data = new byte[0];

            if (Valid)
            {
                data = File.ReadAllBytes(DataFilePath());
            }
            else
            {
                SoundToolKitDebug.LogError("Tried to read a non-existant SerializedGeometry file. " +
                                           "Make sure to serialize geometry before attempting to deserialize it.");
            }

            return(data);
        }
        public void OnStkInitialized(SoundToolKitManager soundToolKitManager)
        {
            if (!Initialized)
            {
                SoundToolKitDebug.Assert(soundToolKitManager.StkAudioEngine != null, "AudioEngine is null");

                m_receiver = soundToolKitManager.StkAudioEngine.Scene.Receiver;
                SoundToolKitDebug.Assert(m_receiver != null, "Receiver is null");

                soundToolKitManager.ResourceContainer.AudioListener = this;

                SubscribeOnTransformChanged(m_receiver);

                Initialized = true;
            }
        }
Beispiel #29
0
        public void OnStkInitialized(SoundToolKitManager soundToolKitManager)
        {
            if (!Initialized)
            {
                SoundToolKitDebug.Assert(soundToolKitManager.StkAudioEngine != null, "AudioEngine is null");

                m_editorExtensions   = new EditorExtensions(soundToolKitManager.StkAudioEngine);
                m_resourcesContainer = soundToolKitManager.ResourceContainer;

                m_resourcesContainer.OnMeshesChanged += RedrawAcoustic;

#if !UNITY_EDITOR
                UpdateMeshDrawer();
#endif
            }
        }
Beispiel #30
0
        internal EffectCoefficients(float[] coefficients)
        {
            SoundToolKitDebug.Assert(coefficients.Length == 8, "Improper number of coefficients used when trying" +
                                     " to initialize EffectCoefficients!");

            m_coefficientsArray = new float[8] {
                0, 0, 0, 0, 0, 0, 0, 0
            };

            Band125Hz   = coefficients[0];
            Band250Hz   = coefficients[1];
            Band500Hz   = coefficients[2];
            Band1000Hz  = coefficients[3];
            Band2000Hz  = coefficients[4];
            Band4000Hz  = coefficients[5];
            Band8000Hz  = coefficients[6];
            Band16000Hz = coefficients[7];
        }