eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

error.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Error handling APIs.
  2. *
  3. * Copyright (C) 2016 Reece H. Dunn
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see: <http://www.gnu.org/licenses/>.
  17. */
  18. #include "config.h"
  19. #include <errno.h>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <espeak-ng/espeak_ng.h>
  25. #include "error.h"
  26. #include "speech.h"
  27. #include "phoneme.h"
  28. #include "synthesize.h"
  29. espeak_ng_STATUS
  30. create_file_error_context(espeak_ng_ERROR_CONTEXT *context,
  31. espeak_ng_STATUS status,
  32. const char *filename)
  33. {
  34. if (context) {
  35. if (*context) {
  36. free((*context)->name);
  37. } else {
  38. *context = malloc(sizeof(espeak_ng_ERROR_CONTEXT_));
  39. if (!*context)
  40. return ENOMEM;
  41. }
  42. (*context)->type = ERROR_CONTEXT_FILE;
  43. (*context)->name = strdup(filename);
  44. (*context)->version = 0;
  45. (*context)->expected_version = 0;
  46. }
  47. return status;
  48. }
  49. espeak_ng_STATUS
  50. create_version_mismatch_error_context(espeak_ng_ERROR_CONTEXT *context,
  51. const char *path_home,
  52. int version,
  53. int expected_version)
  54. {
  55. if (context) {
  56. if (*context) {
  57. free((*context)->name);
  58. } else {
  59. *context = malloc(sizeof(espeak_ng_ERROR_CONTEXT_));
  60. if (!*context)
  61. return ENOMEM;
  62. }
  63. (*context)->type = ERROR_CONTEXT_VERSION;
  64. (*context)->name = strdup(path_home);
  65. (*context)->version = version;
  66. (*context)->expected_version = expected_version;
  67. }
  68. return ENS_VERSION_MISMATCH;
  69. }
  70. #pragma GCC visibility push(default)
  71. ESPEAK_NG_API void
  72. espeak_ng_ClearErrorContext(espeak_ng_ERROR_CONTEXT *context)
  73. {
  74. if (context && *context) {
  75. free((*context)->name);
  76. free(*context);
  77. *context = NULL;
  78. }
  79. }
  80. ESPEAK_NG_API void
  81. espeak_ng_GetStatusCodeMessage(espeak_ng_STATUS status,
  82. char *buffer,
  83. size_t length)
  84. {
  85. switch (status)
  86. {
  87. case ENS_COMPILE_ERROR:
  88. strncpy0(buffer, "Compile error", length);
  89. break;
  90. case ENS_VERSION_MISMATCH:
  91. strncpy0(buffer, "Wrong version of espeak-ng-data", length);
  92. break;
  93. case ENS_FIFO_BUFFER_FULL:
  94. strncpy0(buffer, "The FIFO buffer is full", length);
  95. break;
  96. case ENS_NOT_INITIALIZED:
  97. strncpy0(buffer, "The espeak-ng library has not been initialized", length);
  98. break;
  99. case ENS_AUDIO_ERROR:
  100. strncpy0(buffer, "Cannot initialize the audio device", length);
  101. break;
  102. case ENS_VOICE_NOT_FOUND:
  103. strncpy0(buffer, "The specified espeak-ng voice does not exist", length);
  104. break;
  105. case ENS_MBROLA_NOT_FOUND:
  106. strncpy0(buffer, "Could not load the mbrola.dll file", length);
  107. break;
  108. case ENS_MBROLA_VOICE_NOT_FOUND:
  109. strncpy0(buffer, "Could not load the specified mbrola voice file", length);
  110. break;
  111. case ENS_EVENT_BUFFER_FULL:
  112. strncpy0(buffer, "The event buffer is full", length);
  113. break;
  114. case ENS_NOT_SUPPORTED:
  115. strncpy0(buffer, "The requested functionality has not been built into espeak-ng", length);
  116. break;
  117. case ENS_UNSUPPORTED_PHON_FORMAT:
  118. strncpy0(buffer, "The phoneme file is not in a supported format", length);
  119. break;
  120. case ENS_NO_SPECT_FRAMES:
  121. strncpy0(buffer, "The spectral file does not contain any frame data", length);
  122. break;
  123. case ENS_EMPTY_PHONEME_MANIFEST:
  124. strncpy0(buffer, "The phoneme manifest file does not contain any phonemes", length);
  125. break;
  126. case ENS_UNKNOWN_PHONEME_FEATURE:
  127. strncpy0(buffer, "The phoneme feature is not recognised", length);
  128. break;
  129. case ENS_UNKNOWN_TEXT_ENCODING:
  130. strncpy0(buffer, "The text encoding is not supported", length);
  131. break;
  132. default:
  133. if ((status & ENS_GROUP_MASK) == ENS_GROUP_ERRNO)
  134. strerror_r(status, buffer, length);
  135. else
  136. snprintf(buffer, length, "Unspecified error 0x%x", status);
  137. break;
  138. }
  139. }
  140. ESPEAK_NG_API void
  141. espeak_ng_PrintStatusCodeMessage(espeak_ng_STATUS status,
  142. FILE *out,
  143. espeak_ng_ERROR_CONTEXT context)
  144. {
  145. char error[512];
  146. espeak_ng_GetStatusCodeMessage(status, error, sizeof(error));
  147. if (context) {
  148. switch (context->type)
  149. {
  150. case ERROR_CONTEXT_FILE:
  151. fprintf(out, "Error processing file '%s': %s.\n", context->name, error);
  152. break;
  153. case ERROR_CONTEXT_VERSION:
  154. fprintf(out, "Error: %s at '%s' (expected 0x%x, got 0x%x).\n",
  155. error, context->name, context->expected_version, context->version);
  156. break;
  157. }
  158. } else
  159. fprintf(out, "Error: %s.\n", error);
  160. }
  161. #pragma GCC visibility pop