ttstoy: accessibility command group with per-speaker voices
Consolidate accessibility into one command group: ttstoy accessibility start ttstoy accessibility stop ttstoy accessibility engine <dectalk|piper> ttstoy accessibility voice [name|auto] Per-speaker distinct voices so concurrent users in accessibility auto-read can be told apart: - Auto-assigned round-robin from the active engine's voice pool (9 DECTalk named voices, 19 Piper models), guaranteeing distinct voices until the pool is exhausted (then wraps). - Users set their own voice with 'accessibility voice <name>', 'auto' to return to auto-assignment. - Assignments are in-memory only and are NOT saved between sessions; they reset on start/stop, engine change, and when the bot changes voice channel. - dectalk voice travels as voice_id, piper voice as the model id.
This commit is contained in:
+153
-23
@@ -82,6 +82,18 @@ DECTALK_VOICES = {
|
||||
CHATTERBOX_DEFAULT_URL = "http://127.0.0.1:8099"
|
||||
PIPER_DEFAULT_URL = "http://127.0.0.1:5006"
|
||||
|
||||
# Accessibility auto-read voice pools (used to tell speakers apart).
|
||||
# DECTalk: the nine built-in named voices.
|
||||
DECTALK_VOICE_POOL = ["paul", "betty", "harry", "frank", "dennis", "kit", "ursula", "rita", "wendy"]
|
||||
# Piper: model ids available on the Piper server (filenames without .onnx).
|
||||
PIPER_VOICE_POOL = [
|
||||
"en_US-ryan-medium", "en_US-amy-medium", "en_US-hfc_male-medium", "en_US-hfc_female-medium",
|
||||
"en_US-joe-medium", "en_US-kristin-medium", "en_US-john-medium", "en_US-kathleen-low",
|
||||
"en_US-lessac-medium", "en_US-norman-medium", "en_US-danny-low", "en_US-sam-medium",
|
||||
"en_US-bryce-medium", "en_US-mike-medium", "en_US-libritts_r-medium", "en_US-ljspeech-medium",
|
||||
"en_US-amy-low", "en_US-lessac-high", "en_US-ryan-high",
|
||||
]
|
||||
|
||||
_DEFAULT_EMOJI_SFX = {
|
||||
"🎉": "party",
|
||||
"😂": "laugh",
|
||||
@@ -385,6 +397,8 @@ class TtsToy(Cog):
|
||||
self.tts_locks = {} # guild_id -> asyncio.Lock
|
||||
self.tts_processors = {} # guild_id -> asyncio.Task
|
||||
self._access_last_speaker = {} # guild_id -> last author id spoken (accessibility auto-read)
|
||||
self._access_user_voice = {} # guild_id -> {user_id: voice} (in-memory only, not persisted)
|
||||
self._access_assign_idx = {} # guild_id -> next round-robin index into the voice pool
|
||||
|
||||
self._ensure_sfx_folders()
|
||||
self.clear_old_tts.start()
|
||||
@@ -2040,43 +2054,115 @@ class TtsToy(Cog):
|
||||
await self.config.dectalk_api_url.set(url)
|
||||
await ctx.send(f"✅ DECTalk API URL set to: `{url}`")
|
||||
|
||||
@ttstoy_group.command(name="accessibility")
|
||||
@commands.is_owner()
|
||||
async def accessibility_toggle(self, ctx: commands.Context):
|
||||
"""Toggle accessibility mode (forces DECTalk, disables SFX emojis)."""
|
||||
current = await self.config.accessibility_mode()
|
||||
new_state = not current
|
||||
await self.config.accessibility_mode.set(new_state)
|
||||
|
||||
if new_state:
|
||||
@ttstoy_group.group(name="accessibility", aliases=["access"], invoke_without_command=True)
|
||||
async def accessibility_group(self, ctx: commands.Context):
|
||||
"""Accessibility mode: auto-read the VC side chat with per-speaker voices."""
|
||||
if ctx.invoked_subcommand is None:
|
||||
on = await self.config.accessibility_mode()
|
||||
engine = await self.config.accessibility_engine()
|
||||
await ctx.send(
|
||||
"**Accessibility mode enabled.**\n"
|
||||
"- TTS forced to DECTalk\n"
|
||||
"- Emoji SFX disabled (emojis read as text instead)\n"
|
||||
"- Auto-reads the side chat of the voice channel the bot is in (no command prefix needed)"
|
||||
f"Accessibility mode is **{'ON' if on else 'OFF'}** (engine: `{engine}`).\n"
|
||||
f"- `{ctx.clean_prefix}ttstoy accessibility start`\n"
|
||||
f"- `{ctx.clean_prefix}ttstoy accessibility stop`\n"
|
||||
f"- `{ctx.clean_prefix}ttstoy accessibility engine <dectalk|piper>`\n"
|
||||
f"- `{ctx.clean_prefix}ttstoy accessibility voice [name]`"
|
||||
)
|
||||
else:
|
||||
await ctx.send("**Accessibility mode disabled.** Normal TTS settings restored.")
|
||||
|
||||
@ttstoy_group.command(name="accessibilityengine", aliases=["accessengine", "accessibilitytts"])
|
||||
@accessibility_group.command(name="start")
|
||||
@commands.is_owner()
|
||||
async def accessibility_start(self, ctx: commands.Context):
|
||||
"""Enable accessibility mode."""
|
||||
await self.config.accessibility_mode.set(True)
|
||||
engine = await self.config.accessibility_engine()
|
||||
# Fresh session: clear any lingering per-user voice assignments.
|
||||
if ctx.guild:
|
||||
self._access_user_voice.pop(ctx.guild.id, None)
|
||||
self._access_assign_idx.pop(ctx.guild.id, None)
|
||||
self._access_last_speaker.pop(ctx.guild.id, None)
|
||||
await ctx.send(
|
||||
"**Accessibility mode started.**\n"
|
||||
f"- Engine: `{engine}`\n"
|
||||
"- Emoji SFX disabled (emojis read as text instead)\n"
|
||||
"- Auto-reads the side chat of the voice channel the bot is in (no command prefix needed)\n"
|
||||
"- Each speaker is auto-assigned a distinct voice so they can be told apart\n"
|
||||
f"- Change your own voice with `{ctx.clean_prefix}ttstoy accessibility voice`"
|
||||
)
|
||||
|
||||
@accessibility_group.command(name="stop")
|
||||
@commands.is_owner()
|
||||
async def accessibility_stop(self, ctx: commands.Context):
|
||||
"""Disable accessibility mode."""
|
||||
await self.config.accessibility_mode.set(False)
|
||||
# Drop all in-memory per-user voice assignments (never persisted anyway).
|
||||
if ctx.guild:
|
||||
self._access_user_voice.pop(ctx.guild.id, None)
|
||||
self._access_assign_idx.pop(ctx.guild.id, None)
|
||||
self._access_last_speaker.pop(ctx.guild.id, None)
|
||||
await ctx.send("**Accessibility mode stopped.** Normal TTS settings restored.")
|
||||
|
||||
@accessibility_group.command(name="engine", aliases=["tts"])
|
||||
@commands.is_owner()
|
||||
async def accessibility_engine_cmd(self, ctx: commands.Context, engine: Optional[str] = None):
|
||||
"""
|
||||
Show or set the engine accessibility mode uses: dectalk or piper.
|
||||
|
||||
- `[p]ttstoy accessibilityengine` -> show current engine
|
||||
- `[p]ttstoy accessibilityengine piper` -> use the Piper TTS server
|
||||
- `[p]ttstoy accessibilityengine dectalk` -> use the DECTalk server
|
||||
Changing the engine clears per-user voice assignments (voices differ
|
||||
between engines).
|
||||
"""
|
||||
if engine is None:
|
||||
current = await self.config.accessibility_engine()
|
||||
return await ctx.send(f"Accessibility engine: `{current}`\nSet with `{ctx.clean_prefix}ttstoy accessibilityengine <dectalk|piper>`")
|
||||
return await ctx.send(f"Accessibility engine: `{current}`\nSet with `{ctx.clean_prefix}ttstoy accessibility engine <dectalk|piper>`")
|
||||
|
||||
engine = engine.strip().lower()
|
||||
if engine not in ("dectalk", "piper"):
|
||||
return await ctx.send("Engine must be `dectalk` or `piper`.")
|
||||
await self.config.accessibility_engine.set(engine)
|
||||
await ctx.send(f"Accessibility engine set to `{engine}`.")
|
||||
if ctx.guild:
|
||||
self._access_user_voice.pop(ctx.guild.id, None)
|
||||
self._access_assign_idx.pop(ctx.guild.id, None)
|
||||
await ctx.send(f"Accessibility engine set to `{engine}`. Per-user voices reset.")
|
||||
|
||||
@accessibility_group.command(name="voice")
|
||||
@commands.guild_only()
|
||||
async def accessibility_voice(self, ctx: commands.Context, *, voice: Optional[str] = None):
|
||||
"""
|
||||
Pick your own accessibility voice for the current engine.
|
||||
|
||||
- `[p]ttstoy accessibility voice` -> list the voices and show yours
|
||||
- `[p]ttstoy accessibility voice <name>` -> set your voice
|
||||
- `[p]ttstoy accessibility voice auto` -> go back to an auto-assigned voice
|
||||
|
||||
Voices are not saved between sessions.
|
||||
"""
|
||||
engine = await self.config.accessibility_engine()
|
||||
if engine not in ("dectalk", "piper"):
|
||||
engine = "dectalk"
|
||||
pool = self._access_voice_pool(engine)
|
||||
|
||||
if voice is None:
|
||||
current = self._access_voice_for(ctx.guild.id, ctx.author.id, engine)
|
||||
listing = ", ".join(f"`{v}`" for v in pool)
|
||||
return await ctx.send(
|
||||
f"Engine `{engine}`. Your voice: `{current}`.\n"
|
||||
f"Available: {listing}\n"
|
||||
f"Set with `{ctx.clean_prefix}ttstoy accessibility voice <name>` "
|
||||
f"(or `auto` to be auto-assigned)."
|
||||
)
|
||||
|
||||
choice = voice.strip()
|
||||
gmap = self._access_user_voice.setdefault(ctx.guild.id, {})
|
||||
if choice.lower() == "auto":
|
||||
gmap.pop(ctx.author.id, None)
|
||||
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}`.")
|
||||
|
||||
# Match case-insensitively against the pool.
|
||||
match = next((v for v in pool if v.lower() == choice.lower()), None)
|
||||
if match is None:
|
||||
listing = ", ".join(f"`{v}`" for v in pool)
|
||||
return await ctx.send(f"`{choice}` is not a `{engine}` voice. Available: {listing}")
|
||||
gmap[ctx.author.id] = match
|
||||
await ctx.send(f"Your accessibility voice is now `{match}` (not saved between sessions).")
|
||||
|
||||
@ttstoy_group.command(name="piperurl")
|
||||
@commands.is_owner()
|
||||
@@ -2617,6 +2703,43 @@ class TtsToy(Cog):
|
||||
# ---------------------------------------------------------------------
|
||||
# ACCESSIBILITY AUTO-READ (VC side-chat, no command prefix)
|
||||
# ---------------------------------------------------------------------
|
||||
def _access_voice_pool(self, engine: str) -> list:
|
||||
"""Return the list of selectable voices for the given accessibility engine."""
|
||||
return PIPER_VOICE_POOL if engine == "piper" else DECTALK_VOICE_POOL
|
||||
|
||||
def _access_voice_for(self, guild_id: int, user_id: int, engine: str) -> str:
|
||||
"""Resolve a speaker's accessibility voice.
|
||||
|
||||
Returns an explicit per-user override if one is set, otherwise
|
||||
auto-assigns the next voice from the engine's pool (round-robin, so
|
||||
concurrent speakers get distinct voices). Assignments live in memory
|
||||
only and are not persisted between sessions.
|
||||
"""
|
||||
pool = self._access_voice_pool(engine)
|
||||
if not pool:
|
||||
return None
|
||||
gmap = self._access_user_voice.setdefault(guild_id, {})
|
||||
existing = gmap.get(user_id)
|
||||
# Keep an existing assignment only if it is valid for the current engine.
|
||||
if existing in pool:
|
||||
return existing
|
||||
# Prefer a voice not already handed out in this guild, round-robin.
|
||||
used = set(gmap.values())
|
||||
idx = self._access_assign_idx.get(guild_id, 0)
|
||||
chosen = None
|
||||
for _ in range(len(pool)):
|
||||
cand = pool[idx % len(pool)]
|
||||
idx += 1
|
||||
if cand not in used:
|
||||
chosen = cand
|
||||
break
|
||||
if chosen is None: # pool exhausted: wrap around and reuse
|
||||
chosen = pool[self._access_assign_idx.get(guild_id, 0) % len(pool)]
|
||||
idx = self._access_assign_idx.get(guild_id, 0) + 1
|
||||
self._access_assign_idx[guild_id] = idx
|
||||
gmap[user_id] = chosen
|
||||
return chosen
|
||||
|
||||
def _access_describe_message(self, message: discord.Message) -> Optional[str]:
|
||||
"""Build the spoken text for an accessibility auto-read of a VC side-chat message.
|
||||
|
||||
@@ -2697,16 +2820,21 @@ class TtsToy(Cog):
|
||||
engine = "dectalk"
|
||||
dectalk_url = await self._get_dectalk_api_url()
|
||||
piper_url = await self._get_piper_api_url()
|
||||
piper_voice = await self.config.piper_voice()
|
||||
sfx_volume = await self.config.sfx_volume()
|
||||
|
||||
# Distinct per-speaker voice so users can be told apart.
|
||||
user_voice = self._access_voice_for(guild.id, message.author.id, engine)
|
||||
# dectalk voice travels as voice_id; piper voice as piper_voice.
|
||||
dectalk_voice = user_voice if engine == "dectalk" else None
|
||||
piper_voice = user_voice if engine == "piper" else await self.config.piper_voice()
|
||||
|
||||
self.tts_storage.mkdir(parents=True, exist_ok=True)
|
||||
audio_path = str(self.tts_storage.joinpath(f"access-{message.id}.mp3"))
|
||||
|
||||
try:
|
||||
await asyncio.to_thread(
|
||||
self._save_prompt_audio,
|
||||
None, None, None, spoken, audio_path, sfx_volume,
|
||||
None, None, dectalk_voice, spoken, audio_path, sfx_volume,
|
||||
engine, dectalk_url, "vox", None, None, None, 0.0, None, None,
|
||||
piper_url, piper_voice,
|
||||
)
|
||||
@@ -2743,6 +2871,8 @@ class TtsToy(Cog):
|
||||
return
|
||||
if before.channel != after.channel and member.guild:
|
||||
self._access_last_speaker.pop(member.guild.id, None)
|
||||
self._access_user_voice.pop(member.guild.id, None)
|
||||
self._access_assign_idx.pop(member.guild.id, None)
|
||||
except Exception:
|
||||
log.exception("[Accessibility] voice reset error")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user