| 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 | #include <cstddef> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "glk.h" |
| 25 | #include "garglk.h" |
| 26 | |
| 27 | #ifdef GARGLK_CONFIG_DLOPEN_LIBSPEECHD |
| 28 | |
| 29 | #include <dlfcn.h> |
| 30 | #include <iostream> |
| 31 | |
| 32 | // Redefine some types from libspeechd.h |
| 33 | |
| 34 | typedef enum { |
| 35 | SPD_IMPORTANT = 1, |
| 36 | SPD_MESSAGE = 2, |
| 37 | SPD_TEXT = 3, |
| 38 | SPD_NOTIFICATION = 4, |
| 39 | SPD_PROGRESS = 5 |
| 40 | } SPDPriority; |
| 41 | |
| 42 | typedef enum { |
| 43 | SPD_MODE_SINGLE = 0, |
| 44 | SPD_MODE_THREADED = 1 |
| 45 | } SPDConnectionMode; |
| 46 | |
| 47 | struct SPDConnection; |
| 48 | |
| 49 | // Prefix names of libspeechd symbols with underscores |
| 50 | // in this file to avoid any possible conflicts |
| 51 | |
| 52 | #define spd_open _spd_open |
| 53 | #define spd_set_language _spd_set_language |
| 54 | #define spd_say _spd_say |
| 55 | #define spd_cancel _spd_cancel |
| 56 | #define spd_close _spd_close |
| 57 | |
| 58 | #define LIBSPEECHD_FUNCS \ |
| 59 | FUNC( \ |
| 60 | spd_open, \ |
| 61 | SPDConnection*, \ |
| 62 | (const char*, const char*, const char*, SPDConnectionMode) \ |
| 63 | ) \ |
| 64 | FUNC(spd_set_language, int, (SPDConnection*, const char*)) \ |
| 65 | FUNC(spd_say, int, (SPDConnection*, SPDPriority, const char*)) \ |
| 66 | FUNC(spd_cancel, int, (SPDConnection*)) \ |
| 67 | FUNC(spd_close, void, (SPDConnection*)) |
| 68 | |
| 69 | #define FUNC(name, type, params) \ |
| 70 | typedef type(*name##_fn) params; \ |
| 71 | static name##_fn name; |
| 72 | |
| 73 | LIBSPEECHD_FUNCS |
| 74 | |
| 75 | #undef FUNC |
| 76 | |
| 77 | #else |
| 78 | |
| 79 | #include <libspeechd.h> |
| 80 | |
| 81 | #endif |
| 82 | |
| 83 | static SPDConnection *spd; |
| 84 | static std::vector<glui32> txtbuf; |
| 85 | |
| 86 | void gli_initialize_tts() |
| 87 | { |
| 88 | if (gli_conf_speak) { |
| 89 | #ifdef GARGLK_CONFIG_DLOPEN_LIBSPEECHD |
| 90 | void *libspeechd = dlopen("libspeechd.so.2", RTLD_LAZY | RTLD_LOCAL); |
| 91 | |
| 92 | if (libspeechd == nullptr) { |
| 93 | std::cerr << |
| 94 | "Failed to load libspeechd. " |
| 95 | "Text-to-speech will be disabled." << |
| 96 | std::endl; |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | // Firefox checks for ABI compatibility by checking whether the |
| 101 | // spd_get_volume symbol exists, so we follow their lead. |
| 102 | if (dlsym(libspeechd, "spd_get_volume") == nullptr) { |
| 103 | std::cerr << |
| 104 | "An unsupported version of libspeechd was found. " |
| 105 | "Text-to-speech will be disabled." << |
| 106 | std::endl; |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | #define FUNC(name, type, params) \ |
| 111 | name = (name##_fn) dlsym(libspeechd, #name); \ |
| 112 | if (name == nullptr) \ |
| 113 | { \ |
| 114 | std::cerr << \ |
| 115 | "The symbol \"" << #name << "\" wasn't found in libspeechd. " \ |
| 116 | "Text-to-speech will be disabled." << \ |
| 117 | std::endl; \ |
| 118 | return; \ |
| 119 | } |
| 120 | |
| 121 | LIBSPEECHD_FUNCS |
| 122 | |
| 123 | #undef FUNC |
| 124 | #endif |
| 125 | |
| 126 | spd = spd_open("gargoyle", "main", nullptr, SPD_MODE_SINGLE); |
| 127 | |
| 128 | if (spd != nullptr && !gli_conf_speak_language.empty()) { |
| 129 | spd_set_language(spd, gli_conf_speak_language.c_str()); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | txtbuf.clear(); |
| 134 | } |
| 135 | |
| 136 | static std::string unicode_to_utf8(const std::vector<glui32> &src) |
| 137 | { |
| 138 | std::string dst; |
| 139 | |
| 140 | for (const auto &c : src) { |
| 141 | if (c < 0x80) { |
| 142 | dst.push_back(c); |
| 143 | } else if (c < 0x800) { |
| 144 | dst.push_back(0xc0 | ((c >> 6) & 0x1f)); |
| 145 | dst.push_back(0x80 | ((c ) & 0x3f)); |
| 146 | } else if (c < 0x10000) { |
| 147 | dst.push_back(0xe0 | ((c >> 12) & 0x0f)); |
| 148 | dst.push_back(0x80 | ((c >> 6) & 0x3f)); |
| 149 | dst.push_back(0x80 | ((c ) & 0x3f)); |
| 150 | } else if (c < 0x200000) { |
| 151 | dst.push_back(0xf0 | ((c >> 18) & 0x07)); |
| 152 | dst.push_back(0x80 | ((c >> 12) & 0x3f)); |
| 153 | dst.push_back(0x80 | ((c >> 6) & 0x3f)); |
| 154 | dst.push_back(0x80 | ((c ) & 0x3f)); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return dst; |
| 159 | } |
| 160 | |
| 161 | void gli_tts_flush() |
| 162 | { |
| 163 | if (spd != nullptr && !txtbuf.empty()) { |
| 164 | auto utf8 = unicode_to_utf8(txtbuf); |
| 165 | spd_say(spd, SPD_MESSAGE, utf8.c_str()); |
| 166 | } |
| 167 | |
| 168 | txtbuf.clear(); |
| 169 | } |
| 170 | |
| 171 | void gli_tts_purge() |
| 172 | { |
| 173 | if (spd != nullptr) { |
| 174 | spd_cancel(spd); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void gli_tts_speak(const glui32 *buf, std::size_t len) |
| 179 | { |
| 180 | if (spd == nullptr) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | for (std::size_t i = 0; i < len; i++) { |
| 185 | if (buf[i] == '>' || buf[i] == '*') { |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | txtbuf.push_back(buf[i]); |
| 190 | |
| 191 | if (buf[i] == '.' || buf[i] == '!' || buf[i] == '?' || buf[i] == '\n') { |
| 192 | gli_tts_flush(); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void gli_free_tts() |
| 198 | { |
| 199 | if (spd != nullptr) { |
| 200 | spd_close(spd); |
| 201 | } |
| 202 | } |