espeak_api.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* An implementation of the eSpeak API using the espeak-ng API.
  2. *
  3. * Copyright (C) 2005 to 2013 by Jonathan Duddington
  4. * email: jonsd@users.sourceforge.net
  5. * Copyright (C) 2016 Reece H. Dunn
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see: <http://www.gnu.org/licenses/>.
  19. */
  20. #include "config.h"
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include "espeak_ng.h"
  24. #include "speak_lib.h"
  25. #include "encoding.h"
  26. #include "speech.h"
  27. #include "synthesize.h"
  28. #include "translate.h"
  29. static espeak_ERROR status_to_espeak_error(espeak_ng_STATUS status)
  30. {
  31. switch (status)
  32. {
  33. case ENS_OK: return EE_OK;
  34. case ENS_SPEECH_STOPPED: return EE_OK;
  35. case ENS_VOICE_NOT_FOUND: return EE_NOT_FOUND;
  36. case ENS_MBROLA_NOT_FOUND: return EE_NOT_FOUND;
  37. case ENS_MBROLA_VOICE_NOT_FOUND: return EE_NOT_FOUND;
  38. case ENS_FIFO_BUFFER_FULL: return EE_BUFFER_FULL;
  39. default: return EE_INTERNAL_ERROR;
  40. }
  41. }
  42. #pragma GCC visibility push(default)
  43. ESPEAK_API int espeak_Initialize(espeak_AUDIO_OUTPUT output_type, int buf_length, const char *path, int options)
  44. {
  45. espeak_ng_InitializePath(path);
  46. espeak_ng_ERROR_CONTEXT context = NULL;
  47. espeak_ng_STATUS result = espeak_ng_Initialize(&context);
  48. if (result != ENS_OK) {
  49. espeak_ng_PrintStatusCodeMessage(result, stderr, context);
  50. espeak_ng_ClearErrorContext(&context);
  51. if ((options & espeakINITIALIZE_DONT_EXIT) == 0)
  52. exit(1);
  53. }
  54. switch (output_type)
  55. {
  56. case AUDIO_OUTPUT_PLAYBACK:
  57. espeak_ng_InitializeOutput(ENOUTPUT_MODE_SPEAK_AUDIO, buf_length, NULL);
  58. break;
  59. case AUDIO_OUTPUT_RETRIEVAL:
  60. espeak_ng_InitializeOutput(static_cast<espeak_ng_OUTPUT_MODE>(0), buf_length, NULL);
  61. break;
  62. case AUDIO_OUTPUT_SYNCHRONOUS:
  63. espeak_ng_InitializeOutput(ENOUTPUT_MODE_SYNCHRONOUS, buf_length, NULL);
  64. break;
  65. case AUDIO_OUTPUT_SYNCH_PLAYBACK:
  66. espeak_ng_InitializeOutput(static_cast<espeak_ng_OUTPUT_MODE>(ENOUTPUT_MODE_SYNCHRONOUS | ENOUTPUT_MODE_SPEAK_AUDIO), buf_length, NULL);
  67. break;
  68. }
  69. option_phoneme_events = (options & (espeakINITIALIZE_PHONEME_EVENTS | espeakINITIALIZE_PHONEME_IPA));
  70. return espeak_ng_GetSampleRate();
  71. }
  72. ESPEAK_API espeak_ERROR espeak_Synth(const void *text, size_t size,
  73. unsigned int position,
  74. espeak_POSITION_TYPE position_type,
  75. unsigned int end_position, unsigned int flags,
  76. unsigned int *unique_identifier, void *user_data)
  77. {
  78. return status_to_espeak_error(espeak_ng_Synthesize(text, size, position, position_type, end_position, flags, unique_identifier, user_data));
  79. }
  80. ESPEAK_API espeak_ERROR espeak_Synth_Mark(const void *text, size_t size,
  81. const char *index_mark,
  82. unsigned int end_position,
  83. unsigned int flags,
  84. unsigned int *unique_identifier,
  85. void *user_data)
  86. {
  87. return status_to_espeak_error(espeak_ng_SynthesizeMark(text, size, index_mark, end_position, flags, unique_identifier, user_data));
  88. }
  89. ESPEAK_API espeak_ERROR espeak_Key(const char *key_name)
  90. {
  91. return status_to_espeak_error(espeak_ng_SpeakKeyName(key_name));
  92. }
  93. ESPEAK_API espeak_ERROR espeak_Char(wchar_t character)
  94. {
  95. return status_to_espeak_error(espeak_ng_SpeakCharacter(character));
  96. }
  97. ESPEAK_API espeak_ERROR espeak_SetParameter(espeak_PARAMETER parameter, int value, int relative)
  98. {
  99. return status_to_espeak_error(espeak_ng_SetParameter(parameter, value, relative));
  100. }
  101. ESPEAK_API espeak_ERROR espeak_SetPunctuationList(const wchar_t *punctlist)
  102. {
  103. return status_to_espeak_error(espeak_ng_SetPunctuationList(punctlist));
  104. }
  105. ESPEAK_API espeak_ERROR espeak_SetVoiceByName(const char *name)
  106. {
  107. return status_to_espeak_error(espeak_ng_SetVoiceByName(name));
  108. }
  109. ESPEAK_API espeak_ERROR espeak_SetVoiceByProperties(espeak_VOICE *voice_selector)
  110. {
  111. return status_to_espeak_error(espeak_ng_SetVoiceByProperties(voice_selector));
  112. }
  113. ESPEAK_API espeak_ERROR espeak_Cancel(void)
  114. {
  115. return status_to_espeak_error(espeak_ng_Cancel());
  116. }
  117. ESPEAK_API espeak_ERROR espeak_Synchronize(void)
  118. {
  119. return status_to_espeak_error(espeak_ng_Synchronize());
  120. }
  121. ESPEAK_API espeak_ERROR espeak_Terminate(void)
  122. {
  123. return status_to_espeak_error(espeak_ng_Terminate());
  124. }
  125. ESPEAK_API void espeak_CompileDictionary(const char *path, FILE *log, int flags)
  126. {
  127. espeak_ng_ERROR_CONTEXT context = NULL;
  128. espeak_ng_STATUS result = espeak_ng_CompileDictionary(path, dictionary_name, log, flags, &context);
  129. if (result != ENS_OK) {
  130. espeak_ng_PrintStatusCodeMessage(result, stderr, context);
  131. espeak_ng_ClearErrorContext(&context);
  132. }
  133. }
  134. #pragma GCC visibility pop