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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
|
/*Id: This code comes from atcleci.cpp 4231 2006-10-13 02:43:46Z tv.raman.tv */
// Jan 2007 Gilles Casse <gcasse@oralux.org>
// * eSpeak driver for emacspeak
//
// Mar 2007
// * Language switching.
//
//<copyright info
/* 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.
*/
//>
//<includes
#include <sys/time.h>
#include <dlfcn.h>
#include <tcl.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <assert.h>
#include "speak_lib.h"
#ifndef ESPEAK_API_REVISION
#define ESPEAK_API_REVISION 1
#endif
#include <string>
using std::string;
#define PACKAGENAME "tts"
#define PACKAGEVERSION "1.0"
#define EXPORT
//using namespace std;
//>
//<decls and function prototypes
extern "C" EXPORT int Tclespeak_Init (Tcl_Interp * interp);
int SetRate (ClientData, Tcl_Interp *, int, Tcl_Obj * CONST[]);
int GetRate (ClientData, Tcl_Interp *, int, Tcl_Obj * CONST[]);
int getTTSVersion (ClientData, Tcl_Interp *, int, Tcl_Obj * CONST[]);
int Punct (ClientData, Tcl_Interp *, int, Tcl_Obj * CONST[]);
int Caps (ClientData, Tcl_Interp *, int, Tcl_Obj * CONST[]);
int Say (ClientData, Tcl_Interp *, int, Tcl_Obj * CONST[]);
int SetLanguage (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[]);
static void initLanguage (Tcl_Interp * interp);
static int getLangIndex (Tcl_Interp * interp, int* theIndex);
//>
//< preferred languages
// Uncomment below your preferred languages
static const char* ThePreferredLanguages[]=
{
// "af", // afrikaans
// "cs", // czech-test
// "cy", // welsh-test
// "de", // german
// "el", // greek_test
// "en-r", // en-rhotic
// "en-sc", // en-scottish
"en-uk", // english
// "en-uk-north", // lancashire
// "en-uk-rp", // english_rp
// "en-uk-wmids", // english_wmids
// "eo", // esperanto
// "es", // spanish
// "fi" // finnish
"fr", // french-test
// "fr-ca", // quebec-test
// "hi", // hindi-test
// "hu", // hungarian
// "it", // italian
// "nl", // dutch-test
// "no", // norwegian-test
// "pl", // polish_test
// "pt", // brazil
// "pt-pt", // portugal
// "ro", // romanian
// "ro", // romanian-mbrola
// "ru", // russian_test
// "sk", // slovak-test
// "sv", // swedish-test
// "sw", // swahihi-test
// "vi", // vietnam-test
// "zh", // cantonese-test
};
#define MaxPreferredLang (int)(sizeof(ThePreferredLanguages)/sizeof(ThePreferredLanguages[0]))
//>
//<TclEspeakFree
void
TclEspeakFree (ClientData handle)
{
espeak_Terminate();
}
//>
//<Tclespeak_init
int
Tclespeak_Init (Tcl_Interp * interp)
{
int rc;
void* handle=NULL;
//<setup package, create tts handle
if (Tcl_PkgProvide (interp, PACKAGENAME, PACKAGEVERSION) != TCL_OK)
{
Tcl_AppendResult (interp, "Error loading ", PACKAGENAME, NULL);
return TCL_ERROR;
}
#if ESPEAK_API_REVISION == 1
espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 512, NULL);
#else
espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 512, NULL, 0);
#endif
//>
//<register tcl commands
Tcl_CreateObjCommand (interp, "setRate", SetRate,
(ClientData) handle, TclEspeakFree);
Tcl_CreateObjCommand (interp, "getRate", GetRate, (ClientData) handle,
TclEspeakFree);
Tcl_CreateObjCommand (interp, "ttsVersion", getTTSVersion,
(ClientData) handle, TclEspeakFree);
Tcl_CreateObjCommand (interp, "punct", Punct, (ClientData) handle, NULL);
Tcl_CreateObjCommand (interp, "caps", Caps, (ClientData) handle, NULL);
Tcl_CreateObjCommand (interp, "say", Say, (ClientData) handle,
TclEspeakFree);
Tcl_CreateObjCommand (interp, "synth", Say, (ClientData) handle, NULL);
Tcl_CreateObjCommand (interp, "synchronize", Synchronize,
(ClientData) handle, TclEspeakFree);
Tcl_CreateObjCommand (interp, "stop", Stop, (ClientData) handle,
TclEspeakFree);
Tcl_CreateObjCommand (interp, "speakingP", SpeakingP,
(ClientData) handle, TclEspeakFree);
Tcl_CreateObjCommand (interp, "pause", Pause, (ClientData) handle,
TclEspeakFree);
Tcl_CreateObjCommand (interp, "resume", Resume, (ClientData) handle,
TclEspeakFree);
Tcl_CreateObjCommand (interp, "setLanguage", SetLanguage, (ClientData) handle,
TclEspeakFree);
//>
initLanguage (interp);
//<set up index processing
rc = Tcl_Eval (interp, "proc index x {global tts; \
set tts(last_index) $x}");
//>
return TCL_OK;
}
int
GetRate (ClientData handle, 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 = espeak_GetParameter(espeakRATE, 1);
Tcl_SetObjResult (interp, Tcl_NewIntObj (rate));
return TCL_OK;
}
int
SetRate (ClientData handle, 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;
return (espeak_SetParameter(espeakRATE, rate, 0) == EE_OK) ? TCL_OK : TCL_ERROR;
}
//>
//<say
static bool closeTags(string input, string& output)
{
char* tag_orig = strdup(input.c_str());
output = "";
// check that a text (non whitespace) is present
char* tag = tag_orig;
int a_tag_count=0;
bool a_text_is_present = false;
while (*tag)
{
if (*tag == '<')
{
a_tag_count++;
}
if((a_tag_count == 0)
&& (*tag != ' ')
&& (*tag != '\n')
&& (*tag != '\r')
&& (*tag != '\t'))
{
a_text_is_present = true;
break;
}
if ((*tag == '>') && a_tag_count)
{
a_tag_count--;
}
tag++;
}
if (a_text_is_present)
{
tag = tag_orig;
while (tag)
{
// look for a '<'
tag = strrchr(tag_orig, '<');
if (tag)
{
char* end = strchr(tag, ' ');
if (!end && (NULL == strchr(tag, '/')))
{
end = strchr(tag, '>');
}
if (end && (tag+1<end))
{
*end=0;
output += "</" + string(tag+1) + ">";
}
*tag = 0;
}
}
}
free(tag_orig);
return a_text_is_present;
}
int
Say (ClientData handle, 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"))
{
//TBD
// espeakReset (handle);
// if ((espeakSetParam (handle, espeakInputType, 1) == -1)
// || (espeakSetParam (handle, espeakSynthMode, 1) == -1)
// || (espeakSetParam (handle, espeakSampleRate, 1) == -1))
// {
// Tcl_AppendResult (interp, "Could not re-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;
// TBD
// rc = espeakInsertIndex (handle, index);
// if (!rc)
// {
// Tcl_AppendResult (interp, "Could not insert index", TCL_STATIC);
// return TCL_ERROR;
// }
}
else
{
// TBD: need a forthcoming eSpeak service (a deffered espeak_Synth).
char* a_text = (char*)Tcl_GetStringFromObj (objv[i], NULL);
if (a_text)
{
string a_begin_ssml = a_text;
string a_end_ssml;
if (closeTags(a_begin_ssml, a_end_ssml))
{
string a_ssml = a_begin_ssml + a_end_ssml;
unsigned int unique_identifier=0;
espeak_Synth(a_ssml.c_str(), a_ssml.length()+1, 0, POS_CHARACTER, 0, espeakCHARS_UTF8|espeakSSML, &unique_identifier, NULL);
}
// TBD:: EE_BUFFER_FULL?
}
}
}
// TBD
if (Tcl_StringMatch (Tcl_GetStringFromObj (objv[0], NULL), "synth"))
{
// TBD: need a forthcoming eSpeak service.
// rc = espeakSynthesize (handle);
// if (!rc)
// {
// Tcl_SetResult (interp, "Internal tts synth error", TCL_STATIC);
// return TCL_ERROR;
// }
}
return TCL_OK;
}
//>
//<stop, pause, resume
//<synchronize, stop
int
Synchronize (ClientData handle,
Tcl_Interp * interp, int objc, Tcl_Obj * CONST objv[])
{
espeak_Synchronize();
return TCL_OK;
}
int
Stop (ClientData handle,
Tcl_Interp * interp, int objc, Tcl_Obj * CONST objv[])
{
espeak_Cancel();
return TCL_OK;
}
//>
int
SpeakingP (ClientData handle, Tcl_Interp * interp, int objc,
Tcl_Obj * CONST objv[])
{
if (espeak_IsPlaying())
{
Tcl_SetObjResult (interp, Tcl_NewIntObj (1));
}
else
{
Tcl_SetObjResult (interp, Tcl_NewIntObj (0));
}
return TCL_OK;
}
int
Pause (ClientData handle, Tcl_Interp * interp, int objc,
Tcl_Obj * CONST objv[])
{
// TBD: need a forthcoming eSpeak service.
return TCL_OK;
}
int
Resume (ClientData handle, Tcl_Interp * interp, int objc,
Tcl_Obj * CONST objv[])
{
// TBD: need a forthcoming eSpeak service.
return TCL_OK;
}
//>
//<setOutput:NoOp
int
setOutput (ClientData handle, Tcl_Interp * interp, int objc,
Tcl_Obj * CONST objv[])
{
return TCL_OK;
}
//>
//< Caps
int
Caps (ClientData handle,
Tcl_Interp * interp, int objc, Tcl_Obj * CONST objv[])
{
char* a_mode = (char*)Tcl_GetStringFromObj (objv[1], NULL);
if (a_mode)
{
int a_type = 0; // none
if (strcmp(a_mode,"tone") == 0)
{
a_type = 1;
}
else if (strcmp(a_mode,"spelling") == 0)
{
a_type = 2;
}
else if (strcmp(a_mode,"pitch") == 0)
{
a_type = 30;
}
espeak_SetParameter(espeakCAPITALS, a_type, 0);
}
return TCL_OK;
}
//>
//< Punct
int
Punct (ClientData handle,
Tcl_Interp * interp, int objc, Tcl_Obj * CONST objv[])
{
char* a_mode = (char*)Tcl_GetStringFromObj (objv[1], NULL);
if (a_mode)
{
espeak_PUNCT_TYPE a_type = espeakPUNCT_NONE;
if (strcmp(a_mode,"none")==0)
{
a_type = espeakPUNCT_NONE;
}
else if (strcmp(a_mode,"all")==0)
{
a_type = espeakPUNCT_ALL;
}
else if (strcmp(a_mode,"some")==0)
{
a_type = espeakPUNCT_SOME;
}
espeak_SetParameter(espeakPUNCTUATION, a_type, 0);
}
return TCL_OK;
}
//>
//<getVersion
int
getTTSVersion (ClientData handle, Tcl_Interp * interp,
int objc, Tcl_Obj * CONST objv[])
{
if (objc != 1)
{
Tcl_AppendResult (interp, "Usage: ttsVersion ", TCL_STATIC);
return TCL_ERROR;
}
// TBD: need a forthcoming eSpeak service.
char *version = (char *) malloc (16);
strcpy(version, "????");
Tcl_SetResult (interp, version, TCL_STATIC);
return TCL_OK;
}
//>
//<SetLanguage
int SetLanguage (ClientData eciHandle, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[])
{
int aIndex=0;
if (getLangIndex( interp, &aIndex))
{
espeak_VOICE a_voice;
memset( &a_voice, 0, sizeof(espeak_VOICE));
a_voice.languages = (char*)(ThePreferredLanguages[aIndex]);
a_voice.gender = 1;
espeak_SetVoiceByProperties(&a_voice);
}
return TCL_OK;
}
//>
//<initLanguage, getLangIndex
static void
initLanguage (Tcl_Interp * interp)
{
// List the available languages
int i=0;
int j=0;
char* aDefaultLang = (char*)getenv("LANGUAGE");
if (aDefaultLang == NULL)
{
aDefaultLang = (char*)getenv("LANG");
if (aDefaultLang == NULL)
{
aDefaultLang = "en";
}
}
Tcl_SetVar2(interp, "langsynth", "current", "0", 0);
const espeak_VOICE **voices = espeak_ListVoices(NULL);
int langInfoMax = 0;
for (i = 0; voices[i] != NULL; i++)
{
char buffer_i[3];
snprintf(buffer_i, 3, "%d", i);
Tcl_SetVar2(interp, "langalias", voices[i]->languages, buffer_i, 0);
}
langInfoMax = i;
int aLang;
for (aLang = 0; aLang < MaxPreferredLang; aLang++)
{
char buffer_i[3];
char buffer_j[3];
for (i = 0; i < langInfoMax; i++)
{
if (voices[i]
&& voices[i]->languages
&& (strcmp(1 + voices[i]->languages, ThePreferredLanguages[aLang]) == 0))
break;
}
if(i == langInfoMax)
{
continue;
}
char* aLangCode = 1 + voices[i]->languages;
snprintf(buffer_i, 3, "%d", aLang);
snprintf(buffer_j, 3, "%d", j++);
Tcl_SetVar2(interp, "langsynth", buffer_j, buffer_i, 0);
if (strncmp(aDefaultLang, aLangCode, 2) == 0)
{
Tcl_SetVar2(interp, "langsynth", "current", buffer_i, 0);
Tcl_SetVar2(interp, "langcode", "current", aLangCode, 0);
}
Tcl_SetVar2(interp, "langlabel", buffer_j, voices[i]->name, 0);
Tcl_SetVar2(interp, "langcode", buffer_j, aLangCode, 0);
Tcl_SetVar2(interp, "langsynth", "top", buffer_j, 0);
}
}
static int
getLangIndex (Tcl_Interp * interp, int* theIndex)
{
int aStatus = 0;
const char* aInfo = Tcl_GetVar2(interp, "langsynth", "current", 0);
if (aInfo)
{
*theIndex = atoi(aInfo);
if ((*theIndex > 0) && (*theIndex < MaxPreferredLang))
{
aStatus = 1;
}
}
return aStatus;
}
//>
//<end of file
//local variables:
//folded-file: t
//end:
//>
|