public void Update(Location position, Location forward, Location up, Location velocity, bool selected) { ALError err = AL.GetError(); if (err != ALError.NoError) { SysConsole.Output(OutputType.WARNING, "Found audio error " + err + ", rebuilding audio..."); Init(TheClient, CVars); return; } bool sel = CVars.a_quietondeselect.ValueB ? selected : true; Selected = sel; for (int i = 0; i < PlayingNow.Count; i++) { if (!PlayingNow[i].Exists || AL.GetSourceState(PlayingNow[i].Src) == ALSourceState.Stopped) { PlayingNow[i].Destroy(); PlayingNow.RemoveAt(i); i--; } else if (!sel && PlayingNow[i].IsBackground && !PlayingNow[i].Backgrounded) { AL.Source(PlayingNow[i].Src, ALSourcef.Gain, 0.0001f); PlayingNow[i].Backgrounded = true; } else if (sel && PlayingNow[i].Backgrounded) { AL.Source(PlayingNow[i].Src, ALSourcef.Gain, PlayingNow[i].Gain); PlayingNow[i].Backgrounded = false; } } if (Microphone != null) { Microphone.Tick(); } Vector3 pos = ClientUtilities.Convert(position); Vector3 forw = ClientUtilities.Convert(forward); Vector3 upvec = ClientUtilities.Convert(up); Vector3 vel = ClientUtilities.Convert(velocity); AL.Listener(ALListener3f.Position, ref pos); AL.Listener(ALListenerfv.Orientation, ref forw, ref upvec); AL.Listener(ALListener3f.Velocity, ref vel); float globvol = CVars.a_globalvolume.ValueF; AL.Listener(ALListenerf.Gain, globvol <= 0 ? 0.001f: (globvol > 1 ? 1: globvol)); }
/// <summary> /// Updates the sound engine. /// </summary> /// <param name="position">Current position.</param> /// <param name="forward">Forward vector.</param> /// <param name="up">Up vcector.</param> /// <param name="velocity">Velocity.</param> /// <param name="selected">Whether the system is selected.</param> public void Update(Location position, Location forward, Location up, Location velocity, bool selected) { CPosition = position; if (AudioInternal == null) { ALError err = AL.GetError(); if (err != ALError.NoError) { SysConsole.Output(OutputType.WARNING, "Found audio error " + err + "!"); //init(TheClient, CVars); return; } } bool sel = Client.QuietOnDeselect ? selected : true; Selected = sel; /*if (DeafenTime > 0.0) * { * TimeDeaf += Engine.Delta; * DeafenTime -= Engine.Delta; * if (DeafNoise == null) * { * DeafNoise = PlaySimpleInternal(DeafLoop, true); * if (DeafNoise == null) * { * DeafenTime = 0; * TimeDeaf = 0; * } * } * if (DeafenTime < 0) * { * TimeDeaf = 0; * DeafenTime = 0; * DeafNoise.Stop(); * DeafNoise.Destroy(); * DeafNoise = null; * } * } * if (TimeDeaf > 0.001 && DeafenTime > 0.001) * { * float weaken = (float)Math.Min(DeafenTime, TimeDeaf); * if (weaken < 1.0) * { * DeafNoise.Gain = (float)weaken * 0.5f; * DeafNoise.UpdateGain(); * } * else * { * DeafNoise.Gain = 0.5f; * DeafNoise.UpdateGain(); * } * } * DeafLoop.LastUse = Engine.GlobalTickTime;*/ for (int i = 0; i < PlayingNow.Count; i++) { if (!PlayingNow[i].Exists || (AudioInternal == null && PlayingNow[i].Src < 0) || (AudioInternal == null ? AL.GetSourceState(PlayingNow[i].Src) == ALSourceState.Stopped : PlayingNow[i].AudioInternal.State == AudioState.DONE)) { PlayingNow[i].Destroy(); if (AudioInternal == null) { CheckError("Destroy:" + PlayingNow[i].Effect.Name); } PlayingNow.RemoveAt(i); i--; continue; } PlayingNow[i].Effect.LastUse = Client.GlobalTickTime; /*if ((TimeDeaf > 0.0) && sel && !PlayingNow[i].IsBackground) * { * PlayingNow[i].IsDeafened = true; * float lesser = (float)Math.Min(DeafenTime, TimeDeaf); * if (lesser < 0.999) * { * if (AudioInternal == null) * { * AL.Source(PlayingNow[i].Src, ALSourcef.Gain, PlayingNow[i].Gain * (1.0f - lesser)); * } * else * { * PlayingNow[i].AudioInternal.Gain = PlayingNow[i].Gain * (1.0f - lesser); * } * } * else * { * if (AudioInternal == null) * { * AL.Source(PlayingNow[i].Src, ALSourcef.Gain, 0.0001f); * } * else * { * PlayingNow[i].AudioInternal.Gain = 0.0001f; * } * } * } * else */if (/*(TimeDeaf <= 0.0) && */ sel && !PlayingNow[i].IsBackground) { if (AudioInternal == null) { AL.Source(PlayingNow[i].Src, ALSourcef.Gain, PlayingNow[i].Gain); } else { } PlayingNow[i].IsDeafened = false; } if (/*(TimeDeaf <= 0.0) && */ !sel && PlayingNow[i].IsBackground && !PlayingNow[i].Backgrounded) { if (AudioInternal == null) { AL.Source(PlayingNow[i].Src, ALSourcef.Gain, 0.0001f); } else { PlayingNow[i].AudioInternal.Gain = 0.0001f; } PlayingNow[i].Backgrounded = true; } else if (/*(TimeDeaf <= 0.0) && */ sel && PlayingNow[i].Backgrounded) { if (AudioInternal == null) { AL.Source(PlayingNow[i].Src, ALSourcef.Gain, PlayingNow[i].Gain); } else { PlayingNow[i].AudioInternal.Gain = PlayingNow[i].Gain; } PlayingNow[i].Backgrounded = false; PlayingNow[i].IsDeafened = false; } } CheckError("Setup"); /*if (Microphone != null) * { * Microphone.Tick(); * }*/ CheckError("Microphone"); float globvol = GlobalVolume; globvol = globvol <= 0 ? 0.001f : (globvol > 1 ? 1 : globvol); if (AudioInternal == null) { Vector3 pos = position.ToOpenTK(); Vector3 forw = forward.ToOpenTK(); Vector3 upvec = up.ToOpenTK(); Vector3 vel = velocity.ToOpenTK(); AL.Listener(ALListener3f.Position, ref pos); AL.Listener(ALListenerfv.Orientation, ref forw, ref upvec); AL.Listener(ALListener3f.Velocity, ref vel); CheckError("Positioning"); AL.Listener(ALListenerf.Gain, globvol); CheckError("Gain"); } else { // TODO: vel //AudioInternal.Left = CVars.a_left.ValueB; //AudioInternal.Right = CVars.a_right.ValueB; AudioInternal.Position = position; AudioInternal.ForwardDirection = forward; AudioInternal.UpDirection = up; AudioInternal.Volume = globvol; } TimeTowardsNextClean += Client.Delta; if (TimeTowardsNextClean > 10.0) { CleanTick(); TimeTowardsNextClean = 0.0; } }