From f53ebf469c9ca54e2af569e1ff467a856b5771f1 Mon Sep 17 00:00:00 2001 From: Juho Hiltunen Date: Sat, 5 Nov 2022 09:44:40 +0200 Subject: [PATCH 1/5] cleanup: remove code duplication with TerminateBufWithSpaceAndZero(). One redundant ix++ was removed because there's a return right after that. --- src/libespeak-ng/readclause.c | 37 ++++++++++++++++------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/libespeak-ng/readclause.c b/src/libespeak-ng/readclause.c index ea286fd3..b406b102 100644 --- a/src/libespeak-ng/readclause.c +++ b/src/libespeak-ng/readclause.c @@ -51,6 +51,8 @@ #define N_XML_BUF 500 +static void TerminateBufWithSpaceAndZero(char *buf, int index); + static const char *xmlbase = ""; // base URL from static int namedata_ix = 0; @@ -341,8 +343,7 @@ static int AnnouncePunctuation(Translator *tr, int c1, int *c2_ptr, char *output // end the clause now and pick up the punctuation next time UngetC(c2); ungot_char2 = c1; - buf[0] = ' '; - buf[1] = 0; + TerminateBufWithSpaceAndZero(buf, 0); } } @@ -607,8 +608,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ // Perhaps not enough room, end the clause before the SSML tag UngetC(c2); ungot_char2 = c1; - buf[ix] = ' '; - buf[ix+1] = 0; + TerminateBufWithSpaceAndZero(buf, ix); return CLAUSE_NONE; } @@ -625,8 +625,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ terminator = ProcessSsmlTag(xml_buf, buf, &ix, n_buf, xmlbase, &audio_text, current_voice_id, &base_voice, base_voice_variant_name, &ignore_text, &clear_skipping_text, &sayas_mode, &sayas_start, ssml_stack, &n_ssml_stack, &n_param_stack, (int *)speech_parameters); if (terminator != 0) { - buf[ix] = ' '; - buf[ix++] = 0; + TerminateBufWithSpaceAndZero(buf, ix); if (terminator & CLAUSE_TYPE_VOICE_CHANGE) strcpy(voice_change, current_voice_id); @@ -652,8 +651,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ ix += utf8_out(c1, &buf[ix]); terminator = CLAUSE_PERIOD; // line doesn't end in punctuation, assume period } - buf[ix] = ' '; - buf[ix+1] = 0; + TerminateBufWithSpaceAndZero(buf, ix); return terminator; } @@ -753,8 +751,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ if (end_clause_after_tag) RemoveChar(&buf[end_clause_index]); // delete clause-end punctiation - buf[ix] = ' '; - buf[ix+1] = 0; + TerminateBufWithSpaceAndZero(buf, ix); if (parag > 3) parag = 3; if (option_ssml) parag = 1; @@ -764,8 +761,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ if (linelength <= option_linelength) { // treat lines shorter than a specified length as end-of-clause UngetC(c2); - buf[ix] = ' '; - buf[ix+1] = 0; + TerminateBufWithSpaceAndZero(buf, ix); return CLAUSE_COLON; } @@ -786,8 +782,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ if (!IsAlpha(c1) || !iswlower(c1)) { UngetC(c2); ungot_char2 = c1; - buf[end_clause_index] = ' '; // delete the end-clause punctuation - buf[end_clause_index+1] = 0; + TerminateBufWithSpaceAndZero(buf, end_clause_index); return end_clause_after_tag; } end_clause_after_tag = 0; @@ -921,8 +916,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ if (is_end_clause) { UngetC(c_next); - buf[ix] = ' '; - buf[ix+1] = 0; + TerminateBufWithSpaceAndZero(buf, ix); if (iswdigit(cprev) && !IsAlpha(c_next)) // ???? punct_data &= ~CLAUSE_DOT_AFTER_LAST_WORD; @@ -967,8 +961,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ // clause too long, getting near end of buffer, so break here // try to break at a word boundary (unless we actually reach the end of buffer). // (n_buf-4) is to allow for 3 bytes of multibyte character plus terminator. - buf[ix] = ' '; - buf[ix+1] = 0; + TerminateBufWithSpaceAndZero(buf, ix); UngetC(c2); return CLAUSE_NONE; } @@ -978,8 +971,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ ix += utf8_out(CHAR_EMPHASIS, &buf[ix]); if (end_clause_after_tag) RemoveChar(&buf[end_clause_index]); // delete clause-end punctiation - buf[ix] = ' '; - buf[ix+1] = 0; + TerminateBufWithSpaceAndZero(buf, ix); return CLAUSE_EOF; // end of file } @@ -1022,3 +1014,8 @@ void InitText2(void) xmlbase = NULL; } + +static void TerminateBufWithSpaceAndZero(char *buf, int index) { + buf[index] = ' '; + buf[index+1] = 0; +} \ No newline at end of file From 95e5c4458d3387c26730882a3666b600279b7cc3 Mon Sep 17 00:00:00 2001 From: Juho Hiltunen Date: Sat, 5 Nov 2022 11:46:45 +0200 Subject: [PATCH 2/5] cleanup: remove duplication with DecodeWithPhonemeMode(). TODO: think of a better function name. --- src/libespeak-ng/readclause.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/libespeak-ng/readclause.c b/src/libespeak-ng/readclause.c index b406b102..34524811 100644 --- a/src/libespeak-ng/readclause.c +++ b/src/libespeak-ng/readclause.c @@ -51,6 +51,7 @@ #define N_XML_BUF 500 +static void DecodeWithPhonemeMode(char *buf, char *phonemes, Translator *tr, Translator *tr2, unsigned int flags[]); static void TerminateBufWithSpaceAndZero(char *buf, int index); static const char *xmlbase = ""; // base URL from @@ -192,10 +193,7 @@ static const char *LookupSpecial(Translator *tr, const char *string, char *text_ flags[0] = flags[1] = 0; if (LookupDictList(tr, &string1, phonemes, flags, 0, NULL)) { - char phonemes2[55]; - SetWordStress(tr, phonemes, flags, -1, 0); - DecodePhonemes(phonemes, phonemes2); - sprintf(text_out, "[\002%s]]", phonemes2); + DecodeWithPhonemeMode(text_out, phonemes, tr, NULL, flags); return text_out; } return NULL; @@ -256,16 +254,11 @@ static const char *LookupCharName(Translator *tr, int c, int only) } if (phonemes[0]) { - char phonemes2[60]; if (lang_name) { - SetWordStress(translator2, phonemes, flags, -1, 0); - DecodePhonemes(phonemes, phonemes2); - sprintf(buf, "[\002_^_%s %s _^_%s]]", ESPEAKNG_DEFAULT_VOICE, phonemes2, WordToString2(tr->translator_name)); + DecodeWithPhonemeMode(buf, phonemes, tr, translator2, flags); SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table } else { - SetWordStress(tr, phonemes, flags, -1, 0); - DecodePhonemes(phonemes, phonemes2); - sprintf(buf, "[\002%s]] ", phonemes2); + DecodeWithPhonemeMode(buf, phonemes, tr, NULL, flags); } } else if (only == 0) strcpy(buf, "[\002(X1)(X1)(X1)]]"); @@ -1018,4 +1011,17 @@ void InitText2(void) static void TerminateBufWithSpaceAndZero(char *buf, int index) { buf[index] = ' '; buf[index+1] = 0; +} + +static void DecodeWithPhonemeMode(char *buf, char *phonemes, Translator *tr, Translator *tr2, unsigned int flags[]) { + char phonemes2[55]; + if (tr2 == NULL) { + SetWordStress(tr, phonemes, flags, -1, 0); + DecodePhonemes(phonemes, phonemes2); + sprintf(buf, "[\002%s]]", phonemes2); + } else { + SetWordStress(tr2, phonemes, flags, -1, 0); + DecodePhonemes(phonemes, phonemes2); + sprintf(buf, "[\002_^_%s %s _^_%s]]", ESPEAKNG_DEFAULT_VOICE, phonemes2, WordToString2(tr->translator_name)); + } } \ No newline at end of file From acc43a76e45482118cf86252f10ddc90cb5857c6 Mon Sep 17 00:00:00 2001 From: Juho Hiltunen Date: Sat, 5 Nov 2022 12:14:55 +0200 Subject: [PATCH 3/5] cleanup: remove code duplication with UnGetC() --- src/libespeak-ng/readclause.c | 36 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/libespeak-ng/readclause.c b/src/libespeak-ng/readclause.c index 34524811..543e9afa 100644 --- a/src/libespeak-ng/readclause.c +++ b/src/libespeak-ng/readclause.c @@ -52,7 +52,7 @@ #define N_XML_BUF 500 static void DecodeWithPhonemeMode(char *buf, char *phonemes, Translator *tr, Translator *tr2, unsigned int flags[]); -static void TerminateBufWithSpaceAndZero(char *buf, int index); +static void TerminateBufWithSpaceAndZero(char *buf, int index, int *ungetc); static const char *xmlbase = ""; // base URL from @@ -334,9 +334,8 @@ static int AnnouncePunctuation(Translator *tr, int c1, int *c2_ptr, char *output punctname, punct_count, punctname); } else { // end the clause now and pick up the punctuation next time - UngetC(c2); ungot_char2 = c1; - TerminateBufWithSpaceAndZero(buf, 0); + TerminateBufWithSpaceAndZero(buf, 0, &c2); } } @@ -599,9 +598,8 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ // check for space in the output buffer for embedded commands produced by the SSML tag if (ix > (n_buf - 20)) { // Perhaps not enough room, end the clause before the SSML tag - UngetC(c2); ungot_char2 = c1; - TerminateBufWithSpaceAndZero(buf, ix); + TerminateBufWithSpaceAndZero(buf, ix, &c2); return CLAUSE_NONE; } @@ -618,7 +616,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ terminator = ProcessSsmlTag(xml_buf, buf, &ix, n_buf, xmlbase, &audio_text, current_voice_id, &base_voice, base_voice_variant_name, &ignore_text, &clear_skipping_text, &sayas_mode, &sayas_start, ssml_stack, &n_ssml_stack, &n_param_stack, (int *)speech_parameters); if (terminator != 0) { - TerminateBufWithSpaceAndZero(buf, ix); + TerminateBufWithSpaceAndZero(buf, ix, NULL); if (terminator & CLAUSE_TYPE_VOICE_CHANGE) strcpy(voice_change, current_voice_id); @@ -644,7 +642,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ ix += utf8_out(c1, &buf[ix]); terminator = CLAUSE_PERIOD; // line doesn't end in punctuation, assume period } - TerminateBufWithSpaceAndZero(buf, ix); + TerminateBufWithSpaceAndZero(buf, ix, NULL); return terminator; } @@ -740,11 +738,9 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ } if (parag > 0) { // 2nd newline, assume paragraph - UngetC(c2); - if (end_clause_after_tag) RemoveChar(&buf[end_clause_index]); // delete clause-end punctiation - TerminateBufWithSpaceAndZero(buf, ix); + TerminateBufWithSpaceAndZero(buf, ix, &c2); if (parag > 3) parag = 3; if (option_ssml) parag = 1; @@ -753,8 +749,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ if (linelength <= option_linelength) { // treat lines shorter than a specified length as end-of-clause - UngetC(c2); - TerminateBufWithSpaceAndZero(buf, ix); + TerminateBufWithSpaceAndZero(buf, ix, &c2); return CLAUSE_COLON; } @@ -773,9 +768,8 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ if (!iswspace(c1)) { if (!IsAlpha(c1) || !iswlower(c1)) { - UngetC(c2); ungot_char2 = c1; - TerminateBufWithSpaceAndZero(buf, end_clause_index); + TerminateBufWithSpaceAndZero(buf, end_clause_index, &c2); return end_clause_after_tag; } end_clause_after_tag = 0; @@ -908,8 +902,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ } if (is_end_clause) { - UngetC(c_next); - TerminateBufWithSpaceAndZero(buf, ix); + TerminateBufWithSpaceAndZero(buf, ix, &c_next); if (iswdigit(cprev) && !IsAlpha(c_next)) // ???? punct_data &= ~CLAUSE_DOT_AFTER_LAST_WORD; @@ -954,8 +947,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ // clause too long, getting near end of buffer, so break here // try to break at a word boundary (unless we actually reach the end of buffer). // (n_buf-4) is to allow for 3 bytes of multibyte character plus terminator. - TerminateBufWithSpaceAndZero(buf, ix); - UngetC(c2); + TerminateBufWithSpaceAndZero(buf, ix, &c2); return CLAUSE_NONE; } } @@ -964,7 +956,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ ix += utf8_out(CHAR_EMPHASIS, &buf[ix]); if (end_clause_after_tag) RemoveChar(&buf[end_clause_index]); // delete clause-end punctiation - TerminateBufWithSpaceAndZero(buf, ix); + TerminateBufWithSpaceAndZero(buf, ix, NULL); return CLAUSE_EOF; // end of file } @@ -1008,9 +1000,13 @@ void InitText2(void) xmlbase = NULL; } -static void TerminateBufWithSpaceAndZero(char *buf, int index) { +static void TerminateBufWithSpaceAndZero(char *buf, int index, int *ungetc) { buf[index] = ' '; buf[index+1] = 0; + + if (ungetc != NULL) { + UngetC(*ungetc); + } } static void DecodeWithPhonemeMode(char *buf, char *phonemes, Translator *tr, Translator *tr2, unsigned int flags[]) { From ca0019b1973410e9fe78bdccac71f28e8dc3d0f7 Mon Sep 17 00:00:00 2001 From: Juho Hiltunen Date: Sat, 5 Nov 2022 14:05:48 +0200 Subject: [PATCH 4/5] cleanup: use bool in LookupCharName() --- src/libespeak-ng/readclause.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libespeak-ng/readclause.c b/src/libespeak-ng/readclause.c index 543e9afa..c35c6edc 100644 --- a/src/libespeak-ng/readclause.c +++ b/src/libespeak-ng/readclause.c @@ -199,7 +199,7 @@ static const char *LookupSpecial(Translator *tr, const char *string, char *text_ return NULL; } -static const char *LookupCharName(Translator *tr, int c, int only) +static const char *LookupCharName(Translator *tr, int c, bool only) { // Find the phoneme string (in ascii) to speak the name of character c // Used for punctuation characters and symbols @@ -238,7 +238,7 @@ static const char *LookupCharName(Translator *tr, int c, int only) } } - if ((only == 0) && ((phonemes[0] == 0) || (phonemes[0] == phonSWITCH)) && (tr->translator_name != L('e', 'n'))) { + if ((only == false) && ((phonemes[0] == 0) || (phonemes[0] == phonSWITCH)) && (tr->translator_name != L('e', 'n'))) { // not found, try English SetTranslator2(ESPEAKNG_DEFAULT_VOICE); string = &single_letter[1]; @@ -260,7 +260,7 @@ static const char *LookupCharName(Translator *tr, int c, int only) } else { DecodeWithPhonemeMode(buf, phonemes, tr, NULL, flags); } - } else if (only == 0) + } else if (only == false) strcpy(buf, "[\002(X1)(X1)(X1)]]"); return buf; @@ -296,7 +296,7 @@ static int AnnouncePunctuation(Translator *tr, int c1, int *c2_ptr, char *output punctname = ph_buf; // use word for 'period' instead of 'dot' } if (punctname == NULL) - punctname = LookupCharName(tr, c1, 0); + punctname = LookupCharName(tr, c1, false); if (punctname == NULL) return -1; @@ -831,7 +831,7 @@ int ReadClause(Translator *tr, char *buf, short *charix, int *charix_top, int n_ char *p2; p2 = &buf[ix]; - sprintf(p2, "%s", LookupCharName(tr, c1, 1)); + sprintf(p2, "%s", LookupCharName(tr, c1, true)); if (p2[0] != 0) { ix += strlen(p2); announced_punctuation = c1; From c5fac3185cb0553e213bec91d2dc340218b02c78 Mon Sep 17 00:00:00 2001 From: Juho Hiltunen Date: Sat, 5 Nov 2022 14:50:05 +0200 Subject: [PATCH 5/5] cleanup: join two adjacent if (only == false) blocks. Note: The latter part is untested code. --- src/libespeak-ng/readclause.c | 36 ++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/libespeak-ng/readclause.c b/src/libespeak-ng/readclause.c index c35c6edc..47c63a4d 100644 --- a/src/libespeak-ng/readclause.c +++ b/src/libespeak-ng/readclause.c @@ -222,10 +222,12 @@ static const char *LookupCharName(Translator *tr, int c, bool only) ix = utf8_out(c, &single_letter[2]); single_letter[2+ix] = 0; - if (only) { + if (only == true) { string = &single_letter[2]; LookupDictList(tr, &string, phonemes, flags, 0, NULL); - } else { + } + + if (only == false) { string = &single_letter[1]; if (LookupDictList(tr, &string, phonemes, flags, 0, NULL) == 0) { // try _* then * @@ -236,21 +238,21 @@ static const char *LookupCharName(Translator *tr, int c, bool only) TranslateRules(tr, &single_letter[2], phonemes, sizeof(phonemes), NULL, 0, NULL); } } - } - - if ((only == false) && ((phonemes[0] == 0) || (phonemes[0] == phonSWITCH)) && (tr->translator_name != L('e', 'n'))) { - // not found, try English - SetTranslator2(ESPEAKNG_DEFAULT_VOICE); - string = &single_letter[1]; - single_letter[1] = '_'; - if (LookupDictList(translator2, &string, phonemes, flags, 0, NULL) == 0) { - string = &single_letter[2]; - LookupDictList(translator2, &string, phonemes, flags, 0, NULL); - } - if (phonemes[0]) - lang_name = ESPEAKNG_DEFAULT_VOICE; - else - SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table + + if (((phonemes[0] == 0) || (phonemes[0] == phonSWITCH)) && (tr->translator_name != L('e', 'n'))) { + // not found, try English + SetTranslator2(ESPEAKNG_DEFAULT_VOICE); + string = &single_letter[1]; + single_letter[1] = '_'; + if (LookupDictList(translator2, &string, phonemes, flags, 0, NULL) == 0) { + string = &single_letter[2]; + LookupDictList(translator2, &string, phonemes, flags, 0, NULL); + } + if (phonemes[0]) + lang_name = ESPEAKNG_DEFAULT_VOICE; + else + SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table + } } if (phonemes[0]) {