File size: 15,200 Bytes
ab1b068
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d052418
 
 
 
 
 
ab1b068
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d052418
 
 
 
 
 
 
 
 
ab1b068
 
 
 
 
 
 
d052418
 
 
 
 
 
 
 
 
 
 
 
ab1b068
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// tts_hi: Hindi tiny-voca TTS, one executable, no Python.
//
//   tts_hi --model models/tts_hi_q8_0.gguf --text "नमस्ते, आप कैसे हैं?" --style conv --out hello.wav
//   tts_hi --model models/tts_hi_q8_0.gguf --serve 8090 --page web     (http://127.0.0.1:8090)
//
// The text should already be normalized (numbers/dates/English -> Devanagari words): the page does that in the
// browser with app/hindi_norm.js (a checked port of small_tts/normalize_hi.py). Reading follows
// tiny_voca.infer.speak_text: sentences, a full stop added when missing, too-long pieces cut after commas or halved.
#include "httplib.h"     // first: it brings winsock2.h before windows.h

#include <algorithm>
#include <atomic>
#include <chrono>
#include <functional>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <mutex>
#include <sstream>
#include <string>
#include <vector>

#include "g2p.h"
#include "tts.h"

#ifdef _WIN32
#include <psapi.h>
#else
#include <sys/resource.h>
#include <unistd.h>
#ifdef __APPLE__
#include <mach/mach.h>
#endif
#endif

namespace {

const float PEAK = 0.99f;
int g_flow_steps = 0;          // --steps: flow models' midpoint steps (0 = the GGUF's default)
float g_guidance = -1.0f;      // --guidance: flow models' CFG strength (< 0 = the GGUF's default)

struct Speaker {
    std::unique_ptr<TtsModel> model;
    std::unique_ptr<HindiG2P> g2p;
};

// tiny_voca.infer.speak_text
std::vector<float> speak_text(Speaker& s, const std::string& text, int style, float speed, uint64_t seed) {
    const int limit = s.model->max_tokens() - 12;
    std::vector<std::vector<float>> waves;
    std::function<void(const std::string&)> read = [&](const std::string& piece) {
        Phonemized p;
        try { p = s.g2p->phonemize(piece); } catch (const NothingToPronounce&) { return; }
        if (int(p.ids.size()) <= limit) {
            try {
                TtsOptions o; o.style = style; o.speed = speed; o.seed = seed + waves.size(); o.flow_steps = g_flow_steps; o.guidance = g_guidance;
                waves.push_back(s.model->synthesize(p.ids, p.stress, o));
                return;
            } catch (const UtteranceTooLong&) {}
        }
        const std::vector<std::string> parts = shorter(piece);
        if (parts.empty()) throw std::runtime_error("one word is too long to speak");
        for (const auto& part : parts) read(part);
    };
    for (const auto& sentence : split_sentences(finished(text))) read(sentence);
    if (waves.empty()) throw NothingToPronounce("nothing in the text could be read (Devanagari only)");
    std::vector<float> wave;
    for (auto& w : waves) wave.insert(wave.end(), w.begin(), w.end());
    float loudest = 0;
    for (float v : wave) loudest = std::max(loudest, std::fabs(v));
    if (loudest > PEAK) for (float& v : wave) v *= PEAK / loudest;
    return wave;
}

std::string wav_bytes(const std::vector<float>& wave, int sr) {
    std::string out;
    auto u32 = [&](uint32_t v) { out.append(reinterpret_cast<const char*>(&v), 4); };
    auto u16 = [&](uint16_t v) { out.append(reinterpret_cast<const char*>(&v), 2); };
    const uint32_t data = uint32_t(wave.size() * 2);
    out += "RIFF"; u32(36 + data); out += "WAVEfmt "; u32(16); u16(1); u16(1); u32(sr); u32(sr * 2); u16(2); u16(16);
    out += "data"; u32(data);
    for (float v : wave) {
        const float c = std::max(-1.0f, std::min(1.0f, v));
        u16(uint16_t(int16_t(std::lround(c * 32767.0f))));
    }
    return out;
}

double peak_ram_mb() {
#ifdef _WIN32
    PROCESS_MEMORY_COUNTERS pmc{};
    if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) return pmc.PeakWorkingSetSize / 1048576.0;
#else
    struct rusage ru{};
    if (getrusage(RUSAGE_SELF, &ru) == 0) {
#ifdef __APPLE__
        return ru.ru_maxrss / 1048576.0;   // bytes on macOS
#else
        return ru.ru_maxrss / 1024.0;      // kilobytes on Linux
#endif
    }
#endif
    return 0;
}
double ram_mb() {
#ifdef _WIN32
    PROCESS_MEMORY_COUNTERS pmc{};
    if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) return pmc.WorkingSetSize / 1048576.0;
#elif defined(__APPLE__)
    mach_task_basic_info info{};
    mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
    if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, task_info_t(&info), &count) == KERN_SUCCESS)
        return info.resident_size / 1048576.0;
#else
    if (FILE* f = std::fopen("/proc/self/statm", "r")) {      // pages: total, resident
        long total = 0, resident = 0;
        const int n = std::fscanf(f, "%ld %ld", &total, &resident);
        std::fclose(f);
        if (n == 2) return double(resident) * double(sysconf(_SC_PAGESIZE)) / 1048576.0;
    }
#endif
    return 0;
}

std::string read_file(const std::string& path) {
    std::ifstream f(path, std::ios::binary);
    if (!f) return {};
    std::ostringstream ss; ss << f.rdbuf();
    return ss.str();
}

std::string hex(const std::string& s) {
    static const char* d = "0123456789abcdef";
    std::string o;
    for (unsigned char c : s) { o += d[c >> 4]; o += d[c & 15]; }
    return o;
}

// minimal JSON helpers for the /speak request: {"text": "...", "style": "...", "speed": 1.0, "model": "q8_0"}
std::string json_string(const std::string& body, const std::string& key) {
    const std::string k = "\"" + key + "\"";
    size_t p = body.find(k);
    if (p == std::string::npos) return {};
    p = body.find(':', p + k.size());
    if (p == std::string::npos) return {};
    p = body.find('"', p);
    if (p == std::string::npos) return {};
    std::string out;
    for (size_t i = p + 1; i < body.size(); ++i) {
        const char c = body[i];
        if (c == '"') return out;
        if (c != '\\') { out += c; continue; }
        if (++i >= body.size()) break;
        const char e = body[i];
        switch (e) {
            case 'n': out += '\n'; break; case 't': out += '\t'; break; case 'r': out += '\r'; break;
            case 'b': case 'f': out += ' '; break;
            case 'u': {
                if (i + 4 >= body.size()) return out;
                uint32_t cp = std::stoul(body.substr(i + 1, 4), nullptr, 16); i += 4;
                if (cp >= 0xD800 && cp <= 0xDBFF && i + 6 < body.size() && body[i + 1] == '\\' && body[i + 2] == 'u') {
                    const uint32_t lo = std::stoul(body.substr(i + 3, 4), nullptr, 16);
                    cp = 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00); i += 6;
                }
                if (cp < 0x80) out += char(cp);
                else if (cp < 0x800) { out += char(0xC0 | (cp >> 6)); out += char(0x80 | (cp & 0x3F)); }
                else if (cp < 0x10000) { out += char(0xE0 | (cp >> 12)); out += char(0x80 | ((cp >> 6) & 0x3F)); out += char(0x80 | (cp & 0x3F)); }
                else { out += char(0xF0 | (cp >> 18)); out += char(0x80 | ((cp >> 12) & 0x3F)); out += char(0x80 | ((cp >> 6) & 0x3F)); out += char(0x80 | (cp & 0x3F)); }
                break;
            }
            default: out += e;
        }
    }
    return out;
}
double json_number(const std::string& body, const std::string& key, double fallback) {
    const std::string k = "\"" + key + "\"";
    size_t p = body.find(k);
    if (p == std::string::npos) return fallback;
    p = body.find(':', p + k.size());
    if (p == std::string::npos) return fallback;
    try { return std::stod(body.substr(p + 1)); } catch (...) { return fallback; }
}
std::string json_escape(const std::string& s) {
    std::string o;
    for (char c : s) { if (c == '"' || c == '\\') o += '\\'; if (c == '\n') { o += "\\n"; continue; } o += c; }
    return o;
}

int style_index(const TtsModel& m, const std::string& name) {
    const auto& st = m.styles();
    for (size_t i = 0; i < st.size(); ++i) if (st[i] == name) return int(i);
    return -1;
}

}  // namespace

static std::vector<std::string> utf8_args(int argc, char** argv) {
    std::vector<std::string> out;
#ifdef _WIN32
    // argv is in the ANSI code page (Devanagari would become "?"): take the UTF-16 command line instead
    (void)argv;
    int n = 0;
    LPWSTR* w = CommandLineToArgvW(GetCommandLineW(), &n);
    for (int i = 0; i < n; ++i) {
        const int len = WideCharToMultiByte(CP_UTF8, 0, w[i], -1, nullptr, 0, nullptr, nullptr);
        std::string s(size_t(std::max(len - 1, 0)), '\0');
        WideCharToMultiByte(CP_UTF8, 0, w[i], -1, s.data(), len, nullptr, nullptr);
        out.push_back(s);
    }
    LocalFree(w);
#else
    for (int i = 0; i < argc; ++i) out.emplace_back(argv[i]);
#endif
    return out;
}

int main(int argc, char** argv) {
    const std::vector<std::string> args = utf8_args(argc, argv);
    std::map<std::string, std::string> a;
    for (size_t i = 1; i < args.size(); ++i) {
        const std::string& k = args[i];
        if (k.rfind("--", 0) == 0) a[k.substr(2)] = (i + 1 < args.size() && args[i + 1].rfind("--", 0) != 0) ? args[++i] : "1";
    }
    if (a.count("text-file")) a["text"] = read_file(a["text-file"]);
    if (!a.count("model")) {
        std::cerr << "usage: tts_hi --model M.gguf [--full M_f32.gguf] (--text TEXT --out OUT.wav [--style S] [--speed X] |"
                     " --serve PORT [--page DIR]) [--threads N]\n";
        return 2;
    }
    const int threads = a.count("threads") ? std::stoi(a["threads"]) : 4;
    if (a.count("steps")) g_flow_steps = std::stoi(a["steps"]);
    if (a.count("guidance")) g_guidance = std::stof(a["guidance"]);
    std::map<std::string, Speaker> speakers;
    auto add = [&](const std::string& name, const std::string& path) {
        Speaker s;
        s.model = TtsModel::load(path, threads);
        s.g2p = std::make_unique<HindiG2P>(s.model->symbols());
        speakers[name] = std::move(s);
    };
    try {
        add("int8", a["model"]);
        if (a.count("full")) add("float", a["full"]);
    } catch (const std::exception& e) { std::cerr << "load failed: " << e.what() << "\n"; return 1; }
    Speaker& main_speaker = speakers["int8"];
    const int sr = main_speaker.model->sample_rate();

    if (a.count("text")) {
        const int style = style_index(*main_speaker.model, a.count("style") ? a["style"] : "neutral");
        if (style < 0) { std::cerr << "unknown style\n"; return 2; }
        const auto t0 = std::chrono::steady_clock::now();
        try {
            const auto wave = speak_text(main_speaker, a["text"], style, a.count("speed") ? std::stof(a["speed"]) : 1.0f, 0);
            const double secs = std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
            std::ofstream(a.count("out") ? a["out"] : "out.wav", std::ios::binary) << wav_bytes(wave, sr);
            std::fprintf(stderr, "%.2f s of audio in %.3f s (%.0fx real time), peak RAM %.1f MB\n", double(wave.size()) / sr, secs,
                         double(wave.size()) / sr / secs, peak_ram_mb());
        } catch (const std::exception& e) { std::cerr << "error: " << e.what() << "\n"; return 1; }
        return 0;
    }

    if (!a.count("serve")) { std::cerr << "give --text or --serve\n"; return 2; }
    const int port = std::stoi(a["serve"]);
    const std::string page_dir = a.count("page") ? a["page"] : "web";
    std::mutex lock;
    httplib::Server server;
    server.Get("/", [&](const httplib::Request&, httplib::Response& res) {
        const std::string html = read_file(page_dir + "/index.html");
        if (html.empty()) { res.status = 404; res.set_content("page not found: " + page_dir + "/index.html", "text/plain"); return; }
        res.set_content(html, "text/html; charset=utf-8");
    });
    server.Get("/hindi_norm.js", [&](const httplib::Request&, httplib::Response& res) {
        std::string js = read_file(page_dir + "/hindi_norm.js");
        if (js.empty()) js = read_file("app/hindi_norm.js");
        if (js.empty()) { res.status = 404; return; }
        res.set_content(js, "application/javascript; charset=utf-8");
    });
    server.Get("/info", [&](const httplib::Request&, httplib::Response& res) {
        std::string styles, models, sizes;
        for (const auto& s : main_speaker.model->styles()) styles += (styles.empty() ? "\"" : ",\"") + s + "\"";
        for (const auto& [name, sp] : speakers) {
            models += (models.empty() ? "\"" : ",\"") + name + "\"";
            char b[64]; std::snprintf(b, sizeof b, "\"%s\":%.1f", name.c_str(), sp.model->weight_bytes() / 1e6);
            sizes += (sizes.empty() ? "" : ",") + std::string(b);
        }
        char ram[64]; std::snprintf(ram, sizeof ram, "%.1f", ram_mb());
        res.set_content("{\"styles\":[" + styles + "],\"models\":[" + models + "],\"sizes_mb\":{" + sizes +
                        "},\"engine\":\"cpp\",\"ram_mb\":" + ram + ",\"normalize_in_browser\":true}", "application/json");
    });
    server.Post("/speak", [&](const httplib::Request& req, httplib::Response& res) {
        auto fail = [&](int code, const std::string& msg) {
            res.status = code; res.set_content("{\"detail\":\"" + json_escape(msg) + "\"}", "application/json");
        };
        const std::string text = json_string(req.body, "text");
        if (text.find_first_not_of(" \t\r\n") == std::string::npos) return fail(400, "empty text");
        if (text.size() > 12000) return fail(400, "text too long");
        const std::string style_name = json_string(req.body, "style");
        const int style = style_index(*main_speaker.model, style_name.empty() ? "neutral" : style_name);
        if (style < 0) return fail(400, "unknown style " + style_name);
        const float speed = float(std::min(2.0, std::max(0.5, json_number(req.body, "speed", 1.0))));
        auto it = speakers.find(json_string(req.body, "model"));
        Speaker& sp = it != speakers.end() ? it->second : main_speaker;
        try {
            std::lock_guard<std::mutex> g(lock);
            const auto t0 = std::chrono::steady_clock::now();
            const auto wave = speak_text(sp, text, style, speed, 0);
            const double took = std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
            char a1[32], a2[32];
            std::snprintf(a1, sizeof a1, "%.2f", double(wave.size()) / sr);
            std::snprintf(a2, sizeof a2, "%.3f", took);
            res.set_header("X-Audio-Seconds", a1);
            res.set_header("X-Compute-Seconds", a2);
            res.set_header("X-Spoken-As", hex(text));
            res.set_content(wav_bytes(wave, sr), "audio/wav");
        } catch (const std::exception& e) {
            fail(400, e.what());
        }
    });
    std::fprintf(stderr, "Hindi TTS (C++/ggml): http://127.0.0.1:%d\n", port);
    std::fprintf(stderr, "  model: %s (%.1f MB of weights)%s\n", a["model"].c_str(), main_speaker.model->weight_bytes() / 1e6,
                 a.count("full") ? " + full-precision copy" : "");
    std::fprintf(stderr, "  RAM now %.1f MB (process working set, as Task Manager's details view; it grows a little while "
                         "speaking: the page shows it after each request)\n", ram_mb());
    if (!server.listen("127.0.0.1", port)) { std::cerr << "cannot listen on port " << port << "\n"; return 1; }
    return 0;
}