eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

speak_riscos 26KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. /**************************************************************************
  2. * Copyright (C) 2005,2006 by Jonathan Duddington *
  3. * [email protected] *
  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 2 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 to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #define USE_MODULE
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include <locale.h>
  26. #include "kernel.h"
  27. #include "speech.h"
  28. #include "speak_lib.h"
  29. #include "voice.h"
  30. #include "phoneme.h"
  31. #include "synthesize.h"
  32. #include "translate.h"
  33. #define os_X 0x20000
  34. // interface to the assembler section
  35. extern "C" {
  36. extern void DMA_Handler(void);
  37. // used from the cmhgfile
  38. extern _kernel_oserror *user_init(char *cmd_fail, int podule_base, void *pw);
  39. extern _kernel_oserror *swi_handler(int swi_no, int *r, void *pw);
  40. extern _kernel_oserror *command_handler(char *arg_string, int argc, int cmd_no, void *pw);
  41. extern int callback_handler(_kernel_swi_regs *r, void *pw);
  42. extern int callback_entry(_kernel_swi_regs *r, void *pw);
  43. extern int sound_handler(_kernel_swi_regs *r, void *pw);
  44. extern int sound_entry(_kernel_swi_regs *r, void *pw);
  45. }
  46. extern int Generate(PHONEME_LIST *phoneme_list, int *n_ph, int resume);
  47. extern void RiscosOpenSound();
  48. extern int WcmdqUsed();
  49. extern void FreePhData();
  50. extern void FreeDictionary();
  51. extern int wcmdq_head;
  52. extern int wcmdq_tail;
  53. extern int current_source_index;
  54. FILE *f_text;
  55. int (* uri_callback)(int, const char *, const char *) = NULL;
  56. int amp = 8; // default
  57. char path_home[80] = "";
  58. char wavefile[120];
  59. char textbuffile[L_tmpnam];
  60. int sample_rate_index; // current value
  61. int current_voice_num=0;
  62. int n_voice_files = 0;
  63. // output sound buffer, 2 bytes per sample
  64. static unsigned short SoundBuf[4096];
  65. static void *module_data;
  66. static int callback_inhibit = 0;
  67. static int more_text=0;
  68. #define N_VOICE_NAMES 40
  69. static char *voice_names[40];
  70. #define N_STATIC_BUF 8000
  71. static char static_buf[N_STATIC_BUF];
  72. static _kernel_oserror errblk;
  73. USHORT voice_pcnt[N_PEAKS+1][3];
  74. voice_t voice_data;
  75. voice_t *voice;
  76. static const char *help_text =
  77. "\nspeak [options] [\"<words>\"]\n\n"
  78. "-f <text file> Text file to speak\n"
  79. "--stdin Read text input from stdin instead of a file\n\n"
  80. "If neither -f nor --stdin, <words> are spoken, or if none then text is\n"
  81. "spoken from stdin, each line separately.\n\n"
  82. "-a <integer>\n"
  83. "\t Amplitude, 0 to 200, default is 100\n"
  84. "-l <integer>\n"
  85. "\t Line length. If not zero (which is the default), consider\n"
  86. "\t lines less than this length as end-of-clause\n"
  87. "-p <integer>\n"
  88. "\t Pitch adjustment, 0 to 99, default is 50\n"
  89. "-s <integer>\n"
  90. "\t Speed in words per minute 80 to 370, default is 170\n"
  91. "-v <voice name>\n"
  92. "\t Use voice file of this name from espeak-data/voices\n"
  93. "-w <wave file name>\n"
  94. "\t Write output to this WAV file, rather than speaking it directly\n"
  95. "-b\t Input text is 8-bit encoding\n"
  96. "-m\t Interpret SSML markup, and ignore other < > tags\n"
  97. "-q\t Quiet, don't produce any speech (may be useful with -x)\n"
  98. "-x\t Write phoneme mnemonics to stdout\n"
  99. "-X\t Write phonemes mnemonics and translation trace to stdout\n"
  100. "--stdout Write speech output to stdout\n"
  101. "--compile=<voice name>\n"
  102. "\t Compile the pronunciation rules and dictionary in the current\n"
  103. "\t directory. =<voice name> is optional and specifies which language\n"
  104. "--punct=\"<characters>\"\n"
  105. "\t Speak the names of punctuation characters during speaking. If\n"
  106. "\t =<characters> is omitted, all punctuation is spoken.\n"
  107. "--voices=<langauge>\n"
  108. "\t List the available voices for the specified language.\n"
  109. "\t If <language> is omitted, then list all voices.\n";
  110. // additional Latin characters beyond the Latin1 character set
  111. #define MAX_WALPHA 0x233
  112. // indexed by character - 0x100
  113. // 0=not alphabetic, 0xff=lower case, other=value to add to upper case to convert to lower case
  114. static unsigned char walpha_tab[MAX_WALPHA-0xff] = {
  115. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 100
  116. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 110
  117. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 120
  118. 0xff,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, 1,0xff, 1,0xff, 1, // 130
  119. 0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, 1,0xff, 1,0xff, // 140
  120. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 150
  121. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 160
  122. 1,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, // 170
  123. 0xff, 210, 1,0xff, 1,0xff, 206, 1,0xff, 205, 205, 1,0xff,0xff, 79, 202, // 180
  124. 203, 1,0xff, 205, 207,0xff, 211, 209, 1,0xff,0xff,0xff, 211, 213,0xff, 214, // 190
  125. 1,0xff, 1,0xff, 1,0xff, 218, 1,0xff, 218,0xff,0xff, 1,0xff, 218, 1, // 1a0
  126. 0xff, 217, 217, 1,0xff, 1,0xff, 219, 1,0xff,0xff,0xff, 1,0xff,0xff,0xff, // 1b0
  127. 0xff,0xff,0xff,0xff, 2, 1,0xff, 2, 1,0xff, 2, 1,0xff, 1,0xff, 1, // 1c0
  128. 0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, // 1d0
  129. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 1e0
  130. 0xff, 2, 1,0xff, 1,0xff,0xff,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 1f0
  131. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 200
  132. 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 210
  133. 0xff, 0, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 220
  134. 1,0xff, 1,0xff }; // 230
  135. // use ctype.h functions for Latin1 (character < 0x100)
  136. int iswalpha(int c)
  137. {
  138. if(c < 0x100)
  139. return(isalpha(c));
  140. if(c > MAX_WALPHA)
  141. return(0);
  142. return(walpha_tab[c-0x100]);
  143. }
  144. int iswdigit(int c)
  145. {
  146. if(c < 0x100)
  147. return(isdigit(c));
  148. return(0);
  149. }
  150. int iswalnum(int c)
  151. {
  152. if(iswdigit(c))
  153. return(1);
  154. return(iswalpha(c));
  155. }
  156. int towlower(int c)
  157. {
  158. int x;
  159. if(c < 0x100)
  160. return(tolower(c));
  161. if((c > MAX_WALPHA) || ((x = walpha_tab[c-0x100])==0xff))
  162. return(c); // already lower case
  163. return(c + x); // convert to lower case
  164. }
  165. int iswupper(int c)
  166. {
  167. int x;
  168. if(c < 0x100)
  169. return(isupper(c));
  170. if(((c > MAX_WALPHA) || (x = walpha_tab[c-0x100])==0) || (x == 0xff))
  171. return(0);
  172. return(1);
  173. }
  174. int iswlower(int c)
  175. {
  176. if(c < 0x100)
  177. return(islower(c));
  178. if((c > MAX_WALPHA) || (walpha_tab[c-0x100] != 0xff))
  179. return(0);
  180. return(1);
  181. }
  182. int iswspace(int c)
  183. {
  184. if(c < 0x100)
  185. return(isspace(c));
  186. return(0);
  187. }
  188. int iswpunct(int c)
  189. {
  190. if(c < 0x100)
  191. return(ispunct(c));
  192. return(0);
  193. }
  194. const wchar_t *wcschr(const wchar_t *str, int c)
  195. {
  196. while(*str != 0)
  197. {
  198. if(*str == c)
  199. return(str);
  200. str++;
  201. }
  202. return(NULL);
  203. }
  204. const int wcslen(const wchar_t *str)
  205. {
  206. int ix=0;
  207. while(*str != 0)
  208. {
  209. ix++;
  210. }
  211. return(ix);
  212. }
  213. int GetFileLength(const char *filename)
  214. {//====================================
  215. int length=0;
  216. int type;
  217. _kernel_swi_regs regs;
  218. _kernel_oserror *error;
  219. regs.r[0] = 5;
  220. regs.r[1] = (int)filename;
  221. regs.r[2] = 0;
  222. regs.r[3] = 0;
  223. regs.r[4] = 0;
  224. regs.r[5] = 0;
  225. error = _kernel_swi(0x20008,&regs,&regs);
  226. if(error)
  227. return(0);
  228. type = regs.r[0];
  229. length = regs.r[4];
  230. if(type==2)
  231. return(-2); // a directory
  232. if((type!=1) && (type!=3))
  233. return(0); /* not a file */
  234. return(length);
  235. } /* end of GetFileLength */
  236. void ReadVoiceNames2(char *directory)
  237. {//==================================
  238. int len;
  239. int *type;
  240. char *p;
  241. _kernel_swi_regs regs;
  242. _kernel_oserror *error;
  243. char buf[80];
  244. char directory2[80];
  245. regs.r[0] = 10;
  246. regs.r[1] = (int)directory;
  247. regs.r[2] = (int)buf;
  248. regs.r[3] = 1;
  249. regs.r[4] = 0;
  250. regs.r[5] = sizeof(buf);
  251. regs.r[6] = 0;
  252. while(regs.r[3] > 0)
  253. {
  254. error = _kernel_swi(0x0c+os_X,&regs,&regs); /* OS_GBPB 10, read directory entries */
  255. if((error != NULL) || (regs.r[3] == 0))
  256. {
  257. break;
  258. }
  259. type = (int *)(&buf[16]);
  260. len = strlen(&buf[20]);
  261. if(*type == 2)
  262. {
  263. // a sub-directory
  264. sprintf(directory2,"%s.%s",directory,&buf[20]);
  265. ReadVoiceNames2(directory2);
  266. }
  267. else
  268. {
  269. p = Alloc(len+1);
  270. strcpy(p,&buf[20]);
  271. voice_names[n_voice_files++] = p;
  272. if(n_voice_files >= (N_VOICE_NAMES-1))
  273. break;
  274. }
  275. }
  276. }
  277. void ReadVoiceNames()
  278. {//===================
  279. char directory[80];
  280. for(n_voice_files=0; n_voice_files<N_VOICE_NAMES; n_voice_files++)
  281. voice_names[n_voice_files] = NULL;
  282. n_voice_files = 0;
  283. sprintf(directory,"%s.voices",path_home);
  284. ReadVoiceNames2(directory);
  285. }
  286. #ifdef USE_MODULE
  287. char *Alloc(int size)
  288. /*******************/
  289. { // version of malloc() for use in RISC_OS module
  290. _kernel_swi_regs regs;
  291. regs.r[0] = 6;
  292. regs.r[3] = size;
  293. _kernel_swi(0x1e, &regs, &regs); /* OS_Module 6 claim memory */
  294. return(char *)regs.r[2];
  295. } /* end of module_malloc */
  296. void Free(void *ptr)
  297. /******************/
  298. { // version of free() for use in RISC_OS module
  299. _kernel_swi_regs regs;
  300. if(ptr == NULL)
  301. return;
  302. regs.r[0] = 7;
  303. regs.r[2] = (int)ptr;
  304. _kernel_swi(0x1e, &regs, &regs); /* OS_Module 7 free memory */
  305. } /* end of Free */
  306. #else
  307. char *Alloc(int size)
  308. {//==================
  309. char *p;
  310. if((p = (char *)malloc(size)) == NULL)
  311. fprintf(stderr,"Can't allocate memory\n");
  312. return(p);
  313. }
  314. void Free(void *ptr)
  315. {//=================
  316. free(ptr);
  317. }
  318. #endif
  319. void MarkerEvent(int type, unsigned int char_position, int value, unsigned char *out_ptr)
  320. {//======================================================================================
  321. // Do nothing in the command-line version.
  322. } // end of MarkerEvent
  323. void FillSoundBuf(int size)
  324. {//========================
  325. // Fill the buffer with output sound
  326. // size is number of samples*4
  327. size = size;
  328. if(size > sizeof(SoundBuf))
  329. size = sizeof(SoundBuf);
  330. out_ptr = (unsigned char *)(&SoundBuf[0]);
  331. out_end = (unsigned char *)(&SoundBuf[size]);
  332. WavegenFill(1);
  333. }
  334. int initialise(void)
  335. {//=================
  336. sprintf(path_home,"%s.espeak-data","<eSpeak$Dir>");
  337. if(GetFileLength(path_home) != -2)
  338. {
  339. // not found, try the 10 character version of the directory name
  340. sprintf(path_home,"%s.espeak-dat","<eSpeak$Dir>");
  341. }
  342. if(GetFileLength(path_home) != -2)
  343. {
  344. // still can't find data directory
  345. sprintf(errblk.errmess,"Speak: Can't find data directory: '%s'\n",path_home);
  346. return(-1);
  347. }
  348. WavegenInit(22050,0);
  349. LoadPhData();
  350. SynthesizeInit();
  351. return(0);
  352. }
  353. void speak_text_string(char *data, int terminator, int len, int wait, int voice_num)
  354. /**********************************************************************************/
  355. /* 'wait' indictes wait until speaking is finished before returning */
  356. {
  357. int c;
  358. int ix;
  359. static static_length=0;
  360. static int user_token=0; /* increment for each call of translate() */
  361. _kernel_swi_regs regs;
  362. regs.r[0] = (int)callback_entry;
  363. regs.r[1] = (int)module_data;
  364. _kernel_swi(0x5f, &regs, &regs);
  365. if((voice_num >= 0) && (voice_num != current_voice_num) && (voice_num < N_VOICE_NAMES))
  366. {
  367. LoadVoice(voice_names[voice_num],0);
  368. WavegenSetVoice(voice);
  369. }
  370. current_voice_num = voice_num;
  371. /* don't count CR as terminator if length is specified */
  372. if(len > 0) terminator = 0;
  373. ix = 0;
  374. if(more_text == 0)
  375. static_length = 0;
  376. else
  377. {
  378. strcat(&static_buf[static_length]," : ");
  379. static_length+=3;
  380. }
  381. if(terminator == 0)
  382. {
  383. while(((c = data[ix++]) != 0) && (static_length < N_STATIC_BUF-4))
  384. {
  385. static_buf[static_length++] = c;
  386. if(ix == len)
  387. break;
  388. }
  389. }
  390. else
  391. {
  392. while(((c = data[ix++]) != '\r') && (c != '\n') && (c != 0) && (static_length < N_STATIC_BUF-3))
  393. {
  394. static_buf[static_length++] = c;
  395. if(ix == len)
  396. break;
  397. }
  398. }
  399. static_buf[static_length] = 0;
  400. if(option_waveout==0)
  401. {
  402. if(more_text == 0)
  403. {
  404. InitText(0);
  405. RiscosOpenSound();
  406. more_text = SpeakNextClause(NULL,(void *)static_buf,0);
  407. }
  408. while(wait)
  409. {
  410. if((more_text==0) && (wcmdq_head == wcmdq_tail))
  411. break;
  412. //we need to block to allow the callback handler to run
  413. regs.r[0] = 129; // wait for key press
  414. regs.r[1] = 10;
  415. regs.r[2] = 0;
  416. _kernel_swi(0x06, &regs, &regs); // OS_Byte
  417. }
  418. }
  419. else
  420. {
  421. more_text = 0;
  422. SpeakNextClause(NULL,(void *)static_buf,0);
  423. for(;;)
  424. {
  425. if(WavegenFile() != 0)
  426. break; // finished, wavegen command queue is empty
  427. if(Generate(phoneme_list,&n_phoneme_list,1)==0)
  428. SpeakNextClause(NULL,NULL,1);
  429. }
  430. CloseWaveFile(samplerate);
  431. }
  432. } /* end of speak_text_string */
  433. void speak_file(char *fname)
  434. {//=========================
  435. FILE *f_in;
  436. char buf[120];
  437. f_in = fopen(fname,"r");
  438. if(f_in == NULL)
  439. {
  440. fprintf(stderr,"Can't read file: '%s'",fname);
  441. return;
  442. }
  443. more_text = 1;
  444. if(option_waveout == 0)
  445. {
  446. RiscosOpenSound();
  447. SpeakNextClause(f_in,NULL,0);
  448. }
  449. else
  450. {
  451. more_text = 0;
  452. SpeakNextClause(f_in,NULL,0);
  453. for(;;)
  454. {
  455. if(WavegenFile() != 0)
  456. break; // finished, wavegen command queue is empty
  457. if(Generate(phoneme_list,&n_phoneme_list,1)==0)
  458. SpeakNextClause(NULL,NULL,1);
  459. }
  460. CloseWaveFile(samplerate);
  461. }
  462. }
  463. void set_say_options(int reg2, int reg3)
  464. /**************************************/
  465. /* Sets options from information in 'say' SWI */
  466. /* R3 bits 0-7 stress indicator character
  467. bit 8 inhibit unpronouncable check */
  468. {
  469. option_linelength = 0;
  470. option_phonemes = 0;
  471. option_waveout = 0;
  472. option_harmonic1 = 8;
  473. option_multibyte = 0; // auto
  474. option_capitals = 0;
  475. option_punctuation = 0;
  476. option_punctlist[0] = 0;
  477. } /* end of set_say_options */
  478. void jsd_swi_functions(int *r)
  479. /****************************/
  480. {
  481. switch(r[0])
  482. {
  483. case 0: /* major version */
  484. r[0] = 3;
  485. break;
  486. case 1: /* register user */
  487. break;
  488. case 2: /* deregister user */
  489. break;
  490. case 3:
  491. // r[0] = (int)speech_to_phonemes((char *)r[1]);
  492. break;
  493. case 4:
  494. // r[0] = reload_word_dict(NULL);
  495. break;
  496. case 5: /* get table of voice names */
  497. r[0] = (int)voice_names;
  498. break;
  499. case 6: /* update voice data, r1 = voice_number */
  500. if(r[1] < N_VOICE_NAMES)
  501. {
  502. LoadVoice(voice_names[r[1]],0);
  503. current_voice_num = r[1];
  504. WavegenSetVoice(voice);
  505. }
  506. break;
  507. case 7: /* load voice data */
  508. // init_voice((char *)r[1]);
  509. break;
  510. default:
  511. r[0] = 0;
  512. r[1] = 0;
  513. break;
  514. }
  515. } /* end of jsd_swi_functions */
  516. _kernel_oserror *swi_handler(int swi_no, int *r, void *pw)
  517. /*********************************************************/
  518. {
  519. int value;
  520. int q_length;
  521. int speed;
  522. int amp;
  523. value = r[0];
  524. switch(swi_no)
  525. {
  526. case 0: // ready ?
  527. // returns the index into the source text of the currently speaking word
  528. if(current_source_index > 0)
  529. r[1] = current_source_index-1;
  530. else
  531. r[1] = current_source_index; /* source index */
  532. r[2] = 0; /* source tag */
  533. r[3] = 0; /* for future expansion */
  534. r[4] = 0;
  535. r[5] = 0;
  536. if(wcmdq_head == wcmdq_tail)
  537. {
  538. r[0] = -1; /* ready, or nearly */
  539. }
  540. else
  541. r[0] = 0;
  542. break;
  543. case 1: /* restore old sound channel. DO NOTHING */
  544. break;
  545. case 2: /* miscellaneous functions */
  546. jsd_swi_functions(r);
  547. break;
  548. case 3: /* speak text */
  549. // _kernel_irqs_on();
  550. set_say_options(r[2],r[3]);
  551. speak_text_string((char *)r[0],'\r',r[1],0,r[2]);
  552. break;
  553. case 4: /* speak text and wait */
  554. // _kernel_irqs_on(); /* enable interrupts */
  555. set_say_options(r[2],r[3]);
  556. speak_text_string((char *)r[0],'\r',r[1],1,r[2]);
  557. break;
  558. case 5: /* stop speaking */
  559. SpeakNextClause(NULL,NULL,2);
  560. more_text = 0;
  561. break;
  562. case 7: /* pitch */
  563. // not implemented
  564. break;
  565. case 8: /* speed */
  566. speed = (value * 160)/140;
  567. SetParameter(espeakRATE,speed,0);
  568. break;
  569. case 9: /* word_gap */
  570. // not implemented
  571. break;
  572. case 10: /* pitch_range */
  573. // not implemented
  574. break;
  575. case 12: /* reset */
  576. // not implemented
  577. break;
  578. case 13: /* volume */
  579. amp = (value*45)/100;
  580. SetParameter(espeakVOLUME,amp,0);
  581. WavegenSetVoice(voice);
  582. break;
  583. }
  584. return(NULL);
  585. } /* end of swi_handler */
  586. void PitchAdjust(int pitch_adjustment)
  587. {//===================================
  588. int ix, factor;
  589. voice_t *voice = &voice_data;
  590. extern unsigned char pitch_adjust_tab[100];
  591. voice->pitch_base = (voice->pitch_base * pitch_adjust_tab[pitch_adjustment])/128;
  592. // adjust formants to give better results for a different voice pitch
  593. factor = 256 + (25 * (pitch_adjustment - 50))/50;
  594. for(ix=0; ix<=5; ix++)
  595. {
  596. voice->freq[ix] = (voice->freq2[ix] * factor)/256;
  597. }
  598. } // end of PitchAdjustment
  599. char *param_string(char **argp)
  600. {//============================
  601. char *p;
  602. int ix=0;
  603. static char buf[80];
  604. p = *argp;
  605. while(*p == ' ') p++;
  606. while(!isspace(*p))
  607. buf[ix++] = *p++;
  608. buf[ix]=0;
  609. *argp = p;
  610. return(buf);
  611. }
  612. int param_number(char **argp)
  613. {//==========================
  614. int value;
  615. char *p;
  616. p = *argp;
  617. while(*p == ' ') p++;
  618. value = atoi(p);
  619. while(!isspace(*p)) p++;
  620. *argp = p;
  621. return(value);
  622. }
  623. void command_line(char *arg_string, int wait)
  624. {//==========================================
  625. int option_index = 0;
  626. int c;
  627. int value;
  628. int speed;
  629. int amp;
  630. int speaking = 0;
  631. int flag_stdin = 0;
  632. int flag_compile = 0;
  633. int error;
  634. int pitch_adjustment = 50;
  635. char filename[80];
  636. char voicename[40];
  637. char command[80];
  638. char *p;
  639. int ix;
  640. voicename[0] = 0;
  641. wavefile[0] = 0;
  642. filename[0] = 0;
  643. option_linelength = 0;
  644. option_phonemes = 0;
  645. option_waveout = 0;
  646. option_quiet = 0;
  647. option_harmonic1 = 8;
  648. option_multibyte = 0; // auto
  649. option_capitals = 0;
  650. option_punctuation = 0;
  651. option_punctlist[0] = 0;
  652. f_trans = stdout;
  653. p = arg_string;
  654. for(;;)
  655. {
  656. while(*p==' ') p++; // skip spaces
  657. if(*p == '\r') break; // end of line
  658. if(*p == '-')
  659. {
  660. // a command line argument
  661. p++;
  662. switch(*p++)
  663. {
  664. case 'h':
  665. printf("\n");
  666. printf("speak text-to-speech: %s\n%s",version_string,help_text);
  667. return;
  668. case 'a':
  669. amp = param_number(&p);
  670. SetParameter(espeakVOLUME,amp,0);
  671. break;
  672. case 'f':
  673. strcpy(filename,param_string(&p));
  674. break;
  675. case 'k':
  676. option_capitals = param_number(&p);
  677. SetParameter(espeakCAPITALS,option_capitals,0);
  678. break;
  679. case 'l':
  680. option_linelength = param_number(&p);
  681. break;
  682. case 'p':
  683. pitch_adjustment = param_number(&p);
  684. break;
  685. case 'q':
  686. option_quiet = 1;
  687. break;
  688. case 's':
  689. speed = param_number(&p);
  690. SetParameter(espeakRATE,speed,0);
  691. break;
  692. case 'v':
  693. strcpy(voicename,param_string(&p));
  694. break;
  695. case 'w':
  696. option_waveout=1;
  697. strcpy(wavefile,param_string(&p));
  698. break;
  699. case 'x':
  700. option_phonemes = 1;
  701. break;
  702. case 'X':
  703. option_phonemes = 2;
  704. break;
  705. case '-':
  706. strcpy(command,param_string(&p));
  707. if(memcmp(command,"compile=",8)==0)
  708. {
  709. CompileDictionary(NULL,&command[8],NULL,NULL);
  710. return;
  711. }
  712. else
  713. if(strcmp(command,"help")==0)
  714. {
  715. printf("\n%s\n%s",version_string,help_text);
  716. return;
  717. }
  718. else
  719. if(memcmp(command,"punct",5)==0)
  720. {
  721. option_punctuation = 1;
  722. if((command[5]=='=') && (command[6]=='"'))
  723. {
  724. ix = 0;
  725. while((ix < N_PUNCTLIST) && ((option_punctlist[ix] = command[ix+7]) != 0)) ix++;
  726. option_punctlist[N_PUNCTLIST-1] = 0;
  727. option_punctuation = 2;
  728. }
  729. SetParameter(espeakPUNCTUATION,option_punctuation,0);
  730. }
  731. else
  732. {
  733. printf("Command not recognised\n");
  734. }
  735. break;
  736. default:
  737. printf("Command not recognised\n");
  738. break;
  739. }
  740. }
  741. else
  742. {
  743. break;
  744. }
  745. }
  746. LoadVoice(voicename,0);
  747. if((filename[0]==0) && (p[0]=='\r'))
  748. {
  749. // nothing to speak
  750. if(option_quiet)
  751. {
  752. SpeakNextClause(NULL,NULL,2); // stop speaking
  753. more_text = 0;
  754. }
  755. }
  756. if(option_waveout || option_quiet)
  757. {
  758. // write speech to a WAV file
  759. if(option_quiet)
  760. {
  761. OpenWaveFile(NULL,samplerate);
  762. option_waveout = 2;
  763. }
  764. else
  765. {
  766. if(OpenWaveFile(wavefile,samplerate) != 0)
  767. {
  768. fprintf(stderr,"Can't write to output file '%s'\n'",wavefile);
  769. return;
  770. }
  771. }
  772. }
  773. if(pitch_adjustment != 50)
  774. {
  775. PitchAdjust(pitch_adjustment);
  776. }
  777. WavegenSetVoice(voice);
  778. if(filename[0]==0)
  779. speak_text_string(p,'\r',0,wait,-1);
  780. else
  781. speak_file(filename);
  782. }
  783. _kernel_oserror *command_handler(char *arg_string, int argc, int cmd_no, void *pw)
  784. /********************************************************************************/
  785. {
  786. switch(cmd_no)
  787. {
  788. case 0: /* Say <string> */
  789. command_line(arg_string,0); // for compatibility with speak V2
  790. break;
  791. case 1: /* Sayw <string */
  792. command_line(arg_string,1);
  793. break;
  794. case 2: /* speak [options] [<string>] */
  795. command_line(arg_string,0);
  796. break;
  797. }
  798. return(NULL);
  799. } /* end of cmd_handler */
  800. // sound handler data
  801. int current_sound_handler=0;
  802. int prev_sound_handler=0;
  803. int prev_sound_data=0;
  804. int prev_sound_rate=13;
  805. int sound_handler_changed=0;
  806. void RiscosCloseSound()
  807. {//====================
  808. _kernel_swi_regs regs;
  809. if((sound_handler_changed) && (prev_sound_handler != (int)DMA_Handler))
  810. {
  811. // check whether current handler is ours
  812. regs.r[0]=0;
  813. _kernel_swi(0x40145,&regs,&regs);
  814. if(regs.r[1] == (int)DMA_Handler)
  815. {
  816. regs.r[0]=1;
  817. regs.r[1]=prev_sound_handler;
  818. regs.r[2]=prev_sound_data;
  819. _kernel_swi(0x40145,&regs,&regs); // Sound LinearHandler 1
  820. // reset to the previous sample rate
  821. regs.r[0]=3;
  822. regs.r[1]=prev_sound_rate;
  823. _kernel_swi(0x40146,&regs,&regs); // Sound_SampleRate 3
  824. current_sound_handler = prev_sound_handler;
  825. sound_handler_changed = 0;
  826. }
  827. }
  828. } // end of RiscosCloseSound
  829. void RiscosOpenSound()
  830. {//===================
  831. _kernel_swi_regs regs;
  832. if(current_sound_handler != (int)DMA_Handler)
  833. {
  834. // register the sound handler
  835. regs.r[0]=1;
  836. regs.r[1]=(int)DMA_Handler;
  837. regs.r[2]=(int)module_data;
  838. _kernel_swi(0x40145,&regs,&regs); // Sound_LinearHandler 1
  839. prev_sound_handler = regs.r[1];
  840. prev_sound_data = regs.r[2];
  841. // set the sample rate
  842. regs.r[0]=3;
  843. regs.r[1]=sample_rate_index;
  844. regs.r[2]=0;
  845. _kernel_swi(0x40146,&regs,&regs); // Sound_SampleRate
  846. prev_sound_rate = regs.r[1];
  847. current_sound_handler = (int)DMA_Handler;
  848. sound_handler_changed = 1;
  849. }
  850. } // end of RiscosOpenSound
  851. int callback_handler(_kernel_swi_regs *r, void *pw)
  852. /*************************************************/
  853. {
  854. if(Generate(phoneme_list,&n_phoneme_list,1)==0)
  855. {
  856. more_text = SpeakNextClause(NULL,NULL,1);
  857. }
  858. if((WcmdqUsed() == 0) && (more_text == 0))
  859. {
  860. RiscosCloseSound();
  861. }
  862. callback_inhibit = 0;
  863. return(1);
  864. } /* end of callback_handler */
  865. int sound_handler(_kernel_swi_regs *r, void *pw)
  866. /**********************************************/
  867. {
  868. int n_queue;
  869. int size;
  870. int *dma_buf;
  871. int x;
  872. int ix;
  873. module_data = (int *)pw;
  874. dma_buf = (int *)r->r[1];
  875. size = (r->r[2] - r->r[1])/4;
  876. FillSoundBuf(size);
  877. for(ix=0; ix<size; ix++)
  878. {
  879. x = SoundBuf[ix];
  880. dma_buf[ix] = x + (x << 16);
  881. }
  882. n_queue = WcmdqUsed();
  883. r->r[0] = 0;
  884. if(callback_inhibit == 0)
  885. {
  886. // set a callback either:
  887. // - queue is low and there is more text to be processed
  888. // - queue is empty and no more text, so callback handler will remove the sound handler
  889. if(((n_queue < 20) && (more_text != 0)) ||
  890. ((n_queue==0) && (more_text == 0)))
  891. {
  892. callback_inhibit = 1;
  893. r->r[0] = 1;
  894. r->r[1] = (int)pw;
  895. }
  896. }
  897. return(1);
  898. }
  899. int InitSound16(int sample_rate)
  900. /******************************/
  901. /* Find sample rate index */
  902. {
  903. int current_rate_index; // current value
  904. int sound_mode;
  905. int sound_config;
  906. int srate;
  907. int n_srix;
  908. int ix;
  909. _kernel_swi_regs regs;
  910. _kernel_oserror *error;
  911. sound_mode = 0;
  912. regs.r[0] = 0;
  913. error = _kernel_swi(0x40144+os_X,&regs,&regs);
  914. sound_mode = regs.r[0];
  915. sound_config = regs.r[1];
  916. if((error == NULL) && (sound_mode == 1))
  917. {
  918. /* 16 bit sound, find sample rate index */
  919. regs.r[0] = 0;
  920. regs.r[1] = 0;
  921. _kernel_swi(0x40146,&regs,&regs);
  922. n_srix = regs.r[1];
  923. regs.r[0] = 1;
  924. regs.r[1] = 0;
  925. _kernel_swi(0x40146,&regs,&regs);
  926. current_rate_index = regs.r[1]; // current sample rate index
  927. srate = regs.r[2];
  928. for(ix=1; ix<=n_srix; ix++)
  929. {
  930. regs.r[0] = 2;
  931. regs.r[1] = ix;
  932. _kernel_swi(0x40146,&regs,&regs);
  933. srate = regs.r[2];
  934. if(srate >= (sample_rate*1024))
  935. {
  936. return(ix);
  937. }
  938. }
  939. }
  940. return(14); // this was the index for 22050
  941. } // end of InitSound16
  942. void RemoveCallback()
  943. /*******************/
  944. {
  945. _kernel_swi_regs regs;
  946. regs.r[0] = (int)callback_entry;
  947. regs.r[1] = (int)module_data;
  948. _kernel_swi(0x5f, &regs, &regs);
  949. }
  950. void terminate_module(void)
  951. /*************************/
  952. {
  953. RiscosCloseSound();
  954. RemoveCallback();
  955. delete translator;
  956. FreePhData();
  957. } /* end of terminate_module */
  958. void kill_module(void)
  959. /********************/
  960. {
  961. _kernel_swi_regs regs;
  962. regs.r[0]=4;
  963. regs.r[1]=(int)"Speak";
  964. _kernel_swi(0x1e,&regs,&regs); /* RMKill */
  965. }
  966. _kernel_oserror *user_init(char *cmd_fail, int podule_base, void *pw)
  967. /*******************************************************************/
  968. {
  969. _kernel_swi_regs regs;
  970. _kernel_oserror *error;
  971. int param;
  972. // It seems that the wctype functions don't work until the locale has been set
  973. // to something other than the default "C". Then, not only Latin1 but also the
  974. // other characters give the correct results with iswalpha() etc.
  975. static char *locale = "ISO8859-1";
  976. setlocale(LC_CTYPE,locale);
  977. module_data = pw;
  978. sample_rate_index = InitSound16(22050);
  979. if(initialise() < 0)
  980. {
  981. // exit module, errblk.errmess is set by initialise()
  982. errblk.errnum = 0x101;
  983. return(&errblk);
  984. }
  985. ReadVoiceNames();
  986. LoadVoice("default",0);
  987. for(param=0; param<N_SPEECH_PARAM; param++)
  988. param_stack[0].parameter[param] = param_defaults[param];
  989. SetParameter(espeakRATE,170,0);
  990. SetParameter(espeakVOLUME,65,0);
  991. WavegenSetVoice(voice);
  992. atexit(terminate_module);
  993. return(NULL);
  994. } /* end of user_init */