Example #1
0
 public AudioMessage(string pType, AudioResource pResource, float pVolume, int pId)
 {
     Type      = pType;
     Resource  = pResource;
     Volume    = pVolume;
     ID        = pId;
     Sender    = null;
     Recipient = null;
 }
Example #2
0
        /// <summary>
        /// Will play the given AudioResource at the specified volume
        /// </summary>
        /// <param name="pResource">The resource to play</param>
        /// <param name="pVolume">The volume at which to play the sound</param>
        /// <returns>ID for the instance of the sound play</returns>
        public static int Play(AudioResource pResource, float pVolume)
        {
            System.Diagnostics.Debug.Assert(pVolume >= MinVolume);
            System.Diagnostics.Debug.Assert(pVolume <= MaxVolume);

            var id = GetSoundId();

            _messages.SendMessage(new AudioMessage("Play", pResource, pVolume, id));
            return(id);
        }
Example #3
0
        /// <summary>
        /// Will play the given sound only if it is not currently playing at the specified volume
        /// </summary>
        /// <param name="pResource">The resource to play</param>
        /// <param name="pVolume">The volume at which to play the sound</param>
        /// <param name="pId">The ID of last time the resource was played</param>
        /// <returns>ID of the instance of the sound play</returns>
        public static int PlayOnce(AudioResource pResource, float pVolume, int pId)
        {
            System.Diagnostics.Debug.Assert(pVolume >= MinVolume);
            System.Diagnostics.Debug.Assert(pVolume <= MaxVolume);

            if (_playingSounds.ContainsKey(pId))
            {
                return(pId);
            }

            return(Play(pResource, pVolume));
        }
Example #4
0
        public void ReceiveMessage(IMessage pMessage)
        {
            var           m        = (AudioMessage)pMessage;
            AudioResource resource = m.Resource;
            float         volume   = m.Volume;
            SourceVoice   voice;

            switch (pMessage.Type)
            {
            case "Play":
            case "Loop":
                System.Diagnostics.Debug.Assert(!resource.Disposed, "Audio resource has been disposed");

                GetVoice(resource, m.ID, pMessage.Type == "Loop", out voice);
                if (voice.Volume != volume)
                {
                    voice.SetVolume(volume);
                    Device.CommitChanges();
                }
                resource.Play(voice);
                _playingSounds.Add(m.ID, voice);
                break;

            case "Stop":
                if (_playingSounds.ContainsKey(m.ID))
                {
                    voice = _playingSounds[m.ID];
                    voice.Stop();
                    voice.FlushSourceBuffers();
                    _playingSounds.Remove(m.ID);
                }
                break;

            case "Pause":
                if (_playingSounds.ContainsKey(m.ID))
                {
                    voice = _playingSounds[m.ID];
                    voice.Stop();
                }
                break;

            case "Resume":
                if (_playingSounds.ContainsKey(m.ID))
                {
                    voice = _playingSounds[m.ID];
                    voice.Start();
                }
                break;
            }
        }
Example #5
0
        /// <summary>
        /// Gets a free voice from the voice pool.  If none are available a new one is created
        /// </summary>
        /// <param name="pSound">Sound to play with the voice</param>
        private static void GetVoice(AudioResource pSound, int pId, bool pLoop, out SourceVoice pVoice)
        {
            lock (_freeVoices)
            {
                if (_freeVoices.Count == 0)
                {
                    pVoice = new SourceVoice(Device, pSound.Stream, true);
                }
                else
                {
                    pVoice = _freeVoices[_freeVoices.Count - 1];
                    _freeVoices.RemoveAt(_freeVoices.Count - 1);
                }

                var cb = new SoundCompleteCallback(pVoice, pId, pSound);
                cb.AddCallback(pLoop);
            }
        }
Example #6
0
 public void SetAudio(string pAlias)
 {
     _sound = ResourceManager.Request <AudioResource>(pAlias);
 }
Example #7
0
 public AudioComponent(string pAlias)
 {
     _sound = ResourceManager.Request <AudioResource>(pAlias);
     Volume = AudioPlayer.MaxVolume;
 }
Example #8
0
 /// <summary>
 /// Will play the given sound only if it is not currently playing
 /// </summary>
 /// <param name="pResource">The resource to play</param>
 /// <param name="pId">The ID of last time the resource was played</param>
 /// <returns>ID of the instance of the sound play</returns>
 public static int PlayOnce(AudioResource pResource, int pId)
 {
     return(PlayOnce(pResource, MaxVolume, pId));
 }
Example #9
0
 /// <summary>
 /// Will play the given AudioResource
 /// </summary>
 /// <param name="pResource">The resource to play</param>
 /// <returns>ID for the instance of the sound play</returns>
 public static int Play(AudioResource pResource)
 {
     return(Play(pResource, MaxVolume));
 }
Example #10
0
 public SoundCompleteCallback(SourceVoice pVoice, int pId, AudioResource pResources)
 {
     _voice    = pVoice;
     _resource = pResources;
     _id       = pId;
 }
Example #11
0
 /// <summary>
 /// Will loop the sound indefinitely until <see cref="Stop(int)"/> is called
 /// </summary>
 /// <param name="pResource">The resource to loop</param>
 /// <returns>ID of the instance of the sound play</returns>
 public static int Loop(AudioResource pResource)
 {
     return(Loop(pResource, MaxVolume));
 }