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

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