From ba13f633722bac14cf7240f2de3222ac2ea5ece1 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Wed, 24 May 2023 14:05:13 -0500 Subject: [PATCH] Add espeak_TextToPhonemesWithTerminator --- src/include/espeak-ng/speak_lib.h | 6 ++++++ src/libespeak-ng/speech.c | 36 ++++++++++++++++++++++--------- src/libespeak-ng/translate.c | 13 ++++++++++- 3 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/include/espeak-ng/speak_lib.h b/src/include/espeak-ng/speak_lib.h index f060bd04c7..c15d140965 100644 --- a/src/include/espeak-ng/speak_lib.h +++ b/src/include/espeak-ng/speak_lib.h @@ -541,6 +541,12 @@ ESPEAK_API const char *espeak_TextToPhonemes(const void **textptr, int textmode, */ +#ifdef __cplusplus +extern "C" +#endif +ESPEAK_API const char *espeak_TextToPhonemesWithTerminator(const void **textptr, int textmode, int phonememode, int *terminator); +/* Version of espeak_TextToPhonemes that also returns the clause terminator (e.g., CLAUSE_INTONATION_FULL_STOP) */ + #ifdef __cplusplus extern "C" #endif diff --git a/src/libespeak-ng/speech.c b/src/libespeak-ng/speech.c index 9f90d10543..eb3b0f0b34 100644 --- a/src/libespeak-ng/speech.c +++ b/src/libespeak-ng/speech.c @@ -850,6 +850,31 @@ ESPEAK_API void espeak_SetPhonemeTrace(int phonememode, FILE *stream) f_trans = stderr; } +// Same as espeak_TextToPhonemes except we also get the clause terminator used (full stop, comma, etc.). +// Depends on the added TranslateClauseWithTerminator in +ESPEAK_API const char* espeak_TextToPhonemesWithTerminator(const void** textptr, int textmode, int phonememode, int* terminator) +{ + /* phoneme_mode + bit 1: 0=eSpeak's ascii phoneme names, 1= International Phonetic + Alphabet (as UTF-8 characters). bit 7: use (bits 8-23) as a tie within + multi-letter phonemes names bits 8-23: separator character, between + phoneme names + */ + + if (p_decoder == NULL) + p_decoder = create_text_decoder(); + + if (text_decoder_decode_string_multibyte( + p_decoder, *textptr, translator->encoding, textmode) + != ENS_OK) + return NULL; + + TranslateClauseWithTerminator(translator, NULL, NULL, terminator); + *textptr = text_decoder_get_buffer(p_decoder); + + return GetTranslatedPhonemeString(phonememode); +} + ESPEAK_API const char *espeak_TextToPhonemes(const void **textptr, int textmode, int phonememode) { /* phoneme_mode @@ -858,16 +883,7 @@ ESPEAK_API const char *espeak_TextToPhonemes(const void **textptr, int textmode, bits 8-23: separator character, between phoneme names */ - if (p_decoder == NULL) - p_decoder = create_text_decoder(); - - if (text_decoder_decode_string_multibyte(p_decoder, *textptr, translator->encoding, textmode) != ENS_OK) - return NULL; - - TranslateClause(translator, NULL, NULL); - *textptr = text_decoder_get_buffer(p_decoder); - - return GetTranslatedPhonemeString(phonememode); + return espeak_TextToPhonemesWithTerminator(textptr, textmode, phonememode, NULL); } ESPEAK_NG_API espeak_ng_STATUS espeak_ng_Cancel(void) diff --git a/src/libespeak-ng/translate.c b/src/libespeak-ng/translate.c index 6e1860e020..2b04fdd7ff 100644 --- a/src/libespeak-ng/translate.c +++ b/src/libespeak-ng/translate.c @@ -919,7 +919,9 @@ static int UpperCaseInWord(Translator *tr, char *word, int c) return 0; } -void TranslateClause(Translator *tr, int *tone_out, char **voice_change) +// Same as TranslateClause except we also get the clause terminator used (full stop, comma, etc.). +// Used by espeak_TextToPhonemesWithTerminator. +void TranslateClauseWithTerminator(Translator *tr, int *tone_out, char **voice_change, int *terminator_out) { int ix; int c; @@ -983,6 +985,10 @@ void TranslateClause(Translator *tr, int *tone_out, char **voice_change) MAKE_MEM_UNDEFINED(&source, sizeof(source)); terminator = ReadClause(tr, source, charix, &charix_top, N_TR_SOURCE, &tone, voice_change_name); + if (terminator_out != NULL) { + *terminator_out = terminator; + } + if (tone_out != NULL) { if (tone == 0) *tone_out = (terminator & CLAUSE_INTONATION_TYPE) >> 12; // tone type not overridden in ReadClause, use default @@ -1668,6 +1674,11 @@ void TranslateClause(Translator *tr, int *tone_out, char **voice_change) } } +void TranslateClause(Translator *tr, int *tone_out, char **voice_change) +{ + TranslateClauseWithTerminator(tr, tone_out, voice_change, NULL); +} + static int CalcWordLength(int source_index, int charix_top, short int *charix, WORD_TAB *words, int word_count) { int j; int k;