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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "common.h" // for GetFileLength
  36. #include "error.h" // for create_file_error_context
  37. #include "speech.h" // for path_home, 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. unsigned 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 header[3];
  64. if (fseek(f, 20, SEEK_SET) == -1) {
  65. int error = errno;
  66. fclose(f);
  67. return create_file_error_context(context, error, fname);
  68. }
  69. for (ix = 0; ix < 3; ix++)
  70. header[ix] = Read4Bytes(f);
  71. // if the sound file is not mono, 16 bit signed, at the correct sample rate, then convert it
  72. if ((header[0] != 0x10001) || (header[1] != samplerate) || (header[2] != samplerate*2)) {
  73. fclose(f);
  74. f = NULL;
  75. #if HAVE_MKSTEMP
  76. strcpy(fname_temp, "/tmp/espeakXXXXXX");
  77. int fd_temp;
  78. if ((fd_temp = mkstemp(fname_temp)) >= 0)
  79. close(fd_temp);
  80. #else
  81. strcpy(fname_temp, tmpnam(NULL));
  82. #endif
  83. // sprintf(command, "sox \"%s\" -r %d -c1 -b 16 -t wav %s\n", fname, samplerate, fname_temp);
  84. // if (system(command) == 0)
  85. fname = fname_temp;
  86. }
  87. }
  88. if (f == NULL) {
  89. f = fopen(fname, "rb");
  90. if (f == NULL)
  91. return create_file_error_context(context, errno, fname);
  92. }
  93. length = GetFileLength(fname);
  94. if (length < 0) { // length == -errno
  95. fclose(f);
  96. return create_file_error_context(context, -length, fname);
  97. }
  98. if (fseek(f, 0, SEEK_SET) == -1) {
  99. int error = errno;
  100. fclose(f);
  101. return create_file_error_context(context, error, fname);
  102. }
  103. if ((p = realloc(soundicon_tab[index].data, length)) == NULL) {
  104. fclose(f);
  105. return ENOMEM;
  106. }
  107. if (fread(p, 1, length, f) != length) {
  108. int error = errno;
  109. fclose(f);
  110. if (fname_temp[0])
  111. remove(fname_temp);
  112. free(p);
  113. return create_file_error_context(context, error, fname);
  114. }
  115. fclose(f);
  116. if (fname_temp[0])
  117. remove(fname_temp);
  118. length = p[40] | (p[41] << 8) | (p[42] << 16) | (p[43] << 24);
  119. soundicon_tab[index].length = length / 2; // length in samples
  120. soundicon_tab[index].data = (char *) p;
  121. return ENS_OK;
  122. }
  123. int LookupSoundicon(int c)
  124. {
  125. // Find the sound icon number for a punctuation character and load the audio file if it's not yet loaded
  126. int ix;
  127. for (ix = 0; ix < n_soundicon_tab; ix++) {
  128. if (soundicon_tab[ix].name == c) {
  129. if (soundicon_tab[ix].length == 0) { // not yet loaded, load now
  130. if (LoadSoundFile(NULL, ix, NULL) != ENS_OK) {
  131. return -1; // sound file is not available
  132. }
  133. }
  134. return ix;
  135. }
  136. }
  137. return -1;
  138. }
  139. int LoadSoundFile2(const char *fname)
  140. {
  141. // Load a sound file into the sound icon table and memory
  142. // (if it's not already loaded)
  143. // returns -1 on error or the index of loaded file on success
  144. int ix;
  145. for (ix = 0; ix < n_soundicon_tab; ix++) {
  146. if (((soundicon_tab[ix].filename != NULL) && strcmp(fname, soundicon_tab[ix].filename) == 0)) {
  147. // the file information is found. If length = 0 it needs to be loaded to memory
  148. if (soundicon_tab[ix].length == 0) {
  149. if (LoadSoundFile(NULL, ix, NULL) != ENS_OK)
  150. return -1; // sound file is not available
  151. }
  152. return ix; // sound file already loaded to memory
  153. }
  154. }
  155. // load the file into the current slot and increase index
  156. if (LoadSoundFile(fname, n_soundicon_tab, NULL) != ENS_OK)
  157. return -1;
  158. soundicon_tab[n_soundicon_tab].filename = (char *)realloc(soundicon_tab[n_soundicon_tab].filename, strlen(fname)+1);
  159. strcpy(soundicon_tab[n_soundicon_tab].filename, fname);
  160. n_soundicon_tab++;
  161. return n_soundicon_tab - 1;
  162. }