2
/***************************************************************************
3
* Copyright (C) 2005,2006 by Jonathan Duddington *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU General Public License as published by *
8
* the Free Software Foundation; either version 2 of the License, or *
9
* (at your option) any later version. *
11
* This program is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU General Public License for more details. *
16
* You should have received a copy of the GNU General Public License *
17
* along with this program; if not, write to the *
18
* Free Software Foundation, Inc., *
19
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20
***************************************************************************/
33
#include "synthesize.h"
34
#include "translate.h"
38
// interface to the assembler section
40
extern void DMA_Handler(void);
43
// used from the cmhgfile
44
extern _kernel_oserror *user_init(char *cmd_fail, int podule_base, void *pw);
45
extern _kernel_oserror *swi_handler(int swi_no, int *r, void *pw);
46
extern _kernel_oserror *command_handler(char *arg_string, int argc, int cmd_no, void *pw);
47
extern int callback_handler(_kernel_swi_regs *r, void *pw);
48
extern int callback_entry(_kernel_swi_regs *r, void *pw);
49
extern int sound_handler(_kernel_swi_regs *r, void *pw);
50
extern int sound_entry(_kernel_swi_regs *r, void *pw);
54
extern int Generate(PHONEME_LIST *phoneme_list, int resume);
55
extern void RiscosOpenSound();
56
extern int WavegenFill(void);
57
extern int WcmdqUsed();
58
extern void FreePhData();
59
extern void FreeDictionary();
61
extern int wcmdq_head;
62
extern int wcmdq_tail;
63
extern unsigned char *out_ptr;
64
extern unsigned char *out_end;
65
extern int current_source_index;
69
int amp = 8; // default
71
char path_home[80] = "";
72
char path_source[80] = "";
74
char textbuffile[L_tmpnam];
75
int sample_rate_index; // current value
76
int current_voice_num=0;
78
// output sound buffer, 2 bytes per sample
79
static unsigned short SoundBuf[4096];
80
static void *module_data;
81
static int callback_inhibit = 0;
82
static int more_text=0;
84
#define N_VOICE_NAMES 40
85
static char *voice_names[40];
87
#define N_STATIC_BUF 8000
88
static char static_buf[N_STATIC_BUF];
89
static _kernel_oserror errblk;
92
const char *version = "Speak text-to-speech: 3.09 30.Apr.2006";
95
USHORT voice_pcnt[N_PEAKS+1][3];
99
const char *help_text =
100
"\nspeak [options] [\"<words>\"]\n\n"
101
"-f <text file> Speak text from file rather than <words>\n"
102
"-q\t Quiet, don't produce any speech (can use with -p)\n"
104
"\t Amplitude, 0 to 20, default is 8\n"
106
"\t Line length. If not zero (which is the default), consider\n"
107
"\t lines less than this length as and-of-clause\n"
109
"\t Pitch adjustment, 0 to 99, default is 50\n"
111
"\t Speed in words per minute, default is 160\n"
113
"\t Use voice file of this name from espeak-data/voices\n"
114
"-w <wave file name>\n"
115
"\t Write output to this WAV file, rather than speaking it directly\n"
116
"-x\t Write phoneme mnemonics to stdout\n"
117
"-X\t Write phonemes mnemonics and translation trace to stdout\n"
118
"--compile=<language>\n"
119
"\t Compile the pronunciation rules and dictionary for the\n"
120
"\t directory <language>\n";
122
// additional Latin characters beyond the Latin1 character set
123
#define MAX_WALPHA 0x233
124
// indexed by character - 0x100
125
// 0=not alphabetic, 0xff=lower case, other=value to add to upper case to convert to lower case
126
static unsigned char walpha_tab[MAX_WALPHA-0xff] = {
127
1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 100
128
1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 110
129
1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 120
130
0xff,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, 1,0xff, 1,0xff, 1, // 130
131
0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, 1,0xff, 1,0xff, // 140
132
1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 150
133
1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 160
134
1,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, // 170
135
0xff, 210, 1,0xff, 1,0xff, 206, 1,0xff, 205, 205, 1,0xff,0xff, 79, 202, // 180
136
203, 1,0xff, 205, 207,0xff, 211, 209, 1,0xff,0xff,0xff, 211, 213,0xff, 214, // 190
137
1,0xff, 1,0xff, 1,0xff, 218, 1,0xff, 218,0xff,0xff, 1,0xff, 218, 1, // 1a0
138
0xff, 217, 217, 1,0xff, 1,0xff, 219, 1,0xff,0xff,0xff, 1,0xff,0xff,0xff, // 1b0
139
0xff,0xff,0xff,0xff, 2, 1,0xff, 2, 1,0xff, 2, 1,0xff, 1,0xff, 1, // 1c0
140
0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff,0xff, 1,0xff, // 1d0
141
1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 1e0
142
0xff, 2, 1,0xff, 1,0xff,0xff,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 1f0
143
1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 200
144
1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 210
145
0xff, 0, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, 1,0xff, // 220
146
1,0xff, 1,0xff }; // 230
148
// use ctype.h functions for Latin1 (character < 0x100)
155
return(walpha_tab[c-0x100]);
177
if((c > MAX_WALPHA) || ((x = walpha_tab[c-0x100])==0xff))
178
return(c); // already lower case
179
return(c + x); // convert to lower case
187
if(((c > MAX_WALPHA) || (x = walpha_tab[c-0x100])==0) || (x == 0xff))
197
if((x > MAX_WALPHA) || (walpha_tab[c-0x100] != 0xff))
218
int GetFileLength(const char *filename)
219
{//====================================
222
_kernel_swi_regs regs;
223
_kernel_oserror *error;
226
regs.r[1] = (int)filename;
232
error = _kernel_swi(0x20008,®s,®s);
240
return(-2); // a directory
241
if((type!=1) && (type!=3))
242
return(0); /* not a file */
245
} /* end of GetFileLength */
249
void ReadVoiceNames()
254
_kernel_swi_regs regs;
255
_kernel_oserror *error;
259
for(n_files=0; n_files<N_VOICE_NAMES; n_files++)
260
voice_names[n_files] = NULL;
262
sprintf(directory,"%s.voices",path_home);
264
regs.r[1] = (int)directory;
265
regs.r[2] = (int)buf;
268
regs.r[5] = sizeof(buf);
274
error = _kernel_swi(0x0c+os_X,®s,®s); /* OS_GBPB 10, read directory entries */
275
if((error != NULL) || (regs.r[3] == 0))
282
voice_names[n_files++] = p;
283
if(n_files >= N_VOICE_NAMES)
291
char *Alloc(int size)
292
/*******************/
293
{ // version of malloc() for use in RISC_OS module
294
_kernel_swi_regs regs;
298
_kernel_swi(0x1e, ®s, ®s); /* OS_Module 6 claim memory */
300
return(char *)regs.r[2];
301
} /* end of module_malloc */
307
{ // version of free() for use in RISC_OS module
308
_kernel_swi_regs regs;
314
regs.r[2] = (int)ptr;
315
_kernel_swi(0x1e, ®s, ®s); /* OS_Module 7 free memory */
320
char *Alloc(int size)
321
{//==================
323
if((p = (char *)malloc(size)) == NULL)
324
fprintf(stderr,"Can't allocate memory\n");
337
void FillSoundBuf(int size)
338
{//========================
339
// Fill the buffer with output sound
341
// size is number of samples*4
344
if(size > sizeof(SoundBuf))
345
size = sizeof(SoundBuf);
347
out_ptr = (unsigned char *)(&SoundBuf[0]);
348
out_end = (unsigned char *)(&SoundBuf[size]);
356
sprintf(path_home,"%s.espeak-data","<eSpeak$Dir>");
357
if(GetFileLength(path_home) != -2)
359
// not found, try the 10 character version of the directory name
360
sprintf(path_home,"%s.espeak-dat","<eSpeak$Dir>");
362
if(GetFileLength(path_home) != -2)
364
// still can't find data directory
365
sprintf(errblk.errmess,"Speak: Can't find data directory: '%s'\n",path_home);
369
WavegenInit(22050,0);
377
void speak_text_string(char *data, int terminator, int len, int wait, int voice_num)
378
/**********************************************************************************/
379
/* 'wait' indictes wait until speaking is finished before returning */
383
static static_length=0;
384
static int user_token=0; /* increment for each call of translate() */
385
_kernel_swi_regs regs;
387
regs.r[0] = (int)callback_entry;
388
regs.r[1] = (int)module_data;
389
_kernel_swi(0x5f, ®s, ®s);
392
if((voice_num >= 0) && (voice_num != current_voice_num) && (voice_num < N_VOICE_NAMES))
394
LoadVoice(voice_names[voice_num],0);
395
WavegenSetVoice(voice);
397
current_voice_num = voice_num;
399
/* don't count CR as terminator if length is specified */
400
if(len > 0) terminator = 0;
407
strcat(&static_buf[static_length]," : ");
413
while(((c = data[ix++]) != 0) && (static_length < N_STATIC_BUF-4))
415
static_buf[static_length++] = c;
422
while(((c = data[ix++]) != '\r') && (c != '\n') && (c != 0) && (static_length < N_STATIC_BUF-3))
424
static_buf[static_length++] = c;
429
static_buf[static_length] = 0;
431
if(option_waveout==0)
436
more_text = SpeakNextClause(NULL,static_buf,0);
441
if((more_text==0) && (wcmdq_head == wcmdq_tail))
444
//we need to block to allow the callback handler to run
445
regs.r[0] = 129; // wait for key press
448
_kernel_swi(0x06, ®s, ®s); // OS_Byte
454
SpeakNextClause(NULL,static_buf,0);
458
if(WavegenFile() != 0)
459
break; // finished, wavegen command queue is empty
461
if(Generate(phoneme_list,1)==0)
462
SpeakNextClause(NULL,NULL,1);
465
CloseWaveFile(samplerate);
468
} /* end of speak_text_string */
473
void speak_file(char *fname)
474
{//=========================
478
f_in = fopen(fname,"r");
481
fprintf(stderr,"Can't read file: '%s'",fname);
486
if(option_waveout == 0)
489
SpeakNextClause(f_in,NULL,0);
494
SpeakNextClause(f_in,NULL,0);
498
if(WavegenFile() != 0)
499
break; // finished, wavegen command queue is empty
501
if(Generate(phoneme_list,1)==0)
502
SpeakNextClause(NULL,NULL,1);
505
CloseWaveFile(samplerate);
511
void set_say_options(int reg2, int reg3)
512
/**************************************/
513
/* Sets options from information in 'say' SWI */
514
/* R3 bits 0-7 stress indicator character
515
bit 8 inhibit unpronouncable check */
517
option_linelength = 0;
520
option_harmonic1 = 8;
523
option_punctuation = 0;
524
option_punctlist[0] = 0;
525
} /* end of set_say_options */
529
void jsd_swi_functions(int *r)
530
/****************************/
534
case 0: /* major version */
538
case 1: /* register user */
541
case 2: /* deregister user */
545
// r[0] = (int)speech_to_phonemes((char *)r[1]);
549
// r[0] = reload_word_dict(NULL);
552
case 5: /* get table of voice names */
553
r[0] = (int)voice_names;
556
case 6: /* update voice data, r1 = voice_number */
557
if(r[1] < N_VOICE_NAMES)
559
LoadVoice(voice_names[r[1]],0);
560
current_voice_num = r[1];
561
WavegenSetVoice(voice);
565
case 7: /* load voice data */
566
// init_voice((char *)r[1]);
574
} /* end of jsd_swi_functions */
579
_kernel_oserror *swi_handler(int swi_no, int *r, void *pw)
580
/*********************************************************/
591
// returns the index into the source text of the currently speaking word
592
r[1] = current_source_index; /* source index */
593
r[2] = 0; /* source tag */
594
r[3] = 0; /* for future expansion */
598
if(wcmdq_head == wcmdq_tail)
600
r[0] = -1; /* ready, or nearly */
606
case 1: /* restore old sound channel. DO NOTHING */
609
case 2: /* miscellaneous functions */
610
jsd_swi_functions(r);
613
case 3: /* speak text */
614
// _kernel_irqs_on();
615
set_say_options(r[2],r[3]);
616
speak_text_string((char *)r[0],'\r',r[1],0,r[2]);
619
case 4: /* speak text and wait */
620
// _kernel_irqs_on(); /* enable interrupts */
621
set_say_options(r[2],r[3]);
622
speak_text_string((char *)r[0],'\r',r[1],1,r[2]);
625
case 5: /* stop speaking */
626
SpeakNextClause(NULL,NULL,2);
635
global_speed = (value * 160)/146;
636
SetSpeed(global_speed,3);
639
case 9: /* word_gap */
643
case 10: /* pitch_range */
651
case 13: /* volume */
657
} /* end of swi_handler */
661
void PitchAdjust(int pitch_adjustment)
662
{//===================================
664
voice_t *voice = &voice_data;
665
extern unsigned char pitch_adjust_tab[100];
667
voice->pitch_base = (voice->pitch_base * pitch_adjust_tab[pitch_adjustment])/128;
669
// adjust formants to give better results for a different voice pitch
670
factor = 256 + (25 * (pitch_adjustment - 50))/50;
671
for(ix=0; ix<=5; ix++)
673
voice->freq[ix] = (voice->freq2[ix] * factor)/256;
675
} // end of PitchAdjustment
679
char *param_string(char **argp)
680
{//============================
686
while(*p == ' ') p++;
695
int param_number(char **argp)
696
{//==========================
701
while(*p == ' ') p++;
704
while(!isspace(*p)) p++;
709
void command_line(char *arg_string, int wait)
710
{//==========================================
712
int option_index = 0;
720
int flag_compile = 0;
722
int pitch_adjustment = 50;
728
speed2 = global_speed; // default
733
option_linelength = 0;
736
option_harmonic1 = 8;
739
option_punctuation = 0;
740
option_punctlist[0] = 0;
748
while(*p==' ') p++; // skip spaces
749
if(*p == '\r') break; // end of line
753
// a command line argument
758
printf("\n%s\n%s",version,help_text);
762
amp = param_number(&p);
766
strcpy(filename,param_string(&p));
770
option_capitals = param_number(&p);
774
option_linelength = param_number(&p);
778
pitch_adjustment = param_number(&p);
786
global_speed = param_number(&p);
790
strcpy(voicename,param_string(&p));
795
strcpy(wavefile,param_string(&p));
808
strcpy(command,param_string(&p));
809
if(memcmp(command,"compile=",8)==0)
811
CompileDictionary(&command[8],0);
815
if(strcmp(command,"help")==0)
817
printf("\n%s\n%s",version,help_text);
821
if(memcmp(command,"punct",5)==0)
823
option_punctuation = 1;
824
if(memcmp(&command[5],"=\"",2)==0)
826
strcpy(option_punctlist,&command[7]);
827
option_punctlist[strlen(option_punctlist)-1]=0;
832
printf("Command not recognised\n");
837
printf("Command not recognised\n");
848
SetSpeed(global_speed,3);
850
LoadVoice(voicename,0);
852
if((filename[0]==0) && (p[0]=='\r'))
857
SpeakNextClause(NULL,NULL,2); // stop speaking
862
if(option_waveout || quiet)
864
// write speech to a WAV file
867
OpenWaveFile(NULL,samplerate);
872
if(OpenWaveFile(wavefile,samplerate) != 0)
874
fprintf(stderr,"Can't write to output file '%s'\n'",wavefile);
880
if(pitch_adjustment != 50)
882
PitchAdjust(pitch_adjustment);
884
WavegenSetVoice(voice);
887
speak_text_string(p,'\r',0,wait,-1);
889
speak_file(filename);
894
_kernel_oserror *command_handler(char *arg_string, int argc, int cmd_no, void *pw)
895
/********************************************************************************/
899
case 0: /* Say <string> */
900
command_line(arg_string,0); // for compatibility with speak V2
903
case 1: /* Sayw <string */
904
command_line(arg_string,1);
907
case 2: /* speak [options] [<string>] */
908
command_line(arg_string,0);
913
} /* end of cmd_handler */
916
// sound handler data
917
int current_sound_handler=0;
918
int prev_sound_handler=0;
919
int prev_sound_data=0;
920
int prev_sound_rate=13;
921
int sound_handler_changed=0;
924
void RiscosCloseSound()
925
{//====================
926
_kernel_swi_regs regs;
927
if((sound_handler_changed) && (prev_sound_handler != (int)DMA_Handler))
929
// check whether current handler is ours
931
_kernel_swi(0x40145,®s,®s);
932
if(regs.r[1] == (int)DMA_Handler)
935
regs.r[1]=prev_sound_handler;
936
regs.r[2]=prev_sound_data;
937
_kernel_swi(0x40145,®s,®s); // Sound LinearHandler 1
939
// reset to the previous sample rate
941
regs.r[1]=prev_sound_rate;
942
_kernel_swi(0x40146,®s,®s); // Sound_SampleRate 3
944
current_sound_handler = prev_sound_handler;
945
sound_handler_changed = 0;
948
} // end of RiscosCloseSound
952
void RiscosOpenSound()
953
{//===================
954
_kernel_swi_regs regs;
956
if(current_sound_handler != (int)DMA_Handler)
958
// register the sound handler
960
regs.r[1]=(int)DMA_Handler;
961
regs.r[2]=(int)module_data;
962
_kernel_swi(0x40145,®s,®s); // Sound_LinearHandler 1
963
prev_sound_handler = regs.r[1];
964
prev_sound_data = regs.r[2];
966
// set the sample rate
968
regs.r[1]=sample_rate_index;
970
_kernel_swi(0x40146,®s,®s); // Sound_SampleRate
971
prev_sound_rate = regs.r[1];
973
current_sound_handler = (int)DMA_Handler;
974
sound_handler_changed = 1;
976
} // end of RiscosOpenSound
982
int callback_handler(_kernel_swi_regs *r, void *pw)
983
/*************************************************/
985
if(Generate(phoneme_list,1)==0)
987
more_text = SpeakNextClause(NULL,NULL,1);
990
if((WcmdqUsed() == 0) && (more_text == 0))
994
callback_inhibit = 0;
997
} /* end of callback_handler */
1001
int sound_handler(_kernel_swi_regs *r, void *pw)
1002
/**********************************************/
1010
module_data = (int *)pw;
1011
dma_buf = (int *)r->r[1];
1012
size = (r->r[2] - r->r[1])/4;
1015
for(ix=0; ix<size; ix++)
1018
dma_buf[ix] = x + (x << 16);
1021
n_queue = WcmdqUsed();
1024
if(callback_inhibit == 0)
1026
// set a callback either:
1027
// - queue is low and there is more text to be processed
1028
// - queue is empty and no more text, so callback handler will remove the sound handler
1029
if(((n_queue < 20) && (more_text != 0)) ||
1030
((n_queue==0) && (more_text == 0)))
1032
callback_inhibit = 1;
1043
int InitSound16(int sample_rate)
1044
/******************************/
1045
/* Find sample rate index */
1047
int current_rate_index; // current value
1054
_kernel_swi_regs regs;
1055
_kernel_oserror *error;
1059
error = _kernel_swi(0x40144+os_X,®s,®s);
1060
sound_mode = regs.r[0];
1061
sound_config = regs.r[1];
1063
if((error == NULL) && (sound_mode == 1))
1065
/* 16 bit sound, find sample rate index */
1068
_kernel_swi(0x40146,®s,®s);
1073
_kernel_swi(0x40146,®s,®s);
1074
current_rate_index = regs.r[1]; // current sample rate index
1076
for(ix=1; ix<=n_srix; ix++)
1080
_kernel_swi(0x40146,®s,®s);
1082
if(srate >= (sample_rate*1024))
1088
return(14); // this was the index for 22050
1089
} // end of InitSound16
1093
void RemoveCallback()
1094
/*******************/
1096
_kernel_swi_regs regs;
1098
regs.r[0] = (int)callback_entry;
1099
regs.r[1] = (int)module_data;
1100
_kernel_swi(0x5f, ®s, ®s);
1104
void terminate_module(void)
1105
/*************************/
1111
} /* end of terminate_module */
1114
void kill_module(void)
1115
/********************/
1117
_kernel_swi_regs regs;
1119
regs.r[1]=(int)"Speak";
1120
_kernel_swi(0x1e,®s,®s); /* RMKill */
1124
_kernel_oserror *user_init(char *cmd_fail, int podule_base, void *pw)
1125
/*******************************************************************/
1127
_kernel_swi_regs regs;
1128
_kernel_oserror *error;
1132
sample_rate_index = InitSound16(22050);
1134
if(initialise() < 0)
1136
// exit module, errblk.errmess is set by initialise()
1137
errblk.errnum = 0x101;
1142
LoadVoice("default",0);
1143
SetSpeed(global_speed,3);
1145
WavegenSetVoice(voice);
1146
atexit(terminate_module);
1148
} /* end of user_init */