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

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