File: tcleci.cpp

package info (click to toggle)
emacspeak 15-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,596 kB
  • ctags: 3,499
  • sloc: lisp: 33,875; makefile: 592; sh: 495; tcl: 492; perl: 351; cpp: 239
file content (323 lines) | stat: -rw-r--r-- 12,375 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*$Id: tcleci.cpp,v 15.0 2001/11/20 20:05:31 raman Exp $*/
/* Tcl ViaVoiceOutloud Interface program
   (c) Copyright 1999 by Paige Phault

The author hereby grants permission to use, copy, modify, distribute, and 
license this software for any purpose, provided that existing copyright notices
are retained in all copies and that this notice is included verbatim in any 
distributions. No written agreement, license, or royalty fee is required for 
any of the authorized uses.  Modifications to this software may be copyrighted 
by their authors and need not follow the licensing terms described here, 
provided that the new terms are clearly indicated on the first page of each 
file where they apply.

IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR 
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 
OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, 
EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, 
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN 
"AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE 
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

dynamic loading of eci library contributed by Jeffrey Sorensen
--this allows a compiled version  of this 
speech server to be distributed without violating the IBM
Viavoice license.
This means that end-users only need install the Viavoice RTK
(Runtime toolkit)
to use Emacspeak with the ViaVoice TTS engine.
*/
/* TCL usage
	package require tts

	proc index x {
		puts "I just played index $x"
	}

	synth "Hello world"
	synth -index 0 "This is some" -index 1 "really wierd"
	say -index 2 "text"
	say -reset

The only difference bewtween say and synth is that synth calls
eciSynthesize and say doesn't.  You can put as many text blocks as
you like after a command.
*/

#include <tcl.h>
#include <dlfcn.h>

#define PACKAGENAME "tts"
#define PACKAGEVERSION "1.0"

#ifdef WIN32
#define EXPORT __declspec(dllexport) 
#else
#define EXPORT
#endif

#define ECILIBRARYNAME "/usr/lib/libibmeci50.so"

/* The following declarations are derived from the publically available
   documentation for ViaVoice TTS outloud.  
--they are placed here to obviate the need for having the
ViaVoice SDK installed.
*/

static void *(*_eciNew)();
static void (*_eciDelete)(void *);
static int (*_eciReset)(void *);
static int (*_eciStop)(void *);
static int (*_eciPause)(void *, int);
static int (*_eciSynthesize)(void *);
static int (*_eciSynchronize)(void *);
static int (*_eciSpeaking)(void *);
static int (*_eciAddText)(void *, char *);
static int (*_eciInsertIndex)(void *,int);
static int (*_eciSetParam)(void *, int, int);
static int (*_eciGetVoiceParam)(void *, int, int); 
static int (*_eciSetVoiceParam)(void *, int, int, int); 
static void (*_eciRegisterCallback)(void*,int(*)(void*,int,long,void*),void*);

extern "C" EXPORT int Tcleci_Init(Tcl_Interp *interp);
int SetRate(ClientData, Tcl_Interp *, int, Tcl_Obj * CONST []);
int GetRate(ClientData, Tcl_Interp *, int, Tcl_Obj * CONST []);
int Say(ClientData, Tcl_Interp *, int, Tcl_Obj * CONST []);
int Stop(ClientData, Tcl_Interp *, int, Tcl_Obj * CONST []);
int SpeakingP(ClientData, Tcl_Interp *, int, Tcl_Obj * CONST []);
int Synchronize(ClientData, Tcl_Interp *, int, Tcl_Obj * CONST []);
int Pause(ClientData, Tcl_Interp *, int, Tcl_Obj * CONST []);
int Resume(ClientData, Tcl_Interp *, int, Tcl_Obj * CONST []);
int eciCallback(void *, int, long, void *);

void TclEciFree(ClientData eciHandle) {
  _eciDelete(eciHandle);
}

int Tcleci_Init(Tcl_Interp *interp) {
  int rc;
  void *eciHandle;
  void *eciLib;

  eciLib = dlopen(ECILIBRARYNAME, RTLD_LAZY);  
  if (eciLib == NULL) {
    Tcl_AppendResult(interp, "Could not load ",
	ECILIBRARYNAME, "\nPlease install the IBM ViaVoice Outloud RTK",
	NULL);
    return TCL_ERROR;
  }
		
  _eciNew = (void *(*)()) dlsym(eciLib, "eciNew");
  _eciDelete = (void(*)(void*)) dlsym(eciLib, "eciDelete");
  _eciReset = (int (*)(void*)) dlsym(eciLib, "eciReset");
  _eciStop = (int (*)(void*)) dlsym(eciLib, "eciStop");
  _eciPause = (int (*)(void*, int)) dlsym(eciLib, "eciPause");
  _eciSynthesize = (int (*)(void*)) dlsym(eciLib, "eciSynthesize");
  _eciSynchronize = (int (*)(void*)) dlsym(eciLib, "eciSynchronize");
  _eciSpeaking = (int (*)(void*)) dlsym(eciLib, "eciSpeaking");
  _eciInsertIndex = (int(*)(void*,int)) dlsym(eciLib, "eciInsertIndex");
  _eciAddText = (int (*)(void*,char *)) dlsym(eciLib, "eciAddText");
  _eciSetParam = (int (*)(void*,int,int)) dlsym(eciLib, "eciSetParam");
  _eciGetVoiceParam = (int (*)(void*,int,int)) 
	dlsym(eciLib, "eciGetVoiceParam");
  _eciSetVoiceParam = (int (*)(void*,int, int, int)) 
	dlsym(eciLib, "eciSetVoiceParam");
  _eciRegisterCallback = (void (*)(void*,int(*)(void*,int,long,void*),void*)) 
	dlsym(eciLib, "eciRegisterCallback");

  int okay = 1;
  if(!_eciNew){okay=0; Tcl_AppendResult(interp, "eciNew undef\n", NULL);}
  if(!_eciDelete){okay=0;Tcl_AppendResult(interp, "eciDelete undef\n", NULL);}
  if(!_eciReset){okay=0;Tcl_AppendResult(interp, "eciReset undef\n", NULL);}
  if(!_eciStop){okay=0;Tcl_AppendResult(interp, "eciStop undef\n", NULL);}
  if(!_eciPause){okay=0;Tcl_AppendResult(interp, "eciPause undef\n", NULL);}
  if(!_eciSynthesize){okay=0;Tcl_AppendResult(interp, "eciSynthesize undef\n", NULL);}
  if(!_eciSpeaking){okay=0;Tcl_AppendResult(interp, "eciSpeaking undef\n", NULL);}
  if(!_eciInsertIndex){okay=0;Tcl_AppendResult(interp, "eciInsertIndex undef\n", NULL);}
  if(!_eciAddText){okay=0;Tcl_AppendResult(interp, "eciAddText undef\n", NULL);}
  if(!_eciSetParam){okay=0;Tcl_AppendResult(interp, "eciSetParam undef\n", NULL);}
  if(!_eciSetParam){okay=0;Tcl_AppendResult(interp, "eciSetParam undef\n", NULL);}
  if(!_eciGetVoiceParam){okay=0;Tcl_AppendResult(interp, "eciGetVoiceParam undef\n", NULL);}
  if(!_eciSetVoiceParam){okay=0;Tcl_AppendResult(interp, "eciSetVoiceParam undef\n", NULL);}
  if(!_eciRegisterCallback){okay=0;Tcl_AppendResult(interp, "eciRegisterCallback undef\n", NULL);}

  if (!okay) {
    Tcl_AppendResult(interp, "Missing symbols from ", ECILIBRARYNAME, NULL);
    return TCL_ERROR;
  }

  if (Tcl_PkgProvide(interp, PACKAGENAME, PACKAGEVERSION) != TCL_OK) {
    Tcl_AppendResult(interp, "Error loading ", PACKAGENAME, NULL);
    return TCL_ERROR;
  }

  eciHandle = _eciNew();
  if (eciHandle == 0) { 
    Tcl_AppendResult(interp, "Could not open text-to-speech engine", NULL);
    return TCL_ERROR;
  }

  if (   (_eciSetParam(eciHandle, 1/*eciInputType*/, 1) == -1) 
         || (_eciSetParam(eciHandle, 0/*eciSynthMode/*, 1) == -1)
         //|| (_eciSetParam(eciHandle,8/*eciRealWorldUnits*/, 1) == -1)
         ) {
    Tcl_AppendResult(interp, "Could not initialized tts", NULL);
    _eciDelete(eciHandle); 
    return TCL_ERROR;
  }

  Tcl_CreateObjCommand(interp, "setRate", SetRate,
                       (ClientData) eciHandle, TclEciFree);
  Tcl_CreateObjCommand(interp, "getRate", GetRate, (ClientData) eciHandle, TclEciFree);
  Tcl_CreateObjCommand(interp, "say", Say, (ClientData) eciHandle, TclEciFree);
  Tcl_CreateObjCommand(interp, "synth", Say, (ClientData)
                       eciHandle, NULL);
  Tcl_CreateObjCommand(interp, "synchronize", Synchronize, (ClientData) eciHandle, TclEciFree);
  Tcl_CreateObjCommand(interp, "stop", Stop, (ClientData)
                       eciHandle, TclEciFree);
  Tcl_CreateObjCommand(interp, "speakingP", SpeakingP, (ClientData) eciHandle, TclEciFree);
  Tcl_CreateObjCommand(interp, "pause", Pause, (ClientData)
                       eciHandle, TclEciFree);
  Tcl_CreateObjCommand(interp, "resume", Resume, (ClientData) eciHandle, TclEciFree);
  rc = Tcl_Eval(interp,
                "proc index x {global tts; 
set tts(last_index) $x}");
  _eciRegisterCallback( eciHandle, eciCallback, interp );
  return TCL_OK;
}

int eciCallback(void *eciHandle, int msg, long lparam, void *data) {
  int rc;
  Tcl_Interp *interp = (Tcl_Interp *) data;
  if (msg == 2 /* eciIndexReply */) {
    char buffer[128];
    sprintf(buffer, "index %d", lparam);
    rc = Tcl_Eval(interp, buffer);
    if (rc != TCL_OK) Tcl_BackgroundError(interp);
  }
  return 1;
}

int GetRate(ClientData eciHandle, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
  int  rc, rate, voice;
  if (objc!=2) {
    Tcl_AppendResult(interp, "Usage: getRate voiceCode  ", TCL_STATIC);
    return TCL_ERROR;
  }
  rc = Tcl_GetIntFromObj(interp, objv[1], &voice);
  if (rc != TCL_OK) return rc;
  rate = _eciGetVoiceParam(eciHandle, voice, 6/*eciSpeed*/);
  Tcl_SetObjResult( interp, Tcl_NewIntObj( rate ));
  return TCL_OK;
}


int SetRate(ClientData eciHandle, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
  int  rc, rate, voice ;
  if (objc!=3) {
    Tcl_AppendResult(interp, "Usage: setRate voiceCode speechRate ", TCL_STATIC);
    return TCL_ERROR;
  }
  rc = Tcl_GetIntFromObj(interp, objv[1], &voice);
  if (rc != TCL_OK) return rc;
  rc = Tcl_GetIntFromObj(interp, objv[2], &rate);
  if (rc != TCL_OK) return rc;
  fprintf(stderr, "Setting rate to %d for voice %d\n",
          rate, voice);
  rc = _eciSetVoiceParam (eciHandle, voice,  6/*eciSpeed*/, rate);
  if (rc == -1) {
    Tcl_AppendResult(interp, "Could not set rate", TCL_STATIC);
    return TCL_ERROR;
  }
  fprintf(stderr, "setRate returned %d\n", rc);
  rate = _eciGetVoiceParam(eciHandle, voice, 6/*eciSpeed*/);
  fprintf(stderr, "eciGetVoiceParam returned %d for voice %d \n",
          rate, voice );
  return TCL_OK;
}
      

int Say(ClientData eciHandle, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
  int i, rc, index, length;
  for (i=1; i<objc; i++) {
    // if string begins with -, assume it is an index value
    char *txt = Tcl_GetStringFromObj(objv[i], &length);
    if (Tcl_StringMatch(txt, "-reset")) {
      _eciReset(eciHandle);
    if (   (_eciSetParam(eciHandle, 1/*eciInputType*/, 1) == -1) 
         || (_eciSetParam(eciHandle, 0/*eciSynthMode*/, 1) == -1)) {
    Tcl_AppendResult(interp, "Could not initialized tts", NULL);
    return TCL_ERROR;
  }
    } else if (Tcl_StringMatch(txt, "-index")) {
      i++;
      if (i==objc) {
        Tcl_AppendResult(interp, "missing index parameter", TCL_STATIC);
        return TCL_ERROR;
      }
      rc = Tcl_GetIntFromObj(interp, objv[i], &index);
      if (rc != TCL_OK) return rc;
      rc = _eciInsertIndex(eciHandle, index);
      if (!rc) {
        Tcl_AppendResult(interp, "Could not insert index", TCL_STATIC);
        return TCL_ERROR;
      }
    } else {
      // assume objv[i] is text to synthesize...
      rc = _eciAddText(eciHandle, Tcl_GetStringFromObj(objv[i],NULL));
      if (!rc) {
        Tcl_SetResult(interp, "Internal tts error", TCL_STATIC);
        return TCL_ERROR;
      }
    }
  }
  if (Tcl_StringMatch(Tcl_GetStringFromObj(objv[0],NULL), "synth")) {
    rc = _eciSynthesize(eciHandle);
    if (!rc) {
      Tcl_SetResult(interp, "Internal tts synth error", TCL_STATIC);
      return TCL_ERROR;
    }
    _eciSpeaking(eciHandle); // should this be here!?
  }
  return TCL_OK;
}

int Synchronize(ClientData eciHandle, Tcl_Interp *interp,
                int objc, Tcl_Obj *CONST objv[]) {
  int rc = _eciSynchronize(eciHandle);
  if (!rc) {
    Tcl_SetResult(interp, "Internal tts synth error", TCL_STATIC);
    return TCL_ERROR;
  }
  return TCL_OK;
}

int Stop(ClientData eciHandle, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
  if (_eciStop(eciHandle)) return TCL_OK;
  Tcl_SetResult(interp, "Could not stop synthesis", TCL_STATIC);
  return TCL_ERROR;
}

int SpeakingP(ClientData eciHandle, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
  if ( _eciSpeaking(eciHandle)) {
    return 1;
  } else {
    return 0;
  }
}  

int Pause(ClientData eciHandle, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
  if (_eciPause(eciHandle, 1)) return TCL_OK;
  Tcl_SetResult(interp, "Could not pause synthesis", TCL_STATIC);
  return TCL_ERROR;
}


int Resume(ClientData eciHandle, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {
  if (_eciPause(eciHandle, 0)) return TCL_OK;
  Tcl_SetResult(interp, "Could not resume synthesis", TCL_STATIC);
  return TCL_ERROR;
}