ttstoy: simplified voice names and cleaner accessibility voice list layout
This commit is contained in:
+25
-27
@@ -2151,18 +2151,12 @@ 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)
|
||||||
current_label = self._access_voice_label(engine, current)
|
current_label = self._access_voice_label(engine, current)
|
||||||
lines = []
|
options = "\n".join(self._access_voice_label(engine, v) for v in pool)
|
||||||
for v in pool:
|
|
||||||
lbl = self._access_voice_label(engine, v)
|
|
||||||
marker = ">" if v == current else " "
|
|
||||||
lines.append(f"{marker} {lbl}")
|
|
||||||
block = "\n".join(lines)
|
|
||||||
return await ctx.send(
|
return await ctx.send(
|
||||||
f"**Accessibility voices** (engine: `{engine}`)\n"
|
f"Current voice: {current_label}\n\n"
|
||||||
f"Your voice: `{current_label}` (marked with `>` below)\n"
|
f"list\n\n"
|
||||||
f"```\n{block}\n```\n"
|
f"{options}\n\n"
|
||||||
f"Set yours: `{ctx.clean_prefix}ttstoy accessibility voice <name>` "
|
f"{ctx.clean_prefix}ttstoy accessibility voice (voice name)"
|
||||||
f"(or `auto`)"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
choice = voice.strip()
|
choice = voice.strip()
|
||||||
@@ -2744,41 +2738,45 @@ class TtsToy(Cog):
|
|||||||
def _access_voice_label(engine: str, voice: str) -> str:
|
def _access_voice_label(engine: str, voice: str) -> str:
|
||||||
"""Short, user-facing name for a pool voice.
|
"""Short, user-facing name for a pool voice.
|
||||||
|
|
||||||
For piper, strip the `en_US-` locale prefix so `en_US-amy-medium`
|
For piper, strip the `en_US-` locale prefix AND the quality suffix
|
||||||
displays and is typed as `amy-medium`. DECTalk names are already short.
|
(`-medium`/`-low`/`-high`) so `en_US-hfc_male-medium` displays and is
|
||||||
|
typed as `hfc_male`. DECTalk names are already short.
|
||||||
"""
|
"""
|
||||||
if engine == "piper" and voice:
|
if engine == "piper" and voice:
|
||||||
return re.sub(r"^[a-z]{2}_[A-Z]{2}-", "", voice)
|
name = re.sub(r"^[a-z]{2}_[A-Z]{2}-", "", voice)
|
||||||
|
name = re.sub(r"-(low|medium|high)$", "", name)
|
||||||
|
return name
|
||||||
return voice
|
return voice
|
||||||
|
|
||||||
def _access_match_voice(self, engine: str, choice: str) -> Optional[str]:
|
def _access_match_voice(self, engine: str, choice: str) -> Optional[str]:
|
||||||
"""Resolve a user-typed voice name to a full pool id, or None.
|
"""Resolve a user-typed voice name to a full pool id, or None.
|
||||||
|
|
||||||
Accepts, case-insensitively: the full id (`en_US-amy-medium`), the
|
Accepts, case-insensitively: the simplified name (`hfc_male`), the
|
||||||
locale-stripped label (`amy-medium`), or the bare speaker name (`amy`,
|
locale-stripped form with quality (`hfc_male-medium`), the full id
|
||||||
matching the first pool voice whose label starts with it). This is what
|
(`en_US-hfc_male-medium`), or an unambiguous prefix.
|
||||||
lets people type `amy` instead of `en_US-amy-medium`.
|
|
||||||
"""
|
"""
|
||||||
pool = self._access_voice_pool(engine)
|
pool = self._access_voice_pool(engine)
|
||||||
c = choice.strip().lower()
|
c = choice.strip().lower()
|
||||||
|
|
||||||
|
def locale_stripped(v):
|
||||||
|
return re.sub(r"^[a-z]{2}_[A-Z]{2}-", "", v).lower() if engine == "piper" else v.lower()
|
||||||
|
|
||||||
# 1) exact full id
|
# 1) exact full id
|
||||||
for v in pool:
|
for v in pool:
|
||||||
if v.lower() == c:
|
if v.lower() == c:
|
||||||
return v
|
return v
|
||||||
# 2) exact locale-stripped label
|
# 2) exact simplified label (speaker only, e.g. 'hfc_male')
|
||||||
for v in pool:
|
for v in pool:
|
||||||
if self._access_voice_label(engine, v).lower() == c:
|
if self._access_voice_label(engine, v).lower() == c:
|
||||||
return v
|
return v
|
||||||
# 3) bare speaker name: label up to the first '-' (piper) or whole name
|
# 3) exact locale-stripped form with quality (e.g. 'hfc_male-medium')
|
||||||
for v in pool:
|
for v in pool:
|
||||||
label = self._access_voice_label(engine, v).lower()
|
if locale_stripped(v) == c:
|
||||||
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 v
|
||||||
|
# 4) unambiguous prefix of the simplified label (e.g. 'kris' -> kristin)
|
||||||
|
matches = [v for v in pool if self._access_voice_label(engine, v).lower().startswith(c)]
|
||||||
|
if len(matches) == 1:
|
||||||
|
return matches[0]
|
||||||
return None
|
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:
|
||||||
|
|||||||
Reference in New Issue
Block a user