12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469 |
-
- #include "StdAfx.h"
-
- #include "stdio.h"
- #include "ctype.h"
- #include "wctype.h"
- #include "string.h"
- #include "stdlib.h"
- #include "speech.h"
-
- #ifdef PLATFORM_WINDOWS
- #include "windows.h"
- #else
- #ifndef PLATFORM_RISCOS
- #include "dirent.h"
- #endif
- #endif
-
- #include "speak_lib.h"
- #include "voice.h"
- #include "phoneme.h"
- #include "synthesize.h"
- #include "translate.h"
-
-
- MNEM_TAB genders [] = {
- {"unknown", 0},
- {"male", 1},
- {"female", 2},
- {NULL, 0 }};
-
-
- int tone_points[10] = {600,170, 1200,135, 2000,110, 3000,110, -1,0};
-
-
-
- static int formant_rate_22050[9] = {50, 100, 160, 200, 200, 200, 200, 200, 200};
- int formant_rate[9];
-
-
-
- #define DEFAULT_LANGUAGE_PRIORITY 5
- #define N_VOICES_LIST 100
- static int n_voices_list = 0;
- static espeak_VOICE *voices_list[N_VOICES_LIST];
- static int len_path_voices;
-
- espeak_VOICE *voice_selected = NULL;
- espeak_VOICE *first_voice = NULL;
- char voice_name[40];
-
-
-
- #define V_NAME 1
- #define V_LANGUAGE 2
- #define V_GENDER 3
- #define V_TRANSLATOR 4
- #define V_PHONEMES 5
- #define V_DICTIONARY 6
-
-
- #define V_FORMANT 7
- #define V_PITCH 8
- #define V_ECHO 9
- #define V_FLUTTER 10
- #define V_ROUGHNESS 11
- #define V_CLARITY 12
- #define V_TONE 13
-
-
- #define V_WORDGAP 15
- #define V_INTONATION 16
- #define V_STRESSLENGTH 17
- #define V_STRESSAMP 18
- #define V_STRESSADD 19
- #define V_DICTRULES 20
- #define V_STRESSRULE 21
- #define V_CHARSET 22
- #define V_NUMBERS 23
- #define V_OPTION 24
-
- #define V_MBROLA 25
-
-
- #define V_REPLACE 26
-
-
-
- typedef struct {
- const char *mnem;
- int data;
- } keywtab_t;
-
- static keywtab_t keyword_tab[] = {
- {"name", V_NAME},
- {"language", V_LANGUAGE},
- {"gender", V_GENDER},
-
- {"formant", V_FORMANT},
- {"pitch", V_PITCH},
- {"phonemes", V_PHONEMES},
- {"translator", V_TRANSLATOR},
- {"dictionary", V_DICTIONARY},
- {"stressLength", V_STRESSLENGTH},
- {"stressAmp", V_STRESSAMP},
- {"stressAdd", V_STRESSADD},
- {"intonation", V_INTONATION},
- {"dictrules", V_DICTRULES},
- {"stressrule", V_STRESSRULE},
- {"charset", V_CHARSET},
- {"replace", V_REPLACE},
- {"words", V_WORDGAP},
- {"echo", V_ECHO},
- {"flutter", V_FLUTTER},
- {"roughness", V_ROUGHNESS},
- {"clarity", V_CLARITY},
- {"tone", V_TONE},
- {"numbers", V_NUMBERS},
- {"option", V_OPTION},
- {"mbrola", V_MBROLA},
-
-
- {"l_dieresis", 0x100+LOPT_DIERESES},
-
- {"l_prefix", 0x100+LOPT_PREFIXES},
- {"l_regressive_v", 0x100+LOPT_REGRESSIVE_VOICING},
- {"l_unpronouncable", 0x100+LOPT_UNPRONOUNCABLE},
- {"l_sonorant_min", 0x100+LOPT_SONORANT_MIN},
- {"l_length_mods", 0x100+LOPT_LENGTH_MODS},
- {NULL, 0} };
-
- #define N_VOICES 100
- static int n_voices_tab = 0;
- static voice_t *voices_tab[N_VOICES];
-
- #define N_VOICE_VARIANTS 12
- const char variants_either[N_VOICE_VARIANTS] = {1,2,12,3,13,4,14,5,11,0};
- const char variants_male[N_VOICE_VARIANTS] = {1,2,3,4,5,0};
- const char variants_female[N_VOICE_VARIANTS] = {11,12,13,14,0};
- const char *variant_lists[3] = {variants_either, variants_male, variants_female};
-
-
-
- void SetToneAdjust(voice_t *voice, int *tone_pts)
- {
- int ix;
- int pt;
- int y;
- int freq1=0;
- int freq2;
- int height1 = tone_pts[1];
- int height2;
- double rate;
-
- for(pt=0; pt<10; pt+=2)
- {
- if(tone_pts[pt] == -1)
- {
- tone_pts[pt] = N_TONE_ADJUST*8;
- if(pt > 0)
- tone_pts[pt+1] = tone_pts[pt-1];
- }
- freq2 = tone_pts[pt] / 8;
- height2 = tone_pts[pt+1];
- if((freq2 - freq1) > 0)
- {
- rate = double(height2-height1)/(freq2-freq1);
-
- for(ix=freq1; ix<freq2; ix++)
- {
- y = height1 + int(rate * (ix-freq1));
- if(y > 255)
- y = 255;
- voice->tone_adjust[ix] = y;
- }
- }
- freq1 = freq2;
- height1 = height2;
- }
- }
-
-
- void ReadTonePoints(char *string, int *tone_pts)
- {
-
- int ix;
-
- for(ix=0; ix<10; ix++)
- tone_pts[ix] = -1;
-
- sscanf(string,"%d %d %d %d %d %d %d %d",
- &tone_pts[0],&tone_pts[1],&tone_pts[2],&tone_pts[3],
- &tone_pts[4],&tone_pts[5],&tone_pts[6],&tone_pts[7]);
- }
-
-
-
-
- static espeak_VOICE *ReadVoiceFile(FILE *f_in, const char *fname, const char*leafname)
- {
-
-
-
- char linebuf[120];
- char vname[80];
- char vgender[80];
- char vlanguage[80];
- char languages[300];
-
-
- unsigned int len;
- int langix = 0;
- int n_languages = 0;
- char *p;
- espeak_VOICE *voice_data;
- int priority;
- int age;
- int n_variants = 3;
- int gender;
-
- #ifdef PLATFORM_WINDOWS
- if(memcmp(leafname,"mb-",3) == 0)
- {
-
- memcpy(vname,&leafname[3],3);
- vname[3] = 0;
- sprintf(linebuf,"%s/mbrola/%s",path_home,vname);
-
- if(GetFileLength(linebuf) <= 0)
- return(0);
- }
- #endif
-
- vname[0] = 0;
- vgender[0] = 0;
- age = 0;
-
- while(fgets(linebuf,sizeof(linebuf),f_in) != NULL)
- {
- linebuf[strlen(linebuf)-1] = 0;
-
- if(memcmp(linebuf,"name",4)==0)
- {
- p = &linebuf[4];
- while(isspace(*p)) p++;
- strncpy0(vname,p,sizeof(vname));
- }
- else
- if(memcmp(linebuf,"language",8)==0)
- {
- priority = DEFAULT_LANGUAGE_PRIORITY;
- vlanguage[0] = 0;
-
- sscanf(&linebuf[8],"%s %d",vlanguage,&priority);
- len = strlen(vlanguage) + 2;
-
- if(len < (sizeof(languages)-langix-1))
- {
- languages[langix] = priority;
-
- strcpy(&languages[langix+1],vlanguage);
- langix += len;
- n_languages++;
- }
- }
- else
- if(memcmp(linebuf,"gender",6)==0)
- {
- sscanf(&linebuf[6],"%s %d",vgender,&age);
- }
- else
- if(memcmp(linebuf,"variants",8)==0)
- {
- sscanf(&linebuf[8],"%d",&n_variants);
- }
- }
- languages[langix++] = 0;
-
- gender = LookupMnem(genders,vgender);
-
- if(n_languages == 0)
- {
- #ifdef deleted
-
-
-
-
- if(memcmp(leafname,"!variant",8)==0)
- {
- if(((variant = atoi(&leafname[8])) > 0) && (strlen(variants_either) < N_VOICE_VARIANTS))
- {
- char string[2];
- string[0] = variant;
- string[1] = 0;
- strcat(variants_either,string);
- if(gender == 1)
- strcat(variants_male,string);
- if(gender == 2)
- strcat(variants_female,string);
- }
- }
- #endif
- return(NULL);
- }
-
- p = (char *)calloc(sizeof(espeak_VOICE) + langix + strlen(fname) + strlen(vname) + 3, 1);
- voice_data = (espeak_VOICE *)p;
- p = &p[sizeof(espeak_VOICE)];
-
- memcpy(p,languages,langix);
- voice_data->languages = p;
-
- strcpy(&p[langix],fname);
- voice_data->identifier = &p[langix];
- voice_data->name = &p[langix];
-
- if(vname[0] != 0)
- {
- langix += strlen(fname)+1;
- strcpy(&p[langix],vname);
- voice_data->name = &p[langix];
- }
-
- voice_data->age = age;
- voice_data->gender = gender;
- voice_data->variant = 0;
- voice_data->xx1 = n_variants;
- return(voice_data);
- }
-
-
-
-
- void VoiceReset(int tone_only)
- {
-
- int pk;
-
-
- voice->pitch_base = 0x49000;
- voice->pitch_range = 0x0f30;
-
- voice->echo_delay = 0;
- voice->echo_amp = 0;
- voice->flutter = 64;
- voice->n_harmonic_peaks = 5;
- voice->peak_shape = 1;
- #ifdef PLATFORM_RISCOS
- voice->roughness = 1;
- #else
- voice->roughness = 2;
- #endif
-
- for(pk=0; pk<N_PEAKS; pk++)
- {
- voice->freq[pk] = 256;
- voice->height[pk] = 256;
- voice->width[pk] = 256;
-
-
- formant_rate[pk] = (formant_rate_22050[pk] * 22050)/samplerate;
- }
-
-
-
-
-
- SetToneAdjust(voice,tone_points);
-
-
- voice->speedf1 = 256;
- voice->speedf2 = 238;
- voice->speedf3 = 232;
-
- if(tone_only == 0)
- {
- n_replace_phonemes = 0;
- option_tone1 = 0;
- option_quiet = 0;
- LoadMbrolaTable(NULL,NULL);
- }
- }
-
-
- static void VoiceFormant(char *p)
- {
-
- int ix;
- int formant;
- int freq = -1;
- int height = -1;
- int width = -1;
-
- ix = sscanf(p,"%d %d %d %d",&formant,&freq,&height,&width);
- if(ix < 2)
- return;
-
- if((formant < 0) || (formant > 8))
- return;
-
- if(freq >= 0)
- voice->freq[formant] = int(freq * 2.56001);
- if(height >= 0)
- voice->height[formant] = int(height * 2.56001);
- if(width >= 0)
- voice->width[formant] = int(width * 2.56001);
- }
-
-
- static voice_t *VoiceLookup(char *voicename)
- {
- int ix;
- voice_t *v;
-
- for(ix=0; ix < N_VOICES; ix++)
- {
- if(ix == n_voices_tab)
- {
-
- v = (voice_t *)Alloc(sizeof(voice_t));
- if(v == NULL)
- return(NULL);
- voices_tab[n_voices_tab++] = v;
- strncpy0(v->name,voicename,sizeof(v->name));
- return(v);
- }
- else
- if(strcmp(voices_tab[ix]->name,voicename)==0)
- {
- return(voices_tab[ix]);
- }
- }
- return(NULL);
- }
-
-
-
-
-
- static void PhonemeReplacement(int type, char *p)
- {
- int n;
- int phon;
- int flags = 0;
- char phon_string1[12];
- char phon_string2[12];
-
- strcpy(phon_string2,"NULL");
- n = sscanf(p,"%d %s %s",&flags,phon_string1,phon_string2);
- if((n < 2) || (n_replace_phonemes >= N_REPLACE_PHONEMES))
- return;
-
- if((phon = LookupPh(phon_string1)) == 0)
- return;
-
- replace_phonemes[n_replace_phonemes].old_ph = phon;
- replace_phonemes[n_replace_phonemes].new_ph = LookupPh(phon_string2);
- replace_phonemes[n_replace_phonemes++].type = flags;
- }
-
-
-
- static int Read8Numbers(char *data_in,int *data)
- {
-
- return(sscanf(data_in,"%d %d %d %d %d %d %d %d",
- &data[0],&data[1],&data[2],&data[3],&data[4],&data[5],&data[6],&data[7]));
- }
-
-
- voice_t *LoadVoice(char *vname, int control)
- {
-
-
-
-
- FILE *f_voice = NULL;
- keywtab_t *k;
- char *p;
- int key;
- int ix;
- int n;
- int value;
- int error = 0;
- int tone_only = control & 2;
- int language_set = 0;
- int phonemes_set = 0;
- int stress_amps_set = 0;
- int stress_lengths_set = 0;
- int stress_add_set = 0;
- int conditional_rules = 0;
- LANGUAGE_OPTIONS *langopts = NULL;
-
- voice_t *v;
- Translator *new_translator = NULL;
-
- char voicename[40];
- char language_name[40];
- char translator_name[40];
- char new_dictionary[40];
- char phonemes_name[40];
- char *language_type;
- char buf[200];
- char path_voices[140];
- char langname[4];
-
- int stress_amps[8];
- int stress_lengths[8];
- int stress_add[8];
-
- int pitch1;
- int pitch2;
-
- strcpy(voicename,vname);
- if(voicename[0]==0)
- strcpy(voicename,"default");
-
- sprintf(path_voices,"%s%cvoices%c",path_home,PATHSEP,PATHSEP);
- sprintf(buf,"%s%s",path_voices,voicename);
-
- if(GetFileLength(buf) <= 0)
- {
-
- langname[0] = voicename[0];
- langname[1] = voicename[1];
- langname[2] = 0;
- sprintf(buf,"%s%s%c%s",path_voices,langname,PATHSEP,voicename);
- }
-
- f_voice = fopen(buf,"r");
-
- language_type = "en";
- if(f_voice == NULL)
- {
- if(control & 3)
- return(NULL);
-
- if(SelectPhonemeTableName(voicename) >= 0)
- language_type = voicename;
- }
-
- if(first_voice == NULL)
- {
- first_voice = ReadVoiceFile(f_voice,buf+strlen(path_voices),voicename);
- rewind(f_voice);
- }
-
-
- if(!tone_only && (translator != NULL))
- {
- delete translator;
- translator = NULL;
- }
-
- strcpy(translator_name,language_type);
- strcpy(new_dictionary,language_type);
- strcpy(phonemes_name,language_type);
-
-
- if(!tone_only)
- {
- if((v = VoiceLookup(voicename)) != NULL)
- voice = v;
- }
- VoiceReset(tone_only);
-
- if(!tone_only)
- SelectPhonemeTableName(phonemes_name);
-
-
- while((f_voice != NULL) && (fgets(buf,sizeof(buf),f_voice) != NULL))
- {
- if((p = strstr(buf,"//")) != NULL)
- *p = 0;
-
-
- for(p=buf; (*p != 0) && !isspace(*p); p++);
- *p++ = 0;
-
- if(buf[0] == 0) continue;
-
- key = 0;
- for(k=keyword_tab; k->mnem != NULL; k++)
- {
- if(strcmp(buf,k->mnem)==0)
- {
- key = k->data;
- break;
- }
- }
-
- switch(key)
- {
- case V_LANGUAGE:
-
- if(language_set || tone_only)
- break;
-
- sscanf(p,"%s",language_name);
- language_type = strtok(language_name,"-");
- language_set = 1;
- strcpy(translator_name,language_type);
- strcpy(new_dictionary,language_type);
- strcpy(phonemes_name,language_type);
- SelectPhonemeTableName(phonemes_name);
-
- if(new_translator != NULL)
- delete new_translator;
-
- new_translator = SelectTranslator(translator_name);
- langopts = &new_translator->langopts;
- break;
-
- case V_NAME:
- case V_GENDER:
- break;
-
- case V_TRANSLATOR:
- if(tone_only) break;
-
- sscanf(p,"%s",translator_name);
-
- if(new_translator != NULL)
- delete new_translator;
-
- new_translator = SelectTranslator(translator_name);
- langopts = &new_translator->langopts;
- break;
-
- case V_DICTIONARY:
- sscanf(p,"%s",new_dictionary);
- break;
-
- case V_PHONEMES:
- sscanf(p,"%s",phonemes_name);
- break;
-
- case V_FORMANT:
- VoiceFormant(p);
- break;
-
- case V_PITCH:
-
- n = sscanf(p,"%d %d",&pitch1,&pitch2);
- voice->pitch_base = (pitch1 - 9) << 12;
- voice->pitch_range = (pitch2 - pitch1) * 108;
- break;
-
- case V_STRESSLENGTH:
- stress_lengths_set = Read8Numbers(p,stress_lengths);
- break;
-
- case V_STRESSAMP:
- stress_amps_set = Read8Numbers(p,stress_amps);
- break;
-
- case V_STRESSADD:
- stress_add_set = Read8Numbers(p,stress_add);
- break;
-
- case V_INTONATION:
- sscanf(p,"%d %d",&option_tone1,&option_tone2);
- break;
-
- case V_DICTRULES:
- while(*p != 0)
- {
- while(isspace(*p)) p++;
- n = -1;
- if((n = atoi(p)) > 0)
- {
- p++;
- conditional_rules |= (1 << n);
- }
- while(isalnum(*p)) p++;
- }
- break;
-
- case V_REPLACE:
- if(phonemes_set == 0)
- {
-
- SelectPhonemeTableName(phonemes_name);
- phonemes_set = 1;
- }
- PhonemeReplacement(key,p);
- break;
-
- case V_WORDGAP:
- sscanf(p,"%d %d",&langopts->word_gap, &langopts->vowel_pause);
- break;
-
- case V_STRESSRULE:
- sscanf(p,"%d %d %d %d",&langopts->stress_rule,
- &langopts->stress_flags,
- &langopts->unstressed_wd1,
- &langopts->unstressed_wd2);
- break;
-
- case V_CHARSET:
- if((sscanf(p,"%d",&value)==1) && (value < N_CHARSETS))
- new_translator->charset_a0 = charsets[value];
- break;
-
- case V_NUMBERS:
- sscanf(p,"%d",&langopts->numbers);
- break;
-
- case V_OPTION:
- if(sscanf(p,"%d %d",&ix,&value) == 2)
- {
- if((ix >= 0) && (ix < N_LOPTS))
- langopts->param[ix] = value;
- }
- break;
-
- case V_ECHO:
-
- value = 0;
- voice->echo_amp = 0;
- sscanf(p,"%d %d",&voice->echo_delay,&voice->echo_amp);
- break;
-
- case V_FLUTTER:
- if(sscanf(p,"%d",&value)==1)
- voice->flutter = value * 32;
- break;
-
- case V_ROUGHNESS:
- if(sscanf(p,"%d",&value)==1)
- voice->roughness = value;
- break;
-
- case V_CLARITY:
- if(sscanf(p,"%d",&value)==1)
- {
- if(value > 4)
- {
- voice->peak_shape = 1;
- value = 4;
- }
- voice->n_harmonic_peaks = 1+value;
- }
- break;
-
- case V_TONE:
- {
- int tone_data[10];
- ReadTonePoints(p,tone_data);
- SetToneAdjust(voice,tone_data);
- }
- break;
-
- case V_MBROLA:
- {
- char name[40];
- char phtrans[40];
- phtrans[0] = 0;
- sscanf(p,"%s %s",name,phtrans);
- LoadMbrolaTable(name,phtrans);
- }
- break;
-
- default:
- if((key & 0xff00) == 0x100)
- {
- sscanf(p,"%d",&langopts->param[key &0xff]);
- }
- else
- {
- fprintf(stderr,"Bad voice attribute: %s\n",buf);
- }
- break;
- }
- }
- if(f_voice != NULL)
- fclose(f_voice);
-
- if((new_translator == NULL) && (!tone_only))
- {
-
- new_translator = SelectTranslator(translator_name);
- }
-
- for(ix=0; ix<N_PEAKS; ix++)
- {
- voice->freq2[ix] = voice->freq[ix];
- voice->height2[ix] = voice->height[ix];
- voice->width2[ix] = voice->width[ix];
- }
-
- if(tone_only)
- {
- new_translator = translator;
- }
- else
- {
- if((ix = SelectPhonemeTableName(phonemes_name)) < 0)
- {
- fprintf(stderr,"Unknown phoneme table: '%s'\n",phonemes_name);
- }
- voice->phoneme_tab_ix = ix;
- error = new_translator->LoadDictionary(new_dictionary, control & 4);
- if(dictionary_name[0]==0)
- return(NULL);
-
- new_translator->dict_condition = conditional_rules;
- }
-
- langopts = &new_translator->langopts;
-
-
- if((value = langopts->param[LOPT_LENGTH_MODS]) != 0)
- {
- SetLengthMods(new_translator,value);
- }
-
- voice->width[0] = (voice->width[0] * 105)/100;
-
- if(!tone_only)
- {
- translator = new_translator;
- strcpy(voice_name,voicename);
- }
-
-
- for(ix=0; ix<stress_lengths_set; ix++)
- {
- translator->stress_lengths[ix] = stress_lengths[ix];
- }
- for(ix=0; ix<stress_add_set; ix++)
- {
- translator->stress_lengths[ix] += stress_add[ix];
- }
- for(ix=0; ix<stress_amps_set; ix++)
- {
- translator->stress_amps[ix] = stress_amps[ix];
- translator->stress_amps_r[ix] = stress_amps[ix] -1;
- }
-
- return(voice);
- }
-
-
- voice_t *LoadVoiceVariant(const char *vname, int variant)
- {
- voice_t *v;
- char *p;
- char buf[40];
-
- strcpy(buf,vname);
- if((p = strchr(buf,'+')) != NULL)
- {
-
- *p = 0;
- variant = atoi(p+1);
- }
- v = LoadVoice(buf,0);
- if((v != NULL) && (variant > 0))
- {
- sprintf(buf,"!variant%d",variant);
- v = LoadVoice(buf,2);
- }
- return(v);
- }
-
-
-
- static int __cdecl VoiceNameSorter(const void *p1, const void *p2)
- {
- int ix;
- espeak_VOICE *v1 = *(espeak_VOICE **)p1;
- espeak_VOICE *v2 = *(espeak_VOICE **)p2;
-
-
- if((ix = strcmp(&v1->languages[1],&v2->languages[1])) != 0)
- return(ix);
- if((ix = v1->languages[0] - v2->languages[0]) != 0)
- return(ix);
- return(strcmp(v1->name,v2->name));
- }
-
-
- static int __cdecl VoiceScoreSorter(const void *p1, const void *p2)
- {
- int ix;
- espeak_VOICE *v1 = *(espeak_VOICE **)p1;
- espeak_VOICE *v2 = *(espeak_VOICE **)p2;
-
- if((ix = v2->score - v1->score) != 0)
- return(ix);
- return(strcmp(v2->name,v1->name));
- }
-
-
- static int ScoreVoice(espeak_VOICE *voice_spec, int spec_n_parts, int spec_lang_len, espeak_VOICE *voice)
- {
- int ix;
- 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;
-
- if(spec_n_parts == 0)
- {
- score = 100;
- }
- else
- {
-
- while(*p != 0)
- {
- language_priority = *p++;
-
- matching = 1;
- matching_parts = 0;
- n_parts = 1;
-
- for(ix=0; ; ix++)
- {
- if((ix >= spec_lang_len) || ((c1 = voice_spec->languages[ix]) == '-'))
- c1 = 0;
- if((c2 = p[ix]) == '-')
- c2 = 0;
-
- if(c1 != c2)
- {
- matching = 0;
- }
-
- if(p[ix] == '-')
- {
- n_parts++;
- if(matching)
- matching_parts++;
- }
- if(p[ix] == 0)
- break;
- }
- p += (ix+1);
- matching_parts += matching;
-
- if(matching_parts == 0)
- break;
-
- x = 5;
-
- if((diff = (spec_n_parts - matching_parts)) > 0)
- x -= diff;
-
-
- if((diff = (n_parts - matching_parts)) > 0)
- x -= diff;
-
- x = x*100 - (language_priority * 2);
-
- if(x > score)
- score = x;
- }
- }
- if(score == 0)
- return(0);
-
- if(voice_spec->name != NULL)
- {
- if(strcmp(voice_spec->name,voice->name)==0)
- {
-
- score += 500;
- }
- else
- if(strcmp(voice_spec->name,voice->identifier)==0)
- {
- score += 400;
- }
- }
-
- if(((voice_spec->gender == 1) || (voice_spec->gender == 2)) &&
- ((voice->gender == 1) || (voice->gender == 2)))
- {
- if(voice_spec->gender == voice->gender)
- score += 50;
- else
- score -= 50;
- }
-
- if((voice_spec->age <= 12) && (voice->gender == 2) && (voice->age > 12))
- {
- score += 5;
- }
-
- if(voice->age != 0)
- {
- if(voice_spec->age == 0)
- required_age = 30;
- else
- required_age = voice_spec->age;
-
- ratio = (required_age*100)/voice->age;
- if(ratio < 100)
- ratio = 10000/ratio;
- ratio = (ratio - 100)/10;
- x = 5 - ratio;
- if(x > 0) x = 0;
-
- score = score + x;
-
- if(voice_spec->age > 0)
- score += 10;
- }
- if(score < 1)
- score = 1;
- return(score);
- }
-
-
- static int SetVoiceScores(espeak_VOICE *voice_select, espeak_VOICE **voices, int control)
- {
-
- int ix;
- int score;
- int nv;
- int n_parts=0;
- int lang_len=0;
- char *p;
- espeak_VOICE *vp;
-
-
- if((voice_select->languages != NULL) && (voice_select->languages[0] != 0))
- {
- n_parts = 1;
- lang_len = strlen(voice_select->languages);
- for(p = voice_select->languages; *p != 0; p++)
- {
- if(*p == '-')
- n_parts++;
- }
- }
-
- nv = 0;
- for(ix=0; ix<n_voices_list; ix++)
- {
- vp = voices_list[ix];
- if(((control & 1) == 0) && (memcmp(vp->identifier,"mb/",3) == 0))
- continue;
-
- if((score = ScoreVoice(voice_select, n_parts, lang_len, voices_list[ix])) > 0)
- {
- voices[nv++] = vp;
- vp->score = score;
- }
- }
- voices[nv] = NULL;
-
- if(nv==0)
- return(0);
-
-
- qsort(voices,nv,sizeof(espeak_VOICE *),(int (__cdecl *)(const void *,const void *))VoiceScoreSorter);
-
- return(nv);
- }
-
-
-
-
- static espeak_VOICE *SelectVoiceByName(espeak_VOICE **voices, const char *name)
- {
- int ix;
- int match_fname = -1;
- int match_fname2 = -1;
- int match_name = -1;
- char *id;
- int last_part_len;
- char last_part[41];
-
- sprintf(last_part,"%c%s",PATHSEP,name);
- last_part_len = strlen(last_part);
-
- for(ix=0; voices[ix] != NULL; ix++)
- {
- if(strcmp(name,voices[ix]->name)==0)
- {
- match_name = ix;
- break;
- }
- else
- if(strcmp(name,id = voices[ix]->identifier)==0)
- {
- match_fname = ix;
- }
- else
- if(strcmp(last_part,&id[strlen(id)-last_part_len])==0)
- {
- match_fname2 = ix;
- }
- }
-
- if(match_name < 0)
- {
- match_name = match_fname;
- if(match_name < 0)
- match_name = match_fname2;
- }
-
- if(match_name < 0)
- return(NULL);
-
- return(voices[match_name]);
- }
-
-
-
-
- espeak_VOICE *SelectVoice(espeak_VOICE *voice_select, int *variant)
- {
-
- int nv;
- int ix, ix2;
- int j;
- int n_variants;
- int variant_number;
- int gender;
- int skip;
- int aged=1;
- const char *p, *p_start;
- espeak_VOICE *vp = NULL;
- espeak_VOICE *vp2;
- espeak_VOICE *voices[N_VOICES_LIST];
- espeak_VOICE *voices2[N_VOICES_LIST+N_VOICE_VARIANTS];
-
- espeak_VOICE voice_variants[N_VOICE_VARIANTS];
-
- if(n_voices_list == 0)
- espeak_ListVoices(NULL);
-
-
- nv = SetVoiceScores(voice_select,voices,0);
-
- if(nv == 0)
- {
-
- if((voices[0] = SelectVoiceByName(voices_list,"default")) != NULL)
- nv = 1;
- }
-
- gender = 0;
- if((voice_select->gender == 2) || ((voice_select->age > 0) && (voice_select->age < 13)))
- gender = 2;
- else
- if(voice_select->gender == 1)
- gender = 1;
-
- #define AGE_OLD 60
- if(voice_select->age < AGE_OLD)
- aged = 0;
-
- p = p_start = variant_lists[gender];
- if(aged == 0)
- p++;
-
-
- n_variants = 0;
- for(ix=0, ix2=0; ix<nv; ix++)
- {
- vp = voices[ix];
-
- skip=0;
- if((gender != 0) && (vp->gender != gender))
- {
- skip=1;
- }
- if((ix2==0) && aged && (vp->age < AGE_OLD))
- {
- skip=1;
- }
- if(skip==0)
- {
- voices2[ix2++] = vp;
- }
-
- for(j=0; (j < vp->xx1) && (n_variants < N_VOICE_VARIANTS);)
- {
- if((variant_number = *p) == 0)
- {
- p = p_start;
- continue;
- }
-
- vp2 = &voice_variants[n_variants++];
- memcpy(vp2,vp,sizeof(espeak_VOICE));
- vp2->variant = variant_number;
- voices2[ix2++] = vp2;
- p++;
- j++;
- }
- }
-
- while((vp != NULL) && ((variant_number = *p++) != 0) && (n_variants < N_VOICE_VARIANTS))
- {
- vp2 = &voice_variants[n_variants++];
- memcpy(vp2,vp,sizeof(espeak_VOICE));
- vp2->variant = variant_number;
- voices2[ix2++] = vp2;
- }
-
-
- vp = voices2[voice_select->variant % ix2];
- *variant = vp->variant;
- return(vp);
- }
-
-
-
- void GetVoices(const char *path)
- {
- FILE *f_voice;
- espeak_VOICE *voice_data;
- int ftype;
- char fname[140];
-
- #ifdef PLATFORM_RISCOS
- #else
- #ifdef PLATFORM_WINDOWS
- WIN32_FIND_DATA FindFileData;
- HANDLE hFind = INVALID_HANDLE_VALUE;
- DWORD dwError;
-
- sprintf(fname,"%s\\*",path);
- hFind = FindFirstFile(fname, &FindFileData);
- if(hFind == INVALID_HANDLE_VALUE)
- return;
-
- do {
- sprintf(fname,"%s%c%s",path,PATHSEP,FindFileData.cFileName);
-
- ftype = GetFileLength(fname);
-
- if((ftype == -2) && (FindFileData.cFileName[0] != '.'))
- {
-
- GetVoices(fname);
- }
- else
- if(ftype > 0)
- {
-
- if((f_voice = fopen(fname,"r")) == NULL)
- continue;
-
-
- voice_data = ReadVoiceFile(f_voice, fname+len_path_voices, FindFileData.cFileName);
- fclose(f_voice);
-
- if(voice_data != NULL)
- {
- voices_list[n_voices_list++] = voice_data;
- }
- }
- } while(FindNextFile(hFind, &FindFileData) != 0);
- FindClose(hFind);
-
- #else
- DIR *dir;
- struct dirent *ent;
-
- if((dir = opendir(path)) == NULL)
- return;
-
- while((ent = readdir(dir)) != NULL)
- {
- if(n_voices_list >= (N_VOICES_LIST-2))
- break;
-
- sprintf(fname,"%s%c%s",path,PATHSEP,ent->d_name);
-
- ftype = GetFileLength(fname);
-
- if((ftype == -2) && (ent->d_name[0] != '.'))
- {
-
- GetVoices(fname);
- }
- else
- if(ftype > 0)
- {
-
- if((f_voice = fopen(fname,"r")) == NULL)
- continue;
-
-
- voice_data = ReadVoiceFile(f_voice, fname+len_path_voices, ent->d_name);
- fclose(f_voice);
-
- if(voice_data != NULL)
- {
- voices_list[n_voices_list++] = voice_data;
- }
- }
- }
- closedir(dir);
- #endif
- #endif
- }
-
-
-
- espeak_ERROR SetVoiceByName(const char *name)
- {
- espeak_VOICE *v;
- int variant=0;
- char *p;
- char variant_name[20];
- static char buf[60];
-
- strncpy0(buf,name,sizeof(buf));
- if((p = strchr(buf,'+')) != NULL)
- {
-
- *p = 0;
- variant = atoi(p+1);
- }
-
-
- if((first_voice == NULL) && (LoadVoice(buf,1) != NULL))
- {
- voice_selected = first_voice;
-
- if(variant > 0)
- {
-
- sprintf(variant_name,"!variant%d",variant);
- LoadVoice(variant_name,2);
- }
-
- WavegenSetVoice(voice);
- return(EE_OK);
- }
-
- if(n_voices_list == 0)
- espeak_ListVoices(NULL);
-
- if((v = SelectVoiceByName(voices_list,buf)) != NULL)
- {
- if(LoadVoiceVariant(v->identifier,variant) != NULL)
- {
- voice_selected = v;
- WavegenSetVoice(voice);
- return(EE_OK);
- }
- }
- return(EE_INTERNAL_ERROR);
- }
-
-
-
- espeak_ERROR SetVoiceByProperties(espeak_VOICE *voice_selector)
- {
- int variant;
-
- voice_selected = SelectVoice(voice_selector,&variant);
-
- LoadVoiceVariant(voice_selected->identifier,variant);
- WavegenSetVoice(voice);
- return(EE_OK);
- }
-
-
-
-
-
-
-
- #pragma GCC visibility push(default)
-
-
- ESPEAK_API const espeak_VOICE **espeak_ListVoices(espeak_VOICE *voice_spec)
- {
- #ifndef PLATFORM_RISCOS
- int ix;
- static espeak_VOICE *voices[N_VOICES_LIST];
- char selected_voice_id[80];
- char path_voices[130];
-
-
- if((voice_selected != NULL) && (voice_selected->identifier != NULL))
- strncpy0(selected_voice_id,voice_selected->identifier,sizeof(selected_voice_id));
- else
- selected_voice_id[0] = 0;
- voice_selected = NULL;
-
- for(ix=0; ix<n_voices_list; ix++)
- {
- if(voices_list[ix] != NULL)
- free(voices_list[ix]);
- }
- n_voices_list = 0;
-
- sprintf(path_voices,"%s%cvoices",path_home,PATHSEP);
- len_path_voices = strlen(path_voices)+1;
-
- GetVoices(path_voices);
- voices_list[n_voices_list] = NULL;
-
-
- qsort(voices_list,n_voices_list,sizeof(espeak_VOICE *),
- (int (__cdecl *)(const void *,const void *))VoiceNameSorter);
-
-
- if(selected_voice_id[0] != 0)
- {
- for(ix=0; ix<n_voices_list; ix++)
- {
- if(strcmp(selected_voice_id, voices_list[ix]->identifier)==0)
- {
- voice_selected = voices_list[ix];
- break;
- }
- }
- }
-
- if(voice_spec)
- {
-
- SetVoiceScores(voice_spec,voices,1);
- return((const espeak_VOICE **)voices);
- }
-
- #endif
- return((const espeak_VOICE **)voices_list);
- }
-
-
-
- ESPEAK_API espeak_VOICE *espeak_GetCurrentVoice(void)
- {
- return(voice_selected);
- }
-
- #pragma GCC visibility pop
-
|