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