// 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include "g2p.h" #include "tts.h" #ifdef _WIN32 #include #else #include #include #ifdef __APPLE__ #include #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 model; std::unique_ptr g2p; }; // tiny_voca.infer.speak_text std::vector 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> waves; std::function 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 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 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& wave, int sr) { std::string out; auto u32 = [&](uint32_t v) { out.append(reinterpret_cast(&v), 4); }; auto u16 = [&](uint16_t v) { out.append(reinterpret_cast(&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 utf8_args(int argc, char** argv) { std::vector 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 args = utf8_args(argc, argv); std::map 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 speakers; auto add = [&](const std::string& name, const std::string& path) { Speaker s; s.model = TtsModel::load(path, threads); s.g2p = std::make_unique(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(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 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(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; }