diff --git a/CMakeLists.txt b/CMakeLists.txt index 6970aaab..7bdbeacb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,10 +99,7 @@ target_compile_definitions( ${PROJECT_NAME} PRIVATE CLASSICAPI_VERSION_VALUE=${CLASSICAPI_VERSION_VALUE}) -# ole32: COM (CoInitializeEx / CoCreateInstance / CoTaskMemFree) for the -# SAPI text-to-speech backend in src/voicechat/Tts.cpp. The SAPI GUIDs are -# instantiated in that TU via , so no sapi.lib is needed. -target_link_libraries(${PROJECT_NAME} PRIVATE minhook advapi32 ole32) +target_link_libraries(${PROJECT_NAME} PRIVATE minhook advapi32) target_include_directories( ${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src diff --git a/README.md b/README.md index 37111aa4..f8110f53 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,6 @@ Full per-function reference: **[docs/API.md](docs/API.md)**. | [UIColor](docs/API.md#uicolor) | `C_UIColor.GetColors` | | [Unit](docs/API.md#unit) | `GetUnitSpeed`, `UnitClassBase`, `UnitGUID`, `UnitInRange`, `UnitIsAFK`, `UnitIsDND`, `UnitIsFeignDeath`, `UnitIsInMyGuild`, `UnitIsPossessed`, `UnitPower`, `UnitPowerMax`, `UnitPowerType`, `UnitRaceBase`, `UnitStandState`, `UnitSubName`, `UnitTokenFromGUID` | | [UnitAuras](docs/API.md#unitauras) | `C_UnitAuras.GetAuraDataByIndex`, `C_UnitAuras.GetAuraDataBySpellName`, `C_UnitAuras.GetAuraDispelTypeColor`, `C_UnitAuras.GetBuffDataByIndex`, `C_UnitAuras.GetDebuffDataByIndex`, `C_UnitAuras.GetPlayerAuraBySpellID`, `C_UnitAuras.GetUnitAuraBySpellID`, `C_UnitAuras.GetUnitAuras` | -| [VoiceChat](docs/API.md#voicechat) | `C_VoiceChat.GetTtsVoices`, `C_VoiceChat.GetRemoteTtsVoices`, `C_VoiceChat.SpeakText`, `C_VoiceChat.StopSpeakingText`, `C_TTSSettings.GetSpeechRate`, `C_TTSSettings.GetSpeechVolume`, `C_TTSSettings.GetSpeechVoiceID`, `C_TTSSettings.GetVoiceOptionName`, `C_TTSSettings.SetSpeechRate`, `C_TTSSettings.SetSpeechVolume`, `C_TTSSettings.SetVoiceOption`, `C_TTSSettings.SetVoiceOptionByName`, `C_TTSSettings.SetDefaultSettings`, `C_TTSSettings.RefreshVoices` | ### GlueXML calls @@ -133,10 +132,6 @@ when launching with `-console`), not as Lua functions. See the | `QUEST_DATA_LOAD_RESULT` | `questID, success` | | `QUEST_TURNED_IN` | `questID, xpReward, moneyReward` | | `UPDATE_SHAPESHIFT_FORM` | *(none)* | -| `VOICE_CHAT_TTS_PLAYBACK_STARTED` | `numConsumers, utteranceID, durationMS, destination` | -| `VOICE_CHAT_TTS_PLAYBACK_FINISHED` | `numConsumers, utteranceID, destination` | -| `VOICE_CHAT_TTS_PLAYBACK_FAILED` | `status, utteranceID, destination` | -| `VOICE_CHAT_TTS_VOICES_UPDATE` | *(none)* | ### Globals diff --git a/docs/API.md b/docs/API.md index 6cccc88f..606d6102 100644 --- a/docs/API.md +++ b/docs/API.md @@ -392,14 +392,6 @@ build instructions. - [`C_UnitAuras.GetUnitAuras(unit [, filter])`](#c_unitaurasgetunitaurasunit--filter) - [`C_UnitAuras.GetAuraDispelTypeColor(dispelName)`](#c_unitaurasgetauradispeltypecolordispelname) -- [VoiceChat](#voicechat) - - [`C_VoiceChat.GetTtsVoices()` / `C_VoiceChat.GetRemoteTtsVoices()`](#c_voicechatgetttsvoices--c_voicechatgetremotettsvoices) - - [`C_VoiceChat.SpeakText(voiceID, text [, destination, rate, volume])`](#c_voicechatspeaktextvoiceid-text--destination-rate-volume) - - [`C_VoiceChat.StopSpeakingText()`](#c_voicechatstopspeakingtext) - - [`C_TTSSettings` — getters & setters](#c_ttssettings--getters--setters) - - [TTS events](#tts-events) - - [TTS CVars](#tts-cvars) - ## Account ### `SaveAccount(name, password)` / `DeleteAccount(name)` / `GetSavedAccounts()` / `LoginWithSavedAccount(name)` — GlueXML only @@ -9463,122 +9455,3 @@ builds. So the returned value carries the mixin methods `.r/.g/.b/.a` fields. Falls back to a plain `{r,g,b,a}` table if `CreateColor` isn't loaded yet (shouldn't happen — `!!!ClassicAPI` loads first thanks to the triple-`!` prefix). - -## VoiceChat - -Backport of modern WoW's text-to-speech surface — the `C_VoiceChat` and -`C_TTSSettings` namespaces — backed by **Windows SAPI** (the OS speech -engine). All playback is **local** (rendered on your own machine); -vanilla 1.12 has no voice-chat transport, so there is nothing "remote" to -transmit synthesized speech into. - -SAPI is created lazily on first use. If SAPI is unavailable (e.g. running -the client under Wine, or a stripped Windows), every call degrades -gracefully: `GetTtsVoices` returns an empty table, `SpeakText` fires -`VOICE_CHAT_TTS_PLAYBACK_FAILED` and does nothing, and the client stays -stable — there is no crash. Install more voices under **Windows Settings → -Time & Language → Speech**, then call `C_TTSSettings.RefreshVoices()`. - -### `C_VoiceChat.GetTtsVoices()` / `C_VoiceChat.GetRemoteTtsVoices()` - -Return a numerically-indexed array of voice tables — one per SAPI voice -installed on the machine: - -```lua -for _, v in ipairs(C_VoiceChat.GetTtsVoices()) do - print(v.voiceID, v.name) -- e.g. 0 "Microsoft David Desktop - English (United States)" -end -``` - -Each entry is `{ voiceID = <0-based index>, name = }`. The -`voiceID` is the index you pass to `SpeakText` and store via -`C_TTSSettings.SetVoiceOption`. `GetRemoteTtsVoices` is an alias returning -the same local list (kept for API parity — vanilla has no voice chat). -Calling either refreshes the cached voice list and fires -`VOICE_CHAT_TTS_VOICES_UPDATE` if it changed. - -### `C_VoiceChat.SpeakText(voiceID, text [, destination, rate, volume])` - -Speaks `text` aloud using the SAPI voice at index `voiceID`. - -```lua -C_VoiceChat.SpeakText( - C_TTSSettings.GetSpeechVoiceID(), -- use the configured voice - UnitName("target") or "no target") -``` - -- **`voiceID`** (number, required) — index into `GetTtsVoices()`. Note this - is an **explicit argument** — `SpeakText` does *not* read the `ttsVoice` - cvar itself, matching Blizzard's contract. Pass - `C_TTSSettings.GetSpeechVoiceID()` to honor the configured default. -- **`text`** (string, required) — UTF-8; spoken as-is. -- **`destination`** (number, optional, default `1`) — `1` = - `LOCAL_PLAYBACK`, `4` = `QUEUED_LOCAL_PLAYBACK`. Accepted for parity; - both queue FIFO through SAPI (no app-side queue). -- **`rate`** (number, optional, default `0`) — speech rate, clamped to - `[-10, 10]`. -- **`volume`** (number, optional, default `100`) — clamped to `[0, 100]`. - -Speech is asynchronous. The call returns immediately and fires -`VOICE_CHAT_TTS_PLAYBACK_STARTED` when SAPI begins the utterance and -`VOICE_CHAT_TTS_PLAYBACK_FINISHED` when it completes (or -`..._PLAYBACK_FAILED` if the voice/engine is unavailable). Raises a Lua -error if `voiceID` isn't a number or `text` isn't a string. - -### `C_VoiceChat.StopSpeakingText()` - -Stops and purges everything currently playing or queued. - -### `C_TTSSettings` — getters & setters - -The settings store (separate from `SpeakText`, which takes its parameters -explicitly). All three values are backed by CVars (see below), so they -**persist across sessions** and are clamped to valid ranges on write. - -| Function | Returns / effect | -|---|---| -| `C_TTSSettings.GetSpeechRate()` | configured rate, `[-10, 10]` (default `0`) | -| `C_TTSSettings.GetSpeechVolume()` | configured volume, `[0, 100]` (default `100`) | -| `C_TTSSettings.GetSpeechVoiceID()` | configured voice index (default `0`) | -| `C_TTSSettings.GetVoiceOptionName()` | display name of the configured voice, or `""` | -| `C_TTSSettings.SetSpeechRate(rate)` | set rate (clamped) | -| `C_TTSSettings.SetSpeechVolume(volume)` | set volume (clamped) | -| `C_TTSSettings.SetVoiceOption(voiceID)` | set the voice by index (clamped to installed count) | -| `C_TTSSettings.SetVoiceOptionByName(name)` | set the voice by display name (case-insensitive exact match) | -| `C_TTSSettings.SetDefaultSettings()` | reset to voice `1` (if present, else `0`), rate `0`, volume `100` | -| `C_TTSSettings.RefreshVoices()` | re-enumerate SAPI voices; fires `VOICE_CHAT_TTS_VOICES_UPDATE` if the list changed | - -```lua -C_TTSSettings.SetVoiceOptionByName("Microsoft Zira Desktop - English (United States)") -C_TTSSettings.SetSpeechVolume(150) -- clamps to 100 -print(C_TTSSettings.GetVoiceOptionName()) -- "Microsoft Zira Desktop - ..." -``` - -### TTS events - -Registered like any engine event (`frame:RegisterEvent("VOICE_CHAT_TTS_PLAYBACK_STARTED")`). - -| Event | Args | -|---|---| -| `VOICE_CHAT_TTS_PLAYBACK_STARTED` | `numConsumers` (number), `utteranceID` (number), `durationMS` (number, always `0`), `destination` (number) | -| `VOICE_CHAT_TTS_PLAYBACK_FINISHED` | `numConsumers` (number), `utteranceID` (number), `destination` (number) | -| `VOICE_CHAT_TTS_PLAYBACK_FAILED` | `status` (string — e.g. `"EngineAllocationFailed"`, `"InternalError"`), `utteranceID` (number), `destination` (number) | -| `VOICE_CHAT_TTS_VOICES_UPDATE` | *(none)* — the installed-voice list changed | -| `VOICE_CHAT_TTS_SPEAK_TEXT_UPDATE` | reserved for parity; not currently fired | - -`utteranceID` correlates the START/FINISHED/FAILED events for a given -`SpeakText` call. `PLAYBACK_FINISHED` fires only when playback actually -completes, so it's the signal to start the next utterance in a queue. - -### TTS CVars - -The `C_TTSSettings` values are stored as engine CVars, persisted to -`WTF\Config.wtf`. You can also read/write them with the standard -`GetCVar`/`SetCVar` (or `/console set`); writes are clamped by the same -validation: - -| CVar | Default | Range | Backing setting | -|---|---|---|---| -| `ttsVoice` | `0` | `[0, voiceCount-1]` | `GetSpeechVoiceID` / `SetVoiceOption` | -| `ttsSpeed` | `0` | `[-10, 10]` | `GetSpeechRate` / `SetSpeechRate` | -| `ttsVolume` | `100` | `[0, 100]` | `GetSpeechVolume` / `SetSpeechVolume` | diff --git a/src/cvar/Factory.h b/src/cvar/Factory.h index ad0a6a22..dfbbc552 100644 --- a/src/cvar/Factory.h +++ b/src/cvar/Factory.h @@ -22,8 +22,8 @@ // an addon to register a cvar with a change-callback or to react to value // changes. This wraps the engine's internal registrar (`FUN_REGISTER_CVAR`) // and value-setter (`FUN_SET_CVAR_VALUE`) so other modules can publish -// persisted, validated settings — e.g. the TTS module's ttsVoice / ttsSpeed -// / ttsVolume. +// persisted, validated settings (clamped via the change callback and saved +// to Config.wtf). // // CVars registered here persist to `WTF\Config.wtf` (the engine forces the // archive flag bit on at registration) and are visible to the in-game Lua diff --git a/src/voicechat/Tts.cpp b/src/voicechat/Tts.cpp deleted file mode 100644 index dede9343..00000000 --- a/src/voicechat/Tts.cpp +++ /dev/null @@ -1,553 +0,0 @@ -// This file is part of ClassicAPI. -// -// ClassicAPI is free software: you can redistribute it and/or modify it under the terms -// of the GNU General Public License as published by the Free Software Foundation, either -// version 3 of the License, or (at your option) any later version. -// -// ClassicAPI is distributed in the hope that it will be useful, but WITHOUT ANY -// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR -// PURPOSE. See the GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License along with -// ClassicAPI. If not, see . - -// Text-to-speech — a Windows SAPI backend behind modern WoW's C_VoiceChat / -// C_TTSSettings Lua surface. Ported from awesome_wotlk's VoiceChat.cpp (the -// 3.3.5 implementation); the SAPI core is client-version-agnostic, so only -// the three integration seams are ClassicAPI-native: -// - namespaces → Game::Lua::RegisterTableFunction -// - events → Event::Custom (AutoReserve + Fire) -// - settings → CVar::Factory (ttsVoice / ttsSpeed / ttsVolume, persisted -// to Config.wtf, clamped via change callbacks) -// -// Lua surface: -// C_VoiceChat -// GetTtsVoices() -> { {voiceID=, name=}, ... } -// GetRemoteTtsVoices() -> same (vanilla has no real voice chat) -// SpeakText(voiceID, text[, destination[, rate[, volume]]]) -// StopSpeakingText() -// C_TTSSettings -// GetSpeechRate() / GetSpeechVolume() / GetSpeechVoiceID() -// GetVoiceOptionName() -// SetDefaultSettings() -// SetSpeechRate(v) / SetSpeechVolume(v) -// SetVoiceOption(id) / SetVoiceOptionByName(name) -// RefreshVoices() -// -// Events: VOICE_CHAT_TTS_PLAYBACK_STARTED/FINISHED/FAILED, -// VOICE_CHAT_TTS_SPEAK_TEXT_UPDATE (reserved, unused), -// VOICE_CHAT_TTS_VOICES_UPDATE. -// -// Threading: SAPI is created on the main (game) thread under an STA, and -// SetNotifyCallbackFunction marshals the notify back to that thread's message -// pump (WoW pumps messages every frame), so OnSpeechNotify runs on the main -// thread and may touch the Lua event dispatcher safely. - -#include "Game.h" -#include "cvar/Factory.h" -#include "event/Custom.h" - -#include // instantiate the SAPI GUIDs in this TU (no sapi.lib) -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace VoiceChat::Tts { - -namespace { - -// --------------------------------------------------------------------------- -// Event names + reservations -// --------------------------------------------------------------------------- -constexpr const char *kEvtPlaybackFailed = "VOICE_CHAT_TTS_PLAYBACK_FAILED"; -constexpr const char *kEvtPlaybackFinished = "VOICE_CHAT_TTS_PLAYBACK_FINISHED"; -constexpr const char *kEvtPlaybackStarted = "VOICE_CHAT_TTS_PLAYBACK_STARTED"; -constexpr const char *kEvtSpeakTextUpdate = "VOICE_CHAT_TTS_SPEAK_TEXT_UPDATE"; -constexpr const char *kEvtVoicesUpdate = "VOICE_CHAT_TTS_VOICES_UPDATE"; - -const Event::Custom::AutoReserve _r1{kEvtPlaybackFailed}; -const Event::Custom::AutoReserve _r2{kEvtPlaybackFinished}; -const Event::Custom::AutoReserve _r3{kEvtPlaybackStarted}; -const Event::Custom::AutoReserve _r4{kEvtSpeakTextUpdate}; -const Event::Custom::AutoReserve _r5{kEvtVoicesUpdate}; - -// --------------------------------------------------------------------------- -// CVar names + handles (registered in RegisterLuaFunctions) -// --------------------------------------------------------------------------- -constexpr const char *kCVarVoice = "ttsVoice"; -constexpr const char *kCVarSpeed = "ttsSpeed"; -constexpr const char *kCVarVolume = "ttsVolume"; - -CVar::Factory::Handle g_cvarVoice = nullptr; -CVar::Factory::Handle g_cvarSpeed = nullptr; -CVar::Factory::Handle g_cvarVolume = nullptr; - -// Destination constants (kept for API parity; no special handling — SAPI -// queues FIFO either way). -enum : int { - DEST_LOCAL_PLAYBACK = 1, - DEST_QUEUED_LOCAL_PLAYBACK = 4, -}; -int ClampDestination(int d) { - return d == DEST_QUEUED_LOCAL_PLAYBACK ? DEST_QUEUED_LOCAL_PLAYBACK - : DEST_LOCAL_PLAYBACK; -} - -// --------------------------------------------------------------------------- -// SAPI state -// --------------------------------------------------------------------------- -ISpVoice *g_pVoice = nullptr; -bool g_comInit = false; - -int g_nextUtteranceID = 1; -int NextUtteranceID() { - if (g_nextUtteranceID >= 0x7fffffff) - g_nextUtteranceID = 1; - return g_nextUtteranceID++; -} - -struct UtteranceMeta { - int id; - int destination; - bool startedEmitted; -}; -std::mutex g_streamMx; -std::unordered_map g_streamMap; - -struct VoiceEntry { - int voiceID; - std::wstring name; -}; -std::vector g_cachedVoices; - -// --------------------------------------------------------------------------- -// UTF-8 / wide conversion -// --------------------------------------------------------------------------- -std::string WideToUtf8(const std::wstring &w) { - if (w.empty()) - return {}; - int need = WideCharToMultiByte(CP_UTF8, 0, w.c_str(), -1, nullptr, 0, nullptr, nullptr); - if (need <= 1) - return {}; - std::string out(static_cast(need - 1), '\0'); - WideCharToMultiByte(CP_UTF8, 0, w.c_str(), -1, &out[0], need - 1, nullptr, nullptr); - return out; -} - -std::wstring Utf8ToWide(const char *s) { - if (s == nullptr || *s == '\0') - return {}; - int need = MultiByteToWideChar(CP_UTF8, 0, s, -1, nullptr, 0); - if (need <= 1) - return {}; - std::wstring out(static_cast(need - 1), L'\0'); - MultiByteToWideChar(CP_UTF8, 0, s, -1, &out[0], need); - return out; -} - -// --------------------------------------------------------------------------- -// Event fire helpers (engine dispatcher via Event::Custom) -// --------------------------------------------------------------------------- -void FireVoicesUpdate() { - Event::Custom::Fire(Event::Custom::Lookup(kEvtVoicesUpdate), ""); -} -void FirePlaybackStarted(int numConsumers, int utteranceID, int durationMS, int dest) { - Event::Custom::Fire(Event::Custom::Lookup(kEvtPlaybackStarted), "%d%d%d%d", - numConsumers, utteranceID, durationMS, dest); -} -void FirePlaybackFinished(int numConsumers, int utteranceID, int dest) { - Event::Custom::Fire(Event::Custom::Lookup(kEvtPlaybackFinished), "%d%d%d", - numConsumers, utteranceID, dest); -} -void FirePlaybackFailed(const char *status, int utteranceID, int dest) { - Event::Custom::Fire(Event::Custom::Lookup(kEvtPlaybackFailed), "%s%d%d", - status, utteranceID, dest); -} - -// --------------------------------------------------------------------------- -// COM / SAPI lifecycle -// --------------------------------------------------------------------------- -void InitCom() { - if (g_comInit) - return; - HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); - // S_FALSE = already initialized on this thread; treat as success. - if (SUCCEEDED(hr) || hr == RPC_E_CHANGED_MODE) - g_comInit = true; -} - -void __stdcall OnSpeechNotify(WPARAM, LPARAM) { - if (g_pVoice == nullptr) - return; - SPEVENT ev = {}; - ULONG fetched = 0; - while (SUCCEEDED(g_pVoice->GetEvents(1, &ev, &fetched)) && fetched == 1) { - if (ev.eEventId == SPEI_START_INPUT_STREAM) { - std::lock_guard lock(g_streamMx); - auto it = g_streamMap.find(ev.ulStreamNum); - if (it != g_streamMap.end() && !it->second.startedEmitted) { - FirePlaybackStarted(1, it->second.id, /*durationMS*/ 0, - it->second.destination); - it->second.startedEmitted = true; - } - } else if (ev.eEventId == SPEI_END_INPUT_STREAM) { - UtteranceMeta meta{0, DEST_LOCAL_PLAYBACK, false}; - { - std::lock_guard lock(g_streamMx); - auto it = g_streamMap.find(ev.ulStreamNum); - if (it != g_streamMap.end()) { - meta = it->second; - g_streamMap.erase(it); - } - } - FirePlaybackFinished(1, meta.id, meta.destination); - } - // Only START/END stream events are requested; neither carries an - // allocated lParam, so no SpClearEvent (sphelper) is needed. - ev = SPEVENT{}; - } -} - -void InitVoice() { - if (g_pVoice != nullptr) - return; - InitCom(); - HRESULT hr = CoCreateInstance(CLSID_SpVoice, nullptr, CLSCTX_ALL, - IID_ISpVoice, reinterpret_cast(&g_pVoice)); - if (FAILED(hr) || g_pVoice == nullptr) { - g_pVoice = nullptr; - return; - } - g_pVoice->SetNotifyCallbackFunction(&OnSpeechNotify, 0, 0); - const ULONGLONG interest = - SPFEI(SPEI_START_INPUT_STREAM) | SPFEI(SPEI_END_INPUT_STREAM); - g_pVoice->SetInterest(interest, interest); -} - -// --------------------------------------------------------------------------- -// Voice enumeration (CLSID_SpObjectTokenCategory; no sphelper) -// --------------------------------------------------------------------------- -std::vector EnumerateVoices() { - InitCom(); - std::vector voices; - - ISpObjectTokenCategory *cat = nullptr; - if (FAILED(CoCreateInstance(CLSID_SpObjectTokenCategory, nullptr, CLSCTX_ALL, - IID_ISpObjectTokenCategory, - reinterpret_cast(&cat))) || - cat == nullptr) - return voices; - - if (SUCCEEDED(cat->SetId(SPCAT_VOICES, FALSE))) { - IEnumSpObjectTokens *en = nullptr; - if (SUCCEEDED(cat->EnumTokens(nullptr, nullptr, &en)) && en != nullptr) { - ISpObjectToken *tok = nullptr; - ULONG fetched = 0; - int index = 0; - while (en->Next(1, &tok, &fetched) == S_OK && fetched == 1) { - LPWSTR desc = nullptr; - if (SUCCEEDED(tok->GetStringValue(nullptr, &desc)) && desc != nullptr) { - voices.push_back({index, desc}); - CoTaskMemFree(desc); - } - tok->Release(); - ++index; - } - en->Release(); - } - } - cat->Release(); - return voices; -} - -int VoiceCount() { - return static_cast(EnumerateVoices().size()); -} - -void RefreshVoices() { - auto fresh = EnumerateVoices(); - bool changed = fresh.size() != g_cachedVoices.size(); - if (!changed) { - for (size_t i = 0; i < fresh.size(); ++i) { - if (fresh[i].name != g_cachedVoices[i].name) { - changed = true; - break; - } - } - } - g_cachedVoices = std::move(fresh); - if (changed) - FireVoicesUpdate(); -} - -// Select the voice at enumeration index `voiceID` on g_pVoice. -bool SelectVoice(int voiceID) { - ISpObjectTokenCategory *cat = nullptr; - if (FAILED(CoCreateInstance(CLSID_SpObjectTokenCategory, nullptr, CLSCTX_ALL, - IID_ISpObjectTokenCategory, - reinterpret_cast(&cat))) || - cat == nullptr) - return false; - - bool set = false; - if (SUCCEEDED(cat->SetId(SPCAT_VOICES, FALSE))) { - IEnumSpObjectTokens *en = nullptr; - if (SUCCEEDED(cat->EnumTokens(nullptr, nullptr, &en)) && en != nullptr) { - ISpObjectToken *tok = nullptr; - ULONG fetched = 0; - int index = 0; - while (en->Next(1, &tok, &fetched) == S_OK && fetched == 1) { - if (index == voiceID) { - set = SUCCEEDED(g_pVoice->SetVoice(tok)); - tok->Release(); - break; - } - tok->Release(); - ++index; - } - en->Release(); - } - } - cat->Release(); - return set; -} - -// --------------------------------------------------------------------------- -// Speak / stop -// --------------------------------------------------------------------------- -void SpeakText(int voiceID, const std::wstring &text, int destination, int rate, - int volume) { - const int utteranceID = NextUtteranceID(); - const int dest = ClampDestination(destination); - - InitVoice(); - if (g_pVoice == nullptr) { - FirePlaybackFailed("EngineAllocationFailed", utteranceID, dest); - return; - } - if (!SelectVoice(voiceID)) { - FirePlaybackFailed("InternalError", utteranceID, dest); - return; - } - - g_pVoice->SetRate(std::clamp(rate, -10, 10)); - g_pVoice->SetVolume(static_cast(std::clamp(volume, 0, 100))); - - ULONG streamNum = 0; - if (FAILED(g_pVoice->Speak(text.c_str(), SPF_ASYNC, &streamNum))) { - FirePlaybackFailed("InternalError", utteranceID, dest); - return; - } - std::lock_guard lock(g_streamMx); - g_streamMap[streamNum] = UtteranceMeta{utteranceID, dest, false}; -} - -void StopAll() { - InitVoice(); - if (g_pVoice == nullptr) - return; - g_pVoice->Speak(nullptr, SPF_PURGEBEFORESPEAK, nullptr); - std::lock_guard lock(g_streamMx); - g_streamMap.clear(); -} - -// --------------------------------------------------------------------------- -// Lua helpers -// --------------------------------------------------------------------------- -// Push a { {voiceID=, name=}, ... } array built from g_cachedVoices. -void PushVoicesTable(void *L) { - Game::Lua::NewTable(L); // outer array at top - for (size_t i = 0; i < g_cachedVoices.size(); ++i) { - Game::Lua::PushNumber(L, static_cast(i + 1)); // 1-based array key - Game::Lua::NewTable(L); // sub-table - Game::Lua::SetFieldNumber(L, "voiceID", g_cachedVoices[i].voiceID); - Game::Lua::SetFieldString(L, "name", - WideToUtf8(g_cachedVoices[i].name).c_str()); - // stack: [outer, key, sub] → outer[key] = sub - Game::Lua::RawSet(L, -3); - } -} - -int OptInt(void *L, int idx, int fallback) { - return Game::Lua::IsNumber(L, idx) - ? static_cast(Game::Lua::ToNumber(L, idx)) - : fallback; -} - -// --------------------------------------------------------------------------- -// C_VoiceChat -// --------------------------------------------------------------------------- -int __fastcall Script_GetTtsVoices(void *L) { - RefreshVoices(); - PushVoicesTable(L); - return 1; -} - -int __fastcall Script_GetRemoteTtsVoices(void *L) { - RefreshVoices(); - PushVoicesTable(L); - return 1; -} - -int __fastcall Script_SpeakText(void *L) { - if (!Game::Lua::IsNumber(L, 1) || !Game::Lua::IsString(L, 2)) { - Game::Lua::Error(L, "Usage: C_VoiceChat.SpeakText(voiceID, text " - "[, destination, rate, volume])"); - return 0; - } - const int voiceID = static_cast(Game::Lua::ToNumber(L, 1)); - const std::wstring text = Utf8ToWide(Game::Lua::ToString(L, 2)); - const int dest = OptInt(L, 3, DEST_LOCAL_PLAYBACK); - const int rate = OptInt(L, 4, 0); - const int volume = OptInt(L, 5, 100); - SpeakText(voiceID, text, dest, rate, volume); - return 0; -} - -int __fastcall Script_StopSpeakingText(void *) { - StopAll(); - return 0; -} - -// --------------------------------------------------------------------------- -// C_TTSSettings -// --------------------------------------------------------------------------- -int __fastcall Script_GetSpeechRate(void *L) { - Game::Lua::PushNumber(L, CVar::Factory::GetInt(g_cvarSpeed, 0)); - return 1; -} -int __fastcall Script_GetSpeechVolume(void *L) { - Game::Lua::PushNumber(L, CVar::Factory::GetInt(g_cvarVolume, 100)); - return 1; -} -int __fastcall Script_GetSpeechVoiceID(void *L) { - Game::Lua::PushNumber(L, CVar::Factory::GetInt(g_cvarVoice, 0)); - return 1; -} -int __fastcall Script_GetVoiceOptionName(void *L) { - const int id = CVar::Factory::GetInt(g_cvarVoice, 0); - auto voices = EnumerateVoices(); - if (id >= 0 && id < static_cast(voices.size())) - Game::Lua::PushString(L, WideToUtf8(voices[id].name).c_str()); - else - Game::Lua::PushString(L, ""); - return 1; -} - -int __fastcall Script_SetSpeechRate(void *L) { - if (Game::Lua::IsNumber(L, 1)) - CVar::Factory::SetInt(g_cvarSpeed, static_cast(Game::Lua::ToNumber(L, 1))); - return 0; -} -int __fastcall Script_SetSpeechVolume(void *L) { - if (Game::Lua::IsNumber(L, 1)) - CVar::Factory::SetInt(g_cvarVolume, static_cast(Game::Lua::ToNumber(L, 1))); - return 0; -} -int __fastcall Script_SetVoiceOption(void *L) { - if (Game::Lua::IsNumber(L, 1)) - CVar::Factory::SetInt(g_cvarVoice, static_cast(Game::Lua::ToNumber(L, 1))); - return 0; -} -int __fastcall Script_SetVoiceOptionByName(void *L) { - if (!Game::Lua::IsString(L, 1)) - return 0; - const std::wstring want = Utf8ToWide(Game::Lua::ToString(L, 1)); - auto voices = EnumerateVoices(); - for (const auto &v : voices) { - if (v.name.size() == want.size()) { - bool eq = true; - for (size_t i = 0; i < want.size(); ++i) { - if (towlower(v.name[i]) != towlower(want[i])) { - eq = false; - break; - } - } - if (eq) { - CVar::Factory::SetInt(g_cvarVoice, v.voiceID); - break; - } - } - } - return 0; -} -int __fastcall Script_SetDefaultSettings(void *) { - const int maxVoice = VoiceCount(); - CVar::Factory::SetInt(g_cvarVoice, maxVoice > 1 ? 1 : 0); - CVar::Factory::SetInt(g_cvarSpeed, 0); - CVar::Factory::SetInt(g_cvarVolume, 100); - FireVoicesUpdate(); - return 0; -} -int __fastcall Script_RefreshVoices(void *) { - RefreshVoices(); - return 0; -} - -// --------------------------------------------------------------------------- -// CVar clamp callbacks (re-entrant SetString to clamp; reject out-of-range) -// --------------------------------------------------------------------------- -int __fastcall ClampRange(CVar::Factory::Handle cvar, const char *, const char *next, - int lo, int hi) { - if (next == nullptr) - return 1; - const int v = std::atoi(next); - const int c = std::clamp(v, lo, hi); - if (c != v) { - char buf[16]; - std::snprintf(buf, sizeof buf, "%d", c); - CVar::Factory::SetString(cvar, buf); - return 0; - } - return 1; -} -int __fastcall OnSpeedChanged(CVar::Factory::Handle c, const char *p, const char *n, void *) { - return ClampRange(c, p, n, -10, 10); -} -int __fastcall OnVolumeChanged(CVar::Factory::Handle c, const char *p, const char *n, void *) { - return ClampRange(c, p, n, 0, 100); -} -int __fastcall OnVoiceChanged(CVar::Factory::Handle c, const char *p, const char *n, void *) { - const int maxIdx = VoiceCount() - 1; - return ClampRange(c, p, n, 0, maxIdx < 0 ? 0 : maxIdx); -} - -// --------------------------------------------------------------------------- -// Registration -// --------------------------------------------------------------------------- -void RegisterLuaFunctions() { - g_cvarVoice = CVar::Factory::Register(kCVarVoice, "0", 0, &OnVoiceChanged); - g_cvarSpeed = CVar::Factory::Register(kCVarSpeed, "0", 0, &OnSpeedChanged); - g_cvarVolume = CVar::Factory::Register(kCVarVolume, "100", 0, &OnVolumeChanged); - - Game::Lua::RegisterTableFunction("C_VoiceChat", "GetTtsVoices", &Script_GetTtsVoices); - Game::Lua::RegisterTableFunction("C_VoiceChat", "GetRemoteTtsVoices", &Script_GetRemoteTtsVoices); - Game::Lua::RegisterTableFunction("C_VoiceChat", "SpeakText", &Script_SpeakText); - Game::Lua::RegisterTableFunction("C_VoiceChat", "StopSpeakingText", &Script_StopSpeakingText); - - Game::Lua::RegisterTableFunction("C_TTSSettings", "GetSpeechRate", &Script_GetSpeechRate); - Game::Lua::RegisterTableFunction("C_TTSSettings", "GetSpeechVolume", &Script_GetSpeechVolume); - Game::Lua::RegisterTableFunction("C_TTSSettings", "GetSpeechVoiceID", &Script_GetSpeechVoiceID); - Game::Lua::RegisterTableFunction("C_TTSSettings", "GetVoiceOptionName", &Script_GetVoiceOptionName); - Game::Lua::RegisterTableFunction("C_TTSSettings", "SetDefaultSettings", &Script_SetDefaultSettings); - Game::Lua::RegisterTableFunction("C_TTSSettings", "SetSpeechRate", &Script_SetSpeechRate); - Game::Lua::RegisterTableFunction("C_TTSSettings", "SetSpeechVolume", &Script_SetSpeechVolume); - Game::Lua::RegisterTableFunction("C_TTSSettings", "SetVoiceOption", &Script_SetVoiceOption); - Game::Lua::RegisterTableFunction("C_TTSSettings", "SetVoiceOptionByName", &Script_SetVoiceOptionByName); - Game::Lua::RegisterTableFunction("C_TTSSettings", "RefreshVoices", &Script_RefreshVoices); -} - -const Game::ModuleAutoRegister _autoreg{&RegisterLuaFunctions}; - -} // namespace - -} // namespace VoiceChat::Tts