|
| 1 | +// c-api-examples/supertonic-tts-en-c-api.c |
| 2 | +// |
| 3 | +// Copyright (c) 2026 zengyw |
| 4 | + |
| 5 | +// This file shows how to use sherpa-onnx C API |
| 6 | +// for English TTS with Supertonic. |
| 7 | +// |
| 8 | +// clang-format off |
| 9 | +/* |
| 10 | +Usage |
| 11 | +
|
| 12 | +wget https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/sherpa-onnx-supertonic-tts-int8-2026-03-06.tar.bz2 |
| 13 | +tar xf sherpa-onnx-supertonic-tts-int8-2026-03-06.tar.bz2 |
| 14 | +rm sherpa-onnx-supertonic-tts-int8-2026-03-06.tar.bz2 |
| 15 | +
|
| 16 | +./supertonic-tts-en-c-api |
| 17 | +
|
| 18 | +*/ |
| 19 | +// clang-format on |
| 20 | + |
| 21 | +#include <stdio.h> |
| 22 | +#include <stdlib.h> |
| 23 | +#include <string.h> |
| 24 | + |
| 25 | +#include "sherpa-onnx/c-api/c-api.h" |
| 26 | + |
| 27 | +static int32_t ProgressCallback(const float* samples, int32_t num_samples, |
| 28 | + float progress, void* arg) { |
| 29 | + fprintf(stderr, "Progress: %.3f%%\n", progress * 100); |
| 30 | + // return 1 to continue generating |
| 31 | + // return 0 to stop generating |
| 32 | + return 1; |
| 33 | +} |
| 34 | + |
| 35 | +int32_t main(int32_t argc, char* argv[]) { |
| 36 | + SherpaOnnxOfflineTtsConfig config; |
| 37 | + memset(&config, 0, sizeof(config)); |
| 38 | + config.model.supertonic.duration_predictor = |
| 39 | + "./sherpa-onnx-supertonic-tts-int8-2026-03-06/" |
| 40 | + "duration_predictor.int8.onnx"; |
| 41 | + config.model.supertonic.text_encoder = |
| 42 | + "./sherpa-onnx-supertonic-tts-int8-2026-03-06/text_encoder.int8.onnx"; |
| 43 | + config.model.supertonic.vector_estimator = |
| 44 | + "./sherpa-onnx-supertonic-tts-int8-2026-03-06/vector_estimator.int8.onnx"; |
| 45 | + config.model.supertonic.vocoder = |
| 46 | + "./sherpa-onnx-supertonic-tts-int8-2026-03-06/vocoder.int8.onnx"; |
| 47 | + config.model.supertonic.tts_json = |
| 48 | + "./sherpa-onnx-supertonic-tts-int8-2026-03-06/tts.json"; |
| 49 | + config.model.supertonic.unicode_indexer = |
| 50 | + "./sherpa-onnx-supertonic-tts-int8-2026-03-06/unicode_indexer.bin"; |
| 51 | + config.model.supertonic.voice_style = |
| 52 | + "./sherpa-onnx-supertonic-tts-int8-2026-03-06/voice.bin"; |
| 53 | + |
| 54 | + config.model.num_threads = 2; |
| 55 | + |
| 56 | + // If you don't want to see debug messages, please set it to 0 |
| 57 | + config.model.debug = 1; |
| 58 | + |
| 59 | + const char* filename = "./generated-supertonic-en-c.wav"; |
| 60 | + const char* text = |
| 61 | + "Today as always, men fall into two groups: slaves and free men. Whoever " |
| 62 | + "does not have two-thirds of his day for himself, is a slave, whatever " |
| 63 | + "he may be: a statesman, a businessman, an official, or a scholar."; |
| 64 | + |
| 65 | + const SherpaOnnxOfflineTts* tts = SherpaOnnxCreateOfflineTts(&config); |
| 66 | + if (!tts) { |
| 67 | + fprintf(stderr, "Error create Offline TTS\n"); |
| 68 | + return -1; |
| 69 | + } |
| 70 | + |
| 71 | + SherpaOnnxGenerationConfig cfg = {0}; |
| 72 | + cfg.sid = 6; |
| 73 | + cfg.num_steps = 5; |
| 74 | + cfg.speed = 1.25f; // larger -> faster |
| 75 | + cfg.extra = "{\"lang\": \"en\"}"; |
| 76 | + |
| 77 | + const SherpaOnnxGeneratedAudio* audio = |
| 78 | + SherpaOnnxOfflineTtsGenerateWithConfig(tts, text, &cfg, ProgressCallback, |
| 79 | + NULL); |
| 80 | + |
| 81 | + fprintf(stderr, "Input text is: %s\n", text); |
| 82 | + |
| 83 | + if (audio) { |
| 84 | + SherpaOnnxWriteWave(audio->samples, audio->n, audio->sample_rate, filename); |
| 85 | + fprintf(stderr, "Saved to: %s\n", filename); |
| 86 | + SherpaOnnxDestroyOfflineTtsGeneratedAudio(audio); |
| 87 | + } |
| 88 | + |
| 89 | + SherpaOnnxDestroyOfflineTts(tts); |
| 90 | + |
| 91 | + return 0; |
| 92 | +} |
0 commit comments