PageRenderTime 115ms CodeModel.GetById 10ms app.highlight 93ms RepoModel.GetById 1ms app.codeStats 0ms

/native/external/espeak/src/voices.cpp

http://eyes-free.googlecode.com/
C++ | 1707 lines | 1297 code | 309 blank | 101 comment | 321 complexity | 8f206aaa6a79bccd37babfabb96657b6 MD5 | raw file
   1/***************************************************************************
   2 *   Copyright (C) 2005 to 2007 by Jonathan Duddington                     *
   3 *   email: jonsd@users.sourceforge.net                                    *
   4 *                                                                         *
   5 *   This program is free software; you can redistribute it and/or modify  *
   6 *   it under the terms of the GNU General Public License as published by  *
   7 *   the Free Software Foundation; either version 3 of the License, or     *
   8 *   (at your option) any later version.                                   *
   9 *                                                                         *
  10 *   This program is distributed in the hope that it will be useful,       *
  11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  13 *   GNU General Public License for more details.                          *
  14 *                                                                         *
  15 *   You should have received a copy of the GNU General Public License     *
  16 *   along with this program; if not, write see:                           *
  17 *               <http://www.gnu.org/licenses/>.                           *
  18 ***************************************************************************/
  19
  20#include "StdAfx.h"
  21
  22#include "stdio.h"
  23#include "ctype.h"
  24#include "wctype.h"
  25#include "string.h"
  26#include "stdlib.h"
  27#include "speech.h"
  28
  29#ifdef PLATFORM_WINDOWS
  30#include "windows.h"
  31#else
  32#ifdef PLATFORM_RISCOS
  33#include "kernel.h"
  34#else
  35#include "dirent.h"
  36#endif
  37#endif
  38
  39#include "speak_lib.h"
  40#include "phoneme.h"
  41#include "synthesize.h"
  42#include "voice.h"
  43#include "translate.h"
  44
  45
  46MNEM_TAB genders [] = {
  47	{"unknown", 0},
  48	{"male", 1},
  49	{"female", 2},
  50	{NULL, 0 }};
  51
  52//int tone_points[10] = {250,140, 1200,110, -1,0, -1,0, -1,0};
  53int tone_points[10] = {600,170, 1200,135, 2000,110, 3000,110, -1,0};
  54
  55// limit the rate of change for each formant number
  56//static int formant_rate_22050[9] = {50, 104, 165, 230, 220, 220, 220, 220, 220};  // values for 22kHz sample rate
  57//static int formant_rate_22050[9] = {240, 180, 180, 180, 180, 180, 180, 180, 180};  // values for 22kHz sample rate
  58static int formant_rate_22050[9] = {240, 170, 170, 170, 170, 170, 170, 170, 170};  // values for 22kHz sample rate
  59int formant_rate[9];         // values adjusted for actual sample rate
  60
  61
  62
  63#define DEFAULT_LANGUAGE_PRIORITY  5
  64#define N_VOICES_LIST  150
  65static int n_voices_list = 0;
  66static espeak_VOICE *voices_list[N_VOICES_LIST];
  67static int len_path_voices;
  68
  69espeak_VOICE voice_selected;
  70
  71
  72
  73#define V_NAME       1
  74#define V_LANGUAGE   2
  75#define V_GENDER     3
  76#define V_TRANSLATOR  4
  77#define V_PHONEMES    5
  78#define V_DICTIONARY  6
  79
  80// these affect voice quality, are independent of language
  81#define V_FORMANT     7
  82#define V_PITCH       8
  83#define V_ECHO        9
  84#define V_FLUTTER    10
  85#define V_ROUGHNESS  11
  86#define V_CLARITY    12
  87#define V_TONE       13
  88#define V_VOICING    14
  89#define V_BREATH     15
  90#define V_BREATHW    16
  91
  92// these override defaults set by the translator
  93#define V_WORDGAP    17
  94#define V_INTONATION 18
  95#define V_STRESSLENGTH  19
  96#define V_STRESSAMP  20
  97#define V_STRESSADD  21
  98#define V_DICTRULES   22
  99#define V_STRESSRULE  23
 100#define V_CHARSET     24
 101#define V_NUMBERS     25
 102#define V_OPTION      26
 103
 104#define V_MBROLA     27
 105
 106// these need a phoneme table to have been specified
 107#define V_REPLACE    28
 108#define V_CONSONANTS 29
 109
 110
 111typedef struct {
 112	const char *mnem;
 113	int data;
 114} keywtab_t;
 115
 116static keywtab_t keyword_tab[] = {
 117	{"name",       V_NAME},
 118	{"language",   V_LANGUAGE},
 119	{"gender",     V_GENDER},
 120
 121	{"formant",    V_FORMANT},
 122	{"pitch",      V_PITCH},
 123	{"phonemes",   V_PHONEMES},
 124   {"translator", V_TRANSLATOR},
 125	{"dictionary", V_DICTIONARY},
 126	{"stressLength", V_STRESSLENGTH},
 127	{"stressAmp",  V_STRESSAMP},
 128	{"stressAdd",  V_STRESSADD},
 129	{"intonation", V_INTONATION},
 130	{"dictrules",	V_DICTRULES},
 131	{"stressrule", V_STRESSRULE},
 132	{"charset",    V_CHARSET},
 133	{"replace",    V_REPLACE},
 134	{"words",      V_WORDGAP},
 135	{"echo",       V_ECHO},
 136	{"flutter",    V_FLUTTER},
 137	{"roughness",  V_ROUGHNESS},
 138	{"clarity",    V_CLARITY},
 139	{"tone",       V_TONE},
 140	{"voicing",    V_VOICING},
 141	{"breath",     V_BREATH},
 142	{"breathw",    V_BREATHW},
 143	{"numbers",    V_NUMBERS},
 144	{"option",     V_OPTION},
 145	{"mbrola",     V_MBROLA},
 146	{"consonants", V_CONSONANTS},
 147
 148	// these just set a value in langopts.param[]
 149	{"l_dieresis", 0x100+LOPT_DIERESES},
 150//	{"l_lengthen", 0x100+LOPT_IT_LENGTHEN},
 151	{"l_prefix",   0x100+LOPT_PREFIXES},
 152	{"l_regressive_v", 0x100+LOPT_REGRESSIVE_VOICING},
 153	{"l_unpronouncable", 0x100+LOPT_UNPRONOUNCABLE},
 154	{"l_sonorant_min", 0x100+LOPT_SONORANT_MIN},
 155	{"l_length_mods", 0x100+LOPT_LENGTH_MODS},
 156	{NULL,   0} };
 157
 158#define N_VOICE_VARIANTS   12
 159const char variants_either[N_VOICE_VARIANTS] = {1,2,12,3,13,4,14,5,11,0};
 160const char variants_male[N_VOICE_VARIANTS] = {1,2,3,4,5,0};
 161const char variants_female[N_VOICE_VARIANTS] = {11,12,13,14,0};
 162const char *variant_lists[3] = {variants_either, variants_male, variants_female};
 163
 164voice_t voicedata;
 165voice_t *voice = &voicedata;
 166
 167char *fgets_strip(char *buf, int size, FILE *f_in)
 168{//===============================================
 169// strip trailing spaces, and truncate lines at // comment
 170	int len;
 171	char *p;
 172
 173	if(fgets(buf,size,f_in) == NULL)
 174		return(NULL);
 175
 176	len = strlen(buf);
 177	while((--len > 0) && isspace(buf[len]))
 178		buf[len] = 0;
 179
 180	if((p = strstr(buf,"//")) != NULL)
 181		*p = 0;
 182
 183	return(buf);
 184}
 185
 186
 187void SetToneAdjust(voice_t *voice, int *tone_pts)
 188{//==============================================
 189	int ix;
 190	int pt;
 191	int y;
 192	int freq1=0;
 193	int freq2;
 194	int height1 = tone_pts[1];
 195	int height2;
 196	double rate;
 197
 198	for(pt=0; pt<10; pt+=2)
 199	{
 200		if(tone_pts[pt] == -1)
 201		{
 202			tone_pts[pt] = N_TONE_ADJUST*8;
 203			if(pt > 0)
 204				tone_pts[pt+1] = tone_pts[pt-1];
 205		}
 206		freq2 = tone_pts[pt] / 8;   // 8Hz steps
 207		height2 = tone_pts[pt+1];
 208		if((freq2 - freq1) > 0)
 209		{
 210			rate = double(height2-height1)/(freq2-freq1);
 211
 212			for(ix=freq1; ix<freq2; ix++)
 213			{
 214				y = height1 + int(rate * (ix-freq1));
 215				if(y > 255)
 216					y = 255;
 217				voice->tone_adjust[ix] = y;
 218			}
 219		}
 220		freq1 = freq2;
 221		height1 = height2;
 222	}
 223}
 224
 225
 226void ReadTonePoints(char *string, int *tone_pts)
 227{//=============================================
 228// tone_pts[] is int[10]
 229	int ix;
 230
 231	for(ix=0; ix<10; ix++)
 232		tone_pts[ix] = -1;
 233
 234	sscanf(string,"%d %d %d %d %d %d %d %d",
 235		&tone_pts[0],&tone_pts[1],&tone_pts[2],&tone_pts[3],
 236		&tone_pts[4],&tone_pts[5],&tone_pts[6],&tone_pts[7]);
 237}
 238
 239
 240
 241
 242static espeak_VOICE *ReadVoiceFile(FILE *f_in, const char *fname, const char*leafname)
 243{//===================================================================================
 244// Read a Voice file, allocate a VOICE_DATA and set data from the
 245// file's  language, gender, name  lines
 246
 247	char linebuf[120];
 248	char vname[80];
 249	char vgender[80];
 250	char vlanguage[80];
 251	char languages[300];  // allow space for several alternate language names and priorities
 252
 253
 254	unsigned int len;
 255	int langix = 0;
 256	int n_languages = 0;
 257	char *p;
 258	espeak_VOICE *voice_data;
 259	int priority;
 260	int age;
 261	int n_variants = 3;    // default, number of variants of this voice before using another voice
 262	int gender;
 263
 264#ifdef PLATFORM_WINDOWS
 265	char fname_buf[sizeof(path_home)+15];
 266	if(memcmp(leafname,"mb-",3) == 0)
 267	{
 268		// check whether the mbrola speech data is present for this voice
 269		memcpy(vname,&leafname[3],3);
 270		vname[3] = 0;
 271		sprintf(fname_buf,"%s/mbrola/%s",path_home,vname);
 272
 273		if(GetFileLength(fname_buf) <= 0)
 274			return(0);
 275	}
 276#endif
 277
 278	vname[0] = 0;
 279	vgender[0] = 0;
 280	age = 0;
 281
 282	while(fgets_strip(linebuf,sizeof(linebuf),f_in) != NULL)
 283	{
 284		if(memcmp(linebuf,"name",4)==0)
 285		{
 286			p = &linebuf[4];
 287			while(isspace(*p)) p++;
 288			strncpy0(vname,p,sizeof(vname));
 289		}
 290		else
 291		if(memcmp(linebuf,"language",8)==0)
 292		{
 293			priority = DEFAULT_LANGUAGE_PRIORITY;
 294			vlanguage[0] = 0;
 295
 296			sscanf(&linebuf[8],"%s %d",vlanguage,&priority);
 297			len = strlen(vlanguage) + 2;
 298			// check for space in languages[]
 299			if(len < (sizeof(languages)-langix-1))
 300			{
 301				languages[langix] = priority;
 302
 303				strcpy(&languages[langix+1],vlanguage);
 304				langix += len;
 305				n_languages++;
 306			}
 307		}
 308		else
 309		if(memcmp(linebuf,"gender",6)==0)
 310		{
 311			sscanf(&linebuf[6],"%s %d",vgender,&age);
 312		}
 313		else
 314		if(memcmp(linebuf,"variants",8)==0)
 315		{
 316			sscanf(&linebuf[8],"%d",&n_variants);
 317		}
 318	}
 319	languages[langix++] = 0;
 320
 321	gender = LookupMnem(genders,vgender);
 322
 323	if(n_languages == 0)
 324	{
 325		return(NULL);    // no language lines in the voice file
 326	}
 327
 328	p = (char *)calloc(sizeof(espeak_VOICE) + langix + strlen(fname) + strlen(vname) + 3, 1);
 329	voice_data = (espeak_VOICE *)p;
 330	p = &p[sizeof(espeak_VOICE)];
 331
 332	memcpy(p,languages,langix);
 333	voice_data->languages = p;
 334
 335	strcpy(&p[langix],fname);
 336	voice_data->identifier = &p[langix];
 337	voice_data->name = &p[langix];
 338
 339	if(vname[0] != 0)
 340	{
 341		langix += strlen(fname)+1;
 342		strcpy(&p[langix],vname);
 343		voice_data->name = &p[langix];
 344	}
 345
 346	voice_data->age = age;
 347	voice_data->gender = gender;
 348	voice_data->variant = 0;
 349	voice_data->xx1 = n_variants;
 350	return(voice_data);
 351}  // end of ReadVoiceFile
 352
 353
 354
 355
 356void VoiceReset(int tone_only)
 357{//===========================
 358// Set voice to the default values
 359
 360	int  pk;
 361	static int breath_widths[N_PEAKS] = {0,200,200,400,400,400,600,600,600};
 362
 363	// default is:  pitch 82,118
 364	voice->pitch_base =   0x49000;    // default, 73 << 12;
 365	voice->pitch_range =  0x0f30;     // default = 0x1000
 366	voice->formant_factor = 256;
 367
 368	voice->echo_delay = 0;
 369	voice->echo_amp = 0;
 370	voice->flutter = 64;
 371	voice->n_harmonic_peaks = 5;
 372	voice->peak_shape = 1;
 373	voice->voicing = 64;
 374	voice->consonant_amp = 100;
 375	voice->consonant_ampv = 100;
 376
 377#ifdef PLATFORM_RISCOS
 378	voice->roughness = 1;
 379#else
 380	voice->roughness = 2;
 381#endif
 382
 383	InitBreath();
 384	for(pk=0; pk<N_PEAKS; pk++)
 385	{
 386		voice->freq[pk] = 256;
 387		voice->height[pk] = 256;
 388		voice->width[pk] = 256;
 389		voice->breath[pk] = 0;
 390		voice->breathw[pk] = breath_widths[pk];  // default breath formant woidths
 391		voice->freqadd[pk] = 0;
 392
 393		// adjust formant smoothing depending on sample rate
 394		formant_rate[pk] = (formant_rate_22050[pk] * 22050)/samplerate;
 395	}
 396	voice->height[2] = 240;  // reduce F2 slightly
 397
 398	// This table provides the opportunity for tone control.
 399	// Adjustment of harmonic amplitudes, steps of 8Hz
 400	// value of 128 means no change
 401//	memset(voice->tone_adjust,128,sizeof(voice->tone_adjust));
 402SetToneAdjust(voice,tone_points);
 403
 404	// default values of speed factors
 405	voice->speedf1 = 256;
 406	voice->speedf2 = 238;
 407	voice->speedf3 = 232;
 408
 409	if(tone_only == 0)
 410	{
 411		n_replace_phonemes = 0;
 412		option_tone1 = 0;
 413		option_quiet = 0;
 414		LoadMbrolaTable(NULL,NULL,0);
 415	}
 416}  // end of VoiceReset
 417
 418
 419static void VoiceFormant(char *p)
 420{//==============================
 421	// Set parameters for a formant
 422	int ix;
 423	int formant;
 424	int freq = 100;
 425	int height = 100;
 426	int width = 100;
 427	int freqadd = 0;
 428
 429	ix = sscanf(p,"%d %d %d %d %d",&formant,&freq,&height,&width,&freqadd);
 430	if(ix < 2)
 431		return;
 432
 433	if((formant < 0) || (formant > 8))
 434		return;
 435
 436	if(freq >= 0)
 437		voice->freq[formant] = int(freq * 2.56001);
 438	if(height >= 0)
 439		voice->height[formant] = int(height * 2.56001);
 440	if(width >= 0)
 441		voice->width[formant] = int(width * 2.56001);
 442	voice->freqadd[formant] = freqadd;
 443}
 444
 445
 446
 447
 448
 449static void PhonemeReplacement(int type, char *p)
 450{//==============================================
 451	int n;
 452	int  phon;
 453	int flags = 0;
 454	char phon_string1[12];
 455	char phon_string2[12];
 456
 457	strcpy(phon_string2,"NULL");
 458	n = sscanf(p,"%d %s %s",&flags,phon_string1,phon_string2);
 459	if((n < 2) || (n_replace_phonemes >= N_REPLACE_PHONEMES))
 460		return;
 461
 462	if((phon = LookupPh(phon_string1)) == 0)
 463		return;  // not recognised
 464
 465	replace_phonemes[n_replace_phonemes].old_ph = phon;
 466	replace_phonemes[n_replace_phonemes].new_ph = LookupPh(phon_string2);
 467	replace_phonemes[n_replace_phonemes++].type = flags;
 468}  //  end of PhonemeReplacement
 469
 470
 471
 472static int Read8Numbers(char *data_in,int *data)
 473{//=============================================
 474// Read 8 integer numbers
 475	return(sscanf(data_in,"%d %d %d %d %d %d %d %d",
 476		&data[0],&data[1],&data[2],&data[3],&data[4],&data[5],&data[6],&data[7]));
 477}
 478
 479
 480voice_t *LoadVoice(const char *vname, int control)
 481{//===============================================
 482// control, bit 0  1= no_default
 483//          bit 1  1 = change tone only, not language
 484//          bit 2  1 = don't report error on LoadDictionary
 485//          bit 4  1 = vname = full path
 486
 487	FILE *f_voice = NULL;
 488	keywtab_t *k;
 489	char *p;
 490	int  key;
 491	int  ix;
 492	int  n;
 493	int  value;
 494	int  error = 0;
 495	int  langix = 0;
 496	int  tone_only = control & 2;
 497	int  language_set = 0;
 498	int  phonemes_set = 0;
 499	int  stress_amps_set = 0;
 500	int  stress_lengths_set = 0;
 501	int  stress_add_set = 0;
 502	int  conditional_rules = 0;
 503	LANGUAGE_OPTIONS *langopts = NULL;
 504
 505	Translator *new_translator = NULL;
 506
 507	char voicename[40];
 508	char language_name[40];
 509	char translator_name[40];
 510	char new_dictionary[40];
 511	char phonemes_name[40];
 512	const char *language_type;
 513	char buf[200];
 514	char path_voices[sizeof(path_home)+12];
 515	char langname[4];
 516
 517	int stress_amps[8];
 518	int stress_lengths[8];
 519	int stress_add[8];
 520
 521	int pitch1;
 522	int pitch2;
 523
 524	static char voice_identifier[40];  // file name for  voice_selected
 525	static char voice_name[40];        // voice name for voice_selected
 526	static char voice_languages[100];  // list of languages and priorities for voice_selected
 527
 528	strcpy(voicename,vname);
 529	if(voicename[0]==0)
 530		strcpy(voicename,"default");
 531
 532	if(control & 0x10)
 533	{
 534		strcpy(buf,vname);
 535		if(GetFileLength(buf) <= 0)
 536			return(NULL);
 537	}
 538	else
 539	{
 540		sprintf(path_voices,"%s%cvoices%c",path_home,PATHSEP,PATHSEP);
 541		sprintf(buf,"%s%s",path_voices,voicename);
 542
 543		if(GetFileLength(buf) <= 0)
 544		{
 545			// look for the voice in a sub-directory of the language name
 546			langname[0] = voicename[0];
 547			langname[1] = voicename[1];
 548			langname[2] = 0;
 549			sprintf(buf,"%s%s%c%s",path_voices,langname,PATHSEP,voicename);
 550		}
 551	}
 552
 553	f_voice = fopen(buf,"r");
 554
 555	language_type = "en";    // default
 556	if(f_voice == NULL)
 557	{
 558		if(control & 3)
 559			return(NULL);  // can't open file
 560
 561		if(SelectPhonemeTableName(voicename) >= 0)
 562			language_type = voicename;
 563	}
 564
 565	if(!tone_only && (translator != NULL))
 566	{
 567		delete translator;
 568		translator = NULL;
 569	}
 570
 571	strcpy(translator_name,language_type);
 572	strcpy(new_dictionary,language_type);
 573	strcpy(phonemes_name,language_type);
 574
 575
 576	if(!tone_only)
 577	{
 578		voice = &voicedata;
 579		strncpy0(voice_identifier,vname,sizeof(voice_identifier));
 580		voice_name[0] = 0;
 581		voice_languages[0] = 0;
 582
 583		voice_selected.identifier = voice_identifier;
 584		voice_selected.name = voice_name;
 585		voice_selected.languages = voice_languages;
 586	}
 587	else
 588	{
 589		// append the variant file name to the voice identifier
 590		if((p = strchr(voice_identifier,'+')) != NULL)
 591			*p = 0;    // remove previous variant name
 592		sprintf(buf,"+%s",&vname[3]);    // omit  !v/  from the variant filename
 593		strcat(voice_identifier,buf);
 594		langopts = &translator->langopts;
 595	}
 596	VoiceReset(tone_only);
 597
 598	if(!tone_only)
 599		SelectPhonemeTableName(phonemes_name);  // set up phoneme_tab
 600
 601
 602	while((f_voice != NULL) && (fgets_strip(buf,sizeof(buf),f_voice) != NULL))
 603	{
 604		// isolate the attribute name
 605		for(p=buf; (*p != 0) && !isspace(*p); p++);
 606		*p++ = 0;
 607
 608		if(buf[0] == 0) continue;
 609
 610		key = 0;
 611		for(k=keyword_tab; k->mnem != NULL; k++)
 612		{
 613			if(strcmp(buf,k->mnem)==0)
 614			{
 615				key = k->data;
 616				break;
 617			}
 618		}
 619
 620		switch(key)
 621		{
 622		case V_LANGUAGE:
 623			{
 624				unsigned int len;
 625				int priority;
 626
 627				if(tone_only)
 628					break;
 629	
 630				priority = DEFAULT_LANGUAGE_PRIORITY;
 631				language_name[0] = 0;
 632	
 633				sscanf(p,"%s %d",language_name,&priority);
 634				if(strcmp(language_name,"variant") == 0)
 635					break;
 636	
 637				len = strlen(language_name) + 2;
 638				// check for space in languages[]
 639				if(len < (sizeof(voice_languages)-langix-1))
 640				{
 641					voice_languages[langix] = priority;
 642	
 643					strcpy(&voice_languages[langix+1],language_name);
 644					langix += len;
 645				}
 646	
 647				// only act on the first language line
 648				if(language_set == 0)
 649				{
 650					language_type = strtok(language_name,"-");
 651					language_set = 1;
 652					strcpy(translator_name,language_type);
 653					strcpy(new_dictionary,language_type);
 654					strcpy(phonemes_name,language_type);
 655					SelectPhonemeTableName(phonemes_name);
 656		
 657					if(new_translator != NULL)
 658							delete new_translator;
 659		
 660					new_translator = SelectTranslator(translator_name);
 661					langopts = &new_translator->langopts;
 662				}
 663			}
 664			break;
 665
 666		case V_NAME:
 667			if(tone_only == 0)
 668			{
 669				while(isspace(*p)) p++;
 670				strncpy0(voice_name,p,sizeof(voice_name));
 671			}
 672			break;
 673
 674		case V_GENDER:
 675			{
 676				int age;
 677				char vgender[80];
 678				sscanf(p,"%s %d",vgender,&age);
 679				voice_selected.gender = LookupMnem(genders,vgender);
 680				voice_selected.age = age;
 681			}
 682			break;
 683
 684		case V_TRANSLATOR:
 685			if(tone_only) break;
 686
 687			sscanf(p,"%s",translator_name);
 688
 689			if(new_translator != NULL)
 690					delete new_translator;
 691
 692			new_translator = SelectTranslator(translator_name);
 693			langopts = &new_translator->langopts;
 694			break;
 695
 696		case V_DICTIONARY:        // dictionary
 697			sscanf(p,"%s",new_dictionary);
 698			break;
 699
 700		case V_PHONEMES:        // phoneme table
 701			sscanf(p,"%s",phonemes_name);
 702			break;
 703
 704		case V_FORMANT:
 705			VoiceFormant(p);
 706			break;
 707
 708		case V_PITCH:
 709			{
 710				double factor;
 711				// default is  pitch 82 118
 712				n = sscanf(p,"%d %d",&pitch1,&pitch2);
 713				voice->pitch_base = (pitch1 - 9) << 12;
 714				voice->pitch_range = (pitch2 - pitch1) * 108;
 715				factor = double(pitch1 - 82)/82;
 716				voice->formant_factor = (int)((1+factor/4) * 256);  // nominal formant shift for a different voice pitch
 717			}
 718			break;
 719
 720		case V_STRESSLENGTH:   // stressLength
 721			stress_lengths_set = Read8Numbers(p,stress_lengths);
 722			break;
 723
 724		case V_STRESSAMP:   // stressAmp
 725			stress_amps_set = Read8Numbers(p,stress_amps);
 726			break;
 727
 728		case V_STRESSADD:   // stressAdd
 729			stress_add_set = Read8Numbers(p,stress_add);
 730			break;
 731
 732		case V_INTONATION:   // intonation
 733			sscanf(p,"%d %d",&option_tone_flags,&option_tone2);
 734			if((option_tone_flags & 0xff) != 0)
 735				langopts->intonation_group = option_tone_flags & 0xff;
 736			break;
 737
 738		case V_DICTRULES:   // conditional dictionary rules and list entries
 739			while(*p != 0)
 740			{
 741				while(isspace(*p)) p++;
 742				n = -1;
 743				if(((n = atoi(p)) > 0) && (n < 32))
 744				{
 745					p++;
 746					conditional_rules |= (1 << n);
 747				}
 748				while(isalnum(*p)) p++;
 749			}
 750			break;
 751
 752		case V_REPLACE:
 753			if(phonemes_set == 0)
 754			{
 755				// must set up a phoneme table before we can lookup phoneme mnemonics
 756				SelectPhonemeTableName(phonemes_name);
 757				phonemes_set = 1;
 758			}
 759			PhonemeReplacement(key,p);
 760			break;
 761
 762		case V_WORDGAP:   // words
 763			sscanf(p,"%d %d",&langopts->word_gap, &langopts->vowel_pause);
 764			break;
 765
 766		case V_STRESSRULE:
 767			sscanf(p,"%d %d %d %d",&langopts->stress_rule,
 768				&langopts->stress_flags,
 769				&langopts->unstressed_wd1,
 770				&langopts->unstressed_wd2);
 771			break;
 772
 773		case V_CHARSET:
 774			if((sscanf(p,"%d",&value)==1) && (value < N_CHARSETS))
 775				new_translator->charset_a0 = charsets[value];
 776			break;
 777
 778		case V_NUMBERS:
 779			sscanf(p,"%d",&langopts->numbers);
 780			break;
 781
 782		case V_OPTION:
 783			if(sscanf(p,"%d %d",&ix,&value) == 2)
 784			{
 785				if((ix >= 0) && (ix < N_LOPTS))
 786					langopts->param[ix] = value;
 787			}
 788			break;
 789
 790		case V_ECHO:
 791			// echo.  suggest: 135mS  11%
 792			value = 0;
 793			voice->echo_amp = 0;
 794			sscanf(p,"%d %d",&voice->echo_delay,&voice->echo_amp);
 795			break;
 796
 797		case V_FLUTTER:   // flutter
 798			if(sscanf(p,"%d",&value)==1)
 799				voice->flutter = value * 32;
 800			break;
 801
 802		case V_ROUGHNESS:   // roughness
 803			if(sscanf(p,"%d",&value)==1)
 804				voice->roughness = value;
 805			break;
 806
 807		case V_CLARITY:  // formantshape
 808			if(sscanf(p,"%d",&value)==1)
 809			{
 810				if(value > 4)
 811				{
 812					voice->peak_shape = 1;  // squarer formant peaks
 813					value = 4;
 814				}
 815				voice->n_harmonic_peaks = 1+value;
 816			}
 817			break;
 818
 819		case V_TONE:
 820			{
 821				int tone_data[10];
 822				ReadTonePoints(p,tone_data);
 823				SetToneAdjust(voice,tone_data);
 824			}
 825			break;
 826
 827		case V_VOICING:
 828			if(sscanf(p,"%d",&value)==1)
 829				voice->voicing = (value * 64)/100;
 830			break;
 831
 832		case V_BREATH:
 833				voice->breath[0] = Read8Numbers(p,&voice->breath[1]);
 834				for(ix=1; ix<8; ix++)
 835				{
 836					if(ix % 2)
 837						voice->breath[ix] = -voice->breath[ix];
 838				}
 839			break;
 840
 841		case V_BREATHW:
 842				voice->breathw[0] = Read8Numbers(p,&voice->breathw[1]);
 843			break;
 844
 845		case V_CONSONANTS:
 846			value = sscanf(p,"%d %d",&voice->consonant_amp, &voice->consonant_ampv);
 847			break;
 848
 849		case V_MBROLA:
 850			{
 851				int srate = 16000;
 852				char name[40];
 853				char phtrans[40];
 854
 855				phtrans[0] = 0;
 856				sscanf(p,"%s %s %d",name,phtrans,&srate);
 857				LoadMbrolaTable(name,phtrans,srate);
 858			}
 859			break;
 860
 861		default:
 862			if((key & 0xff00) == 0x100)
 863			{
 864				sscanf(p,"%d",&langopts->param[key &0xff]);
 865			}
 866			else
 867			{
 868				fprintf(stderr,"Bad voice attribute: %s\n",buf);
 869			}
 870			break;
 871		}
 872	}
 873	if(f_voice != NULL)
 874		fclose(f_voice);
 875
 876	if((new_translator == NULL) && (!tone_only))
 877	{
 878		// not set by language attribute
 879		new_translator = SelectTranslator(translator_name);
 880	}
 881
 882	for(ix=0; ix<N_PEAKS; ix++)
 883	{
 884		voice->freq2[ix] = voice->freq[ix];
 885		voice->height2[ix] = voice->height[ix];
 886		voice->width2[ix] = voice->width[ix];
 887	}
 888
 889	if(tone_only)
 890	{
 891		new_translator = translator;
 892	}
 893	else
 894	{
 895		if((ix = SelectPhonemeTableName(phonemes_name)) < 0)
 896		{
 897			fprintf(stderr,"Unknown phoneme table: '%s'\n",phonemes_name);
 898		}
 899		voice->phoneme_tab_ix = ix;
 900		error = new_translator->LoadDictionary(new_dictionary, control & 4);
 901		if(dictionary_name[0]==0)
 902			return(NULL);   // no dictionary loaded
 903
 904		new_translator->dict_condition = conditional_rules;
 905
 906		voice_languages[langix] = 0;
 907	}
 908
 909	langopts = &new_translator->langopts;
 910
 911
 912	if((value = langopts->param[LOPT_LENGTH_MODS]) != 0)
 913	{
 914		SetLengthMods(new_translator,value);
 915	}
 916
 917	voice->width[0] = (voice->width[0] * 105)/100;
 918
 919	if(!tone_only)
 920	{
 921		translator = new_translator;
 922	}
 923
 924	// relative lengths of different stress syllables
 925	for(ix=0; ix<stress_lengths_set; ix++)
 926	{
 927		translator->stress_lengths[ix] = stress_lengths[ix];
 928	}
 929	for(ix=0; ix<stress_add_set; ix++)
 930	{
 931		translator->stress_lengths[ix] += stress_add[ix];
 932	}
 933	for(ix=0; ix<stress_amps_set; ix++)
 934	{
 935		translator->stress_amps[ix] = stress_amps[ix];
 936		translator->stress_amps_r[ix] = stress_amps[ix] -1;
 937	}
 938
 939	return(voice);
 940}  //  end of LoadVoice
 941
 942
 943char *ExtractVoiceVariantName(char *vname, int variant_num)
 944{//========================================================
 945// Remove any voice variant suffix (name or number) from a voice name
 946// Returns the voice variant name
 947
 948	char *p;
 949	static char variant_name[20];
 950	char variant_prefix[5];
 951
 952	variant_name[0] = 0;
 953	sprintf(variant_prefix,"!v%c",PATHSEP);
 954
 955	if(vname != NULL)
 956	{
 957		if((p = strchr(vname,'+')) != NULL)
 958		{
 959			// The voice name has a +variant suffix
 960			*p++ = 0;   // delete the suffix from the voice name
 961			if(isdigit(*p))
 962			{
 963				variant_num = atoi(p);  // variant number
 964			}
 965			else
 966			{
 967				// voice variant name, not number
 968				strcpy(variant_name,variant_prefix);
 969				strncpy0(&variant_name[3],p,sizeof(variant_name)-3);
 970			}	
 971		}
 972	}
 973	
 974	if(variant_num > 0)
 975	{
 976		if(variant_num < 10)
 977			sprintf(variant_name,"%sm%d",variant_prefix, variant_num);  // male
 978		else
 979			sprintf(variant_name,"%sf%d",variant_prefix, variant_num-10);  // female
 980	}
 981
 982	return(variant_name);
 983}  //  end of ExtractVoiceVariantName
 984
 985
 986
 987voice_t *LoadVoiceVariant(const char *vname, int variant_num)
 988{//==========================================================
 989// Load a voice file.
 990// Also apply a voice variant if specified by "variant", or by "+number" or "+name" in the "vname"
 991
 992	voice_t *v;
 993	char *variant_name;
 994	char buf[60];
 995
 996	strncpy0(buf,vname,sizeof(buf));
 997	variant_name = ExtractVoiceVariantName(buf,variant_num);
 998
 999	if((v = LoadVoice(buf,0)) == NULL)
1000		return(NULL);
1001
1002	if(variant_name[0] != 0)
1003	{
1004		v = LoadVoice(variant_name,2);
1005	}
1006	return(v);
1007}
1008
1009
1010
1011static int __cdecl VoiceNameSorter(const void *p1, const void *p2)
1012{//=======================================================
1013	int ix;
1014	espeak_VOICE *v1 = *(espeak_VOICE **)p1;
1015	espeak_VOICE *v2 = *(espeak_VOICE **)p2;
1016
1017
1018	if((ix = strcmp(&v1->languages[1],&v2->languages[1])) != 0)  // primary language name
1019		return(ix);
1020	if((ix = v1->languages[0] - v2->languages[0]) != 0)  // priority number
1021		return(ix);
1022	return(strcmp(v1->name,v2->name));
1023}
1024
1025
1026static int __cdecl VoiceScoreSorter(const void *p1, const void *p2)
1027{//========================================================
1028	int ix;
1029	espeak_VOICE *v1 = *(espeak_VOICE **)p1;
1030	espeak_VOICE *v2 = *(espeak_VOICE **)p2;
1031
1032	if((ix = v2->score - v1->score) != 0)
1033		return(ix);
1034	return(strcmp(v1->name,v2->name));
1035}
1036
1037
1038static int ScoreVoice(espeak_VOICE *voice_spec, const char *spec_language, int spec_n_parts, int spec_lang_len, espeak_VOICE *voice)
1039{//=========================================================================================================================
1040	int ix;
1041	const char *p;
1042	int c1, c2;
1043	int language_priority;
1044	int n_parts;
1045	int matching;
1046	int matching_parts;
1047	int score = 0;
1048	int x;
1049	int ratio;
1050	int required_age;
1051	int diff;
1052
1053	p = voice->languages;  // list of languages+dialects for which this voice is suitable
1054
1055	if(strcmp(spec_language,"mbrola")==0)
1056	{
1057		// only list mbrola voices
1058		if(memcmp(voice->identifier,"mb/",3) == 0)
1059			return(100);
1060		return(0);
1061	}
1062
1063	if(spec_n_parts == 0)
1064	{
1065		score = 100;
1066	}
1067	else
1068	{
1069		if((*p == 0) && (strcmp(spec_language,"variants")==0))
1070		{
1071			// match on a voice with no languages if the required language is "variants"
1072			score = 100;
1073		}
1074
1075		// compare the required language with each of the languages of this voice
1076		while(*p != 0)
1077		{
1078			language_priority = *p++;
1079
1080			matching = 1;
1081			matching_parts = 0;
1082			n_parts = 1;
1083
1084			for(ix=0; ; ix++)
1085			{
1086				if((ix >= spec_lang_len) || ((c1 = spec_language[ix]) == '-'))
1087					c1 = 0;
1088				if((c2 = p[ix]) == '-')
1089					c2 = 0;
1090
1091				if(c1 != c2)
1092				{
1093					matching = 0;
1094				}
1095
1096				if(p[ix] == '-')
1097				{
1098					n_parts++;
1099					if(matching)
1100						matching_parts++;
1101				}
1102				if(p[ix] == 0)
1103					break;
1104			}
1105			p += (ix+1);
1106			matching_parts += matching;  // number of parts which match
1107
1108			if(matching_parts == 0)
1109				break;   // no matching parts for this language
1110
1111			x = 5;
1112			// reduce the score if not all parts of the required language match
1113			if((diff = (spec_n_parts - matching_parts)) > 0)
1114				x -= diff;
1115
1116			// reduce score if the language is more specific than required
1117			if((diff = (n_parts - matching_parts)) > 0)
1118				x -= diff;
1119
1120			x = x*100 - (language_priority * 2);
1121
1122			if(x > score)
1123				score = x;
1124		}
1125	}
1126	if(score == 0)
1127		return(0);
1128
1129	if(voice_spec->name != NULL)
1130	{
1131		if(strcmp(voice_spec->name,voice->name)==0)
1132		{
1133			// match on voice name
1134			score += 500;
1135		}
1136		else
1137		if(strcmp(voice_spec->name,voice->identifier)==0)
1138		{
1139			score += 400;
1140		}
1141	}
1142
1143	if(((voice_spec->gender == 1) || (voice_spec->gender == 2)) &&
1144		((voice->gender == 1) || (voice->gender == 2)))
1145	{
1146		if(voice_spec->gender == voice->gender)
1147			score += 50;
1148		else
1149			score -= 50;
1150	}
1151
1152	if((voice_spec->age <= 12) && (voice->gender == 2) && (voice->age > 12))
1153	{
1154		score += 5;  // give some preference for non-child female voice if a child is requested
1155	}
1156
1157	if(voice->age != 0)
1158	{
1159		if(voice_spec->age == 0)
1160			required_age = 30;
1161		else
1162			required_age = voice_spec->age;
1163
1164		ratio = (required_age*100)/voice->age;
1165		if(ratio < 100)
1166			ratio = 10000/ratio;
1167		ratio = (ratio - 100)/10;    // 0=exact match, 10=out by factor of 2
1168		x = 5 - ratio;
1169		if(x > 0) x = 0;
1170
1171		score = score + x;
1172
1173		if(voice_spec->age > 0)
1174			score += 10;    // required age specified, favour voices with a specified age (near it)
1175	}
1176	if(score < 1)
1177		score = 1;
1178	return(score);
1179}  // end of ScoreVoice
1180
1181
1182static int SetVoiceScores(espeak_VOICE *voice_select, espeak_VOICE **voices, int control)
1183{//======================================================================================
1184// control: bit0=1  include mbrola voices
1185	int ix;
1186	int score;
1187	int nv;           // number of candidates
1188	int n_parts=0;
1189	int lang_len=0;
1190	espeak_VOICE *vp;
1191	char language[80];
1192
1193	// count number of parts in the specified language
1194	if((voice_select->languages != NULL) && (voice_select->languages[0] != 0))
1195	{
1196		n_parts = 1;
1197		lang_len = strlen(voice_select->languages);
1198		for(ix=0; (ix<=lang_len) && ((unsigned)ix < sizeof(language)); ix++)
1199		{
1200			if((language[ix] = tolower(voice_select->languages[ix])) == '-')
1201				n_parts++;
1202		}
1203	}
1204	// select those voices which match the specified language
1205	nv = 0;
1206	for(ix=0; ix<n_voices_list; ix++)
1207	{
1208		vp = voices_list[ix];
1209
1210		if(((control & 1) == 0) && (memcmp(vp->identifier,"mb/",3) == 0))
1211			continue;
1212
1213		if((score = ScoreVoice(voice_select, language, n_parts, lang_len, voices_list[ix])) > 0)
1214		{
1215			voices[nv++] = vp;
1216			vp->score = score;
1217		}
1218	}
1219	voices[nv] = NULL;  // list terminator
1220
1221	if(nv==0)
1222		return(0);
1223
1224	// sort the selected voices by their score
1225	qsort(voices,nv,sizeof(espeak_VOICE *),(int (__cdecl *)(const void *,const void *))VoiceScoreSorter);
1226
1227	return(nv);
1228}  // end of SetVoiceScores
1229
1230
1231
1232
1233espeak_VOICE *SelectVoiceByName(espeak_VOICE **voices, const char *name)
1234{//=====================================================================
1235	int ix;
1236	int match_fname = -1;
1237	int match_fname2 = -1;
1238	int match_name = -1;
1239	const char *id;
1240	int last_part_len;
1241	char last_part[41];
1242
1243	if(voices == NULL)
1244	{
1245		if(n_voices_list == 0)
1246			espeak_ListVoices(NULL);   // create the voices list
1247		voices = voices_list;
1248	}
1249
1250	sprintf(last_part,"%c%s",PATHSEP,name);
1251	last_part_len = strlen(last_part);
1252
1253	for(ix=0; voices[ix] != NULL; ix++)
1254	{
1255		if(strcmp(name,voices[ix]->name)==0)
1256		{
1257			match_name = ix;   // found matching voice name
1258			break;
1259		}
1260		else
1261		if(strcmp(name,id = voices[ix]->identifier)==0)
1262		{
1263			match_fname = ix;  // matching identifier, use this if no matching name
1264		}
1265		else
1266		if(strcmp(last_part,&id[strlen(id)-last_part_len])==0)
1267		{
1268			match_fname2 = ix;
1269		}
1270	}
1271
1272	if(match_name < 0)
1273	{
1274		match_name = match_fname;  // no matching name, try matching filename
1275		if(match_name < 0)
1276			match_name = match_fname2;  // try matching just the last part of the filename
1277	}
1278
1279	if(match_name < 0)
1280		return(NULL);
1281
1282	return(voices[match_name]);
1283}  //  end of SelectVoiceByName
1284
1285
1286
1287
1288char const *SelectVoice(espeak_VOICE *voice_select)
1289{//================================================
1290// Returns a path within espeak-voices, with a possible +variant suffix
1291// variant is an output-only parameter
1292	int nv;           // number of candidates
1293	int ix, ix2;
1294	int j;
1295	int n_variants;
1296	int variant_number;
1297	int gender;
1298	int skip;
1299	int aged=1;
1300	char *variant_name;
1301	const char *p, *p_start;
1302	espeak_VOICE *vp = NULL;
1303	espeak_VOICE *vp2;
1304	espeak_VOICE voice_select2;
1305	espeak_VOICE *voices[N_VOICES_LIST]; // list of candidates
1306	espeak_VOICE *voices2[N_VOICES_LIST+N_VOICE_VARIANTS];
1307	static espeak_VOICE voice_variants[N_VOICE_VARIANTS];
1308	static char voice_id[50];
1309
1310	memcpy(&voice_select2,voice_select,sizeof(voice_select2));
1311
1312	if(n_voices_list == 0)
1313		espeak_ListVoices(NULL);   // create the voices list
1314
1315	if((voice_select2.languages == NULL) || (voice_select2.languages[0] == 0))
1316	{
1317		// no language is specified. Get language from the named voice
1318		static char buf[60];
1319	
1320		if(voice_select2.name == NULL)
1321		{
1322			if((voice_select2.name = voice_select2.identifier) == NULL)
1323				voice_select2.name = "default";
1324		}
1325	
1326		strncpy0(buf,voice_select2.name,sizeof(buf));
1327		variant_name = ExtractVoiceVariantName(buf,0);
1328
1329		vp = SelectVoiceByName(voices_list,buf);
1330		if(vp != NULL)
1331		{
1332			voice_select2.languages = &(vp->languages[1]);
1333
1334			if((voice_select2.gender==0) && (voice_select2.age==0) && (voice_select2.variant==0))
1335			{
1336				if(variant_name[0] != 0)
1337				{
1338					sprintf(voice_id,"%s+%s",vp->identifier,&variant_name[3]);  // omit the  !v/  from variant_name
1339					return(voice_id);
1340				}
1341
1342				return(vp->identifier);
1343			}
1344		}
1345	}
1346
1347	// select and sort voices for the required language
1348	nv = SetVoiceScores(&voice_select2,voices,0);
1349
1350	if(nv == 0)
1351	{
1352		// no matching voice, choose the default
1353		if((voices[0] = SelectVoiceByName(voices_list,"default")) != NULL)
1354			nv = 1;
1355	}
1356
1357	gender = 0;
1358	if((voice_select2.gender == 2) || ((voice_select2.age > 0) && (voice_select2.age < 13)))
1359		gender = 2;
1360	else
1361	if(voice_select2.gender == 1)
1362		gender = 1;
1363
1364#define AGE_OLD  60
1365	if(voice_select2.age < AGE_OLD)
1366		aged = 0;
1367
1368	p = p_start = variant_lists[gender];
1369	if(aged == 0)
1370		p++;   // the first voice in the variants list is older
1371
1372	// add variants for the top voices
1373	n_variants = 0;
1374	for(ix=0, ix2=0; ix<nv; ix++)
1375	{
1376		vp = voices[ix];
1377		// is the main voice the required gender?
1378		skip=0;
1379		if((gender != 0) && (vp->gender != gender))
1380		{
1381			skip=1;
1382		}
1383		if((ix2==0) && aged && (vp->age < AGE_OLD))
1384		{
1385			skip=1;
1386		}
1387		if(skip==0)
1388		{
1389			voices2[ix2++] = vp; 
1390		}
1391
1392		for(j=0; (j < vp->xx1) && (n_variants < N_VOICE_VARIANTS);)
1393		{
1394			if((variant_number = *p) == 0)
1395			{
1396				p = p_start;
1397				continue;
1398			}
1399
1400			vp2 = &voice_variants[n_variants++];        // allocate space for voice variant
1401			memcpy(vp2,vp,sizeof(espeak_VOICE));        // copy from the original voice
1402			vp2->variant = variant_number;
1403			voices2[ix2++] = vp2;
1404			p++;
1405			j++;
1406		}
1407	}
1408	// add any more variants to the end of the list
1409	while((vp != NULL) && ((variant_number = *p++) != 0) && (n_variants < N_VOICE_VARIANTS))
1410	{
1411		vp2 = &voice_variants[n_variants++];        // allocate space for voice variant
1412		memcpy(vp2,vp,sizeof(espeak_VOICE));        // copy from the original voice
1413		vp2->variant = variant_number;
1414		voices2[ix2++] = vp2;
1415	}
1416
1417	// index the sorted list by the required variant number
1418	vp = voices2[voice_select2.variant % ix2];
1419
1420	if(vp->variant != 0)
1421	{
1422		variant_name = ExtractVoiceVariantName(NULL,vp->variant);
1423		sprintf(voice_id,"%s+%s",vp->identifier,&variant_name[3]);
1424		return(voice_id);
1425	}
1426
1427	return(vp->identifier);
1428}  //  end of SelectVoice
1429
1430
1431
1432void GetVoices(const char *path)
1433{//=============================
1434	FILE *f_voice;
1435	espeak_VOICE *voice_data;
1436	int ftype;
1437	char fname[sizeof(path_home)+100];
1438
1439#ifdef PLATFORM_RISCOS
1440	int len;
1441	int *type;
1442	char *p;
1443	_kernel_swi_regs regs;
1444	_kernel_oserror *error;
1445	char buf[80];
1446	char directory2[sizeof(path_home)+100];
1447
1448	regs.r[0] = 10;
1449	regs.r[1] = (int)path;
1450	regs.r[2] = (int)buf;
1451	regs.r[3] = 1;
1452	regs.r[4] = 0;
1453	regs.r[5] = sizeof(buf);
1454	regs.r[6] = 0;
1455
1456	while(regs.r[3] > 0)
1457	{
1458		error = _kernel_swi(0x0c+0x20000,&regs,&regs);      /* OS_GBPB 10, read directory entries */
1459		if((error != NULL) || (regs.r[3] == 0))
1460		{
1461			break;
1462		}
1463		type = (int *)(&buf[16]);
1464		len = strlen(&buf[20]);
1465		sprintf(fname,"%s.%s",path,&buf[20]);
1466
1467		if(*type == 2)
1468		{
1469			// a sub-directory
1470			GetVoices(fname);
1471		}
1472		else
1473		{
1474			// a regular line, add it to the voices list	
1475			if((f_voice = fopen(fname,"r")) == NULL)
1476				continue;
1477		
1478			// pass voice file name within the voices directory
1479			voice_data = ReadVoiceFile(f_voice, fname+len_path_voices, &buf[20]);
1480			fclose(f_voice);
1481
1482			if(voice_data != NULL)
1483			{
1484				voices_list[n_voices_list++] = voice_data;
1485			}
1486		}
1487	}
1488#else
1489#ifdef PLATFORM_WINDOWS
1490   WIN32_FIND_DATA FindFileData;
1491   HANDLE hFind = INVALID_HANDLE_VALUE;
1492   DWORD dwError;
1493
1494#undef UNICODE         // we need FindFirstFileA() which takes an 8-bit c-string
1495	sprintf(fname,"%s\\*",path);
1496	hFind = FindFirstFile(fname, &FindFileData);
1497	if(hFind == INVALID_HANDLE_VALUE)
1498		return;
1499
1500	do {
1501		sprintf(fname,"%s%c%s",path,PATHSEP,FindFileData.cFileName);
1502
1503		ftype = GetFileLength(fname);
1504
1505		if((ftype == -2) && (FindFileData.cFileName[0] != '.'))
1506		{
1507			// a sub-sirectory
1508			GetVoices(fname);
1509		}
1510		else
1511		if(ftype > 0)
1512		{
1513			// a regular line, add it to the voices list	
1514			if((f_voice = fopen(fname,"r")) == NULL)
1515				continue;
1516		
1517			// pass voice file name within the voices directory
1518			voice_data = ReadVoiceFile(f_voice, fname+len_path_voices, FindFileData.cFileName);
1519			fclose(f_voice);
1520
1521			if(voice_data != NULL)
1522			{
1523				voices_list[n_voices_list++] = voice_data;
1524			}
1525		}
1526	} while(FindNextFile(hFind, &FindFileData) != 0);
1527	FindClose(hFind);
1528
1529#else
1530	DIR *dir;
1531	struct dirent *ent;
1532
1533	if((dir = opendir(path)) == NULL)
1534		return;
1535
1536	while((ent = readdir(dir)) != NULL)
1537	{
1538		if(n_voices_list >= (N_VOICES_LIST-2))
1539			break;   // voices list is full
1540
1541		sprintf(fname,"%s%c%s",path,PATHSEP,ent->d_name);
1542
1543		ftype = GetFileLength(fname);
1544
1545		if((ftype == -2) && (ent->d_name[0] != '.'))
1546		{
1547			// a sub-sirectory
1548			GetVoices(fname);
1549		}
1550		else
1551		if(ftype > 0)
1552		{
1553			// a regular line, add it to the voices list	
1554			if((f_voice = fopen(fname,"r")) == NULL)
1555				continue;
1556		
1557			// pass voice file name within the voices directory
1558			voice_data = ReadVoiceFile(f_voice, fname+len_path_voices, ent->d_name);
1559			fclose(f_voice);
1560
1561			if(voice_data != NULL)
1562			{
1563				voices_list[n_voices_list++] = voice_data;
1564			}
1565		}
1566	}
1567	closedir(dir);
1568#endif
1569#endif
1570}   // end of GetVoices
1571
1572
1573
1574espeak_ERROR SetVoiceByName(const char *name)
1575{//=========================================
1576	espeak_VOICE *v;
1577	espeak_VOICE voice_selector;
1578	char *variant_name;
1579	static char buf[60];
1580
1581	strncpy0(buf,name,sizeof(buf));
1582	variant_name = ExtractVoiceVariantName(buf,0);
1583
1584	memset(&voice_selector,0,sizeof(voice_selector));
1585//	voice_selector.name = buf;
1586	voice_selector.name = (char *)name;  // include variant name in voice stack ??
1587
1588	// first check for a voice with this filename
1589	// This may avoid the need to call espeak_ListVoices().
1590
1591	if(LoadVoice(buf,1) != NULL)
1592	{
1593		if(variant_name[0] != 0)
1594		{
1595			LoadVoice(variant_name,2);
1596		}
1597
1598		DoVoiceChange(voice);
1599		SetVoiceStack(&voice_selector);
1600		return(EE_OK);
1601	}
1602
1603	if(n_voices_list == 0)
1604		espeak_ListVoices(NULL);   // create the voices list
1605
1606	if((v = SelectVoiceByName(voices_list,buf)) != NULL)
1607	{
1608		if(LoadVoice(v->identifier,0) != NULL)
1609		{
1610			if(variant_name[0] != 0)
1611			{
1612				LoadVoice(variant_name,2);
1613			}
1614			DoVoiceChange(voice);
1615			SetVoiceStack(&voice_selector);
1616			return(EE_OK);
1617		}
1618	}
1619	return(EE_INTERNAL_ERROR);   // voice name not found
1620}  // end of SetVoiceByName
1621
1622
1623
1624espeak_ERROR SetVoiceByProperties(espeak_VOICE *voice_selector)
1625{//============================================================
1626	const char *voice_id;
1627
1628	voice_id = SelectVoice(voice_selector);
1629
1630	LoadVoiceVariant(voice_id,0);
1631	DoVoiceChange(voice);
1632	SetVoiceStack(voice_selector);
1633
1634	return(EE_OK);
1635}  //  end of SetVoiceByProperties
1636
1637
1638
1639
1640//=======================================================================
1641//  Library Interface Functions
1642//=======================================================================
1643#pragma GCC visibility push(default)
1644
1645
1646ESPEAK_API const espeak_VOICE **espeak_ListVoices(espeak_VOICE *voice_spec)
1647{//========================================================================
1648#ifndef PLATFORM_RISCOS
1649	int ix;
1650	int j;
1651	espeak_VOICE *v;
1652	static espeak_VOICE *voices[N_VOICES_LIST];
1653	char path_voices[sizeof(path_home)+12];
1654
1655	// free previous voice list data
1656
1657	for(ix=0; ix<n_voices_list; ix++)
1658	{
1659		if(voices_list[ix] != NULL)
1660			free(voices_list[ix]);
1661	}
1662	n_voices_list = 0;
1663
1664	sprintf(path_voices,"%s%cvoices",path_home,PATHSEP);
1665	len_path_voices = strlen(path_voices)+1;
1666
1667	GetVoices(path_voices);
1668	voices_list[n_voices_list] = NULL;  // voices list terminator
1669
1670	// sort the voices list
1671	qsort(voices_list,n_voices_list,sizeof(espeak_VOICE *),
1672		(int (__cdecl *)(const void *,const void *))VoiceNameSorter);
1673
1674
1675	if(voice_spec)
1676	{
1677		// select the voices which match the voice_spec, and sort them by preference
1678		SetVoiceScores(voice_spec,voices,1);
1679	}
1680	else
1681	{
1682		// list all: omit variant voices and mbrola voices
1683		j = 0;
1684		for(ix=0; (v = voices_list[ix]) != NULL; ix++)
1685		{
1686			if((v->languages[0] != 0) && (strcmp(&v->languages[1],"variant") != 0) && (memcmp(v->identifier,"mb/",3) != 0))
1687			{
1688				voices[j++] = v;
1689			}
1690		}
1691		voices[j] = NULL;
1692	}
1693	return((const espeak_VOICE **)voices);
1694#endif
1695	return((const espeak_VOICE **)voices_list);
1696}  //  end of espeak_ListVoices
1697
1698
1699
1700ESPEAK_API espeak_VOICE *espeak_GetCurrentVoice(void)
1701{//==================================================
1702	return(&voice_selected);
1703}
1704
1705#pragma GCC visibility pop
1706
1707