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.

soundicon.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 2005 to 2015 by Jonathan Duddington
  3. * email: [email protected]
  4. * Copyright (C) 2015-2017 Reece H. Dunn
  5. * Copyright (C) 2021 Juho Hiltunen
  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 <ctype.h>
  22. #include <errno.h>
  23. #include <locale.h>
  24. #include <math.h>
  25. #include <stdint.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <espeak-ng/espeak_ng.h>
  31. #include <espeak-ng/speak_lib.h>
  32. #include <espeak-ng/encoding.h>
  33. #include <ucd/ucd.h>
  34. #include "soundicon.h"
  35. #include "error.h" // for create_file_error_context
  36. #include "readclause.h" // for Read4Bytes
  37. #include "speech.h" // for path_home, GetFileLength, PATHSEP
  38. #include "synthesize.h" // for samplerate
  39. int n_soundicon_tab = 0;
  40. SOUND_ICON soundicon_tab[N_SOUNDICON_TAB];
  41. static espeak_ng_STATUS LoadSoundFile(const char *fname, int index, espeak_ng_ERROR_CONTEXT *context)
  42. {
  43. FILE *f;
  44. char *p;
  45. int length;
  46. char fname_temp[100];
  47. char fname2[sizeof(path_home)+13+40];
  48. if (fname == NULL) {
  49. // filename is already in the table
  50. fname = soundicon_tab[index].filename;
  51. }
  52. if (fname == NULL)
  53. return EINVAL;
  54. if (fname[0] != '/') {
  55. // a relative path, look in espeak-ng-data/soundicons
  56. sprintf(fname2, "%s%csoundicons%c%s", path_home, PATHSEP, PATHSEP, fname);
  57. fname = fname2;
  58. }
  59. fname_temp[0] = 0;
  60. f = NULL;
  61. if ((f = fopen(fname, "rb")) != NULL) {
  62. int ix;
  63. int fd_temp;
  64. int header[3];
  65. char command[sizeof(fname2)+sizeof(fname2)+40];
  66. if (fseek(f, 20, SEEK_SET) == -1) {
  67. int error = errno;
  68. fclose(f);
  69. return create_file_error_context(context, error, fname);
  70. }
  71. for (ix = 0; ix < 3; ix++)
  72. header[ix] = Read4Bytes(f);
  73. // if the sound file is not mono, 16 bit signed, at the correct sample rate, then convert it
  74. if ((header[0] != 0x10001) || (header[1] != samplerate) || (header[2] != samplerate*2)) {
  75. fclose(f);
  76. f = NULL;
  77. #ifdef HAVE_MKSTEMP
  78. strcpy(fname_temp, "/tmp/espeakXXXXXX");
  79. if ((fd_temp = mkstemp(fname_temp)) >= 0)
  80. close(fd_temp);
  81. #else
  82. strcpy(fname_temp, tmpnam(NULL));
  83. #endif
  84. sprintf(command, "sox \"%s\" -r %d -c1 -b 16 -t wav %s\n", fname, samplerate, fname_temp);
  85. if (system(command) == 0)
  86. fname = fname_temp;
  87. }
  88. }
  89. if (f == NULL) {
  90. f = fopen(fname, "rb");
  91. if (f == NULL)
  92. return create_file_error_context(context, errno, fname);
  93. }
  94. length = GetFileLength(fname);
  95. if (length < 0) { // length == -errno
  96. fclose(f);
  97. return create_file_error_context(context, -length, fname);
  98. }
  99. if (fseek(f, 0, SEEK_SET) == -1) {
  100. int error = errno;
  101. fclose(f);
  102. return create_file_error_context(context, error, fname);
  103. }
  104. if ((p = (char *)realloc(soundicon_tab[index].data, length)) == NULL) {
  105. fclose(f);
  106. return ENOMEM;
  107. }
  108. if (fread(p, 1, length, f) != length) {
  109. int error = errno;
  110. fclose(f);
  111. if (fname_temp[0])
  112. remove(fname_temp);
  113. free(p);
  114. return create_file_error_context(context, error, fname);
  115. }
  116. fclose(f);
  117. if (fname_temp[0])
  118. remove(fname_temp);
  119. length = p[40] | (p[41] << 8) | (p[42] << 16) | (p[43] << 24);
  120. soundicon_tab[index].length = length / 2; // length in samples
  121. soundicon_tab[index].data = p;
  122. return ENS_OK;
  123. }
  124. int LookupSoundicon(int c)
  125. {
  126. // Find the sound icon number for a punctuation character and load the audio file if it's not yet loaded
  127. int ix;
  128. for (ix = 0; ix < n_soundicon_tab; ix++) {
  129. if (soundicon_tab[ix].name == c) {
  130. if (soundicon_tab[ix].length == 0) { // not yet loaded, load now
  131. if (LoadSoundFile(NULL, ix, NULL) != ENS_OK) {
  132. return -1; // sound file is not available
  133. }
  134. }
  135. return ix;
  136. }
  137. }
  138. return -1;
  139. }
  140. int LoadSoundFile2(const char *fname)
  141. {
  142. // Load a sound file into the sound icon table and memory
  143. // (if it's not already loaded)
  144. // returns -1 on error or the index of loaded file on success
  145. int ix;
  146. for (ix = 0; ix < n_soundicon_tab; ix++) {
  147. if (((soundicon_tab[ix].filename != NULL) && strcmp(fname, soundicon_tab[ix].filename) == 0)) {
  148. // the file information is found. If length = 0 it needs to be loaded to memory
  149. if (soundicon_tab[ix].length == 0) {
  150. if (LoadSoundFile(NULL, ix, NULL) != ENS_OK)
  151. return -1; // sound file is not available
  152. }
  153. return ix; // sound file already loaded to memory
  154. }
  155. }
  156. // load the file into the current slot and increase index
  157. if (LoadSoundFile(fname, n_soundicon_tab, NULL) != ENS_OK)
  158. return -1;
  159. soundicon_tab[n_soundicon_tab].filename = (char *)realloc(soundicon_tab[n_soundicon_tab].filename, strlen(fname)+1);
  160. strcpy(soundicon_tab[n_soundicon_tab].filename, fname);
  161. n_soundicon_tab++;
  162. return n_soundicon_tab - 1;
  163. }