Example #1
0
        private void OnDisposed(bool manually)
        {
            if (manually)
            {
                this.attachedTo  = null;
                this.curPriority = -1;

                if (this.native != null)
                {
                    this.native.Dispose();
                    this.native = null;
                }
                this.UnregisterPlaying();
            }
        }
Example #2
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.native != null)
                {
                    this.native.Dispose();
                    this.native = null;
                }

                if (openmpt_module != IntPtr.Zero)
                {
                    openmpt_module_destroy(openmpt_module);
                    openmpt_module = IntPtr.Zero;
                }

                if (stream != null)
                {
                    stream.Dispose();
                    stream = null;
                }
            }
        }
Example #3
0
        private bool GrabNativeSource()
        {
            if (this.native != null)
            {
                return(true);
            }

            // Retrieve maximum number of sources by kind (2D / 3D)
            int maxNum = DualityApp.Sound.MaxOpenALSources * 3 / 4;

            if (!this.is3D)
            {
                maxNum = DualityApp.Sound.MaxOpenALSources - maxNum;
            }
            // Determine how many sources of this kind (2D / 3D) are playing
            int curNum = this.is3D ? DualityApp.Sound.NumPlaying3D : DualityApp.Sound.NumPlaying2D;
            // Determine how many sources using this sound resource are playing
            int curNumSoundRes = DualityApp.Sound.GetNumPlaying(this.sound);

            if (DualityApp.Sound.NumAvailable > 0 &&
                curNum < maxNum &&
                curNumSoundRes < this.sound.Res.MaxInstances)
            {
                this.native = DualityApp.AudioBackend.CreateSource();
            }
            else
            {
                bool searchSimilar = curNumSoundRes >= this.sound.Res.MaxInstances;
                this.curPriority = this.PreCalcPriority();

                foreach (SoundInstance inst in DualityApp.Sound.Playing)
                {
                    if (inst.native == null)
                    {
                        continue;
                    }
                    if (!searchSimilar && this.is3D != inst.is3D)
                    {
                        continue;
                    }
                    if (searchSimilar && this.sound.Res != inst.sound.Res)
                    {
                        continue;
                    }

                    float ownPrioMult = 1.0f;
                    if (searchSimilar && !inst.Looped)
                    {
                        ownPrioMult *= MathF.Sqrt(inst.playTime + 1.0f);
                    }

                    if (this.curPriority * ownPrioMult > inst.curPriority +
                        (inst.Looped ? PriorityStealLoopedThreshold : PriorityStealThreshold))
                    {
                        this.native = inst.native;
                        this.native.Reset();
                        inst.native = null;
                        break;
                    }
                    // List sorted by priority - if first fails, all will. Exception: Searching
                    // similar sounds where play times are taken into account
                    if (!searchSimilar)
                    {
                        break;
                    }
                }
            }

            this.notYetAssigned = false;
            return(this.native != null);
        }
Example #4
0
        public void Update()
        {
            AudioSourceState nativeState = AudioSourceState.Default;

            nativeState.Volume  = SettingsCache.MusicVolume * curFade;
            nativeState.Lowpass = this.lowpass;

            bool fadeOut = this.fadeTarget <= 0.0f;

            if (this.fadeTarget != this.curFade)
            {
                float fadeTemp = Time.TimeMult * Time.SecondsPerFrame / Math.Max(0.05f, this.fadeTimeSec);

                if (this.fadeTarget > this.curFade)
                {
                    this.curFade += fadeTemp;
                }
                else
                {
                    this.curFade -= fadeTemp;
                }

                if (Math.Abs(this.curFade - this.fadeTarget) < fadeTemp * 2.0f)
                {
                    this.curFade = this.fadeTarget;
                }
            }

            if (this.notYetAssigned)
            {
                this.notYetAssigned = false;

                // Grab a native audio source
                if (openmpt_module == IntPtr.Zero)
                {
                    this.Dispose();
                    return;
                }

                native = DualityApp.AudioBackend.CreateSource();
                native.Play(this);
            }

            // If the source is stopped / finished, dispose and return
            if (this.native == null || this.native.IsFinished)
            {
                this.Dispose();
                return;
            }

            if (fadeOut && nativeState.Volume <= 0.0f)
            {
                this.fadeWaitEnd += Time.TimeMult * Time.MillisecondsPerFrame;
                // After fading out entirely, wait 50 ms before actually stopping the source to prevent unpleasant audio tick / glitch noises
                if (this.fadeWaitEnd > 50.0f)
                {
                    this.Dispose();
                    return;
                }
            }
            else
            {
                this.fadeWaitEnd = 0.0f;
            }

            native.ApplyState(ref nativeState);
        }
Example #5
0
        private void OnDisposed(bool manually)
        {
            if (manually)
            {
                this.attachedTo = null;
                this.curPriority = -1;

                if (this.native != null)
                {
                    this.native.Dispose();
                    this.native = null;
                }
                this.UnregisterPlaying();
            }
        }
Example #6
0
        private bool GrabNativeSource()
        {
            if (this.native != null) return true;

            // Retrieve maximum number of sources by kind (2D / 3D)
            int maxNum = DualityApp.Sound.MaxOpenALSources * 3 / 4;
            if (!this.is3D) maxNum = DualityApp.Sound.MaxOpenALSources - maxNum;
            // Determine how many sources of this kind (2D / 3D) are playing
            int curNum = this.is3D ? DualityApp.Sound.NumPlaying3D : DualityApp.Sound.NumPlaying2D;
            // Determine how many sources using this sound resource are playing
            int curNumSoundRes = DualityApp.Sound.GetNumPlaying(this.sound);

            if (DualityApp.Sound.NumAvailable > 0 &&
                curNum < maxNum &&
                curNumSoundRes < this.sound.Res.MaxInstances)
            {
                this.native = DualityApp.AudioBackend.CreateSource();
            }
            else
            {
                bool searchSimilar = curNumSoundRes >= this.sound.Res.MaxInstances;
                this.curPriority = this.PreCalcPriority();

                foreach (SoundInstance inst in DualityApp.Sound.Playing)
                {
                    if (inst.native == null) continue;
                    if (!searchSimilar && this.is3D != inst.is3D) continue;
                    if (searchSimilar && this.sound.Res != inst.sound.Res) continue;

                    float ownPrioMult = 1.0f;
                    if (searchSimilar && !inst.Looped) ownPrioMult *= MathF.Sqrt(inst.playTime + 1.0f);

                    if (this.curPriority * ownPrioMult > inst.curPriority +
                        (inst.Looped ? PriorityStealLoopedThreshold : PriorityStealThreshold))
                    {
                        this.native = inst.native;
                        this.native.Reset();
                        inst.native = null;
                        break;
                    }
                    // List sorted by priority - if first fails, all will. Exception: Searching
                    // similar sounds where play times are taken into account
                    if (!searchSimilar)
                        break;
                }
            }

            this.notYetAssigned = false;
            return this.native != null;
        }