Skip to content

Commit 30c2240

Browse files
authored
!feat(triggers): sound modifiers for voicetriggers (#111)
1 parent 657bd06 commit 30c2240

6 files changed

Lines changed: 91 additions & 16 deletions

File tree

config/config.json

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,29 @@
2929
"Host": "127.0.0.1",
3030
"Port": 27019,
3131
"SampleRate": 22050,
32-
"Volume": 1.0,
33-
"Speed": 1.0,
34-
"Pitch": 1.0,
35-
"Proxy": ""
32+
"Proxy": "",
33+
34+
"AudioParams":
35+
{
36+
"Volume":
37+
{
38+
"Default": 1.0,
39+
"Min": 0.0,
40+
"Max": 1.0
41+
},
42+
"Speed":
43+
{
44+
"Default": 1.0,
45+
"Min": 0.5,
46+
"Max": 2.0
47+
},
48+
"Pitch":
49+
{
50+
"Default": 1.0,
51+
"Min": 0.5,
52+
"Max": 2.0
53+
}
54+
}
3655
},
3756

3857
"GeoIP":

src/torchlight/AudioManager.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,45 @@ def __init__(self, torchlight: Torchlight) -> None:
2121
def __del__(self) -> None:
2222
self.logger.info("~AudioManager()")
2323

24+
def ParseParams(self, trigger_params: dict, msg: str) -> dict[str, float]:
25+
this_config = self.torchlight.config.config.get("VoiceServer", {}).get("AudioParams", {})
26+
if not this_config:
27+
return trigger_params
28+
29+
params: dict[str, float] = {}
30+
for param in this_config:
31+
if param in trigger_params and trigger_params[param] is not None:
32+
params[param] = float(trigger_params[param])
33+
else:
34+
if isinstance(this_config[param], dict) and "Default" in this_config[param]:
35+
params[param] = float(this_config[param]["Default"])
36+
37+
msg_args = msg.split()
38+
for arg in msg_args:
39+
if "=" in arg:
40+
key, value = arg.split("=", 1)
41+
if not key or not value:
42+
continue
43+
44+
key = key.capitalize()
45+
if key not in this_config:
46+
continue
47+
48+
if isinstance(this_config[key], dict):
49+
val: float = 1.0
50+
try:
51+
val = float(value)
52+
except ValueError:
53+
continue
54+
55+
if "Min" not in this_config[key] or "Max" not in this_config[key]:
56+
continue
57+
58+
val = max(this_config[key]["Min"], min(val, this_config[key]["Max"]))
59+
params[key] = val
60+
61+
return params
62+
2463
def CheckLimits(self, player: Player) -> bool:
2564
level = player.admin.level
2665

src/torchlight/Commands.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,17 @@ async def _func(self, message: list[str], player: Player) -> int:
613613
return -1
614614

615615
voice_trigger = message[0].lower()
616-
trigger_number = message[1].lower()
616+
trigger_number: str = ""
617+
if message[1]:
618+
parts = message[1].split()
619+
trigger_parts = []
620+
for part in parts:
621+
if "=" in part:
622+
break
623+
trigger_parts.append(part)
624+
625+
if trigger_parts:
626+
trigger_number = " ".join(trigger_parts)
617627

618628
sound = self.get_sound_path(
619629
player=player,
@@ -638,12 +648,15 @@ async def _func(self, message: list[str], player: Player) -> int:
638648
self.torchlight.SayChat(f"Now playing {{olive}}{self.random_trigger_name}")
639649
voice_trigger = self.random_trigger_name
640650

651+
self.torchlight.SetPlayerCooldown(player, self.torchlight.config["AntiSpam"]["ChatCooldown"])
652+
641653
params = cast(dict, self.trigger_manager.voice_triggers[voice_trigger]["parameters"])
642-
volume = float(params["Volume"])
643-
speed = float(params["Speed"])
644-
pitch = float(params["Pitch"])
654+
modifiers = self.audio_manager.ParseParams(params, message[1])
655+
656+
volume = modifiers["Volume"]
657+
speed = modifiers["Speed"]
658+
pitch = modifiers["Pitch"]
645659

646-
self.torchlight.SetPlayerCooldown(player, self.torchlight.config["AntiSpam"]["ChatCooldown"])
647660
return audio_clip.Play(volume=volume, speed=speed, pitch=pitch)
648661

649662
def get_sound_path(self, player: Player, voice_trigger: str, trigger_number: str) -> str | None:

src/torchlight/FFmpegAudioPlayer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ def __init__(self, torchlight: Torchlight) -> None:
2929
self.host = self.config["Host"]
3030
self.port = self.config["Port"]
3131
self.sample_rate = float(self.config["SampleRate"])
32-
self.volume = float(self.config["Volume"])
33-
self.speed = float(self.config["Speed"])
34-
self.pitch = float(self.config["Pitch"])
32+
33+
params = self.config.get("AudioParams", {})
34+
35+
self.volume = float(params.get("Volume", {}).get("Default", 1.0))
36+
self.speed = float(params.get("Speed", {}).get("Default", 1.0))
37+
self.pitch = float(params.get("Pitch", {}).get("Default", 1.0))
3538
self.proxy = self.config.get("Proxy", "")
3639

3740
self.started_playing: float | None = None

src/torchlight/TriggerManager.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ def __init__(
2525
def Load(self) -> None:
2626
self.logger.info(f"Loading triggers from {self.config_filepath}")
2727

28+
voice_server_params = self.config.config.get("VoiceServer", {}).get("AudioParams", {})
2829
with open(self.config_filepath) as fp:
2930
default_parameters = {
30-
"Volume": float(self.config.config.get("VoiceServer", {}).get("Volume", 1.0)),
31-
"Speed": float(self.config.config.get("VoiceServer", {}).get("Speed", 1.0)),
32-
"Pitch": float(self.config.config.get("VoiceServer", {}).get("Pitch", 1.0)),
31+
"Volume": float(voice_server_params.get("Volume", {}).get("Default", 1.0)),
32+
"Speed": float(voice_server_params.get("Speed", {}).get("Default", 1.0)),
33+
"Pitch": float(voice_server_params.get("Pitch", {}).get("Default", 1.0)),
3334
}
3435

3536
self.triggers_dict = json.load(fp, object_pairs_hook=OrderedDict)

src/torchlight/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.3.2"
1+
__version__ = "1.4.0"

0 commit comments

Comments
 (0)