Пример #1
0
        public ActiveSound PlaySimpleInternal(SoundEffect sfx, bool loop)
        {
            Func <ActiveSound> playSound = () =>
            {
                ActiveSound actsfx = new ActiveSound(sfx)
                {
                    Engine   = this,
                    Position = Location.NaN,
                    Pitch    = 1.0f,
                    Gain     = 1.0f,
                    Loop     = loop
                };
                actsfx.Create();
                actsfx.Play();
                return(actsfx);
            };

            lock (sfx)
            {
                if (sfx.Internal == -1)
                {
                    return(null); // TODO: Enforce load-NOW?
                }
            }
            return(playSound());
        }
Пример #2
0
        /// <summary>
        /// NOTE: *NOT* guaranteed to play a sound effect immediately, regardless of input! Some sound effects will be delayed!
        /// </summary>
        public void Play(SoundEffect sfx, bool loop, Location pos, float pitch = 1, float volume = 1, float seek_seconds = 0, Action <ActiveSound> callback = null)
        {
            if (sfx.Internal == -2)
            {
                Play(GetSound(sfx.Name), loop, pos, pitch, volume, seek_seconds, callback);
                return;
            }
            if (pitch <= 0 || pitch > 2)
            {
                throw new ArgumentException("Must be between 0 and 2", "pitch");
            }
            if (volume == 0)
            {
                return;
            }
            if (volume <= 0 || volume > 1)
            {
                throw new ArgumentException("Must be between 0 and 1", "volume");
            }
            Action playSound = () =>
            {
                ActiveSound actsfx = new ActiveSound(sfx);
                actsfx.Engine   = this;
                actsfx.Position = pos;
                actsfx.Pitch    = pitch * CVars.a_globalpitch.ValueF;
                actsfx.Gain     = volume;
                actsfx.Loop     = loop;
                actsfx.Create();
                actsfx.Play();
                if (seek_seconds != 0)
                {
                    actsfx.Seek(seek_seconds);
                }
                PlayingNow.Add(actsfx);
                if (callback != null)
                {
                    callback(actsfx);
                }
            };

            lock (sfx)
            {
                if (sfx.Internal == -1)
                {
                    sfx.Loaded += (o, e) =>
                    {
                        playSound();
                    };
                    return;
                }
            }
            playSound();
        }
Пример #3
0
        /// <summary>
        /// NOTE: *NOT* guaranteed to play a sound effect immediately, regardless of input! Some sound effects will be delayed! If too many audible sounds are already playing, this will refuse to play.
        /// </summary>
        public void Play(SoundEffect sfx, bool loop, Location pos, float pitch = 1, float volume = 1, float seek_seconds = 0, Action <ActiveSound> callback = null)
        {
            if (sfx == null)
            {
                //SysConsole.Output(OutputType.DEBUG, "Audio / null");
                return;
            }
            if (PlayingNow.Count > 200 && AudioInternal == null)
            {
                if (!CanClean())
                {
                    //SysConsole.Output(OutputType.DEBUG, "Audio / count");
                    return;
                }
            }
            if (sfx.Internal == -2)
            {
                Play(GetSound(sfx.Name), loop, pos, pitch, volume, seek_seconds, callback);
                return;
            }
            if (pitch <= 0 || pitch > 2)
            {
                throw new ArgumentException("Must be between 0 and 2", "pitch");
            }
            if (volume == 0)
            {
                SysConsole.Output(OutputType.DEBUG, "Audio / volume");
                return;
            }
            if (volume <= 0 || volume > 1)
            {
                throw new ArgumentException("Must be between 0 and 1", "volume");
            }
            Action playSound = () =>
            {
                if (sfx.Clip == null && sfx.Internal < 0)
                {
                    //SysConsole.Output(OutputType.DEBUG, "Audio / clip");
                    return;
                }
                ActiveSound actsfx = new ActiveSound(sfx)
                {
                    Engine   = this,
                    Position = pos,
                    Pitch    = pitch * CVars.a_globalpitch.ValueF,
                    Gain     = volume,
                    Loop     = loop
                };
                actsfx.Create();
                if (actsfx.AudioInternal == null && actsfx.Src < 0)
                {
                    //SysConsole.Output(OutputType.DEBUG, "Audio / src");
                    return;
                }
                CheckError("Create:" + sfx.Name);
                if (TimeDeaf > 0.0)
                {
                    actsfx.IsDeafened = true;
                    if (AudioInternal == null)
                    {
                        AL.Source(actsfx.Src, ALSourcef.Gain, 0.0001f);
                    }
                    else
                    {
                        actsfx.AudioInternal.Gain = 0.0001f;
                    }
                }
                if (seek_seconds != 0)
                {
                    actsfx.Seek(seek_seconds);
                }
                CheckError("Preconfig:" + sfx.Name);
                actsfx.Play();
                CheckError("Play:" + sfx.Name);
                //SysConsole.Output(OutputType.DEBUG, "Audio / sucess");
                PlayingNow.Add(actsfx);
                callback?.Invoke(actsfx);
            };

            lock (sfx)
            {
                if (sfx.Clip == null && sfx.Internal == -1)
                {
                    //SysConsole.Output(OutputType.DEBUG, "Audio / delay");
                    sfx.Loaded += (o, e) =>
                    {
                        playSound();
                    };
                    return;
                }
            }
            playSound();
        }
Пример #4
0
 /// <summary>
 /// NOTE: *NOT* guaranteed to play a sound effect immediately, regardless of input! Some sound effects will be delayed!
 /// </summary>
 public void Play(SoundEffect sfx, bool loop, Location pos, float pitch = 1, float volume = 1, float seek_seconds = 0, Action<ActiveSound> callback = null)
 {
     if (sfx.Internal == -2)
     {
         Play(GetSound(sfx.Name), loop, pos, pitch, volume, seek_seconds, callback);
         return;
     }
     if (pitch <= 0 || pitch > 2)
     {
         throw new ArgumentException("Must be between 0 and 2", "pitch");
     }
     if (volume == 0)
     {
         return;
     }
     if (volume <= 0 || volume > 1)
     {
         throw new ArgumentException("Must be between 0 and 1", "volume");
     }
     Action playSound = () =>
     {
         ActiveSound actsfx = new ActiveSound(sfx);
         actsfx.Engine = this;
         actsfx.Position = pos;
         actsfx.Pitch = pitch * CVars.a_globalpitch.ValueF;
         actsfx.Gain = volume;
         actsfx.Loop = loop;
         actsfx.Create();
         actsfx.Play();
         if (seek_seconds != 0)
         {
             actsfx.Seek(seek_seconds);
         }
         PlayingNow.Add(actsfx);
         if (callback != null)
         {
             callback(actsfx);
         }
     };
     lock (sfx)
     {
         if (sfx.Internal == -1)
         {
             sfx.Loaded += (o, e) =>
             {
                 playSound();
             };
             return;
         }
     }
     playSound();
 }