Ejemplo n.º 1
0
        public static void Stop(Audio.Handle sound)
        {
            if (sound == null)
            {
                return;
            }
#if !NOAUDIO
            sound.Instance.IsLooped = false;
            sound.Instance.Stop();
#endif
        }
Ejemplo n.º 2
0
        public static void FadeOut(Audio.Handle handle, float duration, TweenFunction function = null)
        {
            if (handle == null)
            {
                return;
            }
#if !NOAUDIO
            if (function == null)
            {
                function = TweenFunctions.Linear;
            }
            handle.FadeState    = -1;
            handle.FadeDuration = handle.FadeTimer = duration;
            handle.FadeFunction = function;
            handle.FadeVolume   = handle.Instance.Volume;
#endif
        }
Ejemplo n.º 3
0
        private void RemoveHandle(Audio.Handle handle)
        {
            if (handle == null)
            {
                return;
            }
            //make sure the sound is really stopped!
            if (handle.Instance != null)
            {
                handle.Instance.IsLooped = false;
                handle.Instance.Stop();
            }

            this.handlesMap[handle.Name].Remove(handle);
            this.handles.Remove(handle);

            if (audiolist[handle.Name].Limit > 0)
            {
                this.soundLimits[handle.Name] -= 1;
            }
        }
Ejemplo n.º 4
0
        public static void Stop(bool now = false)
        {
            Debug.WriteLine("[Music] Stop " + (now ? " (now)" : ""));
#if !NOAUDIO
            if (!Sound.HasAudio)
            {
                return;
            }


            if (current != null && ((current.Instance != null && current.Instance.State == Microsoft.Xna.Framework.Audio.SoundState.Playing) || current.SongInstance != null))
            {
                if (Music.FadeTime != 0 && !now)
                {
#if FNA
                    current.SongInstance.Stop();
#else
                    current.Thread.Abort();
#endif
                    current.FadeState    = -1;
                    current.FadeDuration = current.FadeTimer = Music.FadeTime;
                    current.FadeFunction = TweenFunctions.Linear;
                    current.FadeVolume   = current.SongInstance.Volume;
                }
                else
                {
                    Debug.WriteLine("[Music] Actually stopping music");
#if FNA
                    current.SongInstance.Stop();
#else
                    current.Thread.Abort();
                    MediaPlayer.Stop();
#endif
                }
            }
            current = null;
#endif
        }
Ejemplo n.º 5
0
        internal void AddHandle(Audio.Handle handle)
        {
            if (handle == null)
            {
                return;
            }
            if (!this.handlesMap.ContainsKey(handle.Name) || this.handlesMap[handle.Name] == null)
            {
                this.handlesMap[handle.Name] = new List <Audio.Handle>();
            }

            this.handlesMap[handle.Name].Add(handle);
            this.handles.Add(handle);

            if (audiolist[handle.Name].Limit > 0)
            {
                if (!this.soundLimits.ContainsKey(handle.Name))
                {
                    this.soundLimits[handle.Name] = 0;
                }
                this.soundLimits[handle.Name] += 1;
            }
        }
Ejemplo n.º 6
0
        public static void Start(string name, bool looped = true)
        {
            Debug.WriteLine("[Music] Start " + name + (looped ? " (looped)" : ""));
#if !NOAUDIO
            if (!Sound.HasAudio)
            {
                return;
            }

            name = name.Trim();

            if (current != null && current.SongInstance != null)
            {
                current.SongInstance.Stop();
            }
            MediaPlayer.Stop();
            if (current != null && current.Thread != null)
            {
                current.Thread.Abort();
            }

            if (!Audio.Instance.audiolist.ContainsKey(name))
            {
                Debug.WriteLine("Warning: unknown audio asset '" + name + "'.");
                return;
            }

            var info = Audio.Instance.audiolist[name];
            var song = Audio.Instance.LoadSong(info.Asset);
            if (!Sound.HasAudio)
            {
                return;
            }

            float duration = info.Duration > 0 ? info.Duration : (float)song.Duration.TotalSeconds;

            var volume = (info.DefaultVolume > 0 ? info.DefaultVolume * Music.Volume : Music.Volume) * Sound.MasterVolume;
            Debug.WriteLine("[Music] volume is " + volume);

            if (volume <= 0)
            {
                return;
            }

            var fadeVolume = volume;

            if (FadeTime > 0)
            {
                volume = 0;
            }

#if FNA
            Thread t        = null;
            var    instance = song.CreateInstance();
            instance.IsLooped = looped;
            instance.Volume   = volume;
            instance.Play();
#else
            var t = new Thread(new ThreadStart(delegate() {
                var tlooped   = looped;
                var tsong     = song;
                var tduration = duration;
                do
                {
                    Debug.WriteLine("[Music] (re)starting music in it's thread " + tsong.Name + name);
                    MediaPlayer.Play(tsong);

                    Thread.Sleep((int)(tduration * 1000f));
                    //MediaPlayer.Stop();
                } while(tlooped);
            }));
            t.Start();
#endif

            var handle = new Audio.Handle
            {
                Success = true,
#if FNA
                SongInstance = instance,
#else
                SongInstance = song,
#endif
                Name     = info.Name,
                Type     = Audio.Type.Music,
                Thread   = t,
                Looped   = looped,
                Position = 0
            };

            handle.FadeState    = 1;
            handle.FadeDuration = handle.FadeTimer = Music.FadeTime;
            handle.FadeFunction = TweenFunctions.Linear;
            handle.FadeVolume   = fadeVolume;


            Audio.Instance.AddHandle(handle);
            current = handle;
#endif
        }
Ejemplo n.º 7
0
        public static Audio.Handle Play(string sound, float volume = -1f, float panning = 0f)
        {
#if !NOAUDIO
            if (!HasAudio)
            {
                return(default(Audio.Handle));
            }
            sound = sound.Trim();
            if (!Audio.Instance.audiolist.ContainsKey(sound))
            {
                Debug.WriteLine("Warning: unknown audio asset '" + sound + "'.");
                return(default(Audio.Handle));
            }


            var info = Audio.Instance.audiolist[sound];

            // Check if this sound is limited:
            if (info.Limit >= 0 && (info.Limit == 0 || (Audio.Instance.soundLimits.ContainsKey(sound) && Audio.Instance.soundLimits[sound] >= info.Limit)))
            {
                // Trace.WriteLine("Audio limit reached: " + info.Name + " (limit: " + info.Limit + ")");
                return(default(Audio.Handle));
            }

            // Don't play if volume is off:
            volume = Audio.Volume(volume, info) * MasterVolume * FXVolume;
            if (volume <= 0)
            {
                // Trace.WriteLine("Volume is off for : " + info.Name +"");
                return(default(Audio.Handle));
            }



            // Load and create instance: (loading is cached)
            var effect = Audio.Instance.Load(info.Asset);

            if (!HasAudio)
            {
                //Trace.WriteLine("Somehow we have no audio!");

                return(default(Audio.Handle));
            }
            var instance = effect.CreateInstance();

            /*
             * if (Audio.Instance.soundLimits.ContainsKey(sound))
             *  Trace.WriteLine("Playing Sound: " + info.Name + " (limit: " + info.Limit + ", playing: " + Audio.Instance.soundLimits[sound] + ")");
             * else
             *  Trace.WriteLine("Playing Sound: " + info.Name);*/

            instance.Volume   = volume;
            instance.Pan      = panning;
            instance.IsLooped = false;
            instance.Play();

            var handle = new Audio.Handle
            {
                Success  = true,
                Instance = instance,
                Name     = info.Name,
                Type     = Audio.Type.Sound,
                Looped   = false
            };
            Audio.Instance.AddHandle(handle);
            return(handle);
#else
            return(default(Audio.Handle));
#endif
        }