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 24KB

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