YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
KokoroSpeechSynthesizer.h
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#ifndef YARP_KOKOROSPEECHSYNTHESIZER_H
7#define YARP_KOKOROSPEECHSYNTHESIZER_H
8
11#include <yarp/sig/Sound.h>
12
14
15#include <mutex>
16#include <string>
17#include <vector>
18
19#if defined(__unix__) || defined(__APPLE__)
20# include <sys/types.h>
21#endif
22
36{
37public:
43 ~KokoroSpeechSynthesizer() override;
44
45 bool open(yarp::os::Searchable& config) override;
46 bool close() override;
47
48 yarp::dev::ReturnValue setLanguage(const std::string& language = "auto") override;
49 yarp::dev::ReturnValue getLanguage(std::string& language) override;
50 yarp::dev::ReturnValue setVoice(const std::string& voice_name = "auto") override;
51 yarp::dev::ReturnValue getVoice(std::string& voice_name) override;
52 yarp::dev::ReturnValue getAvailableVoices(yarp::sig::VectorOfString& voices) override;
53 yarp::dev::ReturnValue setSpeed(double speed = 0) override;
54 yarp::dev::ReturnValue getSpeed(double& speed) override;
55 yarp::dev::ReturnValue setPitch(double pitch) override;
56 yarp::dev::ReturnValue getPitch(double& pitch) override;
57 yarp::dev::ReturnValue synthesize(const std::string& text, yarp::sig::Sound& sound) override;
58
59private:
60 bool startWorker();
61 void stopWorker();
62 bool workerSynthesize(const std::string& text, yarp::sig::Sound& sound);
63
64 static std::string base64Encode(const std::string& input);
65 static bool base64Decode(const std::string& input, std::vector<unsigned char>& output);
66 static bool knownLanguage(const std::string& language, std::string& kokoroCode);
67 static std::string defaultVoiceFor(const std::string& kokoroCode);
68
69#if defined(__unix__) || defined(__APPLE__)
70 bool writeAll(const std::string& data);
71 bool readLine(std::string& line, int timeoutMs);
72 int m_workerInput{-1};
73 int m_workerOutput{-1};
74 pid_t m_workerPid{-1};
75 std::string m_readBuffer;
76#endif
77
78 std::mutex m_mutex;
79 bool m_open{false};
80 std::string m_pythonExecutable{"python3"};
81 std::string m_workerScript;
82 std::string m_modelRepository;
83 std::string m_device;
84 std::string m_currentLanguage{"en"};
85 std::string m_kokoroLanguage{"a"};
86 std::string m_currentVoice{"auto"};
87 bool m_voiceExplicit{false};
88 double m_currentSpeed{1.0};
89 double m_currentPitch{0.0};
90};
91
92#endif // YARP_KOKOROSPEECHSYNTHESIZER_H
This class is the parameters parser for class KokoroSpeechSynthesizer.
YARP speech synthesizer backed by the Kokoro Python package.
yarp::dev::ReturnValue getLanguage(std::string &language) override
Gets the current language set for speech synthesis.
KokoroSpeechSynthesizer()=default
yarp::dev::ReturnValue setVoice(const std::string &voice_name="auto") override
Sets the voice set for speech synthesis.
yarp::dev::ReturnValue getAvailableVoices(yarp::sig::VectorOfString &voices) override
KokoroSpeechSynthesizer & operator=(KokoroSpeechSynthesizer &&)=delete
yarp::dev::ReturnValue getVoice(std::string &voice_name) override
Gets the current voice set for speech synthesis.
yarp::dev::ReturnValue getPitch(double &pitch) override
Gets the current pitch set for speech synthesis.
yarp::dev::ReturnValue synthesize(const std::string &text, yarp::sig::Sound &sound) override
Performs the speech synthesis.
KokoroSpeechSynthesizer & operator=(const KokoroSpeechSynthesizer &)=delete
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::dev::ReturnValue setLanguage(const std::string &language="auto") override
Sets the language for speech synthesis.
KokoroSpeechSynthesizer(const KokoroSpeechSynthesizer &)=delete
yarp::dev::ReturnValue setSpeed(double speed=0) override
Sets the voice speed for speech synthesis.
yarp::dev::ReturnValue setPitch(double pitch) override
Sets the pitch for speech synthesis.
KokoroSpeechSynthesizer(KokoroSpeechSynthesizer &&)=delete
bool close() override
Close the DeviceDriver.
yarp::dev::ReturnValue getSpeed(double &speed) override
Gets the current voice speed.
Interface implemented by all device drivers.
A generic interface for speech synthesis.
A base class for nested structures that can be searched.
Definition Searchable.h:31
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
Definition Sound.h:42