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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <stdio.h>
  21. #include <stdint.h>
  22. #include <string.h>
  23. #include <malloc.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 create_file_error_context(espeak_ng_ERROR_CONTEXT *context, espeak_ng_STATUS status, const char *filename)
  30. {
  31. if (context) {
  32. if (*context) {
  33. free((*context)->path);
  34. } else {
  35. *context = malloc(sizeof(espeak_ng_ERROR_CONTEXT_));
  36. }
  37. (*context)->type = ERROR_CONTEXT_FILE;
  38. (*context)->path = strdup(filename);
  39. (*context)->version = 0;
  40. (*context)->expected_version = 0;
  41. }
  42. return status;
  43. }
  44. espeak_ng_STATUS create_version_mismatch_error_context(espeak_ng_ERROR_CONTEXT *context, const char *path_home, int version, int expected_version)
  45. {
  46. if (context) {
  47. if (*context) {
  48. free((*context)->path);
  49. } else {
  50. *context = malloc(sizeof(espeak_ng_ERROR_CONTEXT_));
  51. }
  52. (*context)->type = ERROR_CONTEXT_VERSION;
  53. (*context)->path = strdup(path_home);
  54. (*context)->version = version;
  55. (*context)->expected_version = expected_version;
  56. }
  57. return ENS_VERSION_MISMATCH;
  58. }
  59. #pragma GCC visibility push(default)
  60. ESPEAK_NG_API void espeak_ng_ClearErrorContext(espeak_ng_ERROR_CONTEXT *context)
  61. {
  62. if (context && *context) {
  63. free((*context)->path);
  64. free(*context);
  65. *context = NULL;
  66. }
  67. }
  68. ESPEAK_NG_API void espeak_ng_GetStatusCodeMessage(espeak_ng_STATUS status, char *buffer, size_t length)
  69. {
  70. switch (status)
  71. {
  72. case ENS_COMPILE_ERROR:
  73. strncpy0(buffer, "Compile error", length);
  74. break;
  75. case ENS_VERSION_MISMATCH:
  76. strncpy0(buffer, "Wrong version of espeak-data", length);
  77. break;
  78. case ENS_FIFO_BUFFER_FULL:
  79. strncpy0(buffer, "The FIFO buffer is full", length);
  80. break;
  81. case ENS_NOT_INITIALIZED:
  82. strncpy0(buffer, "The espeak-ng library has not been initialized", length);
  83. break;
  84. case ENS_AUDIO_ERROR:
  85. strncpy0(buffer, "Cannot initialize the audio device", length);
  86. break;
  87. case ENS_VOICE_NOT_FOUND:
  88. strncpy0(buffer, "The specified espeak-ng voice does not exist", length);
  89. break;
  90. case ENS_MBROLA_NOT_FOUND:
  91. strncpy0(buffer, "Could not load the mbrola.dll file", length);
  92. break;
  93. case ENS_MBROLA_VOICE_NOT_FOUND:
  94. strncpy0(buffer, "Could not load the specified mbrola voice file", length);
  95. break;
  96. case ENS_EVENT_BUFFER_FULL:
  97. strncpy0(buffer, "The event buffer is full", length);
  98. break;
  99. case ENS_NOT_SUPPORTED:
  100. strncpy0(buffer, "The requested functionality has not been built into espeak-ng", length);
  101. break;
  102. case ENS_UNSUPPORTED_PHON_FORMAT:
  103. strncpy0(buffer, "The phoneme file is not in a supported format", length);
  104. break;
  105. case ENS_NO_SPECT_FRAMES:
  106. strncpy0(buffer, "The spectral file does not contain any frame data", length);
  107. break;
  108. case ENS_EMPTY_PHONEME_MANIFEST:
  109. strncpy0(buffer, "The phoneme manifest file does not contain any phonemes", length);
  110. break;
  111. default:
  112. if ((status & ENS_GROUP_MASK) == ENS_GROUP_ERRNO) {
  113. #ifdef PLATFORM_WINDOWS
  114. strerror_s(buffer, length, status);
  115. #else
  116. strerror_r(status, buffer, length);
  117. #endif
  118. } else {
  119. #ifdef PLATFORM_WINDOWS
  120. sprintf(buffer, "Unspecified error 0x%x", status);
  121. #else
  122. snprintf(buffer, length, "Unspecified error 0x%x", status);
  123. #endif
  124. }
  125. break;
  126. }
  127. }
  128. ESPEAK_NG_API void espeak_ng_PrintStatusCodeMessage(espeak_ng_STATUS status, FILE *out, espeak_ng_ERROR_CONTEXT context)
  129. {
  130. char error[512];
  131. espeak_ng_GetStatusCodeMessage(status, error, sizeof(error));
  132. if (context) {
  133. switch (context->type)
  134. {
  135. case ERROR_CONTEXT_FILE:
  136. fprintf(out, "Error processing file '%s': %s.\n", context->path, error);
  137. break;
  138. case ERROR_CONTEXT_VERSION:
  139. fprintf(out, "Error: %s at '%s' (expected 0x%x, got 0x%x).\n",
  140. error, context->path, context->expected_version, context->version);
  141. break;
  142. }
  143. } else
  144. fprintf(out, "Error: %s.\n", error);
  145. }
  146. #pragma GCC visibility pop