void ArgParse::validateValue(const ArgParse::ValueType type, const std::string name, const std::string choices, const std::string value) const { switch ( type ) { case INT : validateInt(name, choices, value); break; case FLOAT : validateFloat(name, choices, value); break; case BOOL : validateBool(name, choices, value); break; case STRING : validateString(name, choices, value); break; default: break; } }
int main (int argc, char *argv[]) { ProgramExitStatus exitStatus; const char *driver = NULL; void *object; float speechRate; float speechVolume; { static const OptionsDescriptor descriptor = { OPTION_TABLE(programOptions), .applicationName = "spktest", .argumentsSummary = "[driver [parameter=value ...]]" }; PROCESS_OPTIONS(descriptor, argc, argv); } { char **const paths[] = { &opt_driversDirectory, &opt_dataDirectory, NULL }; fixInstallPaths(paths); } speechRate = 1.0; if (opt_speechRate && *opt_speechRate) { static const float minimum = 0.1; static const float maximum = 10.0; if (!validateFloat(&speechRate, opt_speechRate, &minimum, &maximum)) { logMessage(LOG_ERR, "%s: %s", "invalid rate multiplier", opt_speechRate); return PROG_EXIT_SYNTAX; } } speechVolume = 1.0; if (opt_speechVolume && *opt_speechVolume) { static const float minimum = 0.0; static const float maximum = 2.0; if (!validateFloat(&speechVolume, opt_speechVolume, &minimum, &maximum)) { logMessage(LOG_ERR, "%s: %s", "invalid volume multiplier", opt_speechVolume); return PROG_EXIT_SYNTAX; } } if (argc) { driver = *argv++; --argc; } if ((speech = loadSpeechDriver(driver, &object, opt_driversDirectory))) { const char *const *parameterNames = speech->parameters; char **parameterSettings; if (!parameterNames) { static const char *const noNames[] = {NULL}; parameterNames = noNames; } { const char *const *name = parameterNames; unsigned int count; char **setting; while (*name) ++name; count = name - parameterNames; if (!(parameterSettings = malloc((count + 1) * sizeof(*parameterSettings)))) { logMallocError(); return PROG_EXIT_FATAL; } setting = parameterSettings; while (count--) *setting++ = ""; *setting = NULL; } while (argc) { char *assignment = *argv++; int ok = 0; char *delimiter = strchr(assignment, '='); if (!delimiter) { logMessage(LOG_ERR, "missing speech driver parameter value: %s", assignment); } else if (delimiter == assignment) { logMessage(LOG_ERR, "missing speech driver parameter name: %s", assignment); } else { size_t nameLength = delimiter - assignment; const char *const *name = parameterNames; while (*name) { if (strncasecmp(assignment, *name, nameLength) == 0) { parameterSettings[name - parameterNames] = delimiter + 1; ok = 1; break; } ++name; } if (!ok) logMessage(LOG_ERR, "invalid speech driver parameter: %s", assignment); } if (!ok) return PROG_EXIT_SYNTAX; --argc; } initializeSpeechSynthesizer(&spk); identifySpeechDriver(speech, 0); /* start-up messages */ if (speech->construct(&spk, parameterSettings)) { if (speech->setVolume) speech->setVolume(&spk, speechVolume); if (speech->setRate) speech->setRate(&spk, speechRate); if (opt_textString && *opt_textString) { sayString(&spk, opt_textString, 0); } else { processLines(stdin, sayLine, NULL); } speech->destruct(&spk); /* finish with the display */ exitStatus = PROG_EXIT_SUCCESS; } else { logMessage(LOG_ERR, "can't initialize speech driver."); exitStatus = PROG_EXIT_FATAL; } } else { logMessage(LOG_ERR, "can't load speech driver."); exitStatus = PROG_EXIT_FATAL; } return exitStatus; }
static int spk_construct (volatile SpeechSynthesizer *spk, char **parameters) { theta_voice_search criteria; memset(&criteria, 0, sizeof(criteria)); initializeTheta(); spk->setVolume = spk_setVolume; spk->setRate = spk_setRate; if (*parameters[PARM_GENDER]) { const char *const choices[] = {"male", "female", "neuter", NULL}; unsigned int choice; if (validateChoice(&choice, parameters[PARM_GENDER], choices)) { criteria.gender = (char *)choices[choice]; } else { logMessage(LOG_WARNING, "%s: %s", "invalid gender specification", parameters[PARM_GENDER]); } } if (*parameters[PARM_LANGUAGE]) criteria.lang = parameters[PARM_LANGUAGE]; if (*parameters[PARM_AGE]) { const char *word = parameters[PARM_AGE]; int value; int younger; static const int minimumAge = 1; static const int maximumAge = 99; if ((younger = word[0] == '-') && word[1]) ++word; if (validateInteger(&value, word, &minimumAge, &maximumAge)) { if (younger) value = -value; criteria.age = value; } else { logMessage(LOG_WARNING, "%s: %s", "invalid age specification", word); } } { const char *name = parameters[PARM_NAME]; if (name && (*name == '/')) { theta_voice_desc *descriptor = theta_try_voxdir(name, &criteria); if (descriptor) { loadVoice(descriptor); theta_free_voice_desc(descriptor); } } else { theta_voice_desc *descriptors = theta_enum_voices(theta_voxpath, &criteria); if (descriptors) { theta_voice_desc *descriptor; for (descriptor=descriptors; descriptor; descriptor=descriptor->next) { if (*name) if (strcasecmp(name, descriptor->human) != 0) continue; loadVoice(descriptor); if (voice) break; } theta_free_voicelist(descriptors); } } } if (voice) { { float pitch = 0.0; static const float minimumPitch = -2.0; static const float maximumPitch = 2.0; if (validateFloat(&pitch, parameters[PARM_PITCH], &minimumPitch, &maximumPitch)) { theta_set_pitch_shift(voice, pitch, NULL); } else { logMessage(LOG_WARNING, "%s: %s", "invalid pitch shift specification", parameters[PARM_PITCH]); } } logMessage(LOG_INFO, "Theta Engine: version %s", theta_version); return 1; } logMessage(LOG_WARNING, "No voices found."); return 0; }