File: espeak_command.cpp

package info (click to toggle)
espeak 1.48.04%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,040 kB
  • ctags: 3,332
  • sloc: cpp: 31,660; ansic: 598; makefile: 162
file content (707 lines) | stat: -rwxr-xr-x 16,147 bytes parent folder | download | duplicates (13)
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/***************************************************************************
 *   Copyright (C) 2007, Gilles Casse <gcasse@oralux.org>                  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "speech.h"

#ifdef USE_ASYNC
// This source file is only used for asynchronious modes

#include "espeak_command.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <wchar.h>

#include "debug.h"


static unsigned int my_current_text_id=0;


//<create_espeak_text
t_espeak_command* create_espeak_text(const void *text, size_t size, unsigned int position, espeak_POSITION_TYPE position_type, unsigned int end_position, unsigned int flags, void* user_data)
{
  ENTER("create_espeak_text");
  int a_error=1;
  void* a_text = NULL;
  t_espeak_text* data = NULL;
  t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));

  if (!text || !size || !a_command)
    {
      goto text_error;
    }

  a_text = malloc( size+1 );
  if (!a_text)
    {
      goto text_error;
    }
  memcpy(a_text, text, size);

  a_command->type = ET_TEXT;
  a_command->state = CS_UNDEFINED;
  data = &(a_command->u.my_text);
  data->unique_identifier = ++my_current_text_id;
  data->text = a_text;
  data->size = size;
  data->position = position;
  data->position_type = position_type;
  data->end_position = end_position;
  data->flags = flags;
  data->user_data = user_data;
  a_error=0;

  SHOW("ET_TEXT malloc text=%x, command=%x (uid=%d)\n", a_text, a_command, data->unique_identifier);

 text_error:
  if (a_error)
    {
      if (a_text)
	{
	  free (a_text);
	}
      if (a_command)
	{
	  free (a_command);
	}
      a_command = NULL;
    }

  SHOW("command=0x%x\n", a_command);

  return a_command;
}

//>


t_espeak_command* create_espeak_terminated_msg(unsigned int unique_identifier, void* user_data)
{
  ENTER("create_espeak_terminated_msg");
  int a_error=1;
  t_espeak_terminated_msg* data = NULL;
  t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));

  if (!a_command)
    {
      goto msg_error;
    }

  a_command->type = ET_TERMINATED_MSG;
  a_command->state = CS_UNDEFINED;
  data = &(a_command->u.my_terminated_msg);
  data->unique_identifier = unique_identifier;
  data->user_data = user_data;
  a_error=0;

  SHOW("ET_TERMINATED_MSG command=%x (uid=%d, user_data=0x%x)\n", a_command, unique_identifier, (int)user_data);

 msg_error:
  if (a_error)
    {
      if (a_command)
	{
	  free (a_command);
	}
      a_command = NULL;
    }

  SHOW("command=0x%x\n", a_command);

  return a_command;

}




//<create_espeak_mark
t_espeak_command* create_espeak_mark(const void *text, size_t size, const char *index_mark, unsigned int end_position, unsigned int flags, void* user_data)
{
  ENTER("create_espeak_mark");
  int a_error=1;
  void* a_text = NULL;
  char *a_index_mark = NULL;
  t_espeak_mark* data = NULL;
  t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));

  if (!text || !size || !index_mark || !a_command)
    {
      goto mark_error;
    }

  a_text = malloc( size );
  if (!a_text)
    {
      goto mark_error;
    }
  memcpy(a_text, text, size);

  a_index_mark = strdup( index_mark);

  a_command->type = ET_MARK;
  a_command->state = CS_UNDEFINED;
  data = &(a_command->u.my_mark);
  data->unique_identifier = ++my_current_text_id;
  data->text = a_text;
  data->size = size;
  data->index_mark = a_index_mark;
  data->end_position = end_position;
  data->flags = flags;
  data->user_data = user_data;
  a_error=0;

 mark_error:
  if (a_error)
    {
      if (a_text)
	{
	  free (a_text);
	}
      if (a_command)
	{
	  free (a_command);
	}
      a_command = NULL;
      if (a_index_mark)
	{
	  free (a_index_mark);
	}
    }

  SHOW("ET_MARK malloc text=%x, command=%x (uid=%d)\n", a_text, a_command, data->unique_identifier);

  return a_command;
}
//>
//< create_espeak_key, create_espeak_char

t_espeak_command* create_espeak_key(const char *key_name, void *user_data)
{
  ENTER("create_espeak_key");
  int a_error=1;
  t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));

  if (!key_name || !a_command)
    {
      goto key_error;
    }

  a_command->type = ET_KEY;
  a_command->state = CS_UNDEFINED;
  a_command->u.my_key.user_data = user_data;
  a_command->u.my_key.unique_identifier = ++my_current_text_id;
  a_command->u.my_key.key_name = strdup( key_name);
  a_error=0;

 key_error:
  if (a_error)
    {
      if (a_command)
	{
	  free (a_command);
	}
      a_command = NULL;
    }

  SHOW("command=0x%x\n", a_command);

  return a_command;
}

t_espeak_command* create_espeak_char(wchar_t character, void* user_data)
{
  ENTER("create_espeak_char");
  int a_error=1;
  t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));
  if (!a_command)
    {
      goto char_error;
    }

  a_command->type = ET_CHAR;
  a_command->state = CS_UNDEFINED;
  a_command->u.my_char.user_data = user_data;
  a_command->u.my_char.unique_identifier = ++my_current_text_id;
  a_command->u.my_char.character = character;
  a_error=0;

 char_error:
  if (a_error)
    {
      if (a_command)
	{
	  free (a_command);
	}
      a_command = NULL;
    }

  SHOW("command=0x%x\n", a_command);

  return a_command;
}

//>
//< create_espeak_parameter

t_espeak_command* create_espeak_parameter(espeak_PARAMETER parameter, int value, int relative)
{
  ENTER("create_espeak_parameter");
  int a_error=1;
  t_espeak_parameter* data = NULL;
  t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));
  if (!a_command)
    {
      goto param_error;
    }

  a_command->type = ET_PARAMETER;
  a_command->state = CS_UNDEFINED;
  data = &(a_command->u.my_param);
  data->parameter = parameter;
  data->value = value;
  data->relative = relative;
  a_error=0;

 param_error:
  if (a_error)
    {
      if (a_command)
	{
	  free (a_command);
	}
      a_command = NULL;
    }

  SHOW("command=0x%x\n", a_command);

  return a_command;
}

//>
//< create_espeak_punctuation_list

t_espeak_command* create_espeak_punctuation_list(const wchar_t *punctlist)
{
  ENTER("create_espeak_punctuation_list");
  int a_error=1;
  //  wchar_t *a_list = NULL;
  t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));

  if (!punctlist || !a_command)
    {
      goto list_error;
    }

  a_command->type = ET_PUNCTUATION_LIST;
  a_command->state = CS_UNDEFINED;

  {
    size_t len = (wcslen(punctlist) + 1)*sizeof(wchar_t);
    wchar_t* a_list = (wchar_t*)malloc(len);
    memcpy(a_list, punctlist, len);
    a_command->u.my_punctuation_list = a_list;
  }

  a_error=0;

 list_error:
  if (a_error)
    {
      if (a_command)
	{
	  free (a_command);
	}
      a_command = NULL;
    }

  SHOW("command=0x%x\n", a_command);

  return a_command;
}

//>
//< create_espeak_voice_name, create_espeak_voice_spec

t_espeak_command* create_espeak_voice_name(const char *name)
{
  ENTER("create_espeak_voice_name");

  int a_error=1;
  t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));

  if (!name || !a_command)
    {
      goto name_error;
    }

  a_command->type = ET_VOICE_NAME;
  a_command->state = CS_UNDEFINED;
  a_command->u.my_voice_name = strdup( name);
  a_error=0;

 name_error:
  if (a_error)
    {
      if (a_command)
	{
	  free (a_command);
	}
      a_command = NULL;
    }

  SHOW("command=0x%x\n", a_command);

  return a_command;
}

t_espeak_command* create_espeak_voice_spec(espeak_VOICE *voice)
{
  ENTER("create_espeak_voice_spec");
  int a_error=1;
  t_espeak_command* a_command = (t_espeak_command*)malloc(sizeof(t_espeak_command));

  if (!voice || !a_command)
    {
      goto spec_error;
    }

  a_command->type = ET_VOICE_SPEC;
  a_command->state = CS_UNDEFINED;
  {
    espeak_VOICE* data = &(a_command->u.my_voice_spec);
    memcpy(data, voice, sizeof(espeak_VOICE));

    if (voice->name)
      {
	data->name = strdup(voice->name);
      }

    if (voice->languages)
      {
	data->languages = strdup(voice->languages);
      }

    if (voice->identifier)
      {
	data->identifier = strdup(voice->identifier);
      }

    a_error=0;
  }

 spec_error:
  if (a_error)
    {
      if (a_command)
	{
	  free (a_command);
	}
      a_command = NULL;
    }

  SHOW("command=0x%x\n", a_command);

  return a_command;
}

//>
//< delete_espeak_command
int delete_espeak_command( t_espeak_command* the_command)
{
  ENTER("delete_espeak_command");
  int a_status = 0;
  if (the_command)
    {
      switch(the_command->type)
	{
	case ET_TEXT:
	  if (the_command->u.my_text.text)
	    {
	      SHOW("delete_espeak_command > ET_TEXT free text=%x, command=%x, uid=%d\n", the_command->u.my_text.text, the_command, the_command->u.my_text.unique_identifier);
	      free(the_command->u.my_text.text);
	    }
	  break;

	case ET_MARK:
	  if (the_command->u.my_mark.text)
	    {
	      free(the_command->u.my_mark.text);
	    }
	  if (the_command->u.my_mark.index_mark)
	    {
	      free((void*)(the_command->u.my_mark.index_mark));
	    }
	  break;

	case ET_TERMINATED_MSG:
	  {
	    // if the terminated msg is pending,
	    // it must be processed here for informing the calling program
	    // that its message is finished.
	    // This can be important for cleaning the related user data.
	    t_espeak_terminated_msg* data = &(the_command->u.my_terminated_msg);
	    if (the_command->state == CS_PENDING)
	      {
		the_command->state = CS_PROCESSED;
		SHOW("delete_espeak_command > ET_TERMINATED_MSG callback (command=0x%x, uid=%d) \n", the_command, data->unique_identifier);
		sync_espeak_terminated_msg( data->unique_identifier, data->user_data);
	      }
	  }
	  break;

	case ET_KEY:
	  if (the_command->u.my_key.key_name)
	    {
	      free((void*)(the_command->u.my_key.key_name));
	    }
	  break;

	case ET_CHAR:
	case ET_PARAMETER:
	  // No allocation
	  break;

	case ET_PUNCTUATION_LIST:
	  if (the_command->u.my_punctuation_list)
	    {
	      free((void*)(the_command->u.my_punctuation_list));
	    }
	  break;

	case ET_VOICE_NAME:
	  if (the_command->u.my_voice_name)
	  {
	    free((void*)(the_command->u.my_voice_name));
	  }
	  break;

	case ET_VOICE_SPEC:
	  {
		espeak_VOICE* data = &(the_command->u.my_voice_spec);

		if (data->name)
		{
			free((void *)data->name);
		}

		if (data->languages)
		{
			free((void *)data->languages);
		}

		if (data->identifier)
		{
			free((void *)data->identifier);
		}
	  }
	  break;

	default:
	  assert(0);
	}
      SHOW("delete_espeak_command > free command=0x%x\n", the_command);
      free(the_command);
      a_status = 1;
    }
  return a_status;
}
//>
//< process_espeak_command
void process_espeak_command( t_espeak_command* the_command)
{
  ENTER("process_espeak_command");

  SHOW("command=0x%x\n", the_command);

  if (the_command == NULL)
    {
      return;
    }

  the_command->state = CS_PROCESSED;

  switch(the_command->type)
    {
    case ET_TEXT:
      {
	t_espeak_text* data = &(the_command->u.my_text);
	sync_espeak_Synth( data->unique_identifier, data->text, data->size,
			   data->position, data->position_type,
			   data->end_position, data->flags, data->user_data);
      }
      break;

    case ET_MARK:
      {
	t_espeak_mark* data = &(the_command->u.my_mark);
	sync_espeak_Synth_Mark( data->unique_identifier, data->text, data->size,
				data->index_mark, data->end_position, data->flags,
				data->user_data);
      }
      break;

    case ET_TERMINATED_MSG:
      {
	t_espeak_terminated_msg* data = &(the_command->u.my_terminated_msg);
	sync_espeak_terminated_msg( data->unique_identifier, data->user_data);
      }
      break;

    case ET_KEY:
      {
	const char* data = the_command->u.my_key.key_name;
	sync_espeak_Key(data);
      }
      break;

    case ET_CHAR:
      {
	const wchar_t data = the_command->u.my_char.character;
	sync_espeak_Char( data);
      }
      break;

    case ET_PARAMETER:
      {
	t_espeak_parameter* data = &(the_command->u.my_param);
	SetParameter( data->parameter, data->value, data->relative);
      }
      break;

    case ET_PUNCTUATION_LIST:
      {
	const wchar_t* data = the_command->u.my_punctuation_list;
	sync_espeak_SetPunctuationList( data);
      }
      break;

    case ET_VOICE_NAME:
      {
	const char* data = the_command->u.my_voice_name;
	SetVoiceByName( data);
      }
      break;

    case ET_VOICE_SPEC:
      {
	espeak_VOICE* data = &(the_command->u.my_voice_spec);
	SetVoiceByProperties(data);
      }
      break;

    default:
      assert(0);
      break;
    }
}

//>

//< process_espeak_command
void display_espeak_command( t_espeak_command* the_command)
{
  ENTER("display_espeak_command");
#ifdef DEBUG_ENABLED
  if (the_command == NULL)
    {
      SHOW("display_espeak_command > command=%s\n","NULL");
      return;
    }

  SHOW("display_espeak_command > state=%d\n",the_command->state);

  switch(the_command->type)
    {
    case ET_TEXT:
      {
	t_espeak_text* data = &(the_command->u.my_text);
	SHOW("display_espeak_command > (0x%x) uid=%d, TEXT=%s, user_data=0x%x\n", the_command, data->unique_identifier, (char*)data->text, (int)(data->user_data));
      }
      break;

    case ET_MARK:
      {
	t_espeak_mark* data = &(the_command->u.my_mark);
	SHOW("display_espeak_command > (0x%x) uid=%d, MARK=%s, user_data=0x%x\n", the_command, data->unique_identifier, (char*)data->text, (int)(data->user_data));
      }
      break;

    case ET_KEY:
      {
	const char* data = the_command->u.my_key.key_name;
	SHOW("display_espeak_command > (0x%x) KEY=%c\n", the_command, data);
      }
      break;

    case ET_TERMINATED_MSG:
      {
	t_espeak_terminated_msg* data = &(the_command->u.my_terminated_msg);

	SHOW("display_espeak_command > (0x%x) TERMINATED_MSG uid=%d, user_data=0x%x, state=%d\n",
	     the_command, data->unique_identifier, data->user_data,
	     the_command->state);
      }
      break;

    case ET_CHAR:
      {
	const wchar_t data = the_command->u.my_char.character;
	SHOW("display_espeak_command > (0x%x) CHAR=%c\n", the_command, (char)data);
      }
      break;

    case ET_PARAMETER:
      {
	t_espeak_parameter* data = &(the_command->u.my_param);
	SHOW("display_espeak_command > (0x%x) PARAMETER=%d, value=%d, relative=%d\n",
	     the_command, data->parameter, data->value, data->relative);
      }
      break;

    case ET_PUNCTUATION_LIST:
      {
	const wchar_t* data = the_command->u.my_punctuation_list;
	sync_espeak_SetPunctuationList( data);
	SHOW("display_espeak_command > (0x%x) PUNCTLIST=%s\n", the_command, (char*)data);
      }
      break;

    case ET_VOICE_NAME:
      {
	const char* data = the_command->u.my_voice_name;
	SHOW("display_espeak_command > (0x%x) VOICE_NAME=%s\n", the_command, data);
      }
      break;

    case ET_VOICE_SPEC:
      {
	SHOW("display_espeak_command > (0x%x) VOICE_SPEC", the_command);
      }
      break;

    default:
      assert(0);
      break;
    }
#endif
}

#endif
//>