ttstoy: accept short piper voice names and keep voices stable across VC bounce
This commit is contained in:
+60
-12
@@ -2150,9 +2150,10 @@ class TtsToy(Cog):
|
|||||||
|
|
||||||
if voice is None:
|
if voice is None:
|
||||||
current = self._access_voice_for(ctx.guild.id, ctx.author.id, engine)
|
current = self._access_voice_for(ctx.guild.id, ctx.author.id, engine)
|
||||||
listing = ", ".join(f"`{v}`" for v in pool)
|
current_label = self._access_voice_label(engine, current)
|
||||||
|
listing = ", ".join(f"`{self._access_voice_label(engine, v)}`" for v in pool)
|
||||||
return await ctx.send(
|
return await ctx.send(
|
||||||
f"Engine `{engine}`. Your voice: `{current}`.\n"
|
f"Engine `{engine}`. Your voice: `{current_label}`.\n"
|
||||||
f"Available: {listing}\n"
|
f"Available: {listing}\n"
|
||||||
f"Set with `{ctx.clean_prefix}ttstoy accessibility voice <name>` "
|
f"Set with `{ctx.clean_prefix}ttstoy accessibility voice <name>` "
|
||||||
f"(or `auto` to be auto-assigned)."
|
f"(or `auto` to be auto-assigned)."
|
||||||
@@ -2164,17 +2165,23 @@ class TtsToy(Cog):
|
|||||||
# Drop any stale auto-assignment too so a fresh distinct one is picked.
|
# Drop any stale auto-assignment too so a fresh distinct one is picked.
|
||||||
self._access_auto_voice.get(ctx.guild.id, {}).pop(ctx.author.id, None)
|
self._access_auto_voice.get(ctx.guild.id, {}).pop(ctx.author.id, None)
|
||||||
new_voice = self._access_voice_for(ctx.guild.id, ctx.author.id, engine)
|
new_voice = self._access_voice_for(ctx.guild.id, ctx.author.id, engine)
|
||||||
return await ctx.send(f"Your accessibility voice is now auto-assigned: `{new_voice}`.")
|
return await ctx.send(
|
||||||
|
f"Your accessibility voice is now auto-assigned: "
|
||||||
|
f"`{self._access_voice_label(engine, new_voice)}`."
|
||||||
|
)
|
||||||
|
|
||||||
# Match case-insensitively against the pool.
|
# Accept a full id, a locale-stripped label, or a bare speaker name.
|
||||||
match = next((v for v in pool if v.lower() == choice.lower()), None)
|
match = self._access_match_voice(engine, choice)
|
||||||
if match is None:
|
if match is None:
|
||||||
listing = ", ".join(f"`{v}`" for v in pool)
|
listing = ", ".join(f"`{self._access_voice_label(engine, v)}`" for v in pool)
|
||||||
return await ctx.send(f"`{choice}` is not a `{engine}` voice. Available: {listing}")
|
return await ctx.send(f"`{choice}` is not a `{engine}` voice. Available: {listing}")
|
||||||
self._access_user_override.setdefault(ctx.guild.id, {})[ctx.author.id] = match
|
self._access_user_override.setdefault(ctx.guild.id, {})[ctx.author.id] = match
|
||||||
# Clear any auto-assignment so the override is unambiguous.
|
# Clear any auto-assignment so the override is unambiguous.
|
||||||
self._access_auto_voice.get(ctx.guild.id, {}).pop(ctx.author.id, None)
|
self._access_auto_voice.get(ctx.guild.id, {}).pop(ctx.author.id, None)
|
||||||
await ctx.send(f"Your accessibility voice is now `{match}` (not saved between sessions).")
|
await ctx.send(
|
||||||
|
f"Your accessibility voice is now `{self._access_voice_label(engine, match)}` "
|
||||||
|
f"(not saved between sessions)."
|
||||||
|
)
|
||||||
|
|
||||||
@ttstoy_group.command(name="piperurl")
|
@ttstoy_group.command(name="piperurl")
|
||||||
@commands.is_owner()
|
@commands.is_owner()
|
||||||
@@ -2719,6 +2726,47 @@ class TtsToy(Cog):
|
|||||||
"""Return the list of selectable voices for the given accessibility engine."""
|
"""Return the list of selectable voices for the given accessibility engine."""
|
||||||
return PIPER_VOICE_POOL if engine == "piper" else DECTALK_VOICE_POOL
|
return PIPER_VOICE_POOL if engine == "piper" else DECTALK_VOICE_POOL
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _access_voice_label(engine: str, voice: str) -> str:
|
||||||
|
"""Short, user-facing name for a pool voice.
|
||||||
|
|
||||||
|
For piper, strip the `en_US-` locale prefix so `en_US-amy-medium`
|
||||||
|
displays and is typed as `amy-medium`. DECTalk names are already short.
|
||||||
|
"""
|
||||||
|
if engine == "piper" and voice:
|
||||||
|
return re.sub(r"^[a-z]{2}_[A-Z]{2}-", "", voice)
|
||||||
|
return voice
|
||||||
|
|
||||||
|
def _access_match_voice(self, engine: str, choice: str) -> Optional[str]:
|
||||||
|
"""Resolve a user-typed voice name to a full pool id, or None.
|
||||||
|
|
||||||
|
Accepts, case-insensitively: the full id (`en_US-amy-medium`), the
|
||||||
|
locale-stripped label (`amy-medium`), or the bare speaker name (`amy`,
|
||||||
|
matching the first pool voice whose label starts with it). This is what
|
||||||
|
lets people type `amy` instead of `en_US-amy-medium`.
|
||||||
|
"""
|
||||||
|
pool = self._access_voice_pool(engine)
|
||||||
|
c = choice.strip().lower()
|
||||||
|
# 1) exact full id
|
||||||
|
for v in pool:
|
||||||
|
if v.lower() == c:
|
||||||
|
return v
|
||||||
|
# 2) exact locale-stripped label
|
||||||
|
for v in pool:
|
||||||
|
if self._access_voice_label(engine, v).lower() == c:
|
||||||
|
return v
|
||||||
|
# 3) bare speaker name: label up to the first '-' (piper) or whole name
|
||||||
|
for v in pool:
|
||||||
|
label = self._access_voice_label(engine, v).lower()
|
||||||
|
base = label.split("-", 1)[0]
|
||||||
|
if base == c:
|
||||||
|
return v
|
||||||
|
# 4) prefix match as a last resort (e.g. 'less' -> lessac-medium)
|
||||||
|
for v in pool:
|
||||||
|
if self._access_voice_label(engine, v).lower().startswith(c):
|
||||||
|
return v
|
||||||
|
return None
|
||||||
|
|
||||||
def _access_voice_for(self, guild_id: int, user_id: int, engine: str) -> str:
|
def _access_voice_for(self, guild_id: int, user_id: int, engine: str) -> str:
|
||||||
"""Resolve a speaker's accessibility voice.
|
"""Resolve a speaker's accessibility voice.
|
||||||
|
|
||||||
@@ -2896,12 +2944,12 @@ class TtsToy(Cog):
|
|||||||
if member.id != self.bot.user.id:
|
if member.id != self.bot.user.id:
|
||||||
return
|
return
|
||||||
if before.channel != after.channel and member.guild:
|
if before.channel != after.channel and member.guild:
|
||||||
|
# Only the name-prefix "last speaker" resets on a VC bounce.
|
||||||
|
# Voice assignments (auto AND override) are kept: the bot gets
|
||||||
|
# pulled out of the channel constantly (e.g. to show a mute
|
||||||
|
# notice), and wiping voices there made everyone's voice drift
|
||||||
|
# message to message. Voices clear only on start/stop/engine.
|
||||||
self._access_last_speaker.pop(member.guild.id, None)
|
self._access_last_speaker.pop(member.guild.id, None)
|
||||||
# Only auto-assignments reset on a VC bounce; explicit user
|
|
||||||
# picks are sticky so a mid-session reconnect (e.g. the bot
|
|
||||||
# being pulled out to show a mute notice) does not wipe them.
|
|
||||||
self._access_auto_voice.pop(member.guild.id, None)
|
|
||||||
self._access_assign_idx.pop(member.guild.id, None)
|
|
||||||
except Exception:
|
except Exception:
|
||||||
log.exception("[Accessibility] voice reset error")
|
log.exception("[Accessibility] voice reset error")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user