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