Exemplo n.º 1
0
        protected override void InitAudioSystem()
        {
            AudioNativeCalls.InitAudio();

            AudioConfig ac           = default;
            Entity      eAudioConfig = Entity.Null;

            if (!HasSingleton <AudioConfig>())
            {
                eAudioConfig = EntityManager.CreateEntity(typeof(AudioConfig));
            }
            else
            {
                eAudioConfig = GetSingletonEntity <AudioConfig>();
            }

            #if ENABLE_PLAYERCONNECTION
            PlayerConnection.instance.Register(k_EditorMuteMessageId, ToggleMuteFromEditor);
            #endif

            ac.initialized = true;
            ac.unlocked    = true;

            EntityManager.SetComponentData(eAudioConfig, ac);
        }
Exemplo n.º 2
0
        public void StartLoad(
            EntityManager entityManager,
            Entity e,
            ref AudioClip audioClip,
            ref AudioNativeClip audioNativeClip,
            ref AudioClipLoadFromFile loader,
            ref AudioNativeLoading nativeLoading)
        {
            if (audioNativeClip.clipID != 0)
            {
                AudioNativeCalls.AbortLoad(audioNativeClip.clipID);
            }

            if (!entityManager.HasComponent <AudioClipLoadFromFileAudioFile>(e))
            {
                audioNativeClip.clipID = 0;
                audioClip.status       = AudioClipStatus.LoadError;
                return;
            }

            string path = entityManager.GetBufferAsString <AudioClipLoadFromFileAudioFile>(e);

            audioNativeClip.clipID = AudioNativeCalls.StartLoad(path);
            audioClip.status       = audioNativeClip.clipID > 0 ? AudioClipStatus.Loading : AudioClipStatus.LoadError;
        }
Exemplo n.º 3
0
        public LoadResult CheckLoading(IntPtr wrapper,
                                       EntityManager man,
                                       Entity e,
                                       ref AudioClip audioClip, ref AudioNativeClip audioNativeClip, ref AudioClipLoadFromFile param, ref AudioNativeLoading nativeLoading)
        {
            LoadResult result = (LoadResult)AudioNativeCalls.CheckLoading(audioNativeClip.clipID);

            if (result == LoadResult.success)
            {
                audioClip.status = AudioClipStatus.Loaded;
#if ENABLE_DOTSRUNTIME_PROFILER
                ProfilerStats.AccumStats.memAudioCount.Accumulate(1);
                int mem = AudioNativeCalls.GetRequiredMemory(audioNativeClip.clipID);
                ProfilerStats.AccumStats.memAudio.Accumulate(mem);
                ProfilerStats.AccumStats.memReservedAudio.Accumulate(mem);
                ProfilerStats.AccumStats.memUsedAudio.Accumulate(mem);

                // All audio clips in native Tiny audio are decompressed on load
                ProfilerStats.AccumStats.audioSampleMemory.Accumulate(mem);
#endif
            }
            else if (result == LoadResult.failed)
            {
                audioClip.status = AudioClipStatus.LoadError;
            }

            return(result);
        }
Exemplo n.º 4
0
        public void FreeNative(EntityManager man, Entity e, ref AudioNativeClip audioNativeClip)
        {
            if (!man.HasComponent <AudioClipUsage>(e))
            {
                return;
            }
            AudioClipUsage audioClipUsage = man.GetComponentData <AudioClipUsage>(e);
            bool           clipIsPlaying  = audioClipUsage.playingRefCount > 0;

            if (clipIsPlaying)
            {
                AudioNativeCalls.SoundSourcePropertyMutexLock();
                AudioNativeCalls.SoundSourceSampleMutexLock();
            }

            AudioNativeCalls.FreeAudio(audioNativeClip.clipID);
            DynamicBuffer <AudioClipCompressed> audioClipCompressed = man.GetBuffer <AudioClipCompressed>(e);

            audioClipCompressed.ResizeUninitialized(0);

            if (clipIsPlaying)
            {
                AudioNativeCalls.SoundSourcePropertyMutexUnlock();
                AudioNativeCalls.SoundSourceSampleMutexUnlock();
            }
        }
Exemplo n.º 5
0
 private void ReinitIfDefaultDeviceChanged()
 {
     if (AudioNativeCalls.HasDefaultDeviceChanged())
     {
         AudioNativeCalls.ReinitAudio();
     }
 }
Exemplo n.º 6
0
        protected override void DestroyAudioSystem()
        {
            #if ENABLE_PLAYERCONNECTION
            PlayerConnection.instance.Unregister(k_EditorMuteMessageId, ToggleMuteFromEditor);
            #endif

            AudioNativeCalls.DestroyAudio();
        }
Exemplo n.º 7
0
 protected static void SetPitch(EntityManager mgr, Entity e, float pitch)
 {
     if (mgr.HasComponent <AudioSourceID>(e))
     {
         AudioSourceID audioSourceID = mgr.GetComponentData <AudioSourceID>(e);
         if (audioSourceID.sourceID > 0)
         {
             AudioNativeCalls.SetPitch(audioSourceID.sourceID, pitch);
         }
     }
 }
Exemplo n.º 8
0
        protected static unsafe uint PlaySource(EntityManager mgr, Entity e)
        {
            if (mgr.HasComponent <AudioSource>(e))
            {
                AudioSource audioSource = mgr.GetComponentData <AudioSource>(e);
                Entity      clipEntity  = audioSource.clip;
                DynamicBuffer <AudioClipUncompressed> audioClipUncompressed = mgr.GetBuffer <AudioClipUncompressed>(clipEntity);

                if (mgr.HasComponent <AudioNativeClip>(clipEntity))
                {
                    AudioNativeClip audioNativeClip = mgr.GetComponentData <AudioNativeClip>(clipEntity);
                    if (audioNativeClip.clipID > 0)
                    {
                        bool decompressOnPlay = false;
                        if (mgr.HasComponent <AudioClip>(clipEntity))
                        {
                            AudioClip audioClip = mgr.GetComponentData <AudioClip>(clipEntity);
                            decompressOnPlay = (audioClip.loadType == AudioClipLoadType.DecompressOnPlay);

                            if (decompressOnPlay && (audioClipUncompressed.Length == 0))
                            {
                                return(0);
                            }

                            audioClip.status = AudioClipStatus.Loaded;
                            mgr.SetComponentData <AudioClip>(clipEntity, audioClip);
                        }

                        // If there is an existing source, it should re-start.
                        // Do this with a Stop() and let it play below.
                        if (mgr.HasComponent <AudioSourceID>(e))
                        {
                            AudioSourceID ans = mgr.GetComponentData <AudioSourceID>(e);
                            AudioNativeCalls.Stop(ans.sourceID);
                        }

                        float volume = audioSource.volume;
                        float pan    = mgr.HasComponent <Audio2dPanning>(e) ? mgr.GetComponentData <Audio2dPanning>(e).pan : 0.0f;

                        // For 3d sounds, we start at volume zero because we don't know if this sound is close or far from the listener.
                        // It is much smoother to ramp up volume from zero than the alternative.
                        if (mgr.HasComponent <Audio3dPanning>(e))
                        {
                            volume = 0.0f;
                        }

                        uint sourceID = AudioNativeCalls.Play(audioNativeClip.clipID, volume, pan, audioSource.loop ? 1 : 0);
                        return(sourceID);
                    }
                }
            }

            return(0);
        }
Exemplo n.º 9
0
 protected static void StopSource(EntityManager mgr, Entity e)
 {
     if (mgr.HasComponent <AudioSourceID>(e))
     {
         AudioSourceID audioSourceID = mgr.GetComponentData <AudioSourceID>(e);
         if (audioSourceID.sourceID > 0)
         {
             AudioNativeCalls.Stop(audioSourceID.sourceID);
         }
     }
 }
Exemplo n.º 10
0
        protected override void InitAudioSystem()
        {
            AudioNativeCalls.InitAudio();

            TinyEnvironment env = World.TinyEnvironment();
            AudioConfig     ac  = env.GetConfigData <AudioConfig>();

            ac.initialized = true;
            ac.unlocked    = true;
            env.SetConfigData(ac);
        }
Exemplo n.º 11
0
 protected override void StopSource(Entity e)
 {
     if (EntityManager.HasComponent <AudioNativeSource>(e))
     {
         AudioNativeSource audioNativeSource = EntityManager.GetComponentData <AudioNativeSource>(e);
         if (audioNativeSource.sourceID > 0)
         {
             AudioNativeCalls.Stop(audioNativeSource.sourceID);
         }
     }
 }
Exemplo n.º 12
0
        protected override bool PlaySource(Entity e)
        {
            var mgr = EntityManager;

            if (mgr.HasComponent <AudioSource>(e))
            {
                AudioSource audioSource = mgr.GetComponentData <AudioSource>(e);

                Entity clipEntity = audioSource.clip;
                if (mgr.HasComponent <AudioNativeClip>(clipEntity))
                {
                    AudioNativeClip clip = mgr.GetComponentData <AudioNativeClip>(clipEntity);
                    if (clip.clipID > 0)
                    {
                        // If there is an existing source, it should re-start.
                        // Do this with a Stop() and let it play below.
                        if (mgr.HasComponent <AudioNativeSource>(e))
                        {
                            AudioNativeSource ans = mgr.GetComponentData <AudioNativeSource>(e);
                            AudioNativeCalls.Stop(ans.sourceID);
                        }

                        float volume = audioSource.volume;
                        float pan    = mgr.HasComponent <Audio2dPanning>(e) ? mgr.GetComponentData <Audio2dPanning>(e).pan : 0.0f;

                        // For 3d sounds, we start at volume zero because we don't know if this sound is close or far from the listener.
                        // It is much smoother to ramp up volume from zero than the alternative.
                        if (mgr.HasComponent <Audio3dPanning>(e))
                        {
                            volume = 0.0f;
                        }

                        uint sourceID = AudioNativeCalls.Play(clip.clipID, volume, pan, audioSource.loop);

                        AudioNativeSource audioNativeSource = new AudioNativeSource()
                        {
                            sourceID = sourceID
                        };
                        if (mgr.HasComponent <AudioNativeSource>(e))
                        {
                            mgr.SetComponentData(e, audioNativeSource);
                        }
                        else
                        {
                            mgr.AddComponentData(e, audioNativeSource);
                        }
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 13
0
        public void FreeNative(EntityManager man, Entity e, ref AudioNativeClip audioNativeClip)
        {
#if ENABLE_DOTSRUNTIME_PROFILER
            ProfilerStats.AccumStats.memAudioCount.Accumulate(-1);
            int mem = AudioNativeCalls.GetRequiredMemory(audioNativeClip.clipID);
            ProfilerStats.AccumStats.memAudio.Accumulate(-mem);
            ProfilerStats.AccumStats.memReservedAudio.Accumulate(-mem);
            ProfilerStats.AccumStats.memUsedAudio.Accumulate(-mem);

            ProfilerStats.AccumStats.audioSampleMemory.Accumulate(-mem);
#endif
            AudioNativeCalls.FreeAudio(audioNativeClip.clipID);
        }
Exemplo n.º 14
0
        protected static int IsPlaying(EntityManager mgr, Entity e)
        {
            if (mgr.HasComponent <AudioSourceID>(e))
            {
                AudioSourceID audioSourceID = mgr.GetComponentData <AudioSourceID>(e);
                if (audioSourceID.sourceID > 0)
                {
                    return(AudioNativeCalls.IsPlaying(audioSourceID.sourceID));
                }
            }

            return(0);
        }
Exemplo n.º 15
0
        protected override bool SetPitch(Entity e, float pitch)
        {
            if (EntityManager.HasComponent <AudioNativeSource>(e))
            {
                AudioNativeSource audioNativeSource = EntityManager.GetComponentData <AudioNativeSource>(e);
                if (audioNativeSource.sourceID > 0)
                {
                    return(AudioNativeCalls.SetPitch(audioNativeSource.sourceID, pitch));
                }
            }

            return(false);
        }
Exemplo n.º 16
0
        protected override bool IsPlaying(Entity e)
        {
            if (EntityManager.HasComponent <AudioNativeSource>(e))
            {
                AudioNativeSource audioNativeSource = EntityManager.GetComponentData <AudioNativeSource>(e);
                if (audioNativeSource.sourceID > 0)
                {
                    return(AudioNativeCalls.IsPlaying(audioNativeSource.sourceID));
                }
            }

            return(false);
        }
Exemplo n.º 17
0
        protected override void DestroyAudioSystem()
        {
            #if ENABLE_PLAYERCONNECTION
            PlayerConnection.instance.Unregister(k_EditorMuteMessageId, ToggleMuteFromEditor);
            #endif

            AudioNativeCalls.DestroyAudio();

            if (HasSingleton <AudioConfig>())
            {
                EntityManager.DestroyEntity(GetSingletonEntity <AudioConfig>());
            }
        }
Exemplo n.º 18
0
        private void ReinitIfNoAudioConsumed(bool paused)
        {
            const double reinitTime = 0.25;
            double       worldTime  = World.Time.ElapsedTime;
            ulong        audioOutputTimeInFrames = AudioNativeCalls.GetAudioOutputTimeInFrames();
            bool         audioConsumed           = audioOutputTimeInFrames != m_lastAudioOutputTimeInFrames;
            bool         audioNeedsReinit        = worldTime - m_lastWorldTimeAudioConsumed >= reinitTime;

            if (!audioConsumed && !paused && audioNeedsReinit)
            {
                AudioNativeCalls.ReinitAudio();
            }

            m_lastAudioOutputTimeInFrames = audioOutputTimeInFrames;
            m_lastWorldTimeAudioConsumed  = (audioConsumed || paused || audioNeedsReinit) ? worldTime : m_lastWorldTimeAudioConsumed;
        }
Exemplo n.º 19
0
        protected override bool PlaySource(Entity e)
        {
            var mgr = EntityManager;

            if (mgr.HasComponent <AudioSource>(e))
            {
                AudioSource audioSource = mgr.GetComponentData <AudioSource>(e);

                if (mgr.HasComponent <AudioNativeSource>(e))
                {
                    // If there is a native source and it is IsPlaying() then
                    // can't play another, but we are done. (So return true.)
                    // Note that IsPlaying() is synchronous (which is what we want)
                    // as opposed to isPlaying which is async.
                    AudioNativeSource ans = mgr.GetComponentData <AudioNativeSource>(e);
                    if (AudioNativeCalls.IsPlaying(ans.sourceID))
                    {
                        return(true);
                    }
                }

                Entity clipEntity = audioSource.clip;
                if (mgr.HasComponent <AudioNativeClip>(clipEntity))
                {
                    AudioNativeClip clip = mgr.GetComponentData <AudioNativeClip>(clipEntity);
                    if (clip.clipID > 0)
                    {
                        uint sourceID = AudioNativeCalls.Play(clip.clipID, audioSource.volume, audioSource.loop);
                        AudioNativeSource audioNativeSource = new AudioNativeSource()
                        {
                            sourceID = sourceID
                        };
                        if (mgr.HasComponent <AudioNativeSource>(e))
                        {
                            mgr.SetComponentData(e, audioNativeSource);
                        }
                        else
                        {
                            PostUpdateCommands.AddComponent(e, audioNativeSource);
                        }
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 20
0
        public unsafe void StartLoad(
            EntityManager entityManager,
            Entity e,
            ref AudioClip audioClip,
            ref AudioNativeClip audioNativeClip,
            ref AudioClipLoadFromFile loader,
            ref AudioNativeLoading nativeLoading)
        {
            if (audioNativeClip.clipID != 0)
            {
                AudioNativeCalls.AbortLoad(audioNativeClip.clipID);
            }

            if (!entityManager.HasComponent <AudioClipLoadFromFileAudioFile>(e))
            {
                audioNativeClip.clipID = 0;
                audioClip.status       = AudioClipStatus.LoadError;
                return;
            }

            DynamicBuffer <AudioClipUncompressed> audioClipUncompressed = entityManager.GetBuffer <AudioClipUncompressed>(e);

            if (audioClipUncompressed.Length > 0)
            {
                return;
            }

            string path = entityManager.GetBufferAsString <AudioClipLoadFromFileAudioFile>(e);

            if (path[0] == '!')
            {
                // This is a special path name that is used to load up a fake audio asset for our automated tests.
                audioNativeClip.clipID = AudioNativeCalls.StartLoadFromDisk(path);
                audioClip.status       = audioNativeClip.clipID > 0 ? AudioClipStatus.Loading : AudioClipStatus.LoadError;
            }
            else
            {
                // Read the audio clip from disk into an AudioClipCompressed component.
                LoadSoundClipFromDisk(entityManager, e, path);
                DynamicBuffer <AudioClipCompressed> audioClipCompressed = entityManager.GetBuffer <AudioClipCompressed>(e);

                audioNativeClip.clipID = AudioNativeCalls.StartLoadFromMemory(audioClipCompressed.GetUnsafeReadOnlyPtr(), audioClipCompressed.Length);
                audioClip.status       = audioNativeClip.clipID > 0 ? AudioClipStatus.Loading : AudioClipStatus.LoadError;
            }
        }
Exemplo n.º 21
0
        public LoadResult CheckLoading(IntPtr wrapper,
                                       EntityManager man,
                                       Entity e,
                                       ref AudioClip audioClip, ref AudioNativeClip audioNativeClip, ref AudioClipLoadFromFile param, ref AudioNativeLoading nativeLoading)
        {
            LoadResult result = (LoadResult)AudioNativeCalls.CheckLoading(audioNativeClip.clipID);

            if (result == LoadResult.success)
            {
                audioClip.status = AudioClipStatus.Loaded;
            }
            else if (result == LoadResult.failed)
            {
                audioClip.status = AudioClipStatus.LoadError;
            }

            return(result);
        }
Exemplo n.º 22
0
        protected override bool PlaySource(Entity e)
        {
            var mgr = EntityManager;

            if (mgr.HasComponent <AudioSource>(e))
            {
                AudioSource audioSource = mgr.GetComponentData <AudioSource>(e);

                Entity clipEntity = audioSource.clip;
                if (mgr.HasComponent <AudioNativeClip>(clipEntity))
                {
                    AudioNativeClip clip = mgr.GetComponentData <AudioNativeClip>(clipEntity);
                    if (clip.clipID > 0)
                    {
                        // If there is an existing source, it should re-start.
                        // Do this with a Stop() and let it play below.
                        if (mgr.HasComponent <AudioNativeSource>(e))
                        {
                            AudioNativeSource ans = mgr.GetComponentData <AudioNativeSource>(e);
                            AudioNativeCalls.Stop(ans.sourceID);
                        }

                        uint sourceID = AudioNativeCalls.Play(clip.clipID, audioSource.volume, audioSource.loop);

                        AudioNativeSource audioNativeSource = new AudioNativeSource()
                        {
                            sourceID = sourceID
                        };
                        if (mgr.HasComponent <AudioNativeSource>(e))
                        {
                            mgr.SetComponentData(e, audioNativeSource);
                        }
                        else
                        {
                            PostUpdateCommands.AddComponent(e, audioNativeSource);
                        }
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemplo n.º 23
0
        protected override void OnUpdate()
        {
            base.OnUpdate();

            TinyEnvironment env = World.TinyEnvironment();
            AudioConfig     ac  = env.GetConfigData <AudioConfig>();

            AudioNativeCalls.PauseAudio(ac.paused);

            EntityCommandBuffer ecb = new EntityCommandBuffer(Allocator.Temp);

            Entities
            .WithNone <AudioClipLoadFromFileAudioFile>()
            .ForEach((Entity e, ref AudioNativeClip tag) =>
            {
                AudioNativeCalls.FreeAudio(tag.clipID);
                ecb.RemoveComponent <AudioNativeClip>(e);
            });
            ecb.Playback(EntityManager);
            ecb.Dispose();
        }
Exemplo n.º 24
0
        protected override void OnUpdate()
        {
            base.OnUpdate();

            var         mgr = EntityManager;
            AudioConfig ac  = GetSingleton <AudioConfig>();

            AudioNativeCalls.PauseAudio(ac.paused);

            ReinitIfDefaultDeviceChanged();
            ReinitIfNoAudioConsumed(ac.paused);

            // Remove any lingering AudioNativeSources which may have had their AudioSource
            // removed, otherwise we will continue playing looping clips
            Entities
            .WithStructuralChanges()
            .WithNone <AudioSource>()
            .ForEach((Entity e, ref AudioNativeSource source) =>
            {
                AudioNativeCalls.Stop(source.sourceID);
                mgr.RemoveComponent <AudioNativeSource>(e);
            }).Run();

            // Remove any lingering AudioNativeClips which may have had their AudioClip
            // removed, otherwise we will continue holding on to those resources
            Entities
            .WithStructuralChanges()
            .WithNone <AudioClipLoadFromFileAudioFile>()
            .ForEach((Entity e, ref AudioNativeClip tag) =>
            {
                AudioNativeCalls.FreeAudio(tag.clipID);
                mgr.RemoveComponent <AudioNativeClip>(e);
            }).Run();

#if ENABLE_DOTSRUNTIME_PROFILER
            ProfilerStats.AccumStats.audioDspCPUx10.value = (long)(AudioNativeCalls.GetCpuUsage() * 10);
#endif
        }
Exemplo n.º 25
0
 protected override void DestroyAudioSystem()
 {
     AudioNativeCalls.DestroyAudio();
 }
Exemplo n.º 26
0
 static void ToggleMuteFromEditor(MessageEventArgs args)
 {
     s_Muted = !s_Muted;
     AudioNativeCalls.SetIsMuted(s_Muted);
 }
Exemplo n.º 27
0
 public void FinishLoading(EntityManager man, Entity e, ref AudioClip audioClip, ref AudioNativeClip audioNativeClip, ref AudioNativeLoading nativeLoading)
 {
     AudioNativeCalls.FinishedLoading(audioNativeClip.clipID);
 }
Exemplo n.º 28
0
 public void FreeNative(EntityManager man, Entity e, ref AudioNativeClip audioNativeClip)
 {
     AudioNativeCalls.FreeAudio(audioNativeClip.clipID);
 }
Exemplo n.º 29
0
        protected override unsafe void OnUpdate()
        {
            var         mgr         = EntityManager;
            Entity      audioEntity = m_audioEntity;
            double      currentTime = World.Time.ElapsedTime;
            int         uncompressedAudioMemoryBytes = m_uncompressedAudioMemoryBytes;
            double      worldElapsedTime             = World.Time.ElapsedTime;
            AudioConfig ac = GetSingleton <AudioConfig>();

            NativeList <Entity> entitiesPlayed = new NativeList <Entity>(Allocator.Temp);

            base.OnUpdate();

            AudioNativeCalls.PauseAudio(ac.paused);
            ReinitIfDefaultDeviceChanged();
            ReinitIfNoAudioConsumed(ac.paused);

            // We are starting to make AudioSource play/stop and property changes, so block the audio mixer thread from doing any work
            // on this state until we are done.
            AudioNativeCalls.SoundSourcePropertyMutexLock();

            for (int i = 0; i < mgr.GetBuffer <SourceIDToStop>(audioEntity).Length; i++)
            {
                uint id = mgr.GetBuffer <SourceIDToStop>(audioEntity)[i];
                AudioNativeCalls.Stop(id);
            }

            // Play sounds.
            Entities
            .WithAll <AudioSource, AudioSourceStart>()
            .ForEach((Entity e) =>
            {
                uint sourceID = PlaySource(mgr, e);
                if (sourceID > 0)
                {
                    AudioSourceID audioSourceID = mgr.GetComponentData <AudioSourceID>(e);
                    audioSourceID.sourceID      = sourceID;
                    mgr.SetComponentData <AudioSourceID>(e, audioSourceID);

                    entitiesPlayed.Add(e);
                }
            }).Run();

            for (int i = 0; i < entitiesPlayed.Length; i++)
            {
                mgr.RemoveComponent <AudioSourceStart>(entitiesPlayed[i]);
            }

            Entities
            .ForEach((Entity e, ref AudioClipUsage audioClipUsage) =>
            {
                audioClipUsage.playingRefCount = 0;
            }).Run();

            // Re-calculate the playing ref count for each audio clip. Also, update AudioSource's isPlaying bool and remove
            // any AudioSource entities from the list if they are no longer playing.
            Entities
            .ForEach((Entity e, in AudioSource audioSource) =>
            {
                bool audioSourceStarting = mgr.HasComponent <AudioSourceStart>(e);
                if (audioSourceStarting || audioSource.isPlaying)
                {
                    Entity clipEntity             = audioSource.clip;
                    AudioClipUsage audioClipUsage = mgr.GetComponentData <AudioClipUsage>(clipEntity);
                    audioClipUsage.playingRefCount++;
                    audioClipUsage.lastTimeUsed = currentTime;
                    mgr.SetComponentData <AudioClipUsage>(clipEntity, audioClipUsage);
                }
            }).Run();

            if (uncompressedAudioMemoryBytes > ac.maxUncompressedAudioMemoryBytes)
            {
                Entities
                .ForEach((Entity e, ref DynamicBuffer <AudioClipUncompressed> audioClipUncompressed) =>
                {
                    if (audioClipUncompressed.Length > 0)
                    {
                        AudioClipUsage audioClipUsage = mgr.GetComponentData <AudioClipUsage>(e);

                        bool notRecentlyUsed = (audioClipUsage.lastTimeUsed + 15.0f < currentTime);
                        bool largeAudioAsset = audioClipUncompressed.Length > 2 * 1024 * 1024;

                        if ((uncompressedAudioMemoryBytes > ac.maxUncompressedAudioMemoryBytes) &&
                            (audioClipUsage.playingRefCount <= 0) &&
                            (notRecentlyUsed || largeAudioAsset))
                        {
                            int clipUncompressedAudioMemoryBytes = audioClipUncompressed.Length * sizeof(short);

                            AudioNativeClip audioNativeClip = mgr.GetComponentData <AudioNativeClip>(e);
                            AudioNativeCalls.SetUncompressedMemory(audioNativeClip.clipID, (IntPtr)null, 0);

                            AudioClip audioClip = mgr.GetComponentData <AudioClip>(e);
                            audioClip.status    = AudioClipStatus.Loading;
                            mgr.SetComponentData <AudioClip>(e, audioClip);

                            audioClipUncompressed.ResizeUninitialized(0);
                            uncompressedAudioMemoryBytes -= clipUncompressedAudioMemoryBytes;
                        }
                    }
                }).Run();

                Entities
                .ForEach((Entity e, ref DynamicBuffer <AudioClipUncompressed> audioClipUncompressed) =>
                {
                    if (audioClipUncompressed.Length > 0)
                    {
                        AudioClipUsage audioClipUsage = mgr.GetComponentData <AudioClipUsage>(e);

                        if ((uncompressedAudioMemoryBytes > ac.maxUncompressedAudioMemoryBytes) &&
                            (audioClipUsage.playingRefCount <= 0))
                        {
                            int clipUncompressedAudioMemoryBytes = audioClipUncompressed.Length * sizeof(short);

                            AudioNativeClip audioNativeClip = mgr.GetComponentData <AudioNativeClip>(e);
                            AudioNativeCalls.SetUncompressedMemory(audioNativeClip.clipID, (IntPtr)null, 0);

                            AudioClip audioClip = mgr.GetComponentData <AudioClip>(e);
                            audioClip.status    = AudioClipStatus.Loading;
                            mgr.SetComponentData <AudioClip>(e, audioClip);

                            audioClipUncompressed.ResizeUninitialized(0);
                            uncompressedAudioMemoryBytes -= clipUncompressedAudioMemoryBytes;
                        }
                    }
                }).Run();
            }

            DynamicBuffer <EntityPlaying> entitiesPlaying = mgr.GetBuffer <EntityPlaying>(m_audioEntity);

            for (int i = 0; i < entitiesPlaying.Length; i++)
            {
                Entity      e           = entitiesPlaying[i];
                AudioSource audioSource = mgr.GetComponentData <AudioSource>(e);

                audioSource.isPlaying = (IsPlaying(mgr, e) == 1) ? true : false;
                mgr.SetComponentData <AudioSource>(e, audioSource);

                if (audioSource.isPlaying)
                {
                    float volume = audioSource.volume;
                    if (mgr.HasComponent <AudioDistanceAttenuation>(e))
                    {
                        AudioDistanceAttenuation distanceAttenuation = mgr.GetComponentData <AudioDistanceAttenuation>(e);
                        volume *= distanceAttenuation.volume;
                    }
                    SetVolume(mgr, e, volume);

                    if (mgr.HasComponent <Audio3dPanning>(e))
                    {
                        Audio3dPanning panning = mgr.GetComponentData <Audio3dPanning>(e);
                        SetPan(mgr, e, panning.pan);
                    }
                    else if (mgr.HasComponent <Audio2dPanning>(e))
                    {
                        Audio2dPanning panning = mgr.GetComponentData <Audio2dPanning>(e);
                        SetPan(mgr, e, panning.pan);
                    }

                    if (mgr.HasComponent <AudioPitch>(e))
                    {
                        AudioPitch pitchEffect = mgr.GetComponentData <AudioPitch>(e);
                        float      pitch       = (pitchEffect.pitch > 0.0f) ? pitchEffect.pitch : 1.0f;
                        SetPitch(mgr, e, pitch);
                    }
                }
            }

            // We are done making AudioSource property changes, so unblock the audio mixer thread.
            AudioNativeCalls.SoundSourcePropertyMutexUnlock();

#if ENABLE_DOTSRUNTIME_PROFILER
            ProfilerStats.GatheredStats |= ProfilerModes.ProfileAudio;
            ProfilerStats.AccumStats.audioDspCPUx10.value = (long)(AudioNativeCalls.GetCpuUsage() * 10);

            ProfilerStats.AccumStats.memAudioCount.value         = 0;
            ProfilerStats.AccumStats.memAudio.value              = 0;
            ProfilerStats.AccumStats.memReservedAudio.value      = 0;
            ProfilerStats.AccumStats.memUsedAudio.value          = 0;
            ProfilerStats.AccumStats.audioStreamFileMemory.value = 0;
            ProfilerStats.AccumStats.audioSampleMemory.value     = 0;

            Entities
            .ForEach((Entity e, in DynamicBuffer <AudioClipCompressed> audioClipCompressed, in DynamicBuffer <AudioClipUncompressed> audioClipUncompressed) =>
            {
                int audioClipCompressedBytes   = audioClipCompressed.Length;
                int audioClipUncompressedBytes = audioClipUncompressed.Length * sizeof(short);
                int audioClipTotalBytes        = audioClipCompressedBytes + audioClipUncompressedBytes;

                ProfilerStats.AccumStats.memAudioCount.Accumulate(1);
                ProfilerStats.AccumStats.memAudio.Accumulate(audioClipTotalBytes);
                ProfilerStats.AccumStats.memReservedAudio.Accumulate(audioClipTotalBytes);
                ProfilerStats.AccumStats.memUsedAudio.Accumulate(audioClipTotalBytes);
                ProfilerStats.AccumStats.audioSampleMemory.Accumulate(audioClipTotalBytes);
            }).Run();
Exemplo n.º 30
0
 public void OnSuspendResume(object sender, SuspendResumeEvent evt)
 {
     AudioNativeCalls.PauseAudio(evt.Suspend);
 }