Exemplo n.º 1
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 <AudioHTMLClip>(clipEntity))
                {
                    AudioHTMLClip clip = mgr.GetComponentData <AudioHTMLClip>(clipEntity);
                    if (clip.clipID > 0)
                    {
                        if (!unlocked)
                        {
                            AudioHTMLNativeCalls.Unlock();
                            unlocked = AudioHTMLNativeCalls.IsUnlocked();
                            if (unlocked)
                            {
                                TinyEnvironment env = World.TinyEnvironment();
                                AudioConfig     ac  = env.GetConfigData <AudioConfig>();
                                ac.unlocked = unlocked;
                                env.SetConfigData(ac);
                            }
                        }

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

                            int sourceID = ++IDPool.sourceID;
                            AudioHTMLNativeCalls.Play(clip.clipID, sourceID, audioSource.volume, audioSource.loop);
                            AudioHTMLSource audioNativeSource = new AudioHTMLSource()
                            {
                                sourceID = sourceID
                            };
                            // Need a native source as well.
                            if (mgr.HasComponent <AudioHTMLSource>(e))
                            {
                                mgr.SetComponentData(e, audioNativeSource);
                            }
                            else
                            {
                                PostUpdateCommands.AddComponent(e, audioNativeSource);
                            }

                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Exemplo n.º 2
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)
         {
             AudioHTMLNativeCalls.SetPitch((int)audioSourceID.sourceID, pitch);
         }
     }
 }
Exemplo n.º 3
0
 protected static void StopSource(EntityManager mgr, Entity e)
 {
     if (mgr.HasComponent <AudioSourceID>(e))
     {
         AudioSourceID audioSourceID = mgr.GetComponentData <AudioSourceID>(e);
         if (audioSourceID.sourceID > 0)
         {
             AudioHTMLNativeCalls.Stop((int)audioSourceID.sourceID, 1);
         }
     }
 }
Exemplo n.º 4
0
 protected override void StopSource(Entity e)
 {
     if (EntityManager.HasComponent <AudioHTMLSource>(e))
     {
         AudioHTMLSource audioNativeSource = EntityManager.GetComponentData <AudioHTMLSource>(e);
         if (audioNativeSource.sourceID > 0)
         {
             AudioHTMLNativeCalls.Stop(audioNativeSource.sourceID, true);
         }
     }
 }
Exemplo n.º 5
0
 protected override bool IsPlaying(Entity e)
 {
     if (EntityManager.HasComponent <AudioHTMLSource>(e))
     {
         AudioHTMLSource audioHtmlSource = EntityManager.GetComponentData <AudioHTMLSource>(e);
         if (audioHtmlSource.sourceID > 0)
         {
             return(AudioHTMLNativeCalls.IsPlaying(audioHtmlSource.sourceID));
         }
     }
     return(false);
 }
Exemplo n.º 6
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(AudioHTMLNativeCalls.IsPlaying((int)audioSourceID.sourceID));
                }
            }

            return(0);
        }
Exemplo n.º 7
0
        protected override void OnStartRunning()
        {
            AudioHTMLNativeCalls.Unlock();
            unlocked = AudioHTMLNativeCalls.IsUnlocked();
            //Console.WriteLine("(re) checking un-locked: ");
            //Console.WriteLine(unlocked ? "true" : "false");

            AudioConfig ac = GetSingleton <AudioConfig>();

            ac.initialized = true;
            ac.unlocked    = unlocked;
            SetSingleton <AudioConfig>(ac);
        }
Exemplo n.º 8
0
        public LoadResult CheckLoading(IntPtr cppWrapper, EntityManager man, Entity e, ref AudioClip audioClip, ref AudioHTMLClip audioNative, ref AudioClipLoadFromFile param, ref AudioHTMLLoading loading)
        {
            LoadResult result = (LoadResult)AudioHTMLNativeCalls.CheckLoad(audioNative.clipID);

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

            return(result);
        }
Exemplo n.º 9
0
        protected override void InitAudioSystem()
        {
            //Console.WriteLine("InitAudioSystem()");
            AudioHTMLNativeCalls.Init();
            unlocked = AudioHTMLNativeCalls.IsUnlocked();
            //Console.WriteLine("(re) checking un-locked: ");
            //Console.WriteLine(unlocked ? "true" : "false");

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

            ac.initialized = true;
            ac.unlocked    = unlocked;
            env.SetConfigData(ac);
        }
Exemplo n.º 10
0
        protected static int PlaySource(EntityManager mgr, Entity e)
        {
            if (mgr.HasComponent <AudioSource>(e))
            {
                AudioSource audioSource = mgr.GetComponentData <AudioSource>(e);

                Entity clipEntity = audioSource.clip;
                if (mgr.HasComponent <AudioHTMLClip>(clipEntity))
                {
                    AudioHTMLClip clip = mgr.GetComponentData <AudioHTMLClip>(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 <AudioSourceID>(e))
                        {
                            AudioSourceID ans = mgr.GetComponentData <AudioSourceID>(e);
                            AudioHTMLNativeCalls.Stop((int)ans.sourceID, 1);
                        }

                        float volume = audioSource.volume;
                        float pan    = mgr.HasComponent <Audio2dPanning>(e) ? mgr.GetComponentData <Audio2dPanning>(e).pan : 0.0f;
                        float pitch  = mgr.HasComponent <AudioPitch>(e) ? mgr.GetComponentData <AudioPitch>(e).pitch : 1.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;
                        }

                        int sourceID = ++SharedIDPool.Value.Data.sourceID;

                        // Check the return value from Play because it fails sometimes at startup for AudioSources with PlayOnAwake set to true.
                        // If initial attempt fails, try again next frame.
                        if (AudioHTMLNativeCalls.Play(clip.clipID, sourceID, volume, pitch, pan, audioSource.loop ? 1 : 0) == 0)
                        {
                            return(0);
                        }

                        return(sourceID);
                    }
                }
            }

            return(0);
        }
Exemplo n.º 11
0
        protected override void OnUpdate()
        {
            base.OnUpdate();
            TinyEnvironment env = World.TinyEnvironment();
            AudioConfig     ac  = env.GetConfigData <AudioConfig>();

            if (ac.paused != paused)
            {
                paused = ac.paused;
                if (paused)
                {
                    AudioHTMLNativeCalls.Pause();
                }
                else
                {
                    AudioHTMLNativeCalls.Resume();
                }
            }
        }
Exemplo n.º 12
0
        public void StartLoad(
            EntityManager entityManager,
            Entity e,
            ref AudioClip audioClip,
            ref AudioHTMLClip audioHtmlClip,
            ref AudioClipLoadFromFile loader,
            ref AudioHTMLLoading loading)
        {
            if (!entityManager.HasComponent <AudioClipLoadFromFileAudioFile>(e))
            {
                audioHtmlClip.clipID = 0;
                audioClip.status     = AudioClipStatus.LoadError;
                return;
            }

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

            audioHtmlClip.clipID = ++SharedIDPool.Value.Data.clipID;
            AudioHTMLNativeCalls.StartLoad(path, audioHtmlClip.clipID);
            audioClip.status = AudioClipStatus.Loading;
        }
Exemplo n.º 13
0
 protected override void InitAudioSystem()
 {
     //Console.WriteLine("InitAudioSystem()");
     AudioHTMLNativeCalls.Init();
 }
Exemplo n.º 14
0
        protected override void OnUpdate()
        {
            EntityManager       mgr            = EntityManager;
            Entity              audioEntity    = m_audioEntity;
            NativeList <Entity> entitiesPlayed = new NativeList <Entity>(Allocator.Temp);

            base.OnUpdate();

            AudioConfig ac = GetSingleton <AudioConfig>();

            if (!unlocked)
            {
                unlocked    = AudioHTMLNativeCalls.IsUnlocked();
                ac.unlocked = unlocked;
                SetSingleton <AudioConfig>(ac);
            }

            if (ac.paused)
            {
                AudioHTMLNativeCalls.Pause();
            }
            else
            {
                AudioHTMLNativeCalls.Resume();
            }

            AudioHTMLNativeCalls.SetUncompressedAudioMemoryBytesMax(ac.maxUncompressedAudioMemoryBytes);

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

            // Play sounds.
            if (unlocked)
            {
                Entities
                .WithAll <AudioSource, AudioSourceStart>()
                .ForEach((Entity e) =>
                {
                    uint sourceID = (uint)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]);
                }
            }

            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);
                    }
                }
            }

#if ENABLE_DOTSRUNTIME_PROFILER
            ProfilerStats.AccumStats.memAudioCount.value         = 0;
            ProfilerStats.AccumStats.memAudio.value              = 0;
            ProfilerStats.AccumStats.memReservedAudio.value      = 0;
            ProfilerStats.AccumStats.memUsedAudio.value          = 0;
            ProfilerStats.AccumStats.memReservedExternal.value   = 0;
            ProfilerStats.AccumStats.memUsedExternal.value       = 0;
            ProfilerStats.AccumStats.audioStreamFileMemory.value = 0;
            ProfilerStats.AccumStats.audioSampleMemory.value     = 0;

            Entities.ForEach((Entity e, in AudioHTMLClip audioHTMLClip) =>
            {
                int memUncompressedBytes = AudioHTMLNativeCalls.GetRequiredMemoryUncompressed(audioHTMLClip.clipID);
                int memCompressedBytes   = AudioHTMLNativeCalls.GetRequiredMemoryCompressed(audioHTMLClip.clipID);
                long memTotalBytes       = memUncompressedBytes + memCompressedBytes;

                ProfilerStats.AccumStats.memAudioCount.Accumulate(1);
                ProfilerStats.AccumStats.memAudio.Accumulate(memTotalBytes);
                ProfilerStats.AccumStats.memReservedAudio.Accumulate(memTotalBytes);
                ProfilerStats.AccumStats.memUsedAudio.Accumulate(memTotalBytes);
                ProfilerStats.AccumStats.memReservedExternal.Accumulate(memTotalBytes);
                ProfilerStats.AccumStats.memUsedExternal.Accumulate(memTotalBytes);
                ProfilerStats.AccumStats.audioSampleMemory.Accumulate(memTotalBytes);
            }).Run();
#endif

            AudioHTMLNativeCalls.Update();
        }
Exemplo n.º 15
0
 public void FreeNative(EntityManager man, Entity e, ref AudioHTMLClip audioNative)
 {
     AudioHTMLNativeCalls.Free(audioNative.clipID);
 }