Example #1
0
            /// <summary>
            /// Updates the sound source.
            /// </summary>
            /// <param name="emitter">The sources parent <see cref="SoundEmitter"/>.</param>
            /// <returns>True, if the source is still active. False, if it requests to be removed.</returns>
            public bool Update(SoundEmitter emitter)
            {
                // Revalidate Sound reference
                this.sound.MakeAvailable();

                // If the SoundInstance has been disposed, set to null
                if (this.instance != null && this.instance.Disposed) this.instance = null;

                // If there is a SoundInstance playing, but it's the wrong one, stop it
                if (this.instance != null && this.instance.Sound != this.sound)
                {
                    this.instance.Stop();
                    this.instance = null;
                }

                if (this.instance == null)
                {
                    // If this Source isn't looped and it HAS been played already, remove it
                    if (!this.looped && this.hasBeenPlayed) return false;

                    // Play the sound
                    this.instance = DualityApp.Sound.PlaySound3D(this.sound, emitter.GameObj);
                    this.instance.Pos = this.offset;
                    this.instance.Looped = this.looped;
                    this.instance.Volume = this.volume;
                    this.instance.Pitch = this.pitch;
                    this.instance.Paused = this.paused;
                    this.hasBeenPlayed = true;
                }

                return true;
            }
Example #2
0
 /// <summary>
 /// Plays a sound 3d "in space".
 /// </summary>
 /// <param name="snd">The Sound to play.</param>
 /// <param name="attachTo">The GameObject to which the sound will be attached.</param>
 /// <param name="relativePos">The position of the sound relative to the GameObject.</param>
 /// <returns>A new SoundInstance representing the currentply playing sound.</returns>
 public SoundInstance PlaySound3D(ContentRef<Sound> snd, GameObject attachTo, Vector3 relativePos)
 {
     SoundInstance inst = new SoundInstance(snd, attachTo);
     inst.Pos = relativePos;
     this.sounds.Add(inst);
     return inst;
 }
Example #3
0
 /// <summary>
 /// Plays a sound 3d "in space".
 /// </summary>
 /// <param name="snd">The Sound to play.</param>
 /// <param name="attachTo">The GameObject to which the sound will be attached.</param>
 /// <returns>A new SoundInstance representing the currentply playing sound.</returns>
 public SoundInstance PlaySound3D(ContentRef<Sound> snd, GameObject attachTo)
 {
     SoundInstance inst = new SoundInstance(snd, attachTo);
     this.sounds.Add(inst);
     return inst;
 }
Example #4
0
 /// <summary>
 /// Plays a sound 3d "in space".
 /// </summary>
 /// <param name="snd">The Sound to play.</param>
 /// <param name="pos">The position of the sound in space.</param>
 /// <returns>A new SoundInstance representing the currentply playing sound.</returns>
 public SoundInstance PlaySound3D(ContentRef<Sound> snd, Vector3 pos)
 {
     SoundInstance inst = new SoundInstance(snd, pos);
     this.sounds.Add(inst);
     return inst;
 }
Example #5
0
 /// <summary>
 /// Plays a sound.
 /// </summary>
 /// <param name="snd">The Sound to play.</param>
 /// <returns>A new SoundInstance representing the currentply playing sound.</returns>
 public SoundInstance PlaySound(ContentRef<Sound> snd)
 {
     SoundInstance inst = new SoundInstance(snd);
     this.sounds.Add(inst);
     return inst;
 }
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 (ISoundInstance inst_ in DualityApp.Sound.Playing)
                {
                    SoundInstance inst = inst_ as SoundInstance;
                    if (inst == null)
                    {
                        continue;
                    }

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