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.cpp 27KB

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