Example #1
0
        /// <summary>
        /// plays the sound for 3D.
        /// </summary>
        /// <param name="soundName">entried sound name</param>
        /// <param name="emitter">3D emitter</param>
        /// <returns>playing sound cue</returns>
        public Cue PlaySound3D(string soundName, AudioEmitter emitter)
        {
            SoundElement sound;

            if (soundPool.Count > 0)
            {
                // If possible, reuse an existing Cue3D instance.
                sound = soundPool.Pop();
            }
            else
            {
                // Otherwise we have to allocate a new one.
                sound = new SoundElement();
            }

            // Fill in the cue and emitter fields.
            sound.cue     = soundBank.GetCue(soundName);
            sound.emitter = emitter;

            // Set the 3D position of this cue, and then play it.
            Apply3D(sound);

            sound.cue.Play();

            // Remember that this cue is now active.
            activeSounds.Add(sound);

            return(sound.cue);
        }
Example #2
0
        /// <summary>
        /// applies 3D position to sound element.
        /// </summary>
        /// <param name="element"></param>
        private void Apply3D(SoundElement element)
        {
            emitter.Position = element.emitter.Position;
            emitter.Forward  = element.emitter.Forward;
            emitter.Up       = element.emitter.Up;
            emitter.Velocity = element.emitter.Velocity;

            element.cue.Apply3D(listener, emitter);
        }
Example #3
0
        /// <summary>
        /// stops the sound.
        /// </summary>
        /// <param name="sound">playing sound element</param>
        private bool StopSound(SoundElement sound)
        {
            if (soundOn == false || sound == null || sound.cue == null)
            {
                return(false);
            }

            if (sound.cue.IsPaused || sound.cue.IsPlaying)
            {
                sound.cue.Stop(AudioStopOptions.Immediate);
                return(true);
            }

            return(false);
        }
Example #4
0
        /// <summary>
        /// resumes the sound.
        /// </summary>
        /// <param name="sound">paused sound element</param>
        private bool ResumeSound(SoundElement sound)
        {
            if (soundOn == false || sound == null || sound.cue == null)
            {
                return(false);
            }

            if (sound.cue.IsPaused)
            {
                sound.cue.Resume();
                return(true);
            }

            return(false);
        }
Example #5
0
        /// <summary>
        /// pauses the sound.
        /// </summary>
        /// <param name="sound">playing sound element</param>
        private bool PauseSound(SoundElement sound)
        {
            if (soundOn == false || sound == null || sound.cue == null)
            {
                return(false);
            }

            if (sound.cue.IsPlaying)
            {
                sound.cue.Pause();
                return(true);
            }

            return(false);
        }
Example #6
0
        /// <summary>
        /// processes the sound pool and the 3D sound playback.
        /// XNA's AudioEngine is updated here.
        /// </summary>
        public void Update()
        {
            if (soundOn == false)
            {
                return;
            }

            // Loop over all the currently playing 3D sounds.
            int index = 0;

            while (index < activeSounds.Count)
            {
                SoundElement sound = activeSounds[index];

                if (sound.cue.IsStopped)
                {
                    // If the cue has stopped playing, dispose it.
                    //sound.cue.Dispose();

                    sound.cue     = null;
                    sound.emitter = null;

                    // Store the SoundElement instance for future reuse.
                    soundPool.Push(sound);

                    // Remove it from the active list.
                    activeSounds.RemoveAt(index);
                }
                else
                {
                    if (sound.emitter != null)
                    {
                        // If the cue is still playing, update its 3D settings.
                        Apply3D(sound);
                    }

                    index++;
                }
            }

            // Update the XACT engine.
            AudioEngine.Update();
        }
Example #7
0
        /// <summary>
        /// plays the sound.
        /// </summary>
        /// <param name="soundName">entried sound name</param>
        /// <returns>playing sound cue</returns>
        public static Cue PlaySound(string soundName)
        {
            SoundElement sound = null;

            if (soundPool.Count > 0)
            {
                // If possible, reuse an existing Cue3D instance.
                sound = soundPool.Pop();
            }
            else
            {
                // Otherwise we have to allocate a new one.
                sound = new SoundElement();
            }

            // Fill in the cue and emitter fields.
            sound.cue = soundBank.GetCue(soundName);
            sound.cue.Play();

            // Remember that this cue is now active.
            activeSounds.Add(sound);

            return(sound.cue);
        }
Example #8
0
        /// <summary>
        /// stops the sound.
        /// </summary>
        /// <param name="sound">playing sound element</param>
        private bool StopSound(SoundElement sound)
        {
            if (soundOn == false || sound == null || sound.cue == null)
                return false;

            if (sound.cue.IsPaused || sound.cue.IsPlaying)
            {
                sound.cue.Stop(AudioStopOptions.Immediate);
                return true;
            }

            return false;
        }
Example #9
0
        /// <summary>
        /// resumes the sound.
        /// </summary>
        /// <param name="sound">paused sound element</param>
        private bool ResumeSound(SoundElement sound)
        {
            if (soundOn == false || sound == null || sound.cue == null)
                return false;

            if (sound.cue.IsPaused)
            {
                sound.cue.Resume();
                return true;
            }

            return false;
        }
Example #10
0
        /// <summary>
        /// pauses the sound.
        /// </summary>
        /// <param name="sound">playing sound element</param>
        private bool PauseSound(SoundElement sound)
        {
            if (soundOn == false || sound == null || sound.cue == null)
                return false;

            if (sound.cue.IsPlaying)
            {
                sound.cue.Pause();
                return true;
            }

            return false;
        }
Example #11
0
        /// <summary>
        /// applies 3D position to sound element.
        /// </summary>
        /// <param name="element"></param>
        private void Apply3D(SoundElement element)
        {
            emitter.Position = element.emitter.Position;
            emitter.Forward = element.emitter.Forward;
            emitter.Up = element.emitter.Up;
            emitter.Velocity = element.emitter.Velocity;

            element.cue.Apply3D(listener, emitter);
        }
Example #12
0
        /// <summary>
        /// plays the sound.
        /// </summary>
        /// <param name="soundName">entried sound name</param>
        /// <returns>playing sound cue</returns>
        public static Cue PlaySound(string soundName)
        {
            SoundElement sound = null;

            if (soundPool.Count > 0)
            {
                // If possible, reuse an existing Cue3D instance.
                sound = soundPool.Pop();
            }
            else
            {
                // Otherwise we have to allocate a new one.
                sound = new SoundElement();                
            }

            // Fill in the cue and emitter fields.
            sound.cue = soundBank.GetCue(soundName);
            sound.cue.Play();

            // Remember that this cue is now active.
            activeSounds.Add(sound);

            return sound.cue;
        }
Example #13
0
        /// <summary>
        /// plays the sound for 3D. 
        /// </summary>
        /// <param name="soundName">entried sound name</param>
        /// <param name="emitter">3D emitter</param>
        /// <returns>playing sound cue</returns>
        public Cue PlaySound3D(string soundName, AudioEmitter emitter)
        {
            SoundElement sound;

            if (soundPool.Count > 0)
            {
                // If possible, reuse an existing Cue3D instance.
                sound = soundPool.Pop();
            }
            else
            {
                // Otherwise we have to allocate a new one.
                sound = new SoundElement();
            }

            // Fill in the cue and emitter fields.
            sound.cue = soundBank.GetCue(soundName);
            sound.emitter = emitter;

            // Set the 3D position of this cue, and then play it.
            Apply3D(sound);

            sound.cue.Play();

            // Remember that this cue is now active.
            activeSounds.Add(sound);

            return sound.cue;
        }