Skip to content

Commit a3ccd40

Browse files
authored
feat(triggers): start and end modifiers (#115)
1 parent 97cf5f6 commit a3ccd40

5 files changed

Lines changed: 39 additions & 16 deletions

File tree

src/torchlight/AudioClip.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ def __del__(self) -> None:
3636
def Play(
3737
self,
3838
seconds: int | None = None,
39+
duration: int | None = None,
3940
*args: Any,
4041
volume: float | None = None,
4142
speed: float | None = None,
4243
pitch: float | None = None,
4344
) -> bool:
44-
return self.audio_player.PlayURI(self.uri, seconds, *args, volume=volume, speed=speed, pitch=pitch)
45+
return self.audio_player.PlayURI(self.uri, seconds, duration, *args, volume=volume, speed=speed, pitch=pitch)
4546

4647
def Stop(self) -> bool:
4748
return self.audio_player.Stop()

src/torchlight/AudioManager.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ def ParseParams(self, trigger_params: dict, msg: str) -> dict[str, float]:
2727
return trigger_params
2828

2929
params: dict[str, float] = {}
30-
for param in this_config:
30+
for param, config in this_config.items():
3131
if param in trigger_params and trigger_params[param] is not None:
3232
params[param] = float(trigger_params[param])
3333
else:
34-
if isinstance(this_config[param], dict) and "Default" in this_config[param]:
35-
params[param] = float(this_config[param]["Default"])
34+
if isinstance(config, dict) and "Default" in config:
35+
params[param] = float(config["Default"])
3636

3737
msg_args = msg.split()
3838
for arg in msg_args:
@@ -42,21 +42,22 @@ def ParseParams(self, trigger_params: dict, msg: str) -> dict[str, float]:
4242
continue
4343

4444
key = key.capitalize()
45-
if key not in this_config:
46-
continue
4745

48-
if isinstance(this_config[key], dict):
49-
val: float = 1.0
46+
if (key in this_config and isinstance(this_config[key], dict)) or key in ("Start", "End"):
5047
try:
5148
val = float(value)
5249
except ValueError:
5350
continue
5451

55-
if "Min" not in this_config[key] or "Max" not in this_config[key]:
56-
continue
52+
if key in this_config:
53+
if "Min" not in this_config[key] or "Max" not in this_config[key]:
54+
continue
5755

58-
val = max(this_config[key]["Min"], min(val, this_config[key]["Max"]))
59-
params[key] = val
56+
val = max(this_config[key]["Min"], min(val, this_config[key]["Max"]))
57+
params[key] = val
58+
else:
59+
# Key here is either Start or End
60+
params[key] = val
6061

6162
return params
6263

src/torchlight/Commands.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,18 @@ async def _func(self, message: list[str], player: Player) -> int:
701701
speed = modifiers["Speed"]
702702
pitch = modifiers["Pitch"]
703703

704-
return audio_clip.Play(volume=volume, speed=speed, pitch=pitch)
704+
start: int | None = None
705+
duration: int | None = None
706+
707+
if "Start" in modifiers:
708+
start = int(modifiers["Start"])
709+
start = start if start >= 0 else None
710+
if "End" in modifiers:
711+
end = int(modifiers["End"])
712+
if end > 0 and start is not None and end > start:
713+
duration = end - start
714+
715+
return audio_clip.Play(seconds=start, duration=duration, volume=volume, speed=speed, pitch=pitch)
705716

706717
def get_sound_path(self, player: Player, voice_trigger: str, trigger_number: str) -> str | None:
707718
level = player.admin.level
@@ -1189,7 +1200,7 @@ async def Say(self, player: Player, message: str) -> int:
11891200
os.unlink(temp_file.name)
11901201
return 1
11911202

1192-
if audio_clip.Play(None, "-af", "volume=10dB"):
1203+
if audio_clip.Play(None, None, "-af", "volume=10dB"):
11931204
audio_clip.audio_player.AddCallback("Stop", lambda: os.unlink(temp_file.name))
11941205
return 0
11951206

src/torchlight/FFmpegAudioPlayer.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def PlayURI(
5656
self,
5757
uri: str,
5858
position: int | None,
59+
duration: int | None,
5960
*args: Any,
6061
volume: float | None = None,
6162
speed: float | None = None,
@@ -108,7 +109,6 @@ def PlayURI(
108109
"s16le",
109110
"-vn",
110111
*args,
111-
"-",
112112
]
113113

114114
if position is not None:
@@ -121,6 +121,16 @@ def PlayURI(
121121
)
122122
self.position = position
123123

124+
if duration is not None:
125+
ffmpeg_command.extend(
126+
[
127+
"-t",
128+
str(duration),
129+
]
130+
)
131+
132+
ffmpeg_command.append("-")
133+
124134
self.logger.debug(curl_command)
125135
self.logger.debug(ffmpeg_command)
126136

src/torchlight/__init__.py

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

0 commit comments

Comments
 (0)