6#ifndef _USE_MATH_DEFINES
7#define _USE_MATH_DEFINES
13#include <nlohmann/json.hpp>
14#define DR_WAV_IMPLEMENTATION
22using json = nlohmann::json;
31 auto* out =
static_cast<std::string*
>(
userp);
39 auto* out =
static_cast<std::vector<unsigned char>*
>(
userp);
40 const auto* begin =
static_cast<const unsigned char*
>(
contents);
41 out->insert(out->end(), begin, begin +
total);
51struct HttpBytesResponse
54 std::vector<unsigned char> body;
61 throw std::runtime_error(
"curl_easy_init failed");
65 std::string body =
payload.dump();
96 throw std::runtime_error(
"curl_easy_init failed");
119 throw std::runtime_error(
"curl_easy_init failed");
122 HttpBytesResponse
resp;
143 std::vector<int16_t> interleavedPcm16;
148 unsigned int channels = 0;
149 unsigned int sampleRate = 0;
161 throw std::runtime_error(
"Unable to decode WAV from memory");
167 out.sampleRate = sampleRate;
168 out.channels = channels;
185 std::map<std::string, std::string>
profile;
189 if (!value.is_null()) {
190 profile[key] = value.is_string() ? value.get<std::string>() : value.dump();
207 if (
audio.channels == 0 ||
audio.sampleRate == 0) {
208 throw std::runtime_error(
"Invalid decoded audio metadata");
211 if (
audio.interleavedPcm16.size() %
audio.channels != 0) {
212 throw std::runtime_error(
"Invalid PCM layout");
218 snd.setFrequency(
static_cast<int>(
audio.sampleRate));
222 for (
size_t ch = 0; ch <
audio.channels; ++ch) {
224 snd.set(sample, s, ch);
235 if (
resp.status < 200 ||
resp.status >= 300) {
236 throw std::runtime_error(
"Get profiles failed. HTTP " + std::to_string(
resp.status) +
": " +
resp.body);
245 if (
profile.at(
"name") == name) {
256 const std::string& text,
257 const std::string& language,
262 <<
" text='" << text <<
"'"
263 <<
" language='" << language <<
"'"
264 <<
" speed=" << speed;
271 {
"language", language},
274 {
"max_chunk_chars", 800},
275 {
"crossfade_ms", 50},
283 if (
resp.status < 200 ||
resp.status >= 300) {
284 throw std::runtime_error(
"Generate failed. HTTP " + std::to_string(
resp.status) +
": " +
resp.body);
289 if (!j.contains(
"id") || j.at(
"id").is_null()) {
290 throw std::runtime_error(
"Generate response has no valid id: " +
resp.body);
293 std::string
id = j.at(
"id").get<std::string>();
295 throw std::runtime_error(
"Generate response has empty id: " +
resp.body);
348 std::chrono::milliseconds
pollInterval = std::chrono::milliseconds(1000),
349 std::chrono::seconds timeout = std::chrono::seconds(120))
351 const auto deadline = std::chrono::steady_clock::now() + timeout;
353 while (std::chrono::steady_clock::now() <
deadline) {
356 if (
resp.status < 200 ||
resp.status >= 300) {
357 throw std::runtime_error(
"History failed. HTTP " + std::to_string(
resp.status) +
": " +
resp.body);
363 if (j.contains(
"status") && !j.at(
"status").is_null()) {
364 status = j.at(
"status").is_string() ? j.at(
"status").get<std::string>() : j.at(
"status").dump();
368 if (j.contains(
"error") && !j.at(
"error").is_null()) {
369 error = j.at(
"error").is_string() ? j.at(
"error").get<std::string>() : j.at(
"error").dump();
373 if (j.contains(
"audio_path") && !j.at(
"audio_path").is_null()) {
374 audioPath = j.at(
"audio_path").is_string() ? j.at(
"audio_path").get<std::string>() : j.at(
"audio_path").dump();
377 double duration = 0.0;
378 if (j.contains(
"duration") && !j.at(
"duration").is_null()) {
379 duration = j.at(
"duration").get<
double>();
383 <<
"Generation status=" << status
385 <<
"' duration=" << duration
386 <<
" error='" <<
error <<
"'";
388 if (status ==
"error" || status ==
"failed" || !
error.empty()) {
389 throw std::runtime_error(
"Generation failed. Status=" + status +
" error=" +
error);
393 if ((status ==
"completed" || status ==
"done" || status ==
"finished") &&
402 throw std::runtime_error(
"Timeout waiting for Voicebox generation");
441 if (
resp.status < 200 ||
resp.status >= 300) {
443 if (!
resp.body.empty()) {
447 <<
" received " <<
resp.body.size()
449 throw std::runtime_error(
"Audio download failed. HTTP " + std::to_string(
resp.status) +
": " +
errorBody);
452 if (
resp.body.empty()) {
453 throw std::runtime_error(
"Audio download returned empty response");
463 const std::string& text,
464 const std::string& language,
513 if (language.empty()) {
517 if(language.find(
"-")!= std::string::npos) {
518 yCDebug(
VOICEBOXSYNTHESIZER) <<
"Language '" << language <<
"' contains region code. Voicebox may not support this format. Consider using just the language code (e.g. 'en' instead of 'en-US')";
520 m_region = language.substr(language.find(
"-")+1);
528 language = m_language+
"-"+m_region;
554 }
catch (
const std::exception&
ex) {
577 }
catch (
const std::exception&
ex) {
622 }
catch (
const std::exception&
ex) {
static std::string startGeneration(const std::string &baseUrl, const profileMap &profile, const std::string &text, const std::string &language, double speed)
const yarp::os::LogComponent & VOICEBOXSYNTHESIZER()
static void waitUntilFinished(const std::string &baseUrl, const std::string &generationId, std::chrono::milliseconds pollInterval=std::chrono::milliseconds(1000), std::chrono::seconds timeout=std::chrono::seconds(120))
static yarp::sig::Sound synthesizeVoiceboxToYarpSound(const std::string &baseUrl, const profileMap &profile, const std::string &text, const std::string &language, double speed)
static bool findProfileByName(const profilesList &profiles, const std::string &name, profileMap &outProfile)
static profilesList getProfiles(const std::string &baseUrl)
static yarp::sig::Sound downloadAudioAsYarpSound(const std::string &baseUrl, const std::string &generationId)
static yarp::sig::Sound decodedAudioToYarpSound(const DecodedAudio &audio)
std::map< std::string, std::string > profileMap
std::vector< std::map< std::string, std::string > > profilesList
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
yarp::dev::ReturnValue getPitch(double &pitch) override
Gets the current pitch set for speech synthesis.
yarp::dev::ReturnValue getLanguage(std::string &language) override
Gets the current language set for speech synthesis.
yarp::dev::ReturnValue setSpeed(const double speed) override
Sets the voice speed for speech synthesis.
yarp::dev::ReturnValue getSpeed(double &speed) override
Gets the current voice speed.
yarp::dev::ReturnValue getVoice(std::string &voice_name) override
Gets the current voice set for speech synthesis.
bool close() override
Close the DeviceDriver.
yarp::dev::ReturnValue setVoice(const std::string &voice_name) override
Sets the voice set for speech synthesis.
yarp::dev::ReturnValue setLanguage(const std::string &language) override
Sets the language for speech synthesis.
yarp::dev::ReturnValue getAvailableVoices(yarp::sig::VectorOfString &voices) override
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
yarp::dev::ReturnValue synthesize(const std::string &text, yarp::sig::Sound &sound) override
Performs the speech synthesis.
yarp::dev::ReturnValue setPitch(const double pitch) override
Sets the pitch for speech synthesis.
@ return_value_error_method_failed
Method is deprecated.
A mini-server for performing network communication in the background.
BufferedPort()
Constructor.
A base class for nested structures that can be searched.
Class for storing sounds See Audio in YARP for additional documentation on YARP audio.
drwav_int16 * drwav_open_memory_and_read_pcm_frames_s16(const void *data, size_t dataSize, unsigned int *channelsOut, unsigned int *sampleRateOut, drwav_uint64 *totalFrameCountOut, const drwav_allocation_callbacks *pAllocationCallbacks)
void drwav_free(void *p, const drwav_allocation_callbacks *pAllocationCallbacks)
#define yCInfo(component,...)
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.