| 1 | // Copyright (C) 2006-2009 by Tor Andersson. |
| 2 | // Copyright (C) 2017 by Chris Spiegel. |
| 3 | // |
| 4 | // This file is part of Gargoyle. |
| 5 | // |
| 6 | // Gargoyle is free software; you can redistribute it and/or modify |
| 7 | // it under the terms of the GNU General Public License as published by |
| 8 | // the Free Software Foundation; either version 2 of the License, or |
| 9 | // (at your option) any later version. |
| 10 | // |
| 11 | // Gargoyle is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | // |
| 16 | // You should have received a copy of the GNU General Public License |
| 17 | // along with Gargoyle; if not, write to the Free Software |
| 18 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | |
| 20 | #define WIN32_LEAN_AND_MEAN |
| 21 | #define NOMINMAX |
| 22 | |
| 23 | #include <array> |
| 24 | #include <cstddef> |
| 25 | |
| 26 | #include <windows.h> |
| 27 | #include <sapi.h> |
| 28 | |
| 29 | #include "glk.h" |
| 30 | #include "garglk.h" |
| 31 | |
| 32 | static ISpVoice *voice = nullptr; |
| 33 | #define TXTSIZE 4096 |
| 34 | static std::array<WCHAR, TXTSIZE + 1> txtbuf; |
| 35 | static std::size_t txtlen; |
| 36 | |
| 37 | void gli_initialize_tts() |
| 38 | { |
| 39 | if (gli_conf_speak) { |
| 40 | if (CoInitialize(nullptr) == S_OK) { |
| 41 | CoCreateInstance( |
| 42 | CLSID_SpVoice, // rclsid |
| 43 | nullptr, // aggregate |
| 44 | CLSCTX_ALL, // dwClsContext |
| 45 | IID_ISpVoice, // riid |
| 46 | reinterpret_cast<void**>(&voice)); |
| 47 | } |
| 48 | } else { |
| 49 | voice = nullptr; |
| 50 | } |
| 51 | |
| 52 | txtlen = 0; |
| 53 | } |
| 54 | |
| 55 | void gli_tts_flush() |
| 56 | { |
| 57 | if (voice != nullptr) { |
| 58 | txtbuf[txtlen] = 0; |
| 59 | voice->Speak(txtbuf.data(), SPF_ASYNC | SPF_IS_NOT_XML, nullptr); |
| 60 | } |
| 61 | |
| 62 | txtlen = 0; |
| 63 | } |
| 64 | |
| 65 | void gli_tts_purge() |
| 66 | { |
| 67 | if (voice != nullptr) { |
| 68 | voice->Speak(nullptr, SPF_PURGEBEFORESPEAK, nullptr); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void gli_tts_speak(const glui32 *buf, std::size_t len) |
| 73 | { |
| 74 | if (voice == nullptr) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | for (std::size_t i = 0; i < len; i++) { |
| 79 | if (txtlen >= TXTSIZE) { |
| 80 | gli_tts_flush(); |
| 81 | } |
| 82 | |
| 83 | if (buf[i] == '>' || buf[i] == '*') { |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | if (buf[i] < 0x10000) { |
| 88 | txtbuf[txtlen++] = buf[i]; |
| 89 | } |
| 90 | |
| 91 | if (buf[i] == '.' || buf[i] == '!' || buf[i] == '?' || buf[i] == '\n') { |
| 92 | gli_tts_flush(); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void gli_free_tts() |
| 98 | { |
| 99 | if (voice != nullptr) { |
| 100 | voice->Release(); |
| 101 | CoUninitialize(); |
| 102 | } |
| 103 | } |