C++ text to speech: natural voices for games and apps
C++ has no standard text-to-speech library. The usual choices are the operating system’s speech API, which differs on Windows, macOS and Linux, an old formant synthesizer such as eSpeak NG, which sounds robotic, or a neural engine that you call from C++. The last one gives natural voices that sound the same on every platform.
SuperClonica gives you natural voices in 31 languages, free to try online, a free engine with a command line and a Docker HTTP server, and more than 170 catalog voices. This guide shows two ways to use it from C++: voicing lines ahead of time and asking a local server at run time.
Options for speech in C++
Most games use pre-voiced files for fixed dialogue and a server only for text that players write.
- System APIs: SAPI on Windows, AVSpeechSynthesizer on Apple systems, Speech Dispatcher on Linux; three different code paths.
- eSpeak NG: small and embeddable, but clearly robotic.
- Pre-voiced files: the lines of a game or app voiced once and shipped as WAV or OGG.
- A local HTTP server: your program posts text and gets a WAV back.
Pick a voice free online
On the page of any voice, type a line of 30 to 100 characters, choose the language and press Voice it. In a few seconds you hear whether it fits your game or app, with no account.
Voice lines ahead of time
The same text, voice and seed always give the same file, so voiced lines can live in version control and be rebuilt on demand.
- Install the engine: python -m pip install superclonica-cpu.
- From a build script or a tool, call the command line: std::system("superclonica --text \"Welcome, traveler.\" --voice M2 --lang en --output welcome.wav");
- The file name decides the format: WAV, MP3, OGG or FLAC.
- Load the files with the audio library you already use, such as SDL_mixer, SFML, raylib or miniaudio.
A speech server at run time
For text that only exists at run time, start the server once: docker run -d -p 8000:8000 -v superclonica-models:/models superclonica/superclonica:cpu. Then post JSON with libcurl and write the answer to a file (include curl/curl.h and link with libcurl): std::string body = R"({"text": "Welcome, traveler.", "voice": "M2", "lang": "en"})"; CURL* c = curl_easy_init(); curl_slist* h = curl_slist_append(nullptr, "Content-Type: application/json"); curl_easy_setopt(c, CURLOPT_URL, "http://localhost:8000/synthesize"); curl_easy_setopt(c, CURLOPT_HTTPHEADER, h); curl_easy_setopt(c, CURLOPT_POSTFIELDS, body.c_str()); FILE* f = std::fopen("welcome.wav", "wb"); curl_easy_setopt(c, CURLOPT_WRITEDATA, f); curl_easy_perform(c); std::fclose(f); curl_slist_free_all(h); curl_easy_cleanup(c); The answer is a 16-bit mono WAV; the headers X-Sample-Rate and X-Duration-Seconds describe it. Set SUPERCLONICA_API_KEY and keep the server on a private network if it is not on the player’s own machine.
Voices you can ship
For a game or app you ship, use the ten stock voices, M1 to M5 and F1 to F5, free under the OpenRAIL-M license, or your own voice trained in the SuperClonica studio from a recording of an actor who agreed. The engine and the Docker image are Apache 2.0. Fan-made character voices from the catalog are for fan projects, not for commercial releases.