Update torchlight_changes_unloze/torchlight3/Torchlight/AudioManager.py

This commit is contained in:
Metroid_Skittles 2026-02-07 07:19:24 +01:00
parent 3e44425df9
commit 8dc0e15159

View File

@ -4,6 +4,7 @@ import logging
import sys
import io
import math
import time
from .FFmpegAudioPlayer import FFmpegAudioPlayerFactory
class AudioPlayerFactory():
@ -124,12 +125,9 @@ class Advertiser():
self.Torchlight = self.Master.Torchlight
self.LastClips = dict()
self.AdStop = 0
self.NextAdStop = 0
def Think(self, Delta):
def Think(self, Delta, clip, clip_key):
Now = self.Torchlight().Master.Loop.time()
Duration = 0.0
for Key, Clip in list(self.LastClips.items()):
if not Clip["timestamp"]:
@ -140,21 +138,19 @@ class Advertiser():
del self.LastClips[Key]
continue
Duration += Clip["duration"]
self.NextAdStop -= Delta
CeilDur = math.ceil(Duration)
if CeilDur > self.AdStop and self.NextAdStop <= 0 and CeilDur % self.Torchlight().Config["Advertiser"]["AdStop"] == 0:
Clip = self.LastClips.get(clip_key)
if not Clip:
return
if not clip.StopHinted and not Clip.get("hinted") and Clip["duration"] >= self.Torchlight().Config["Advertiser"]["AdStop"]:
self.Torchlight().SayChat("Hint: Type \x07FF0000!stop(ze) !pls(mg)\x01 to stop all currently playing sounds.")
self.AdStop = CeilDur
self.NextAdStop = 0
elif CeilDur < self.AdStop:
self.AdStop = 0
self.NextAdStop = self.Torchlight().Config["Advertiser"]["AdStop"] / 2
Clip["hinted"] = True
clip.StopHinted = True
def OnPlay(self, clip):
Now = self.Torchlight().Master.Loop.time()
self.LastClips[hash(clip)] = dict({"timestamp": Now, "duration": 0.0, "dominant": False, "active": True})
self.LastClips[hash(clip)] = dict({"timestamp": Now, "duration": 0.0, "dominant": False, "active": True, "hinted": False})
HasDominant = False
for Key, Clip in self.LastClips.items():
@ -189,7 +185,7 @@ class Advertiser():
return
Clip["duration"] += Delta
self.Think(Delta)
self.Think(Delta, clip, clip_key)
class AudioManager():
@ -301,6 +297,8 @@ class AudioClip():
self.Type = _type
self.URI = uri
self.LastPosition = None
self.StartedAt = None
self.StopHinted = False
self.Stops = set()
self.Level = 0
@ -328,18 +326,29 @@ class AudioClip():
self.Player.Storage["Audio"]["Uses"] += 1
self.Player.Storage["Audio"]["LastUse"] = self.Torchlight().Master.Loop.time()
self.Player.Storage["Audio"]["LastUseLength"] = 0.0
self.StartedAt = self.Player.Storage["Audio"]["LastUse"]
self.StopHinted = False
def OnStop(self):
self.Logger.debug(sys._getframe().f_code.co_name + ' ' + self.URI)
if self in self.Master.AudioClips:
self.Master.AudioClips.remove(self)
if self.AudioPlayer.Playing:
if self.LastPosition is None:
self.LastPosition = self.AudioPlayer.Position
Delta = self.AudioPlayer.Position - self.LastPosition
self.Player.Storage["Audio"]["TimeUsed"] += Delta
self.Player.Storage["Audio"]["LastUseLength"] += Delta
played = None
if self.LastPosition is not None:
played = self.LastPosition
elif self.AudioPlayer and self.AudioPlayer.StartedPlaying is not None:
elapsed = time.time() - self.AudioPlayer.StartedPlaying
if self.AudioPlayer.Seconds:
elapsed = min(elapsed, self.AudioPlayer.Seconds)
played = max(0.0, elapsed)
if played is not None:
last_length = self.Player.Storage["Audio"]["LastUseLength"]
if played > last_length:
delta = played - last_length
self.Player.Storage["Audio"]["TimeUsed"] += delta
self.Player.Storage["Audio"]["LastUseLength"] += delta
if str(self.Level) in self.Torchlight().Config["AudioLimits"]:
if self.Player.Storage: