PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/native/external/espeak/src/readclause.cpp

http://eyes-free.googlecode.com/
C++ | 2338 lines | 1976 code | 262 blank | 100 comment | 324 complexity | 59e41bf86d1aec9ae0b37c183c043200 MD5 | raw file
Possible License(s): GPL-3.0, Apache-2.0
  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. #include "StdAfx.h"
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <wctype.h>
  25. ////#include <wchar.h>
  26. #include <math.h>
  27. #include "speak_lib.h"
  28. #include "speech.h"
  29. #include "phoneme.h"
  30. #include "synthesize.h"
  31. #include "voice.h"
  32. #include "translate.h"
  33. #ifdef PLATFORM_POSIX
  34. #include <unistd.h>
  35. #endif
  36. #include <locale.h>
  37. #define N_XML_BUF 256
  38. const char *xmlbase = ""; // base URL from <speak>
  39. int namedata_ix=0;
  40. int n_namedata = 0;
  41. char *namedata = NULL;
  42. FILE *f_input = NULL;
  43. int ungot_char2 = 0;
  44. char *p_textinput;
  45. wchar_t *p_wchar_input;
  46. int ungot_char;
  47. const char *ungot_word = NULL;
  48. int end_of_input;
  49. int ignore_text=0; // set during <sub> ... </sub> to ignore text which has been replaced by an alias
  50. int clear_skipping_text = 0; // next clause should clear the skipping_text flag
  51. int count_characters = 0;
  52. int sayas_mode;
  53. int ssml_ignore_l_angle = 0;
  54. static const char *punct_stop = ".:!?"; // pitch fall if followed by space
  55. static const char *punct_close = ")]}>;'\""; // always pitch fall unless followed by alnum
  56. // alter tone for announce punctuation or capitals
  57. static const char *tone_punct_on = "\0016T"; // add reverberation, lower pitch
  58. static const char *tone_punct_off = "\001T";
  59. // punctuations symbols that can end a clause
  60. const unsigned short punct_chars[] = {',','.','?','!',':',';',
  61. 0x2013, // en-dash
  62. 0x2014, // em-dash
  63. 0x2026, // elipsis
  64. 0x037e, // Greek question mark (looks like semicolon)
  65. 0x0387, // Greek semicolon, ano teleia
  66. 0x0964, // Devanagari Danda (fullstop)
  67. 0x3001, // ideograph comma
  68. 0x3002, // ideograph period
  69. 0xff01, // fullwidth exclamation
  70. 0xff0c, // fullwidth comma
  71. 0xff0e, // fullwidth period
  72. 0xff1a, // fullwidth colon
  73. 0xff1b, // fullwidth semicolon
  74. 0xff1f, // fullwidth question mark
  75. 0};
  76. // indexed by (entry num. in punct_chars) + 1
  77. // bits 0-7 pause x 10mS, bits 12-14 intonation type, bit 15 don't need following space or bracket
  78. static const unsigned int punct_attributes [] = { 0,
  79. CLAUSE_COMMA, CLAUSE_PERIOD, CLAUSE_QUESTION, CLAUSE_EXCLAMATION, CLAUSE_COLON, CLAUSE_SEMICOLON,
  80. CLAUSE_SEMICOLON, // en-dash
  81. CLAUSE_SEMICOLON, // em-dash
  82. CLAUSE_SEMICOLON, // elipsis
  83. CLAUSE_QUESTION, // Greek question mark
  84. CLAUSE_SEMICOLON, // Greek semicolon
  85. CLAUSE_PERIOD+0x8000, // Devanagari Danda (fullstop)
  86. CLAUSE_COMMA+0x8000, // ideograph comma
  87. CLAUSE_PERIOD+0x8000, // ideograph period
  88. CLAUSE_EXCLAMATION+0x8000, // fullwidth
  89. CLAUSE_COMMA+0x8000,
  90. CLAUSE_PERIOD+0x8000,
  91. CLAUSE_COLON+0x8000,
  92. CLAUSE_SEMICOLON+0x8000,
  93. CLAUSE_QUESTION+0x8000,
  94. CLAUSE_SEMICOLON, // spare
  95. 0 };
  96. // stack for language and voice properties
  97. // frame 0 is for the defaults, before any ssml tags.
  98. typedef struct {
  99. int tag_type;
  100. int voice_variant;
  101. int voice_gender;
  102. int voice_age;
  103. char voice_name[40];
  104. char language[20];
  105. } SSML_STACK;
  106. #define N_SSML_STACK 20
  107. int n_ssml_stack;
  108. SSML_STACK ssml_stack[N_SSML_STACK];
  109. char current_voice_id[40] = {0};
  110. #define N_PARAM_STACK 20
  111. int n_param_stack;
  112. PARAM_STACK param_stack[N_PARAM_STACK];
  113. int speech_parameters[N_SPEECH_PARAM]; // current values, from param_stack
  114. const int param_defaults[N_SPEECH_PARAM] = {
  115. 0, // silence (internal use)
  116. 170, // rate wpm
  117. 100, // volume
  118. 50, // pitch
  119. 50, // range
  120. 0, // punctuation
  121. 0, // capital letters
  122. 0, // wordgap
  123. 0, // options
  124. 0, // intonation
  125. 0,
  126. 0,
  127. 0, // emphasis
  128. 0, // line length
  129. 0, // voice type
  130. };
  131. #ifdef NEED_WCHAR_FUNCTIONS
  132. // additional Latin characters beyond the Latin1 character set
  133. #define MAX_WALPHA 0x233
  134. // indexed by character - 0x100
  135. // 0=not alphabetic, 0xff=lower case, other=value to add to upper case to convert to lower case
  136. static unsigned char walpha_tab[MAX_WALPHA-0xff] = {
  137. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 100
  138. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 110
  139. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 120
  140. 0xff,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, 1,0xff, 1,0xff, 1, // 130
  141. 0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, 1,0xff, 1,0xff, // 140
  142. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 150
  143. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 160
  144. 1,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, // 170
  145. 0xff, 210, 1,0xff, 1,0xff, 206, 1,0xff, 205, 205, 1,0xff,0xff, 79, 202, // 180
  146. 203, 1,0xff, 205, 207,0xff, 211, 209, 1,0xff,0xff,0xff, 211, 213,0xff, 214, // 190
  147. 1,0xff, 1,0xff, 1,0xff, 218, 1,0xff, 218,0xff,0xff, 1,0xff, 218, 1, // 1a0
  148. 0xff, 217, 217, 1,0xff, 1,0xff, 219, 1,0xff,0xff,0xff, 1,0xff,0xff,0xff, // 1b0
  149. 0xff,0xff,0xff,0xff, 2, 1,0xff, 2, 1,0xff, 2, 1,0xff, 1,0xff, 1, // 1c0
  150. 0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, // 1d0
  151. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 1e0
  152. 0xff, 2, 1,0xff, 1,0xff,0xff,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 1f0
  153. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 200
  154. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 210
  155. 0xff, 0, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 220
  156. 1,0xff, 1,0xff }; // 230
  157. // use ctype.h functions for Latin1 (character < 0x100)
  158. int iswalpha(int c)
  159. {
  160. if(c < 0x100)
  161. return(isalpha(c));
  162. if((c > 0x3040) && (c <= 0xa700))
  163. return(1); // japanese, chinese characters
  164. if(c > MAX_WALPHA)
  165. return(0);
  166. return(walpha_tab[c-0x100]);
  167. }
  168. int iswdigit(int c)
  169. {
  170. if(c < 0x100)
  171. return(isdigit(c));
  172. return(0);
  173. }
  174. int iswalnum(int c)
  175. {
  176. if(iswdigit(c))
  177. return(1);
  178. return(iswalpha(c));
  179. }
  180. int towlower(int c)
  181. {
  182. int x;
  183. if(c < 0x100)
  184. return(tolower(c));
  185. if((c > MAX_WALPHA) || ((x = walpha_tab[c-0x100])==0xff))
  186. return(c); // already lower case
  187. return(c + x); // convert to lower case
  188. }
  189. int towupper(int c)
  190. {
  191. // check whether the previous character code is the upper-case equivalent of this character
  192. if(tolower(c-1) == c)
  193. return(c-1); // yes, use it
  194. return(c); // no
  195. }
  196. int iswupper(int c)
  197. {
  198. int x;
  199. if(c < 0x100)
  200. return(isupper(c));
  201. if(((c > MAX_WALPHA) || (x = walpha_tab[c-0x100])==0) || (x == 0xff))
  202. return(0);
  203. return(1);
  204. }
  205. int iswlower(int c)
  206. {
  207. if(c < 0x100)
  208. return(islower(c));
  209. if((c > MAX_WALPHA) || (walpha_tab[c-0x100] != 0xff))
  210. return(0);
  211. return(1);
  212. }
  213. int iswspace(int c)
  214. {
  215. if(c < 0x100)
  216. return(isspace(c));
  217. return(0);
  218. }
  219. int iswpunct(int c)
  220. {
  221. if(c < 0x100)
  222. return(ispunct(c));
  223. return(0);
  224. }
  225. const wchar_t *wcschr(const wchar_t *str, int c)
  226. {
  227. while(*str != 0)
  228. {
  229. if(*str == c)
  230. return(str);
  231. str++;
  232. }
  233. return(NULL);
  234. }
  235. const int wcslen(const wchar_t *str)
  236. {
  237. int ix=0;
  238. while(*str != 0)
  239. {
  240. ix++;
  241. }
  242. return(ix);
  243. }
  244. float wcstod(const wchar_t *str, wchar_t **tailptr)
  245. {
  246. int ix;
  247. char buf[80];
  248. while(isspace(*str)) str++;
  249. for(ix=0; ix<80; ix++)
  250. {
  251. buf[ix] = str[ix];
  252. if(isspace(buf[ix]))
  253. break;
  254. }
  255. *tailptr = (wchar_t *)&str[ix];
  256. return(atof(buf));
  257. }
  258. #endif
  259. int towlower2(unsigned int c)
  260. {
  261. // check for non-standard upper to lower case conversions
  262. if(c == 'I')
  263. {
  264. if(translator->translator_name == L('t','r'))
  265. {
  266. c = 0x131; // I -> ?
  267. }
  268. }
  269. return(towlower(c));
  270. }
  271. static void GetC_unget(int c)
  272. {//==========================
  273. // This is only called with UTF8 input, not wchar input
  274. if(f_input != NULL)
  275. ungetc(c,f_input);
  276. else
  277. {
  278. p_textinput--;
  279. *p_textinput = c;
  280. end_of_input = 0;
  281. }
  282. }
  283. int Eof(void)
  284. {//==========
  285. if(ungot_char != 0)
  286. return(0);
  287. if(f_input != 0)
  288. return(feof(f_input));
  289. return(end_of_input);
  290. }
  291. static int GetC_get(void)
  292. {//======================
  293. int c;
  294. if(f_input != NULL)
  295. {
  296. c = fgetc(f_input);
  297. if(feof(f_input)) c = ' ';
  298. return(c & 0xff);
  299. }
  300. if(option_multibyte == espeakCHARS_WCHAR)
  301. {
  302. if(*p_wchar_input == 0)
  303. {
  304. end_of_input = 1;
  305. return(0);
  306. }
  307. if(!end_of_input)
  308. return(*p_wchar_input++);
  309. }
  310. else
  311. {
  312. if(*p_textinput == 0)
  313. {
  314. end_of_input = 1;
  315. return(0);
  316. }
  317. if(!end_of_input)
  318. return(*p_textinput++ & 0xff);
  319. }
  320. return(0);
  321. }
  322. static int GetC(void)
  323. {//==================
  324. // Returns a unicode wide character
  325. // Performs UTF8 checking and conversion
  326. int c;
  327. int c1;
  328. int c2;
  329. int cbuf[4];
  330. int ix;
  331. int n_bytes;
  332. unsigned char m;
  333. static int ungot2 = 0;
  334. static const unsigned char mask[4] = {0xff,0x1f,0x0f,0x07};
  335. static const unsigned char mask2[4] = {0,0x80,0x20,0x30};
  336. if((c1 = ungot_char) != 0)
  337. {
  338. ungot_char = 0;
  339. return(c1);
  340. }
  341. if(ungot2 != 0)
  342. {
  343. c1 = ungot2;
  344. ungot2 = 0;
  345. }
  346. else
  347. {
  348. c1 = GetC_get();
  349. }
  350. if(option_multibyte == espeakCHARS_WCHAR)
  351. {
  352. count_characters++;
  353. return(c1); // wchar_t text
  354. }
  355. if((option_multibyte < 2) && (c1 & 0x80))
  356. {
  357. // multi-byte utf8 encoding, convert to unicode
  358. n_bytes = 0;
  359. if(((c1 & 0xe0) == 0xc0) && ((c1 & 0x1e) != 0))
  360. n_bytes = 1;
  361. else
  362. if((c1 & 0xf0) == 0xe0)
  363. n_bytes = 2;
  364. else
  365. if(((c1 & 0xf8) == 0xf0) && ((c1 & 0x0f) <= 4))
  366. n_bytes = 3;
  367. if((ix = n_bytes) > 0)
  368. {
  369. c = c1 & mask[ix];
  370. m = mask2[ix];
  371. while(ix > 0)
  372. {
  373. if((c2 = cbuf[ix] = GetC_get()) == 0)
  374. {
  375. if(option_multibyte==espeakCHARS_AUTO)
  376. option_multibyte=espeakCHARS_8BIT; // change "auto" option to "no"
  377. GetC_unget(' ');
  378. break;
  379. }
  380. if((c2 & 0xc0) != 0x80)
  381. {
  382. // This is not UTF8. Change to 8-bit characterset.
  383. if((n_bytes == 2) && (ix == 1))
  384. ungot2 = cbuf[2];
  385. GetC_unget(c2);
  386. break;
  387. }
  388. m = 0x80;
  389. c = (c << 6) + (c2 & 0x3f);
  390. ix--;
  391. }
  392. if(ix == 0)
  393. {
  394. count_characters++;
  395. return(c);
  396. }
  397. }
  398. // top-bit-set character is not utf8, drop through to 8bit charset case
  399. if((option_multibyte==espeakCHARS_AUTO) && !Eof())
  400. option_multibyte=espeakCHARS_8BIT; // change "auto" option to "no"
  401. }
  402. // 8 bit character set, convert to unicode if
  403. count_characters++;
  404. if(c1 >= 0xa0)
  405. return(translator->charset_a0[c1-0xa0]);
  406. return(c1);
  407. } // end of GetC
  408. static void UngetC(int c)
  409. {//======================
  410. ungot_char = c;
  411. }
  412. const char *WordToString2(unsigned int word)
  413. {//========================================
  414. // Convert a language mnemonic word into a string
  415. int ix;
  416. static char buf[5];
  417. char *p;
  418. p = buf;
  419. for(ix=3; ix>=0; ix--)
  420. {
  421. if((*p = word >> (ix*8)) != 0)
  422. p++;
  423. }
  424. *p = 0;
  425. return(buf);
  426. }
  427. const char *Translator::LookupSpecial(const char *string, char* text_out)
  428. {//======================================================================
  429. unsigned int flags[2];
  430. char phonemes[55];
  431. char phonemes2[55];
  432. char *string1 = (char *)string;
  433. if(LookupDictList(&string1,phonemes,flags,0,NULL))
  434. {
  435. SetWordStress(phonemes,flags[0],-1,0);
  436. DecodePhonemes(phonemes,phonemes2);
  437. sprintf(text_out,"[[%s]]",phonemes2);
  438. option_phoneme_input = 1;
  439. return(text_out);
  440. }
  441. return(NULL);
  442. }
  443. const char *Translator::LookupCharName(int c)
  444. {//==========================================
  445. // Find the phoneme string (in ascii) to speak the name of character c
  446. // Used for punctuation characters and symbols
  447. int ix;
  448. unsigned int flags[2];
  449. char single_letter[24];
  450. char phonemes[60];
  451. char phonemes2[60];
  452. const char *lang_name = NULL;
  453. char *string;
  454. static char buf[60];
  455. buf[0] = 0;
  456. flags[0] = 0;
  457. flags[1] = 0;
  458. single_letter[0] = 0;
  459. single_letter[1] = '_';
  460. ix = utf8_out(c,&single_letter[2]);
  461. single_letter[2+ix]=0;
  462. string = &single_letter[1];
  463. if(LookupDictList(&string, phonemes, flags, 0, NULL) == 0)
  464. {
  465. // try _* then *
  466. string = &single_letter[2];
  467. if(LookupDictList(&string, phonemes, flags, 0, NULL) == 0)
  468. {
  469. // now try the rules
  470. single_letter[1] = ' ';
  471. TranslateRules(&single_letter[2], phonemes, sizeof(phonemes), NULL,0,NULL);
  472. }
  473. }
  474. if((phonemes[0] == 0) && (translator_name != L('e','n')))
  475. {
  476. // not found, try English
  477. SetTranslator2("en");
  478. string = &single_letter[1];
  479. single_letter[1] = '_';
  480. if(translator2->LookupDictList(&string, phonemes, flags, 0, NULL) == 0)
  481. {
  482. string = &single_letter[2];
  483. translator2->LookupDictList(&string, phonemes, flags, 0, NULL);
  484. }
  485. if(phonemes[0])
  486. {
  487. lang_name = "en";
  488. }
  489. else
  490. {
  491. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  492. }
  493. }
  494. if(phonemes[0])
  495. {
  496. if(lang_name)
  497. {
  498. translator2->SetWordStress(phonemes,flags[0],-1,0);
  499. DecodePhonemes(phonemes,phonemes2);
  500. sprintf(buf,"[[_^_%s %s _^_%s]]","en",phonemes2,WordToString2(translator_name));
  501. SelectPhonemeTable(voice->phoneme_tab_ix); // revert to original phoneme table
  502. }
  503. else
  504. {
  505. SetWordStress(phonemes,flags[0],-1,0);
  506. DecodePhonemes(phonemes,phonemes2);
  507. sprintf(buf,"[[%s]] ",phonemes2);
  508. }
  509. }
  510. else
  511. {
  512. strcpy(buf,"[[(X1)(X1)(X1)]]");
  513. }
  514. option_phoneme_input = 1;
  515. return(buf);
  516. }
  517. int Read4Bytes(FILE *f)
  518. {//====================
  519. // Read 4 bytes (least significant first) into a word
  520. int ix;
  521. unsigned char c;
  522. int acc=0;
  523. for(ix=0; ix<4; ix++)
  524. {
  525. c = fgetc(f) & 0xff;
  526. acc += (c << (ix*8));
  527. }
  528. return(acc);
  529. }
  530. static int LoadSoundFile(const char *fname, int index)
  531. {//===================================================
  532. FILE *f;
  533. char *p;
  534. int *ip;
  535. int length;
  536. char fname_temp[100];
  537. char fname2[sizeof(path_home)+13+40];
  538. char command[sizeof(fname2)+sizeof(fname2)+40];
  539. if(fname == NULL)
  540. {
  541. // filename is already in the table
  542. fname = soundicon_tab[index].filename;
  543. }
  544. if(fname==NULL)
  545. return(1);
  546. if(fname[0] != '/')
  547. {
  548. // a relative path, look in espeak-data/soundicons
  549. sprintf(fname2,"%s%csoundicons%c%s",path_home,PATHSEP,PATHSEP,fname);
  550. fname = fname2;
  551. }
  552. f = NULL;
  553. #ifdef PLATFORM_POSIX
  554. if((f = fopen(fname,"rb")) != NULL)
  555. {
  556. int ix;
  557. int fd_temp;
  558. const char *resample;
  559. int header[3];
  560. fseek(f,20,SEEK_SET);
  561. for(ix=0; ix<3; ix++)
  562. header[ix] = Read4Bytes(f);
  563. // if the sound file is not mono, 16 bit signed, at the correct sample rate, then convert it
  564. if((header[0] != 0x10001) || (header[1] != samplerate) || (header[2] != samplerate*2))
  565. {
  566. fclose(f);
  567. f = NULL;
  568. if(header[2] == samplerate)
  569. resample = "";
  570. else
  571. resample = "polyphase";
  572. strcpy(fname_temp,"/tmp/espeakXXXXXX");
  573. if((fd_temp = mkstemp(fname_temp)) >= 0)
  574. {
  575. close(fd_temp);
  576. // sprintf(fname_temp,"%s.wav",tmpnam(NULL));
  577. sprintf(command,"sox \"%s\" -r %d -w -s -c1 %s %s\n", fname, samplerate, fname_temp, resample);
  578. if(system(command) == 0)
  579. {
  580. fname = fname_temp;
  581. }
  582. }
  583. }
  584. }
  585. #endif
  586. if(f == NULL)
  587. {
  588. f = fopen(fname,"rb");
  589. if(f == NULL)
  590. {
  591. fprintf(stderr,"Can't read temp file: %s\n",fname);
  592. return(3);
  593. }
  594. }
  595. length = GetFileLength(fname);
  596. fseek(f,0,SEEK_SET);
  597. if((p = (char *)realloc(soundicon_tab[index].data, length)) == NULL)
  598. {
  599. fclose(f);
  600. return(4);
  601. }
  602. fread(p,length,1,f);
  603. fclose(f);
  604. remove(fname_temp);
  605. ip = (int *)(&p[40]);
  606. soundicon_tab[index].length = (*ip) / 2; // length in samples
  607. soundicon_tab[index].data = p;
  608. return(0);
  609. } // end of LoadSoundFile
  610. static int LookupSoundicon(int c)
  611. {//==============================
  612. // Find the sound icon number for a punctuation chatacter
  613. int ix;
  614. for(ix=N_SOUNDICON_SLOTS; ix<n_soundicon_tab; ix++)
  615. {
  616. if(soundicon_tab[ix].name == c)
  617. {
  618. if(soundicon_tab[ix].length == 0)
  619. {
  620. if(LoadSoundFile(NULL,ix)!=0)
  621. return(-1); // sound file is not available
  622. }
  623. return(ix);
  624. }
  625. }
  626. return(-1);
  627. }
  628. static int LoadSoundFile2(const char *fname)
  629. {//=========================================
  630. // Load a sound file into one of the reserved slots in the sound icon table
  631. // (if it'snot already loaded)
  632. int ix;
  633. static int slot = -1;
  634. for(ix=0; ix<n_soundicon_tab; ix++)
  635. {
  636. if(((soundicon_tab[ix].filename != NULL) && strcmp(fname, soundicon_tab[ix].filename) == 0))
  637. return(ix); // already loaded
  638. }
  639. // load the file into the next slot
  640. slot++;
  641. if(slot >= N_SOUNDICON_SLOTS)
  642. slot = 0;
  643. if(LoadSoundFile(fname, slot) != 0)
  644. return(-1);
  645. soundicon_tab[slot].filename = (char *)realloc(soundicon_tab[ix].filename, strlen(fname)+1);
  646. strcpy(soundicon_tab[slot].filename, fname);
  647. return(slot);
  648. }
  649. int Translator::AnnouncePunctuation(int c1, int c2, char *buf, int bufix)
  650. {//======================================================================
  651. // announce punctuation names
  652. // c1: the punctuation character
  653. // c2: the following character
  654. int punct_count;
  655. const char *punctname;
  656. int found = 0;
  657. int soundicon;
  658. char *p;
  659. if((soundicon = LookupSoundicon(c1)) >= 0)
  660. {
  661. // add an embedded command to play the soundicon
  662. sprintf(&buf[bufix],"\001%dI ",soundicon);
  663. UngetC(c2);
  664. found = 1;
  665. }
  666. else
  667. if((punctname = LookupCharName(c1)) != NULL)
  668. {
  669. found = 1;
  670. if(bufix==0)
  671. {
  672. punct_count=1;
  673. while(c2 == c1)
  674. {
  675. punct_count++;
  676. c2 = GetC();
  677. }
  678. UngetC(c2);
  679. p = &buf[bufix];
  680. if(punct_count==1)
  681. {
  682. sprintf(p,"%s %s %s",tone_punct_on,punctname,tone_punct_off);
  683. }
  684. else
  685. if(punct_count < 4)
  686. {
  687. sprintf(p,"\001+10S%s",tone_punct_on);
  688. while(punct_count-- > 0)
  689. sprintf(buf,"%s %s",buf,punctname);
  690. sprintf(p,"%s %s\001-10S",buf,tone_punct_off);
  691. }
  692. else
  693. {
  694. sprintf(p,"%s %s %d %s %s [[______]]",
  695. tone_punct_on,punctname,punct_count,punctname,tone_punct_off);
  696. option_phoneme_input = 1;
  697. }
  698. }
  699. else
  700. {
  701. // end the clause now and pick up the punctuation next time
  702. UngetC(c2);
  703. if(option_ssml)
  704. {
  705. if((c1 == '<') || (c1 == '&'))
  706. ssml_ignore_l_angle = c1; // this was &lt; which was converted to <, don't pick it up again as <
  707. }
  708. ungot_char2 = c1;
  709. buf[bufix] = ' ';
  710. buf[bufix+1] = 0;
  711. }
  712. }
  713. if(found == 0)
  714. return(-1);
  715. if(c1 == '-')
  716. return(CLAUSE_NONE); // no pause
  717. if((strchr_w(punct_close,c1) != NULL) && !iswalnum(c2))
  718. return(CLAUSE_COLON);
  719. if(iswspace(c2) && strchr_w(punct_stop,c1)!=NULL)
  720. return(punct_attributes[lookupwchar(punct_chars,c1)]);
  721. return(CLAUSE_COMMA);
  722. } // end of AnnouncePunctuation
  723. #define SSML_SPEAK 1
  724. #define SSML_VOICE 2
  725. #define SSML_PROSODY 3
  726. #define SSML_SAYAS 4
  727. #define SSML_MARK 5
  728. #define SSML_SENTENCE 6
  729. #define SSML_PARAGRAPH 7
  730. #define SSML_PHONEME 8
  731. #define SSML_SUB 9
  732. #define SSML_STYLE 10
  733. #define SSML_AUDIO 11
  734. #define SSML_EMPHASIS 12
  735. #define SSML_BREAK 13
  736. #define SSML_METADATA 14
  737. #define HTML_BREAK 15
  738. #define SSML_CLOSE 0x10 // for a closing tag, OR this with the tag type
  739. // these tags have no effect if they are self-closing, eg. <voice />
  740. static char ignore_if_self_closing[] = {0,1,1,1,1,0,0,0,0,1,1,0,1,0,1,0,0};
  741. MNEM_TAB ssmltags[] = {
  742. {"speak", SSML_SPEAK},
  743. {"voice", SSML_VOICE},
  744. {"prosody", SSML_PROSODY},
  745. {"say-as", SSML_SAYAS},
  746. {"mark", SSML_MARK},
  747. {"s", SSML_SENTENCE},
  748. {"p", SSML_PARAGRAPH},
  749. {"phoneme", SSML_PHONEME},
  750. {"sub", SSML_SUB},
  751. {"tts:style", SSML_STYLE},
  752. {"audio", SSML_AUDIO},
  753. {"emphasis", SSML_EMPHASIS},
  754. {"break", SSML_BREAK},
  755. {"metadata", SSML_METADATA},
  756. {"br", HTML_BREAK},
  757. {"li", HTML_BREAK},
  758. {"img", HTML_BREAK},
  759. {"td", HTML_BREAK},
  760. {"h1", SSML_PARAGRAPH},
  761. {"h2", SSML_PARAGRAPH},
  762. {"h3", SSML_PARAGRAPH},
  763. {"h4", SSML_PARAGRAPH},
  764. {"hr", SSML_PARAGRAPH},
  765. {NULL,0}};
  766. static const char *VoiceFromStack()
  767. {//================================
  768. // Use the voice properties from the SSML stack to choose a voice, and switch
  769. // to that voice if it's not the current voice
  770. int ix;
  771. SSML_STACK *sp;
  772. const char *v_id;
  773. int voice_name_specified;
  774. espeak_VOICE voice_select;
  775. char voice_name[40];
  776. char language[40];
  777. strcpy(voice_name,ssml_stack[0].voice_name);
  778. strcpy(language,ssml_stack[0].language);
  779. voice_select.age = ssml_stack[0].voice_age;
  780. voice_select.gender = ssml_stack[0].voice_gender;
  781. voice_select.variant = ssml_stack[0].voice_variant;
  782. voice_select.identifier = NULL;
  783. for(ix=0; ix<n_ssml_stack; ix++)
  784. {
  785. sp = &ssml_stack[ix];
  786. voice_name_specified = 0;
  787. if((sp->voice_name[0] != 0) && (SelectVoiceByName(NULL,sp->voice_name) != NULL))
  788. {
  789. voice_name_specified = 1;
  790. strcpy(voice_name, sp->voice_name);
  791. language[0] = 0;
  792. voice_select.gender = 0;
  793. voice_select.age = 0;
  794. voice_select.variant = 0;
  795. }
  796. if(sp->language[0] != 0)
  797. {
  798. strcpy(language, sp->language);
  799. if(voice_name_specified == 0)
  800. voice_name[0] = 0; // forget a previous voice name if a language is specified
  801. }
  802. if(sp->voice_gender != 0)
  803. voice_select.gender = sp->voice_gender;
  804. if(sp->voice_age != 0)
  805. voice_select.age = sp->voice_age;
  806. if(sp->voice_variant != 0)
  807. voice_select.variant = sp->voice_variant;
  808. }
  809. voice_select.name = voice_name;
  810. voice_select.languages = language;
  811. v_id = SelectVoice(&voice_select);
  812. if(v_id == NULL)
  813. return("default");
  814. return(v_id);
  815. } // end of VoiceFromStack
  816. static void ProcessParamStack(char *outbuf, int &outix)
  817. {//====================================================
  818. // Set the speech parameters from the parameter stack
  819. int param;
  820. int ix;
  821. int value;
  822. char buf[20];
  823. int new_parameters[N_SPEECH_PARAM];
  824. static char cmd_letter[N_SPEECH_PARAM] = {0, 'S','A','P','R', 0, 0, 0, 0, 0, 0, 0, 'F'}; // embedded command letters
  825. for(param=0; param<N_SPEECH_PARAM; param++)
  826. new_parameters[param] = -1;
  827. for(ix=0; ix<n_param_stack; ix++)
  828. {
  829. for(param=0; param<N_SPEECH_PARAM; param++)
  830. {
  831. if(param_stack[ix].parameter[param] >= 0)
  832. new_parameters[param] = param_stack[ix].parameter[param];
  833. }
  834. }
  835. for(param=0; param<N_SPEECH_PARAM; param++)
  836. {
  837. if((value = new_parameters[param]) != speech_parameters[param])
  838. {
  839. buf[0] = 0;
  840. switch(param)
  841. {
  842. case espeakPUNCTUATION:
  843. option_punctuation = value-1;
  844. break;
  845. case espeakCAPITALS:
  846. option_capitals = value;
  847. break;
  848. case espeakRATE:
  849. case espeakVOLUME:
  850. case espeakPITCH:
  851. case espeakRANGE:
  852. case espeakEMPHASIS:
  853. sprintf(buf,"%c%d%c",CTRL_EMBEDDED,value,cmd_letter[param]);
  854. break;
  855. }
  856. speech_parameters[param] = new_parameters[param];
  857. strcpy(&outbuf[outix],buf);
  858. outix += strlen(buf);
  859. }
  860. }
  861. } // end of ProcessParamStack
  862. static PARAM_STACK *PushParamStack(int tag_type)
  863. {//=============================================
  864. int ix;
  865. PARAM_STACK *sp;
  866. sp = &param_stack[n_param_stack];
  867. if(n_param_stack < (N_PARAM_STACK-1))
  868. n_param_stack++;
  869. sp->type = tag_type;
  870. for(ix=0; ix<N_SPEECH_PARAM; ix++)
  871. {
  872. sp->parameter[ix] = -1;
  873. }
  874. return(sp);
  875. } // end of PushParamStack
  876. static void PopParamStack(int tag_type, char *outbuf, int &outix)
  877. {//==============================================================
  878. // unwind the stack up to and including the previous tag of this type
  879. int ix;
  880. int top = 0;
  881. if(tag_type >= SSML_CLOSE)
  882. tag_type -= SSML_CLOSE;
  883. for(ix=0; ix<n_param_stack; ix++)
  884. {
  885. if(param_stack[ix].type == tag_type)
  886. {
  887. top = ix;
  888. }
  889. }
  890. if(top > 0)
  891. {
  892. n_param_stack = top;
  893. }
  894. ProcessParamStack(outbuf, outix);
  895. } // end of PopParamStack
  896. static wchar_t *GetSsmlAttribute(wchar_t *pw, const char *name)
  897. {//============================================================
  898. // Gets the value string for an attribute.
  899. // Returns NULL if the attribute is not present
  900. int ix;
  901. static wchar_t empty[1] = {0};
  902. while(*pw != 0)
  903. {
  904. if(iswspace(pw[-1]))
  905. {
  906. ix = 0;
  907. while(*pw == name[ix])
  908. {
  909. pw++;
  910. ix++;
  911. }
  912. if(name[ix]==0)
  913. {
  914. // found the attribute, now get the value
  915. while(iswspace(*pw)) pw++;
  916. if(*pw == '=') pw++;
  917. while(iswspace(*pw)) pw++;
  918. if(*pw == '"')
  919. return(pw+1);
  920. else
  921. return(empty);
  922. }
  923. }
  924. pw++;
  925. }
  926. return(NULL);
  927. } // end of GetSsmlAttribute
  928. static int attrcmp(const wchar_t *string1, const char *string2)
  929. {//============================================================
  930. int ix;
  931. if(string1 == NULL)
  932. return(1);
  933. for(ix=0; (string1[ix] == string2[ix]) && (string1[ix] != 0); ix++)
  934. {
  935. }
  936. if((string1[ix]=='"') && (string2[ix]==0))
  937. return(0);
  938. return(1);
  939. }
  940. static int attrlookup(const wchar_t *string1, const MNEM_TAB *mtab)
  941. {//================================================================
  942. int ix;
  943. for(ix=0; mtab[ix].mnem != NULL; ix++)
  944. {
  945. if(attrcmp(string1,mtab[ix].mnem) == 0)
  946. return(mtab[ix].value);
  947. }
  948. return(mtab[ix].value);
  949. }
  950. static int attrnumber(const wchar_t *pw, int default_value, int type)
  951. {//==================================================================
  952. int value = 0;
  953. if((pw == NULL) || !isdigit(*pw))
  954. return(default_value);
  955. while(isdigit(*pw))
  956. {
  957. value = value*10 + *pw++ - '0';
  958. }
  959. if((type==1) && (towlower(*pw)=='s'))
  960. {
  961. // time: seconds rather than ms
  962. value *= 1000;
  963. }
  964. return(value);
  965. } // end of attrnumber
  966. static int attrcopy_utf8(char *buf, const wchar_t *pw, int len)
  967. {//============================================================
  968. // Convert attribute string into utf8, write to buf, and return its utf8 length
  969. unsigned int c;
  970. int ix = 0;
  971. int n;
  972. int prev_c = 0;
  973. if(pw != NULL)
  974. {
  975. while((ix < (len-4)) && ((c = *pw++) != 0))
  976. {
  977. if((c=='"') && (prev_c != '\\'))
  978. break; // " indicates end of attribute, unless preceded by backstroke
  979. n = utf8_out(c,&buf[ix]);
  980. ix += n;
  981. prev_c = c;
  982. }
  983. }
  984. buf[ix] = 0;
  985. return(ix);
  986. } // end of attrcopy_utf8
  987. static int attr_prosody_value(int param_type, const wchar_t *pw, int *value_out)
  988. {//=============================================================================
  989. int sign = 0;
  990. wchar_t *tail;
  991. float value;
  992. while(iswspace(*pw)) pw++;
  993. if(*pw == '+')
  994. {
  995. pw++;
  996. sign = 1;
  997. }
  998. if(*pw == '-')
  999. {
  1000. pw++;
  1001. sign = -1;
  1002. }
  1003. value = wcstod(pw,&tail);
  1004. if(tail == pw)
  1005. {
  1006. // failed to find a number, return 100%
  1007. *value_out = 100;
  1008. return(2);
  1009. }
  1010. if(*tail == '%')
  1011. {
  1012. if(sign != 0)
  1013. value = 100 + (sign * value);
  1014. *value_out = (int)value;
  1015. return(2); // percentage
  1016. }
  1017. if((tail[0]=='s') && (tail[1]=='t'))
  1018. {
  1019. double x;
  1020. // convert from semitones to a frequency percentage
  1021. x = pow(double(2.0),double((value*sign)/12)) * 100;
  1022. *value_out = (int)x;
  1023. return(2); // percentage
  1024. }
  1025. if(param_type == espeakRATE)
  1026. {
  1027. *value_out = (int)(value * 100);
  1028. return(2); // percentage
  1029. }
  1030. *value_out = (int)value;
  1031. return(sign); // -1, 0, or 1
  1032. } // end of attr_prosody_value
  1033. int AddNameData(const char *name, int wide)
  1034. {//========================================
  1035. // Add the name to the namedata and return its position
  1036. int ix;
  1037. int len;
  1038. void *vp;
  1039. if(wide)
  1040. {
  1041. len = (wcslen((const wchar_t *)name)+1)*sizeof(wchar_t);
  1042. n_namedata = (n_namedata + sizeof(wchar_t) - 1) % sizeof(wchar_t); // round to wchar_t boundary
  1043. }
  1044. else
  1045. {
  1046. len = strlen(name)+1;
  1047. }
  1048. if(namedata_ix+len >= n_namedata)
  1049. {
  1050. // allocate more space for marker names
  1051. if((vp = realloc(namedata, namedata_ix+len + 300)) == NULL)
  1052. return(-1); // failed to allocate, original data is unchanged but ignore this new name
  1053. namedata = (char *)vp;
  1054. n_namedata = namedata_ix+len + 300;
  1055. }
  1056. memcpy(&namedata[ix = namedata_ix],name,len);
  1057. namedata_ix += len;
  1058. return(ix);
  1059. } // end of AddNameData
  1060. void SetVoiceStack(espeak_VOICE *v)
  1061. {//================================
  1062. SSML_STACK *sp;
  1063. sp = &ssml_stack[0];
  1064. if(v == NULL)
  1065. {
  1066. memset(sp,0,sizeof(ssml_stack[0]));
  1067. return;
  1068. }
  1069. if(v->languages != NULL)
  1070. strcpy(sp->language,v->languages);
  1071. if(v->name != NULL)
  1072. strcpy(sp->voice_name,v->name);
  1073. sp->voice_variant = v->variant;
  1074. sp->voice_age = v->age;
  1075. sp->voice_gender = v->gender;
  1076. }
  1077. static int GetVoiceAttributes(wchar_t *pw, int tag_type)
  1078. {//=====================================================
  1079. // Determines whether voice attribute are specified in this tag, and if so, whether this means
  1080. // a voice change.
  1081. // If it's a closing tag, delete the top frame of the stack and determine whether this implies
  1082. // a voice change.
  1083. // Returns CLAUSE_BIT_VOICE if there is a voice change
  1084. wchar_t *lang;
  1085. wchar_t *gender;
  1086. wchar_t *name;
  1087. wchar_t *age;
  1088. wchar_t *variant;
  1089. const char *new_voice_id;
  1090. SSML_STACK *ssml_sp;
  1091. static const MNEM_TAB mnem_gender[] = {
  1092. {"male", 1},
  1093. {"female", 2},
  1094. {"neutral", 3},
  1095. {NULL, 0}};
  1096. if(tag_type & SSML_CLOSE)
  1097. {
  1098. // delete a stack frame
  1099. if(n_ssml_stack > 1)
  1100. {
  1101. n_ssml_stack--;
  1102. }
  1103. }
  1104. else
  1105. {
  1106. // add a stack frame if any voice details are specified
  1107. lang = GetSsmlAttribute(pw,"xml:lang");
  1108. if(tag_type != SSML_VOICE)
  1109. {
  1110. // only expect an xml:lang attribute
  1111. name = NULL;
  1112. variant = NULL;
  1113. age = NULL;
  1114. gender = NULL;
  1115. }
  1116. else
  1117. {
  1118. name = GetSsmlAttribute(pw,"name");
  1119. variant = GetSsmlAttribute(pw,"variant");
  1120. age = GetSsmlAttribute(pw,"age");
  1121. gender = GetSsmlAttribute(pw,"gender");
  1122. }
  1123. if((tag_type != SSML_VOICE) && (lang==NULL))
  1124. return(0); // <s> or <p> without language spec, nothing to do
  1125. ssml_sp = &ssml_stack[n_ssml_stack++];
  1126. attrcopy_utf8(ssml_sp->language,lang,sizeof(ssml_sp->language));
  1127. attrcopy_utf8(ssml_sp->voice_name,name,sizeof(ssml_sp->voice_name));
  1128. ssml_sp->voice_variant = attrnumber(variant,1,0)-1;
  1129. ssml_sp->voice_age = attrnumber(age,0,0);
  1130. ssml_sp->voice_gender = attrlookup(gender,mnem_gender);
  1131. ssml_sp->tag_type = tag_type;
  1132. }
  1133. new_voice_id = VoiceFromStack();
  1134. if(strcmp(new_voice_id,current_voice_id) != 0)
  1135. {
  1136. // add an embedded command to change the voice
  1137. strcpy(current_voice_id,new_voice_id);
  1138. return(CLAUSE_BIT_VOICE); // change of voice
  1139. }
  1140. return(0);
  1141. } // end of GetVoiceAttributes
  1142. static void SetProsodyParameter(int param_type, wchar_t *attr1, PARAM_STACK *sp)
  1143. {//=============================================================================
  1144. int value;
  1145. int sign;
  1146. static const MNEM_TAB mnem_volume[] = {
  1147. {"default",100},
  1148. {"silent",0},
  1149. {"x-soft",30},
  1150. {"soft",65},
  1151. {"medium",100},
  1152. {"loud",150},
  1153. {"x-loud",230},
  1154. {NULL, -1}};
  1155. static const MNEM_TAB mnem_rate[] = {
  1156. {"default",100},
  1157. {"x-slow",60},
  1158. {"slow",80},
  1159. {"medium",100},
  1160. {"fast",120},
  1161. {"x-fast",150},
  1162. {NULL, -1}};
  1163. static const MNEM_TAB mnem_pitch[] = {
  1164. {"default",100},
  1165. {"x-low",70},
  1166. {"low",85},
  1167. {"medium",100},
  1168. {"high",110},
  1169. {"x-high",120},
  1170. {NULL, -1}};
  1171. static const MNEM_TAB mnem_range[] = {
  1172. {"default",100},
  1173. {"x-low",20},
  1174. {"low",50},
  1175. {"medium",100},
  1176. {"high",140},
  1177. {"x-high",180},
  1178. {NULL, -1}};
  1179. static const MNEM_TAB *mnem_tabs[5] = {
  1180. NULL, mnem_rate, mnem_volume, mnem_pitch, mnem_range };
  1181. if((value = attrlookup(attr1,mnem_tabs[param_type])) >= 0)
  1182. {
  1183. // mnemonic specifies a value as a percentage of the base pitch/range/rate/volume
  1184. sp->parameter[param_type] = (param_stack[0].parameter[param_type] * value)/100;
  1185. }
  1186. else
  1187. {
  1188. sign = attr_prosody_value(param_type,attr1,&value);
  1189. if(sign == 0)
  1190. sp->parameter[param_type] = value; // absolute value in Hz
  1191. else
  1192. if(sign == 2)
  1193. {
  1194. // change specified as percentage or in semitones
  1195. sp->parameter[param_type] = (speech_parameters[param_type] * value)/100;
  1196. }
  1197. else
  1198. {
  1199. // change specified as plus or minus Hz
  1200. sp->parameter[param_type] = speech_parameters[param_type] + (value*sign);
  1201. }
  1202. }
  1203. } // end of SetProsodyParemeter
  1204. static int ProcessSsmlTag(wchar_t *xml_buf, char *outbuf, int &outix, int n_outbuf, int self_closing)
  1205. {//==================================================================================================
  1206. // xml_buf is the tag and attributes with a zero terminator in place of the original '>'
  1207. // returns a clause terminator value.
  1208. unsigned int ix;
  1209. int index;
  1210. int c;
  1211. int tag_type;
  1212. int value;
  1213. int value2;
  1214. int value3;
  1215. int voice_change_flag;
  1216. wchar_t *px;
  1217. wchar_t *attr1;
  1218. wchar_t *attr2;
  1219. wchar_t *attr3;
  1220. int terminator;
  1221. char *uri;
  1222. int param_type;
  1223. char tag_name[40];
  1224. char buf[80];
  1225. PARAM_STACK *sp;
  1226. SSML_STACK *ssml_sp;
  1227. static const MNEM_TAB mnem_punct[] = {
  1228. {"none", 1},
  1229. {"all", 2},
  1230. {"some", 3},
  1231. {NULL, -1}};
  1232. static const MNEM_TAB mnem_capitals[] = {
  1233. {"no", 0},
  1234. {"spelling", 2},
  1235. {"icon", 1},
  1236. {"pitch", 20}, // this is the amount by which to raise the pitch
  1237. {NULL, -1}};
  1238. static const MNEM_TAB mnem_interpret_as[] = {
  1239. {"characters",SAYAS_CHARS},
  1240. {"tts:char",SAYAS_SINGLE_CHARS},
  1241. {"tts:key",SAYAS_KEY},
  1242. {"tts:digits",SAYAS_DIGITS},
  1243. {"telephone",SAYAS_DIGITS1},
  1244. {NULL, -1}};
  1245. static const MNEM_TAB mnem_sayas_format[] = {
  1246. {"glyphs",1},
  1247. {NULL, -1}};
  1248. static const MNEM_TAB mnem_break[] = {
  1249. {"none",0},
  1250. {"x-weak",1},
  1251. {"weak",2},
  1252. {"medium",3},
  1253. {"strong",4},
  1254. {"x-strong",5},
  1255. {NULL,-1}};
  1256. static const MNEM_TAB mnem_emphasis[] = {
  1257. {"none",1},
  1258. {"reduced",2},
  1259. {"moderate",3},
  1260. {"strong",4},
  1261. {NULL,-1}};
  1262. static const char *prosody_attr[5] = {
  1263. NULL, "rate", "volume", "pitch", "range" };
  1264. for(ix=0; ix<(sizeof(tag_name)-1); ix++)
  1265. {
  1266. if(((c = xml_buf[ix]) == 0) || iswspace(c))
  1267. break;
  1268. tag_name[ix] = tolower((char)c);
  1269. }
  1270. tag_name[ix] = 0;
  1271. px = &xml_buf[ix]; // the tag's attributes
  1272. if(tag_name[0] == '/')
  1273. {
  1274. tag_type = LookupMnem(ssmltags,&tag_name[1]) + SSML_CLOSE; // closing tag
  1275. }
  1276. else
  1277. {
  1278. tag_type = LookupMnem(ssmltags,tag_name);
  1279. if(self_closing && ignore_if_self_closing[tag_type])
  1280. return(0);
  1281. }
  1282. voice_change_flag = 0;
  1283. terminator = CLAUSE_NONE;
  1284. ssml_sp = &ssml_stack[n_ssml_stack-1];
  1285. switch(tag_type)
  1286. {
  1287. case SSML_STYLE:
  1288. sp = PushParamStack(tag_type);
  1289. attr1 = GetSsmlAttribute(px,"field");
  1290. attr2 = GetSsmlAttribute(px,"mode");
  1291. if(attrcmp(attr1,"punctuation")==0)
  1292. {
  1293. value = attrlookup(attr2,mnem_punct);
  1294. sp->parameter[espeakPUNCTUATION] = value;
  1295. }
  1296. else
  1297. if(attrcmp(attr1,"capital_letters")==0)
  1298. {
  1299. value = attrlookup(attr2,mnem_capitals);
  1300. sp->parameter[espeakCAPITALS] = value;
  1301. }
  1302. ProcessParamStack(outbuf, outix);
  1303. break;
  1304. case SSML_PROSODY:
  1305. sp = PushParamStack(tag_type);
  1306. // look for attributes: rate, volume, pitch, range
  1307. for(param_type=espeakRATE; param_type <= espeakRANGE; param_type++)
  1308. {
  1309. if((attr1 = GetSsmlAttribute(px,prosody_attr[param_type])) != NULL)
  1310. {
  1311. SetProsodyParameter(param_type, attr1, sp);
  1312. }
  1313. }
  1314. ProcessParamStack(outbuf, outix);
  1315. break;
  1316. case SSML_EMPHASIS:
  1317. sp = PushParamStack(tag_type);
  1318. value = 3; // default is "moderate"
  1319. if((attr1 = GetSsmlAttribute(px,"level")) != NULL)
  1320. {
  1321. value = attrlookup(attr1,mnem_emphasis);
  1322. }
  1323. if(translator->langopts.tone_language == 1)
  1324. {
  1325. static unsigned char emphasis_to_pitch_range[] = {50,50,40,70,90,90};
  1326. static unsigned char emphasis_to_volume[] = {100,100,70,110,140,140};
  1327. // tone language (eg.Chinese) do emphasis by increasing the pitch range.
  1328. sp->parameter[espeakRANGE] = emphasis_to_pitch_range[value];
  1329. sp->parameter[espeakVOLUME] = emphasis_to_volume[value];
  1330. }
  1331. else
  1332. {
  1333. sp->parameter[espeakEMPHASIS] = value;
  1334. }
  1335. ProcessParamStack(outbuf, outix);
  1336. break;
  1337. case SSML_STYLE + SSML_CLOSE:
  1338. case SSML_PROSODY + SSML_CLOSE:
  1339. case SSML_EMPHASIS + SSML_CLOSE:
  1340. PopParamStack(tag_type, outbuf, outix);
  1341. break;
  1342. case SSML_SAYAS:
  1343. attr1 = GetSsmlAttribute(px,"interpret-as");
  1344. attr2 = GetSsmlAttribute(px,"format");
  1345. attr3 = GetSsmlAttribute(px,"detail");
  1346. value = attrlookup(attr1,mnem_interpret_as);
  1347. value2 = attrlookup(attr2,mnem_sayas_format);
  1348. if(value2 == 1)
  1349. value = SAYAS_GLYPHS;
  1350. value3 = attrnumber(attr3,0,0);
  1351. if(value == SAYAS_DIGITS)
  1352. {
  1353. if(value3 <= 1)
  1354. value = SAYAS_DIGITS1;
  1355. else
  1356. value = SAYAS_DIGITS + value3;
  1357. }
  1358. sprintf(buf,"%c%dY",CTRL_EMBEDDED,value);
  1359. strcpy(&outbuf[outix],buf);
  1360. outix += strlen(buf);
  1361. sayas_mode = value; // punctuation doesn't end clause during SAY-AS
  1362. break;
  1363. case SSML_SAYAS + SSML_CLOSE:
  1364. outbuf[outix++] = CTRL_EMBEDDED;
  1365. outbuf[outix++] = 'Y';
  1366. sayas_mode = 0;
  1367. break;
  1368. case SSML_SUB:
  1369. if((attr1 = GetSsmlAttribute(px,"alias")) != NULL)
  1370. {
  1371. // use the alias rather than the text
  1372. ignore_text = 1;
  1373. outix += attrcopy_utf8(&outbuf[outix],attr1,n_outbuf-outix);
  1374. }
  1375. break;
  1376. case SSML_METADATA:
  1377. ignore_text = 1;
  1378. break;
  1379. case SSML_SUB + SSML_CLOSE:
  1380. case SSML_METADATA + SSML_CLOSE:
  1381. ignore_text = 0;
  1382. break;
  1383. case SSML_MARK:
  1384. if((attr1 = GetSsmlAttribute(px,"name")) != NULL)
  1385. {
  1386. // add name to circular buffer of marker names
  1387. attrcopy_utf8(buf,attr1,sizeof(buf));
  1388. if(strcmp(skip_marker,buf)==0)
  1389. {
  1390. // This is the marker we are waiting for before starting to speak
  1391. clear_skipping_text = 1;
  1392. skip_marker[0] = 0;
  1393. return(CLAUSE_NONE);
  1394. }
  1395. if((index = AddNameData(buf,0)) >= 0)
  1396. {
  1397. sprintf(buf,"%c%dM",CTRL_EMBEDDED,index);
  1398. strcpy(&outbuf[outix],buf);
  1399. outix += strlen(buf);
  1400. }
  1401. }
  1402. break;
  1403. case SSML_AUDIO:
  1404. sp = PushParamStack(tag_type);
  1405. if((attr1 = GetSsmlAttribute(px,"src")) != NULL)
  1406. {
  1407. char fname[256];
  1408. attrcopy_utf8(buf,attr1,sizeof(buf));
  1409. if(uri_callback == NULL)
  1410. {
  1411. if((xmlbase != NULL) && (buf[0] != '/'))
  1412. {
  1413. sprintf(fname,"%s/%s",xmlbase,buf);
  1414. index = LoadSoundFile2(fname);
  1415. }
  1416. else
  1417. {
  1418. index = LoadSoundFile2(buf);
  1419. }
  1420. if(index >= 0)
  1421. {
  1422. sprintf(buf,"%c%dI",CTRL_EMBEDDED,index);
  1423. strcpy(&outbuf[outix],buf);
  1424. outix += strlen(buf);
  1425. sp->parameter[espeakSILENCE] = 1;
  1426. }
  1427. }
  1428. else
  1429. {
  1430. if((index = AddNameData(buf,0)) >= 0)
  1431. {
  1432. uri = &namedata[index];
  1433. if(uri_callback(1,uri,xmlbase) == 0)
  1434. {
  1435. sprintf(buf,"%c%dU",CTRL_EMBEDDED,index);
  1436. strcpy(&outbuf[outix],buf);
  1437. outix += strlen(buf);
  1438. sp->parameter[espeakSILENCE] = 1;
  1439. }
  1440. }
  1441. }
  1442. }
  1443. ProcessParamStack(outbuf, outix);
  1444. if(self_closing)
  1445. PopParamStack(tag_type, outbuf, outix);
  1446. return(CLAUSE_NONE);
  1447. case SSML_AUDIO + SSML_CLOSE:
  1448. PopParamStack(tag_type, outbuf, outix);
  1449. return(CLAUSE_NONE);
  1450. case SSML_BREAK:
  1451. value = 21;
  1452. terminator = CLAUSE_NONE;
  1453. if((attr1 = GetSsmlAttribute(px,"strength")) != NULL)
  1454. {
  1455. static int break_value[6] = {0,7,14,21,40,80}; // *10mS
  1456. value = attrlookup(attr1,mnem_break);
  1457. if(value < 3)
  1458. {
  1459. // adjust prepause on the following word
  1460. sprintf(&outbuf[outix],"%c%dB",CTRL_EMBEDDED,value);
  1461. outix += 3;
  1462. terminator = 0;
  1463. }
  1464. value = break_value[value];
  1465. }
  1466. if((attr2 = GetSsmlAttribute(px,"time")) != NULL)
  1467. {
  1468. value = (attrnumber(attr2,0,1) * 25) / speed_factor1; // compensate for speaking speed to keep constant pause length
  1469. if(terminator == 0)
  1470. terminator = CLAUSE_NONE;
  1471. }
  1472. if(terminator)
  1473. {
  1474. if(value > 0xfff)
  1475. value = 0xfff;
  1476. return(terminator + value);
  1477. }
  1478. break;
  1479. case SSML_SPEAK:
  1480. if((attr1 = GetSsmlAttribute(px,"xml:base")) != NULL)
  1481. {
  1482. attrcopy_utf8(buf,attr1,sizeof(buf));
  1483. if((index = AddNameData(buf,0)) >= 0)
  1484. {
  1485. xmlbase = &namedata[index];
  1486. }
  1487. }
  1488. if(GetVoiceAttributes(px, tag_type) == 0)
  1489. return(0); // no voice change
  1490. return(CLAUSE_VOICE);
  1491. case SSML_VOICE:
  1492. if(GetVoiceAttributes(px, tag_type) == 0)
  1493. return(0); // no voice change
  1494. return(CLAUSE_VOICE);
  1495. case SSML_SPEAK + SSML_CLOSE:
  1496. // unwind stack until the previous <voice> or <speak> tag
  1497. while((n_ssml_stack > 1) && (ssml_stack[n_ssml_stack-1].tag_type != SSML_SPEAK))
  1498. {
  1499. n_ssml_stack--;
  1500. }
  1501. return(CLAUSE_PERIOD + GetVoiceAttributes(px, tag_type));
  1502. case SSML_VOICE + SSML_CLOSE:
  1503. // unwind stack until the previous <voice> or <speak> tag
  1504. while((n_ssml_stack > 1) && (ssml_stack[n_ssml_stack-1].tag_type != SSML_VOICE))
  1505. {
  1506. n_ssml_stack--;
  1507. }
  1508. terminator=0; // ?? Sentence intonation, but no pause ??
  1509. return(terminator + GetVoiceAttributes(px, tag_type));
  1510. case HTML_BREAK:
  1511. case HTML_BREAK + SSML_CLOSE:
  1512. return(CLAUSE_COLON);
  1513. case SSML_SENTENCE:
  1514. if(ssml_sp->tag_type == SSML_SENTENCE)
  1515. {
  1516. // new sentence implies end-of-sentence
  1517. voice_change_flag = GetVoiceAttributes(px, SSML_SENTENCE+SSML_CLOSE);
  1518. }
  1519. voice_change_flag |= GetVoiceAttributes(px, tag_type);
  1520. return(CLAUSE_PARAGRAPH + voice_change_flag);
  1521. case SSML_PARAGRAPH:
  1522. if(ssml_sp->tag_type == SSML_SENTENCE)
  1523. {
  1524. // new paragraph implies end-of-sentence or end-of-paragraph
  1525. voice_change_flag = GetVoiceAttributes(px, SSML_SENTENCE+SSML_CLOSE);
  1526. }
  1527. if(ssml_sp->tag_type == SSML_PARAGRAPH)
  1528. {
  1529. // new paragraph implies end-of-sentence or end-of-paragraph
  1530. voice_change_flag |= GetVoiceAttributes(px, SSML_PARAGRAPH+SSML_CLOSE);
  1531. }
  1532. voice_change_flag |= GetVoiceAttributes(px, tag_type);
  1533. return(CLAUSE_PARAGRAPH + voice_change_flag);
  1534. case SSML_SENTENCE + SSML_CLOSE:
  1535. if(ssml_sp->tag_type == SSML_SENTENCE)
  1536. {
  1537. // end of a sentence which specified a language
  1538. voice_change_flag = GetVoiceAttributes(px, tag_type);
  1539. }
  1540. return(CLAUSE_PERIOD + voice_change_flag);
  1541. case SSML_PARAGRAPH + SSML_CLOSE:
  1542. if((ssml_sp->tag_type == SSML_SENTENCE) || (ssml_sp->tag_type == SSML_PARAGRAPH))
  1543. {
  1544. // End of a paragraph which specified a language.
  1545. // (End-of-paragraph also implies end-of-sentence)
  1546. return(GetVoiceAttributes(px, tag_type) + CLAUSE_PARAGRAPH);
  1547. }
  1548. return(CLAUSE_PARAGRAPH);
  1549. }
  1550. return(0);
  1551. } // end of ProcessSsmlTag
  1552. MNEM_TAB xml_char_mnemonics[] = {
  1553. {"gt",'>'},
  1554. {"lt",'<'},
  1555. {"amp", '&'},
  1556. {"quot", '"'},
  1557. {"nbsp", ' '},
  1558. {"apos", '\''},
  1559. {NULL,-1}};
  1560. int Translator::ReadClause(FILE *f_in, char *buf, short *charix, int n_buf)
  1561. {//========================================================================
  1562. /* Find the end of the current clause.
  1563. Write the clause into buf
  1564. returns: clause type (bits 0-7: pause x10mS, bits 8-11 intonation type)
  1565. Also checks for blank line (paragraph) as end-of-clause indicator.
  1566. Does not end clause for:
  1567. punctuation immediately followed by alphanumeric eg. 1.23 !Speak :path
  1568. repeated punctuation, eg. ... !!!
  1569. */
  1570. int c1=' '; // current character
  1571. int c2; // next character
  1572. int cprev=' '; // previous character
  1573. int parag;
  1574. int ix = 0;
  1575. int j;
  1576. int nl_count;
  1577. int linelength = 0;
  1578. int phoneme_mode = 0;
  1579. int n_xml_buf;
  1580. int terminator;
  1581. int punct;
  1582. int found;
  1583. int any_alnum = 0;
  1584. int self_closing;
  1585. int punct_data;
  1586. const char *p;
  1587. wchar_t xml_buf[N_XML_BUF+1];
  1588. #define N_XML_BUF2 20
  1589. char xml_buf2[N_XML_BUF2+2]; // for &<name> and &<number> sequences
  1590. static char ungot_string[N_XML_BUF2+4];
  1591. static int ungot_string_ix = -1;
  1592. if(clear_skipping_text)
  1593. {
  1594. skipping_text = 0;
  1595. clear_skipping_text = 0;
  1596. }
  1597. clause_upper_count = 0;
  1598. clause_lower_count = 0;
  1599. end_of_input = 0;
  1600. f_input = f_in; // for GetC etc
  1601. if(ungot_word != NULL)
  1602. {
  1603. strcpy(buf,ungot_word);
  1604. ix += strlen(ungot_word);
  1605. ungot_word = NULL;
  1606. }
  1607. if(ungot_char2 != 0)
  1608. {
  1609. c2 = ungot_char2;
  1610. }
  1611. else
  1612. {
  1613. c2 = GetC();
  1614. }
  1615. while(!Eof() || (ungot_char != 0) || (ungot_char2 != 0) || (ungot_string_ix >= 0))
  1616. {
  1617. if(!iswalnum(c1))
  1618. {
  1619. if((end_character_position > 0) && (count_characters > end_character_position))
  1620. {
  1621. end_of_input = 1;
  1622. return(CLAUSE_EOF);
  1623. }
  1624. if((skip_characters > 0) && (count_characters > skip_characters))
  1625. {
  1626. // reached the specified start position
  1627. // don't break a word
  1628. clear_skipping_text = 1;
  1629. skip_characters = 0;
  1630. UngetC(c2);
  1631. return(CLAUSE_NONE);
  1632. }
  1633. }
  1634. cprev = c1;
  1635. c1 = c2;
  1636. if(ungot_string_ix >= 0)
  1637. {
  1638. if(ungot_string[ungot_string_ix] == 0)
  1639. ungot_string_ix = -1;
  1640. }
  1641. if((ungot_string_ix == 0) && (ungot_char2 == 0))
  1642. {
  1643. c1 = ungot_string[ungot_string_ix++];
  1644. }
  1645. if(ungot_string_ix >= 0)
  1646. {
  1647. c2 = ungot_string[ungot_string_ix++];
  1648. }
  1649. else
  1650. {
  1651. c2 = GetC();
  1652. if(Eof())
  1653. {
  1654. c2 = ' ';
  1655. }
  1656. }
  1657. ungot_char2 = 0;
  1658. if((option_ssml) && (phoneme_mode==0))
  1659. {
  1660. if((ssml_ignore_l_angle != '&') && (c1 == '&') && ((c2=='#') || ((c2 >= 'a') && (c2 <= 'z'))))
  1661. {
  1662. n_xml_buf = 0;
  1663. c1 = c2;
  1664. while(!Eof() && (iswalnum(c1) || (c1=='#')) && (n_xml_buf < N_XML_BUF2))
  1665. {
  1666. xml_buf2[n_xml_buf++] = c1;
  1667. c1 = GetC();
  1668. }
  1669. xml_buf2[n_xml_buf] = 0;
  1670. c2 = GetC();
  1671. sprintf(ungot_string,"%s%c%c",&xml_buf2[0],c1,c2);
  1672. if(c1 == ';')
  1673. {
  1674. if(xml_buf2[0] == '#')
  1675. {
  1676. // character code number
  1677. if(xml_buf2[1] == 'x')
  1678. found = sscanf(&xml_buf2[2],"%x",(unsigned int *)(&c1));
  1679. else
  1680. found = sscanf(&xml_buf2[1],"%d",&c1);
  1681. }
  1682. else
  1683. {
  1684. if((found = LookupMnem(xml_char_mnemonics,xml_buf2)) != -1)
  1685. {
  1686. c1 = found;
  1687. if(c2 == 0)
  1688. c2 = ' ';
  1689. }
  1690. }
  1691. }
  1692. else
  1693. {
  1694. found = -1;
  1695. }
  1696. if(found <= 0)
  1697. {
  1698. ungot_string_ix = 0;
  1699. c1 = '&';
  1700. c2 = ' ';
  1701. }
  1702. if((c1 <= 0x20) && ((sayas_mode == SAYAS_SINGLE_CHARS) || (sayas_mode == SAYAS_KEY)))
  1703. {
  1704. c1 += 0xe000; // move into unicode private usage area
  1705. }
  1706. }
  1707. else
  1708. if((c1 == '<') && (ssml_ignore_l_angle != '<'))
  1709. {
  1710. // SSML Tag
  1711. n_xml_buf = 0;
  1712. c1 = c2;
  1713. while(!Eof() && (c1 != '>') && (n_xml_buf < N_XML_BUF))
  1714. {
  1715. xml_buf[n_xml_buf++] = c1;
  1716. c1 = GetC();
  1717. }
  1718. xml_buf[n_xml_buf] = 0;
  1719. c2 = ' ';
  1720. buf[ix++] = ' ';
  1721. self_closing = 0;
  1722. if(xml_buf[n_xml_buf-1] == '/')
  1723. {
  1724. // a self-closing tag
  1725. xml_buf[n_xml_buf-1] = ' ';
  1726. self_closing = 1;
  1727. }
  1728. terminator = ProcessSsmlTag(xml_buf,buf,ix,n_buf,self_closing);
  1729. if(terminator != 0)
  1730. {
  1731. buf[ix] = ' ';
  1732. buf[ix++] = 0;
  1733. if(terminator & CLAUSE_BIT_VOICE)
  1734. {
  1735. // a change in voice, write the new voice name to the end of the buf
  1736. p = current_voice_id;
  1737. while((*p != 0) && (ix < (n_buf-1)))
  1738. {
  1739. buf[ix++] = *p++;
  1740. }
  1741. buf[ix++] = 0;
  1742. }
  1743. return(terminator);
  1744. }
  1745. continue;
  1746. }
  1747. }
  1748. ssml_ignore_l_angle=0;
  1749. if(ignore_text)
  1750. continue;
  1751. if((c2=='\n') && (option_linelength == -1))
  1752. {
  1753. // single-line mode, return immediately on NL
  1754. if((punct = lookupwchar(punct_chars,c1)) == 0)
  1755. {
  1756. charix[ix] = count_characters - clause_start_char;
  1757. ix += utf8_out(c1,&buf[ix]);
  1758. terminator = CLAUSE_PERIOD; // line doesn't end in punctuation, assume period
  1759. }
  1760. else
  1761. {
  1762. terminator = punct_attributes[punct];
  1763. }
  1764. buf[ix] = ' ';
  1765. buf[ix+1] = 0;
  1766. return(terminator);
  1767. }
  1768. if((c1 == CTRL_EMBEDDED) || (c1 == ctrl_embedded))
  1769. {
  1770. // an embedded command. If it's a voice change, end the clause
  1771. if(c2 == 'V')
  1772. {
  1773. buf[ix++] = 0; // end the clause at this point
  1774. while(!iswspace(c1 = GetC()) && !Eof() && (ix < (n_buf-1)))
  1775. buf[ix++] = c1; // add voice name to end of buffer, after the text
  1776. buf[ix++] = 0;
  1777. return(CLAUSE_VOICE);
  1778. }
  1779. else
  1780. if(c2 == 'B')
  1781. {
  1782. // set the punctuation option from an embedded command
  1783. // B0 B1 B<punct list><space>
  1784. strcpy(&buf[ix]," ");
  1785. ix += 3;
  1786. if((c2 = GetC()) == '0')
  1787. option_punctuation = 0;
  1788. else
  1789. {
  1790. option_punctuation = 1;
  1791. option_punctlist[0] = 0;
  1792. if(c2 != '1')
  1793. {
  1794. // a list of punctuation characters to be spoken, terminated by space
  1795. j = 0;
  1796. while(!iswspace(c2) && !Eof())
  1797. {
  1798. option_punctlist[j++] = c2;
  1799. c2 = GetC();
  1800. buf[ix++] = ' ';
  1801. }
  1802. option_punctlist[j] = 0; // terminate punctuation list
  1803. option_punctuation = 2;
  1804. }
  1805. }
  1806. c2 = GetC();
  1807. continue;
  1808. }
  1809. }
  1810. linelength++;
  1811. if(iswalnum(c1))
  1812. any_alnum = 1;
  1813. else
  1814. if(iswspace(c1))
  1815. {
  1816. char *p_word;
  1817. if(translator_name == 0x6a626f)
  1818. {
  1819. // language jbo : lojban
  1820. // treat "i" or ".i" as end-of-sentence
  1821. p_word = &buf[ix-1];
  1822. if(p_word[0] == 'i')
  1823. {
  1824. if(p_word[-1] == '.')
  1825. p_word--;
  1826. if(p_word[-1] == ' ')
  1827. {
  1828. ungot_word = "i ";
  1829. UngetC(c2);
  1830. p_word[0] = 0;
  1831. return(CLAUSE_PERIOD);
  1832. }
  1833. }
  1834. }
  1835. }
  1836. if(iswupper(c1))
  1837. {
  1838. clause_upper_count++;
  1839. if((option_capitals == 2) && (sayas_mode == 0) && !iswupper(cprev))
  1840. {
  1841. char text_buf[40];
  1842. char text_buf2[30];
  1843. if(LookupSpecial("_cap",text_buf2) != NULL)
  1844. {
  1845. sprintf(text_buf,"%s%s%s",tone_punct_on,text_buf2,tone_punct_off);
  1846. j = strlen(text_buf);
  1847. if((ix + j) < n_buf)
  1848. {
  1849. strcpy(&buf[ix],text_buf);
  1850. ix += j;
  1851. }
  1852. }
  1853. }
  1854. }
  1855. else
  1856. if(iswalpha(c1))
  1857. clause_lower_count++;
  1858. if(option_phoneme_input)
  1859. {
  1860. if(phoneme_mode > 0)
  1861. phoneme_mode--;
  1862. else
  1863. if((c1 == '[') && (c2 == '['))
  1864. phoneme_mode = -1; // input is phoneme mnemonics, so don't look for punctuation
  1865. else
  1866. if((c1 == ']') && (c2 == ']'))
  1867. phoneme_mode = 2; // set phoneme_mode to zero after the next two characters
  1868. }
  1869. if(c1 == '\n')
  1870. {
  1871. parag = 0;
  1872. // count consecutive newlines, ignoring other spaces
  1873. while(!Eof() && iswspace(c2))
  1874. {
  1875. if(c2 == '\n')
  1876. parag++;
  1877. c2 = GetC();
  1878. }
  1879. if(parag > 0)
  1880. {
  1881. // 2nd newline, assume paragraph
  1882. UngetC(c2);
  1883. buf[ix] = ' ';
  1884. buf[ix+1] = 0;
  1885. if(parag > 3)
  1886. parag = 3;
  1887. if(option_ssml) parag=1;
  1888. return((CLAUSE_PARAGRAPH-30) + 30*parag); // several blank lines, longer pause
  1889. }
  1890. if(linelength <= option_linelength)
  1891. {
  1892. // treat lines shorter than a specified length as end-of-clause
  1893. UngetC(c2);
  1894. buf[ix] = ' ';
  1895. buf[ix+1] = 0;
  1896. return(CLAUSE_COLON);
  1897. }
  1898. linelength = 0;
  1899. }
  1900. if(option_punctuation && (phoneme_mode==0) && (sayas_mode==0) && iswpunct(c1))
  1901. {
  1902. // option is set to explicitly speak punctuation characters
  1903. // if a list of allowed punctuation has been set up, check whether the character is in it
  1904. if((option_punctuation == 1) || (wcschr(option_punctlist,c1) != NULL))
  1905. {
  1906. if((terminator = AnnouncePunctuation(c1, c2, buf, ix)) >= 0)
  1907. return(terminator);
  1908. }
  1909. }
  1910. if((phoneme_mode==0) && (sayas_mode==0) && ((punct = lookupwchar(punct_chars,c1)) != 0))
  1911. {
  1912. if((iswspace(c2) || (punct_attributes[punct] & 0x8000) || IsBracket(c2) || (c2=='?') || (c2=='-') || Eof()))
  1913. {
  1914. // note: (c2='?') is for when a smart-quote has been replaced by '?'
  1915. buf[ix] = ' ';
  1916. buf[ix+1] = 0;
  1917. if((c1 == '.') && (cprev == '.'))
  1918. {
  1919. c1 = 0x2026;
  1920. punct = 9; // elipsis
  1921. }
  1922. nl_count = 0;
  1923. while(!Eof() && iswspace(c2))
  1924. {
  1925. if(c2 == '\n')
  1926. nl_count++;
  1927. c2 = GetC(); // skip past space(s)
  1928. }
  1929. if(!Eof())
  1930. {
  1931. UngetC(c2);
  1932. }
  1933. if((nl_count==0) && (c1 == '.'))
  1934. {
  1935. if(iswdigit(cprev) && (langopts.numbers & 0x10000))
  1936. {
  1937. // dot after a number indicates an ordinal number
  1938. c2 = ' ';
  1939. continue;
  1940. }
  1941. if(iswlower(c2))
  1942. {
  1943. c2 = ' ';
  1944. continue; // next word has no capital letter, this dot is probably from an abbreviation
  1945. }
  1946. if(any_alnum==0)
  1947. {
  1948. c2 = ' '; // no letters or digits yet, so probably not a sentence terminator
  1949. continue;
  1950. }
  1951. }
  1952. punct_data = punct_attributes[punct];
  1953. if(nl_count > 1)
  1954. {
  1955. if((punct_data == CLAUSE_QUESTION) || (punct_data == CLAUSE_EXCLAMATION))
  1956. return(punct_data + 35); // with a longer pause
  1957. return(CLAUSE_PARAGRAPH);
  1958. }
  1959. return(punct_data); // only recognise punctuation if followed by a blank or bracket/quote
  1960. }
  1961. }
  1962. if(speech_parameters[espeakSILENCE]==1)
  1963. continue;
  1964. j = ix+1;
  1965. ix += utf8_out(c1,&buf[ix]); // buf[ix++] = c1;
  1966. if(!iswspace(c1) && !IsBracket(c1))
  1967. {
  1968. charix[ix] = count_characters - clause_start_char;
  1969. while(j < ix)
  1970. charix[j++] = -1; // subsequent bytes of a multibyte character
  1971. }
  1972. if(((ix > (n_buf-20)) && !IsAlpha(c1) && !iswdigit(c1)) || (ix >= (n_buf-2)))
  1973. {
  1974. // clause too long, getting near end of buffer, so break here
  1975. // try to break at a word boundary (unless we actually reach the end of buffer).
  1976. buf[ix] = ' ';
  1977. buf[ix+1] = 0;
  1978. UngetC(c2);
  1979. return(CLAUSE_NONE);
  1980. }
  1981. }
  1982. buf[ix] = ' ';
  1983. buf[ix+1] = 0;
  1984. return(CLAUSE_EOF); // end of file
  1985. } // end of ReadClause
  1986. void InitNamedata(void)
  1987. {//====================
  1988. namedata_ix = 0;
  1989. if(namedata != NULL)
  1990. {
  1991. free(namedata);
  1992. namedata = NULL;
  1993. n_namedata = 0;
  1994. }
  1995. }
  1996. void InitText2(void)
  1997. {//=================
  1998. int param;
  1999. n_ssml_stack =1;
  2000. n_param_stack = 1;
  2001. ssml_stack[0].tag_type = 0;
  2002. for(param=0; param<N_SPEECH_PARAM; param++)
  2003. speech_parameters[param] = param_stack[0].parameter[param]; // set all speech parameters to defaults
  2004. option_punctuation = speech_parameters[espeakPUNCTUATION];
  2005. option_capitals = speech_parameters[espeakCAPITALS];
  2006. current_voice_id[0] = 0;
  2007. ignore_text = 0;
  2008. clear_skipping_text = 0;
  2009. count_characters = -1;
  2010. sayas_mode = 0;
  2011. xmlbase = NULL;
  2012. }