Compare commits

...

2 Commits

Author SHA1 Message Date
Hoan Luu Huu 30f2189986 support playht3.0 (#119)
* support playht3.0

* update language

* wip

* update top_p and repetition_penalty

---------

Co-authored-by: root <root@af6633a5cfe8>
2024-10-09 12:12:59 -04:00
Hoan Luu Huu ffde303446 fixed azure transcribe crashed if both endpointId and alternativeLang… (#124)
* fixed azure transcribe crashed if both endpointId and alternativeLangs are used

* fixed review comments

Signed-off-by: root <root@af6633a5cfe8>

---------

Signed-off-by: root <root@af6633a5cfe8>
Co-authored-by: root <root@af6633a5cfe8>
2024-10-07 09:52:58 -04:00
4 changed files with 81 additions and 24 deletions
+42 -18
View File
@@ -72,10 +72,7 @@ public:
if (switch_true(switch_channel_get_variable(channel, "AZURE_USE_OUTPUT_FORMAT_DETAILED"))) {
speechConfig->SetOutputFormat(OutputFormat::Detailed);
}
if (nullptr != endpointId) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(psession), SWITCH_LOG_DEBUG, "setting endpoint id: %s\n", endpointId);
speechConfig->SetEndpointId(endpointId);
}
if (!sdkInitialized && sdkLog) {
sdkInitialized = true;
speechConfig->SetProperty(PropertyId::Speech_LogFilename, sdkLog);
@@ -104,11 +101,31 @@ public:
languages.push_back( alt_langs[i]);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(psession), SWITCH_LOG_DEBUG, "added alternative lang %s\n", alt_langs[i]);
}
auto autoDetectSourceLanguageConfig = AutoDetectSourceLanguageConfig::FromLanguages(languages);
std::vector<std::shared_ptr<SourceLanguageConfig>> sourceLanguageConfigs;
for (const auto& language : languages) {
std::shared_ptr<SourceLanguageConfig> sourceLanguageConfig;
if (endpointId != nullptr) {
sourceLanguageConfig = SourceLanguageConfig::FromLanguage(language, endpointId);
} else {
sourceLanguageConfig = SourceLanguageConfig::FromLanguage(language);
}
sourceLanguageConfigs.push_back(sourceLanguageConfig);
}
// Create AutoDetectSourceLanguageConfig from SourceLanguageConfigs
auto autoDetectSourceLanguageConfig = AutoDetectSourceLanguageConfig::FromSourceLanguageConfigs(sourceLanguageConfigs);
m_recognizer = SpeechRecognizer::FromConfig(speechConfig, autoDetectSourceLanguageConfig, audioConfig);
}
else {
auto sourceLanguageConfig = SourceLanguageConfig::FromLanguage(lang);
std::shared_ptr<SourceLanguageConfig> sourceLanguageConfig;
if (endpointId != nullptr) {
sourceLanguageConfig = SourceLanguageConfig::FromLanguage(lang, endpointId);
} else {
sourceLanguageConfig = SourceLanguageConfig::FromLanguage(lang);
}
m_recognizer = SpeechRecognizer::FromConfig(speechConfig, sourceLanguageConfig, audioConfig);
}
@@ -487,20 +504,27 @@ extern "C" {
auto read_codec = switch_core_session_get_read_codec(session);
uint32_t sampleRate = read_codec->implementation->actual_samples_per_second;
if (bug) {
struct cap_cb* existing_cb = (struct cap_cb*) switch_core_media_bug_get_user_data(bug);
GStreamer* existing_streamer = (GStreamer*) existing_cb->streamer;
existing_cb->is_keep_alive = 0;
if (!existing_streamer->hasConfigurationChanged(channels, lang, interim, sampleRate, region, subscriptionKey)) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Reuse active azure connection.\n");
try {
struct cap_cb* existing_cb = (struct cap_cb*) switch_core_media_bug_get_user_data(bug);
GStreamer* existing_streamer = (GStreamer*) existing_cb->streamer;
existing_cb->is_keep_alive = 0;
if (!existing_streamer->hasConfigurationChanged(channels, lang, interim, sampleRate, region, subscriptionKey)) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Reuse active azure connection.\n");
return SWITCH_STATUS_SUCCESS;
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Azure configuration is changed, destroy old and create new azure connection\n");
reaper(existing_cb);
streamer = new GStreamer(sessionId, bugname, channels, lang, interim, sampleRate, region, subscriptionKey, responseHandler);
if (!existing_cb->vad) streamer->connect();
existing_cb->streamer = streamer;
*ppUserData = existing_cb;
return SWITCH_STATUS_SUCCESS;
} catch (std::exception& e) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "%s: Error initializing gstreamer: %s.\n",
switch_channel_get_name(channel), e.what());
return SWITCH_STATUS_FALSE;
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Azure configuration is changed, destroy old and create new azure connection\n");
reaper(existing_cb);
streamer = new GStreamer(sessionId, bugname, channels, lang, interim, sampleRate, region, subscriptionKey, responseHandler);
if (!existing_cb->vad) streamer->connect();
existing_cb->streamer = streamer;
*ppUserData = existing_cb;
return SWITCH_STATUS_SUCCESS;
}
int err;
switch_threadattr_t *thd_attr = NULL;
+20
View File
@@ -14,6 +14,10 @@ static void clearPlayht(playht_t* p, int freeAll) {
if (p->seed) free(p->seed);
if (p->temperature) free(p->temperature);
if (p->voice_engine) free(p->voice_engine);
if (p->synthesize_url) free(p->synthesize_url);
if (p->language) free(p->language);
if (p->top_p) free(p->top_p);
if (p->repetition_penalty) free(p->repetition_penalty);
if (p->emotion) free(p->emotion);
if (p->voice_guidance) free(p->voice_guidance);
if (p->style_guidance) free(p->style_guidance);
@@ -36,6 +40,10 @@ static void clearPlayht(playht_t* p, int freeAll) {
p->seed = NULL;
p->temperature = NULL;
p->voice_engine = NULL;
p->synthesize_url = NULL;
p->language = NULL;
p->top_p = NULL;
p->repetition_penalty = NULL;
p->emotion = NULL;
p->voice_guidance = NULL;
p->style_guidance = NULL;
@@ -154,6 +162,18 @@ static void p_text_param_tts(switch_speech_handle_t *sh, char *param, const char
} else if (0 == strcmp(param, "voice_engine")) {
if (p->voice_engine) free(p->voice_engine);
p->voice_engine = strdup(val);
} else if (0 == strcmp(param, "synthesize_url")) {
if (p->synthesize_url) free(p->synthesize_url);
p->synthesize_url = strdup(val);
} else if (0 == strcmp(param, "language")) {
if (p->language) free(p->language);
p->language = strdup(val);
} else if (0 == strcmp(param, "top_p")) {
if (p->top_p) free(p->top_p);
p->top_p = strdup(val);
} else if (0 == strcmp(param, "repetition_penalty")) {
if (p->repetition_penalty) free(p->repetition_penalty);
p->repetition_penalty = strdup(val);
} else if (0 == strcmp(param, "emotion")) {
if (p->emotion) free(p->emotion);
p->emotion = strdup(val);
+4
View File
@@ -11,10 +11,14 @@ typedef struct playht_data {
char *seed;
char *temperature;
char *voice_engine;
char *synthesize_url;
char *language;
char *emotion;
char *voice_guidance;
char *style_guidance;
char *text_guidance;
char *top_p;
char *repetition_penalty;
/* result data */
long response_code;
+15 -6
View File
@@ -780,7 +780,7 @@ extern "C" {
}
/* format url*/
std::string url = "https://api.play.ht/api/v2/tts/stream";
std::string url = p->synthesize_url;
/* create the JSON body */
cJSON * jResult = cJSON_CreateObject();
@@ -800,9 +800,6 @@ extern "C" {
cJSON_AddNumberToObject(jResult, "speed", val);
}
}
if (p->seed) {
cJSON_AddNumberToObject(jResult, "seed", atoi(p->seed));
}
if (p->temperature) {
cJSON_AddNumberToObject(jResult, "temperature", std::strtof(p->temperature, nullptr));
}
@@ -818,6 +815,15 @@ extern "C" {
if (p->text_guidance) {
cJSON_AddNumberToObject(jResult, "text_guidance", atoi(p->text_guidance));
}
if (strcmp(p->voice_engine, "Play3.0") == 0 && p->language) {
cJSON_AddStringToObject(jResult, "language", p->language);
}
if (p->repetition_penalty) {
cJSON_AddNumberToObject(jResult, "repetition_penalty", std::strtof(p->repetition_penalty, nullptr));
}
if (p->top_p) {
cJSON_AddNumberToObject(jResult, "top_p", std::strtof(p->top_p, nullptr));
}
char *json = cJSON_PrintUnformatted(jResult);
cJSON_Delete(jResult);
@@ -889,9 +895,12 @@ extern "C" {
/* call this function to close a socket */
curl_easy_setopt(easy, CURLOPT_CLOSESOCKETFUNCTION, close_socket);
// Play3.0 voice engine doesn't need authorization
if (strcmp(p->voice_engine, "Play3.0") != 0) {
conn->hdr_list = curl_slist_append(conn->hdr_list, api_key_stream.str().c_str());
conn->hdr_list = curl_slist_append(conn->hdr_list, user_id_stream.str().c_str());
}
conn->hdr_list = curl_slist_append(conn->hdr_list, api_key_stream.str().c_str());
conn->hdr_list = curl_slist_append(conn->hdr_list, user_id_stream.str().c_str());
conn->hdr_list = curl_slist_append(conn->hdr_list, "Accept: audio/mpeg");
conn->hdr_list = curl_slist_append(conn->hdr_list, "Content-Type: application/json");
curl_easy_setopt(easy, CURLOPT_HTTPHEADER, conn->hdr_list);