public void ApplyMediaStateOptions(Actor actor, AudioSource soundInstance, MediaStateOptions options, Guid id, bool startSound)
        {
            if (options != null)
            {
                //pause must happen before other sound state changes
                if (options.paused != null && options.paused.Value == true)
                {
                    if (_unpausedSoundInstances.RemoveAll(x => x.id == id) > 0)
                    {
                        soundInstance.Pause();
                    }
                }

                if (options.Volume != null)
                {
                    soundInstance.volume = options.Volume.Value;
                }
                if (options.Pitch != null)
                {
                    //convert from halftone offset (-12/0/12/24/36) to pitch multiplier (0.5/1/2/4/8).
                    soundInstance.pitch = Mathf.Pow(2.0f, (options.Pitch.Value / 12.0f));
                }
                if (options.Looping != null)
                {
                    soundInstance.loop = options.Looping.Value;
                }
                if (options.Doppler != null)
                {
                    soundInstance.dopplerLevel = options.Doppler.Value;
                }
                if (options.Spread != null)
                {
                    soundInstance.spread = options.Spread.Value * 180.0f;
                }
                if (options.RolloffStartDistance != null)
                {
                    soundInstance.minDistance = options.RolloffStartDistance.Value;
                    soundInstance.maxDistance = options.RolloffStartDistance.Value * 1000000.0f;
                }
                if (options.Time != null)
                {
                    soundInstance.time = options.Time.Value;
                }

                //unpause must happen after other sound state changes
                if (!startSound)
                {
                    if (options.paused != null && options.paused.Value == false)
                    {
                        if (!_unpausedSoundInstances.Exists(x => x.id == id))
                        {
                            soundInstance.UnPause();
                            _unpausedSoundInstances.Add(new SoundInstance(id, actor));
                        }
                    }
                }
            }
        }
        public void ApplyMediaStateOptions(Actor actor, AudioStreamPlayer3D soundInstance, MediaStateOptions options, Guid id, bool startSound)
        {
            if (options != null)
            {
                //pause must happen before other sound state changes
                if (options.paused != null && options.paused.Value == true)
                {
                    if (_unpausedSoundInstances.RemoveAll(x => x.id == id) > 0)
                    {
                        soundInstance.StreamPaused = true;
                    }
                }

                if (options.Volume != null)
                {
                    soundInstance.UnitSize = options.Volume.Value;
                }
                if (options.Pitch != null)
                {
                    //convert from halftone offset (-12/0/12/24/36) to pitch multiplier (0.5/1/2/4/8).
                    soundInstance.PitchScale = Mathf.Pow(2.0f, (options.Pitch.Value / 12.0f));
                }
                if (options.Looping != null)
                {
                    if (soundInstance.Stream is AudioStreamSample audioStreamSample)
                    {
                        audioStreamSample.LoopMode = options.Looping.Value ? AudioStreamSample.LoopModeEnum.Forward : AudioStreamSample.LoopModeEnum.Disabled;
                        int d = 1;
                        d *= audioStreamSample.Stereo ? 2 : 1;
                        d *= audioStreamSample.Format == AudioStreamSample.FormatEnum.Format16Bits ? 2 : 1;
                        audioStreamSample.LoopEnd = audioStreamSample.Data.Length / d;
                    }
                    else if (soundInstance.Stream is AudioStreamOGGVorbis audioStreamOGGVorbis)
                    {
                        //FIXME
                    }
                }
                if (options.Doppler != null)
                {
                    soundInstance.DopplerTracking = Mathf.IsZeroApprox(options.Doppler.Value)
                                                ? AudioStreamPlayer3D.DopplerTrackingEnum.Disabled : AudioStreamPlayer3D.DopplerTrackingEnum.PhysicsStep;
                }
                if (options.Spread != null)
                {
                    //Spread not support in Godot.
                    //soundInstance.EmissionAngleDegrees = options.Spread.Value * 90.0f;
                }
                if (options.RolloffStartDistance != null)
                {
                    soundInstance.MaxDistance = options.RolloffStartDistance.Value * 1000000.0f;
                }
                if (options.Time != null)
                {
                    soundInstance.Seek(options.Time.Value);
                }

                //unpause must happen after other sound state changes
                if (!startSound)
                {
                    if (options.paused != null && options.paused.Value == false)
                    {
                        if (!_unpausedSoundInstances.Exists(x => x.id == id))
                        {
                            soundInstance.StreamPaused = false;
                            _unpausedSoundInstances.Add(new SoundInstance(id, actor));
                        }
                    }
                }
            }
        }
        public AudioStreamPlayer3D AddSoundInstance(Actor actor, Guid id, AudioStream audioClip, MediaStateOptions options)
        {
            float offset = options.Time.GetValueOrDefault();

            if (options.Looping != null && options.Looping.Value && audioClip.GetLength() != 0.0f)
            {
                offset = offset % audioClip.GetLength();
            }
            if (offset < audioClip.GetLength())
            {
                var soundInstance = new AudioStreamPlayer3D();
                actor.AddChild(soundInstance);
                soundInstance.Stream = audioClip;
                soundInstance.Seek(offset);
                soundInstance.UnitSize             = 1.0f;
                soundInstance.EmissionAngleDegrees = 45.0f;                   //only affects multichannel sounds. Default to 50% spread, 50% stereo.
                soundInstance.MaxDistance          = 1000000.0f;
                ApplyMediaStateOptions(actor, soundInstance, options, id, true);
                if (options.paused != null && options.paused.Value == true)
                {
                    //start as paused
                    soundInstance.Play();
                    soundInstance.StreamPaused = true;
                }
                else
                {
                    //start as unpaused
                    _unpausedSoundInstances.Add(new SoundInstance(id, actor));
                    soundInstance.Play();
                    soundInstance.StreamPaused = false;
                }
                return(soundInstance);
            }
            return(null);
        }
        public AudioSource AddSoundInstance(Actor actor, Guid id, AudioClip audioClip, MediaStateOptions options)
        {
            float offset = options.Time.GetValueOrDefault();

            if (options.Looping != null && options.Looping.Value && audioClip.length != 0.0f)
            {
                offset = offset % audioClip.length;
            }
            if (offset < audioClip.length)
            {
                var soundInstance = actor.gameObject.AddComponent <AudioSource>();
                soundInstance.clip         = audioClip;
                soundInstance.time         = offset;
                soundInstance.spatialBlend = 1.0f;
                soundInstance.spread       = 90.0f; //only affects multichannel sounds. Default to 50% spread, 50% stereo.
                soundInstance.minDistance  = 1.0f;
                soundInstance.maxDistance  = 1000000.0f;
                ApplyMediaStateOptions(actor, soundInstance, options, id, true);
                if (options.paused != null && options.paused.Value == true)
                {
                    //start as paused
                    soundInstance.Play();
                    soundInstance.Pause();
                }
                else
                {
                    //start as unpaused
                    _unpausedSoundInstances.Add(new SoundInstance(id, actor));
                    soundInstance.Play();
                }
                return(soundInstance);
            }
            return(null);
        }