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

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