YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
kokoro_smoke_test.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6// Self-contained smoke test for the Kokoro YARP device. It does NOT depend on
7// Catch2 or the YARP dev_tests component; it only needs the core YARP
8// libraries (os, sig, dev, init). It exercises the full open + set/get +
9// synthesize path, so it actually loads and runs the Kokoro model.
10
11#include <yarp/os/Property.h>
12#include <yarp/os/Network.h>
13#include <yarp/dev/PolyDriver.h>
15#include <yarp/sig/Sound.h>
16
17#include <iostream>
18#include <cstdlib>
19#include <string>
20
21int main(int argc, char** argv)
22{
24
25 // Optional CLI arguments: [language] [voice]
26 std::string language = "en";
27 std::string voice = "auto";
28 if (argc > 1) {
29 language = argv[1];
30 }
31 if (argc > 2) {
32 voice = argv[2];
33 }
34
36 cfg.put("device", "kokoroSpeechSynthesizer");
37 cfg.put("language", language);
38 cfg.put("voice", voice);
39 cfg.put("speed", 1.0);
40 cfg.put("pitch", 0.0);
41
42 const char* kokoroPython = std::getenv("KOKORO_PYTHON");
43 if (kokoroPython != nullptr && *kokoroPython != '\0') {
44 cfg.put("python_executable", std::string(kokoroPython));
45 }
46
48 if (!dd.open(cfg)) {
49 std::cerr << "ERROR: Failed to open kokoroSpeechSynthesizer device" << std::endl;
50 return 1;
51 }
52
53 yarp::dev::ISpeechSynthesizer* iSynth = nullptr;
54 if (!dd.view(iSynth) || iSynth == nullptr) {
55 std::cerr << "ERROR: Failed to view ISpeechSynthesizer interface" << std::endl;
56 dd.close();
57 return 1;
58 }
59
60 // Interface-level checks that do not need the model.
61 {
62 std::string lang;
63 if (!iSynth->getLanguage(lang)) {
64 std::cerr << "ERROR: getLanguage failed" << std::endl;
65 dd.close();
66 return 1;
67 }
68 std::cout << "getLanguage -> " << lang << std::endl;
69
70 if (!iSynth->setLanguage(language)) {
71 std::cerr << "ERROR: setLanguage(" << language << ") failed" << std::endl;
72 dd.close();
73 return 1;
74 }
75 if (!iSynth->getLanguage(lang) || lang != language) {
76 std::cerr << "ERROR: getLanguage returned unexpected value: " << lang << std::endl;
77 dd.close();
78 return 1;
79 }
80 }
81
82 {
83 std::string v;
84 if (!iSynth->setVoice(voice)) {
85 std::cerr << "ERROR: setVoice(" << voice << ") failed" << std::endl;
86 dd.close();
87 return 1;
88 }
89 if (!iSynth->getVoice(v)) {
90 std::cerr << "ERROR: getVoice failed" << std::endl;
91 dd.close();
92 return 1;
93 }
94 std::cout << "getVoice -> " << v << std::endl;
95 }
96
97 {
98 double speed = 0.0;
99 if (!iSynth->setSpeed(2.0) || !iSynth->getSpeed(speed)) {
100 std::cerr << "ERROR: setSpeed/getSpeed failed" << std::endl;
101 dd.close();
102 return 1;
103 }
104 std::cout << "setSpeed(2.0) -> getSpeed " << speed << std::endl;
105 if (speed != 2.0) {
106 std::cerr << "ERROR: getSpeed returned unexpected value: " << speed << std::endl;
107 dd.close();
108 return 1;
109 }
110 }
111
112 {
113 double pitch = 1.0;
114 if (!iSynth->setPitch(0.5) || !iSynth->getPitch(pitch)) {
115 std::cerr << "ERROR: setPitch/getPitch failed" << std::endl;
116 dd.close();
117 return 1;
118 }
119 std::cout << "setPitch(0.5) -> getPitch " << pitch << std::endl;
120 }
121
122 // Now actually run the model.
123 std::cout << "Synthesizing audio (this loads/runs the Kokoro model, may take a while)..." << std::endl;
124 yarp::sig::Sound sound;
125 if (!iSynth->synthesize("This is a Kokoro smoke test from YARP.", sound)) {
126 std::cerr << "ERROR: synthesize failed" << std::endl;
127 dd.close();
128 return 1;
129 }
130
131 std::cout << "SYNTHESIZED freq=" << sound.getFrequency()
132 << " samples=" << sound.getSamples()
133 << " channels=" << sound.getChannels() << std::endl;
134
135 dd.close();
136 return 0;
137}
bool view(T *&x)
Get an interface to the device driver.
A generic interface for speech synthesis.
virtual yarp::dev::ReturnValue synthesize(const std::string &text, yarp::sig::Sound &sound)=0
Performs the speech synthesis.
virtual yarp::dev::ReturnValue setPitch(const double pitch)=0
Sets the pitch for speech synthesis.
virtual yarp::dev::ReturnValue setVoice(const std::string &voice_name="auto")=0
Sets the voice set for speech synthesis.
virtual yarp::dev::ReturnValue setSpeed(const double speed=0)=0
Sets the voice speed for speech synthesis.
virtual yarp::dev::ReturnValue getLanguage(std::string &language)=0
Gets the current language set for speech synthesis.
virtual yarp::dev::ReturnValue setLanguage(const std::string &language="auto")=0
Sets the language for speech synthesis.
virtual yarp::dev::ReturnValue getSpeed(double &speed)=0
Gets the current voice speed.
virtual yarp::dev::ReturnValue getVoice(std::string &voice_name)=0
Gets the current voice set for speech synthesis.
virtual yarp::dev::ReturnValue getPitch(double &pitch)=0
Gets the current pitch set for speech synthesis.
A container for a device driver.
Definition PolyDriver.h:23
bool close() override
Close the DeviceDriver.
bool open(const std::string &txt)
Construct and configure a device by its common name.
static void initMinimum()
Basic system initialization, not including plugins.
Definition Network.cpp:959
A class for storing options and configuration information.
Definition Property.h:32
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition Property.cpp:980
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition Sound.h:39
size_t getChannels() const
Get the number of channels of the sound.
Definition Sound.cpp:669
int getFrequency() const
Get the frequency of the sound (i.e.
Definition Sound.cpp:364
size_t getSamples() const
Get the number of samples contained in the sound.
Definition Sound.cpp:664
int main(int argc, char **argv)