From 59177db8eff17a32d0c24fe38559eab38e04e23a Mon Sep 17 00:00:00 2001 From: Juho Hiltunen Date: Sun, 23 Oct 2022 20:02:34 +0300 Subject: [PATCH 1/7] cleanup: fix code smell knownConditionTrueFalse Inner 'p->stresslevel>=4' will always be true. Thus checking for count_stressed is redundant. --- src/libespeak-ng/intonation.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/libespeak-ng/intonation.c b/src/libespeak-ng/intonation.c index c1664218..65e4f2d4 100644 --- a/src/libespeak-ng/intonation.c +++ b/src/libespeak-ng/intonation.c @@ -790,13 +790,8 @@ static void CalcPitches_Tone(Translator *tr) p = &phoneme_list[0]; for (ix = 0; ix < n_phoneme_list; ix++, p++) { if ((p->type == phVOWEL) && (p->stresslevel >= 4)) { - if (count_stressed == 0) - final_stressed = ix; - - if (p->stresslevel >= 4) { - final_stressed = ix; - count_stressed++; - } + final_stressed = ix; + count_stressed++; } } From 07da7edeea75ce90396afddae093f63060d3fd2d Mon Sep 17 00:00:00 2001 From: Juho Hiltunen Date: Sun, 23 Oct 2022 21:22:36 +0300 Subject: [PATCH 2/7] cleanup: fix code smell knownConditionTrueFalse Inner 'n_bytes >= 0' will always be true. --- src/libespeak-ng/dictionary.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libespeak-ng/dictionary.c b/src/libespeak-ng/dictionary.c index 8624e350..d506f512 100644 --- a/src/libespeak-ng/dictionary.c +++ b/src/libespeak-ng/dictionary.c @@ -1650,8 +1650,8 @@ static void MatchRule(Translator *tr, char *word[], char *word_start, int group_ letter_group = LetterGroupNo(rule++); if ((n_bytes = IsLetterGroup(tr, post_ptr-1, letter_group, 0)) >= 0) { add_points = (20-distance_right); - if (n_bytes >= 0) // move pointer, if group was found - post_ptr += (n_bytes-1); + // move pointer, if group was found + post_ptr += (n_bytes-1); } else failed = 1; break; @@ -1869,8 +1869,8 @@ static void MatchRule(Translator *tr, char *word[], char *word_start, int group_ letter_group = LetterGroupNo(rule++); if ((n_bytes = IsLetterGroup(tr, pre_ptr, letter_group, 1)) >= 0) { add_points = (20-distance_right); - if (n_bytes >= 0) // move pointer, if group was found - pre_ptr -= (n_bytes-1); + // move pointer, if group was found + pre_ptr -= (n_bytes-1); } else failed = 1; break; From 78a037924e45fbb210f079159221bda83ed104a0 Mon Sep 17 00:00:00 2001 From: Juho Hiltunen Date: Sun, 23 Oct 2022 21:42:40 +0300 Subject: [PATCH 3/7] cleanup: fix code smell knownConditionTrueFalse for strlen() >= 0 Strlen never retuns a negative value. Also combined if statements to reduce complexity. This code currently has no tests. --- src/libespeak-ng/numbers.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/libespeak-ng/numbers.c b/src/libespeak-ng/numbers.c index 6f03b0bd..756afdce 100644 --- a/src/libespeak-ng/numbers.c +++ b/src/libespeak-ng/numbers.c @@ -1202,15 +1202,14 @@ static int LookupNum2(Translator *tr, int value, int thousandplex, const int con sprintf(ph_out, "%s%s%s%s", ph_tens, ph_and, ph_digits, ph_ordinal); used_and = 1; } else { - if (tr->langopts.numbers & NUM_SINGLE_VOWEL) { + if ((tr->langopts.numbers & NUM_SINGLE_VOWEL) && ph_digits[0] != 0) { // remove vowel from the end of tens if units starts with a vowel (LANG=Italian) - if (((ix = strlen(ph_tens)-1) >= 0) && (ph_digits[0] != 0)) { - if ((next_phtype = phoneme_tab[(unsigned int)(ph_digits[0])]->type) == phSTRESS) - next_phtype = phoneme_tab[(unsigned int)(ph_digits[1])]->type; + ix = strlen(ph_tens) - 1; + if ((next_phtype = phoneme_tab[(unsigned int)(ph_digits[0])]->type) == phSTRESS) + next_phtype = phoneme_tab[(unsigned int)(ph_digits[1])]->type; - if ((phoneme_tab[(unsigned int)(ph_tens[ix])]->type == phVOWEL) && (next_phtype == phVOWEL)) - ph_tens[ix] = 0; - } + if ((phoneme_tab[(unsigned int)(ph_tens[ix])]->type == phVOWEL) && (next_phtype == phVOWEL)) + ph_tens[ix] = 0; } if ((tr->langopts.numbers2 & NUM2_ORDINAL_DROP_VOWEL) && (ph_ordinal[0] != 0)) { From 9900d55ccf4d658716b7df9d1d2659e7650df21d Mon Sep 17 00:00:00 2001 From: Juho Hiltunen Date: Tue, 25 Oct 2022 20:02:34 +0300 Subject: [PATCH 4/7] cleanup: reduce variable scopes in setlengths.c --- src/libespeak-ng/setlengths.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/libespeak-ng/setlengths.c b/src/libespeak-ng/setlengths.c index 989278cc..327c06b7 100644 --- a/src/libespeak-ng/setlengths.c +++ b/src/libespeak-ng/setlengths.c @@ -156,10 +156,7 @@ void SetSpeed(int control) wpm = embedded_value[EMBED_S2]; #if HAVE_SONIC_H - int wpm2; int wpm_value; - double sonic; - speed.min_pause = 5; wpm_value = wpm; @@ -170,6 +167,7 @@ void SetSpeed(int control) if (control & 2) DoSonicSpeed(1 * 1024); if ((wpm_value >= espeakRATE_MAXIMUM) || ((wpm_value > speed.fast_settings) && (wpm > 350))) { + int wpm2; wpm2 = wpm; wpm = espeakRATE_NORMAL; @@ -182,6 +180,7 @@ void SetSpeed(int control) speed3 = (x * voice->speedf3)/256; } if (control & 2) { + double sonic; sonic = ((double)wpm2)/wpm; DoSonicSpeed((int)(sonic * 1024)); speed.pause_factor = 85; @@ -297,11 +296,11 @@ espeak_ng_STATUS SetParameter(int parameter, int value, int relative) // relative 0=absolute 1=relative int new_value = value; - int default_value; extern const int param_defaults[N_SPEECH_PARAM]; if (relative) { if (parameter < 5) { + int default_value; default_value = param_defaults[parameter]; new_value = default_value + (default_value * value)/100; } @@ -374,14 +373,13 @@ void CalcLengths(Translator *tr) PHONEME_LIST *p; PHONEME_LIST *p2; - int stress; - int type; static int more_syllables = 0; bool pre_sonorant = false; bool pre_voiced = false; int last_pitch = 0; int pitch_start; int length_mod; + int next2type; int len; int env2; @@ -389,12 +387,16 @@ void CalcLengths(Translator *tr) int embedded_ix = 0; int min_drop; int pitch1; - int emphasized; + int tone_mod; unsigned char *pitch_env = NULL; PHONEME_DATA phdata_tone; + for (ix = 1; ix < n_phoneme_list; ix++) { + int stress; + int emphasized; + prev = &phoneme_list[ix-1]; p = &phoneme_list[ix]; stress = p->stresslevel & 0x7; @@ -405,6 +407,7 @@ void CalcLengths(Translator *tr) if (p->synthflags & SFLAG_EMBEDDED) DoEmbedded2(&embedded_ix); + int type; type = p->type; if (p->synthflags & SFLAG_SYLLABLE) type = phVOWEL; From 8ddcf6ed94e5c32e319eedaa17563dcba7a74586 Mon Sep 17 00:00:00 2001 From: Juho Hiltunen Date: Wed, 26 Oct 2022 17:12:26 +0300 Subject: [PATCH 5/7] cleanup: reduce variable scopes in voices.c --- src/libespeak-ng/voices.c | 42 ++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/src/libespeak-ng/voices.c b/src/libespeak-ng/voices.c index 60d8f23a..2af085a0 100644 --- a/src/libespeak-ng/voices.c +++ b/src/libespeak-ng/voices.c @@ -112,9 +112,7 @@ static void SetToneAdjust(voice_t *voice, int *tone_pts) int pt; int y; int freq1 = 0; - int freq2; int height1 = tone_pts[1]; - int height2; for (pt = 0; pt < 12; pt += 2) { if (tone_pts[pt] == -1) { @@ -122,6 +120,9 @@ static void SetToneAdjust(voice_t *voice, int *tone_pts) if (pt > 0) tone_pts[pt+1] = tone_pts[pt-1]; } + int freq2; + int height2; + freq2 = tone_pts[pt] / 8; // 8Hz steps height2 = tone_pts[pt+1]; if ((freq2 - freq1) > 0) { @@ -378,9 +379,9 @@ void ReadNumbers(char *p, int *flags, int maxValue, const MNEM_TAB *keyword_tab // store them as flags in *flags // the meaning of the numbers is bit ordinals, not integer values // give an error if number > maxValue is read - int n; while (*p != 0) { while (isspace(*p)) p++; + int n; if ((n = atoi(p)) > 0) { p++; if (n < maxValue) { @@ -420,9 +421,6 @@ voice_t *LoadVoice(const char *vname, int control) char phonemes_name[40] = ""; const char *language_type; char buf[sizeof(path_home)+30]; - char path_voices[sizeof(path_home)+12]; - - char name1[40]; char name2[80]; @@ -448,6 +446,7 @@ voice_t *LoadVoice(const char *vname, int control) if (voicename[0] == 0 && !(control & 8)/*compiling phonemes*/) strcpy(voicename, ESPEAKNG_DEFAULT_VOICE); + char path_voices[sizeof(path_home)+12]; sprintf(path_voices, "%s%cvoices%c", path_home, PATHSEP, PATHSEP); sprintf(buf, "%s%s", path_voices, voicename); // look in the main voices directory @@ -729,7 +728,6 @@ static char *ExtractVoiceVariantName(char *vname, int variant_num, int add_dir) // Remove any voice variant suffix (name or number) from a voice name // Returns the voice variant name - char *p; static char variant_name[40]; char variant_prefix[5]; @@ -740,6 +738,7 @@ static char *ExtractVoiceVariantName(char *vname, int variant_num, int add_dir) variant_prefix[0] = 0; if (vname != NULL) { + char *p; if ((p = strchr(vname, '+')) != NULL) { // The voice name has a +variant suffix variant_num = 0; @@ -809,18 +808,9 @@ static int __cdecl VoiceScoreSorter(const void *p1, const void *p2) static int ScoreVoice(espeak_VOICE *voice_spec, const char *spec_language, int spec_n_parts, int spec_lang_len, espeak_VOICE *voice) { - int ix; const char *p; - int c1, c2; - int language_priority; - int n_parts; - int matching; - int matching_parts; int score = 0; int x; - int ratio; - int required_age; - int diff; p = voice->languages; // list of languages+dialects for which this voice is suitable @@ -841,13 +831,15 @@ static int ScoreVoice(espeak_VOICE *voice_spec, const char *spec_language, int s // compare the required language with each of the languages of this voice while (*p != 0) { - language_priority = *p++; + int language_priority = *p++; - matching = 1; - matching_parts = 0; - n_parts = 1; + int n_parts = 1; + int matching = 1; + int matching_parts = 0; + int ix; for (ix = 0;; ix++) { + int c1, c2; if ((ix >= spec_lang_len) || ((c1 = spec_language[ix]) == '-')) c1 = 0; if ((c2 = p[ix]) == '-') @@ -872,6 +864,7 @@ static int ScoreVoice(espeak_VOICE *voice_spec, const char *spec_language, int s x = 5; // reduce the score if not all parts of the required language match + int diff; if ((diff = (spec_n_parts - matching_parts)) > 0) x -= diff; @@ -908,11 +901,13 @@ static int ScoreVoice(espeak_VOICE *voice_spec, const char *spec_language, int s score += 5; // give some preference for non-child female voice if a child is requested if (voice->age != 0) { + int required_age; if (voice_spec->age == 0) required_age = 30; else required_age = voice_spec->age; + int ratio; ratio = (required_age*100)/voice->age; if (ratio < 100) ratio = 10000/ratio; @@ -940,7 +935,6 @@ static int SetVoiceScores(espeak_VOICE *voice_select, espeak_VOICE **voices, int int lang_len = 0; espeak_VOICE *vp; char language[80]; - char buf[sizeof(path_home)+80]; // count number of parts in the specified language if ((voice_select->languages != NULL) && (voice_select->languages[0] != 0)) { @@ -958,6 +952,7 @@ static int SetVoiceScores(espeak_VOICE *voice_select, espeak_VOICE **voices, int lang_len = 2; } + char buf[sizeof(path_home)+80]; sprintf(buf, "%s/voices/%s", path_home, language); if (GetFileLength(buf) == -EISDIR) { // A subdirectory name has been specified. List all the voices in that subdirectory @@ -1393,8 +1388,6 @@ ESPEAK_API const espeak_VOICE **espeak_ListVoices(espeak_VOICE *voice_spec) { char path_voices[sizeof(path_home)+12]; - int ix; - int j; espeak_VOICE *v; static espeak_VOICE **voices = NULL; @@ -1422,6 +1415,9 @@ ESPEAK_API const espeak_VOICE **espeak_ListVoices(espeak_VOICE *voice_spec) SetVoiceScores(voice_spec, voices, 1); } else { // list all: omit variant and mbrola voices + int ix; + int j; + j = 0; for (ix = 0; (v = voices_list[ix]) != NULL; ix++) { if ((v->languages[0] != 0) && (strcmp(&v->languages[1], "variant") != 0) From 14f12376a3f85e46c3a62308142421aff685bd68 Mon Sep 17 00:00:00 2001 From: Juho Hiltunen Date: Wed, 26 Oct 2022 17:17:13 +0300 Subject: [PATCH 6/7] cleanup: change int skip to bool --- src/libespeak-ng/voices.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/libespeak-ng/voices.c b/src/libespeak-ng/voices.c index 2af085a0..3cbae992 100644 --- a/src/libespeak-ng/voices.c +++ b/src/libespeak-ng/voices.c @@ -1054,7 +1054,6 @@ char const *SelectVoice(espeak_VOICE *voice_select, int *found) int n_variants; int variant_number; int gender; - int skip; int aged = 1; char *variant_name; const char *p, *p_start; @@ -1133,14 +1132,14 @@ char const *SelectVoice(espeak_VOICE *voice_select, int *found) for (ix = 0, ix2 = 0; ix < nv; ix++) { vp = voices[ix]; // is the main voice the required gender? - skip = 0; + bool skip = false; if ((gender != ENGENDER_UNKNOWN) && (vp->gender != gender)) - skip = 1; + skip = true; if ((ix2 == 0) && aged && (vp->age < AGE_OLD)) - skip = 1; + skip = true; - if (skip == 0) + if (skip == false) voices2[ix2++] = vp; for (j = 0; (j < vp->xx1) && (n_variants < N_VOICE_VARIANTS);) { From 507a5f5410d561d738b09a273bfcdb9823c5cfb8 Mon Sep 17 00:00:00 2001 From: Juho Hiltunen Date: Wed, 26 Oct 2022 17:42:00 +0300 Subject: [PATCH 7/7] cleanup: fix unreadVariable code smell for variant_name in SelectVoiceByName() --- src/libespeak-ng/voices.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/libespeak-ng/voices.c b/src/libespeak-ng/voices.c index 3cbae992..9665e6e0 100644 --- a/src/libespeak-ng/voices.c +++ b/src/libespeak-ng/voices.c @@ -998,7 +998,6 @@ espeak_VOICE *SelectVoiceByName(espeak_VOICE **voices, const char *name2) int match_fname2 = -1; int match_name = -1; const char *id; // this is the filename within espeak-ng-data/voices - char *variant_name; int last_part_len; char last_part[41]; char name[40]; @@ -1010,10 +1009,6 @@ espeak_VOICE *SelectVoiceByName(espeak_VOICE **voices, const char *name2) } strncpy0(name, name2, sizeof(name)); - if ((variant_name = strchr(name, '+')) != NULL) { - *variant_name = 0; - variant_name++; - } sprintf(last_part, "%c%s", PATHSEP, name); last_part_len = strlen(last_part);