Debian Codegraph @

external/espeak/soundicon.cpp

raw symbols SCIP
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"cxx . . $ `<file>/external/espeak/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"cxx . . $ `<file>/external/espeak/espeak_ng.h`/
34 #include "speak_lib.h"cxx . . $ `<file>/external/espeak/speak_lib.h`/
35 #include "encoding.h"cxx . . $ `<file>/external/espeak/encoding.h`/
36 #include "ucd.h"cxx . . $ `<file>/external/espeak/ucd.h`/
37
38 #include "soundicon.h"cxx . . $ `<file>/external/espeak/soundicon.h`/
39 #include "common.h"cxx . . $ `<file>/external/espeak/common.h`/ // for GetFileLength
40 #include "error.h"cxx . . $ `<file>/external/espeak/error.h`/ // for create_file_error_context
41 #include "speech.h"cxx . . $ `<file>/external/espeak/speech.h`/ // for path_home, PATHSEP
42 #include "synthesize.h"cxx . . $ `<file>/external/espeak/synthesize.h`/ // for samplerate
43
44 #include "espeak_praat.h"cxx . . $ `<file>/external/espeak/espeak_praat.h`/ // theEspeakPraatFileInMemorySet
45
46 int n_soundicon_tabcxx . . $ n_soundicon_tab. = 0;
47 SOUND_ICON soundicon_tabcxx . . $ soundicon_tab.[N_SOUNDICON_TABcxx . . $ `external/espeak/soundicon.h:39:9`!];
48
49 static espeak_ng_STATUS LoadSoundFilecxx . . $ LoadSoundFile(c0e9b29066867e63).(const char *fname, int index, espeak_ng_ERROR_CONTEXT *context)
50 {
51 FileInMemorycxx . . $ 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 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) {
58 // filename is already in the table
59 fname = soundicon_tabcxx . . $ soundicon_tab.[index].filename;
60 }
61
62 if (fname == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!)
63 return static_cast<espeak_ng_STATUS> (EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!);
64
65 if (fname[0] != '/') {
66 // a relative path, look in espeak-ng-data/soundicons
67 sprintf(fname2, "%s%csoundicons%c%s", path_home, PATHSEPcxx . . $ `external/espeak/speech.h:71:9`!, PATHSEPcxx . . $ `external/espeak/speech.h:71:9`!, fname);
68 fname = fname2;
69 }
70
71 fname_temp[0] = 0;
72
73 f = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
74 if ((f = FileInMemorySet_fopencxx . . $ FileInMemorySet_fopen(3c6da2b95de6d644).(theEspeakPraatFileInMemorySetcxx . . $ theEspeakPraatFileInMemorySet(e171e0b8438add33).(), fname, "rb")) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) {
75 int ix;
76 int header[3];
77
78 if (FileInMemory_fseekcxx . . $ FileInMemory_fseek(5881e18e5fd4da4e).(f, 20, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!) == -1) {
79 int error = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
80 FileInMemory_fclosecxx . . $ FileInMemory_fclose(18557d97a31d1c3b).(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] = Read4Bytescxx . . $ Read4Bytes(18557d97a31d1c3b).(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_fclosecxx . . $ FileInMemory_fclose(18557d97a31d1c3b).(f);
90 f = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
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(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!));
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 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) {
108 f = FileInMemorySet_fopencxx . . $ FileInMemorySet_fopen(3c6da2b95de6d644).(theEspeakPraatFileInMemorySetcxx . . $ theEspeakPraatFileInMemorySet(e171e0b8438add33).(), fname, "rb");
109 if (f == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!)
110 return create_file_error_context(context, static_cast<espeak_ng_STATUS> (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!), fname);
111 }
112
113 length = GetFileLengthcxx . . $ GetFileLength(5c4306d8553b3696).(fname);
114 if (length < 0) { // length == -errno
115 FileInMemory_fclosecxx . . $ FileInMemory_fclose(18557d97a31d1c3b).(f);
116 return create_file_error_context(context, static_cast<espeak_ng_STATUS> (-length), fname);
117 }
118 if (FileInMemory_fseekcxx . . $ FileInMemory_fseek(5881e18e5fd4da4e).(f, 0, SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!) == -1) {
119 int error = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
120 FileInMemory_fclosecxx . . $ FileInMemory_fclose(18557d97a31d1c3b).(f);
121 return create_file_error_context(context, static_cast<espeak_ng_STATUS> (error), fname);
122 }
123 if ((p = (unsigned char *) realloccxx . . $ realloc(42b2a9dd8f084bf2).(soundicon_tabcxx . . $ soundicon_tab.[index].data, length)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) {
124 FileInMemory_fclosecxx . . $ FileInMemory_fclose(18557d97a31d1c3b).(f);
125 return static_cast<espeak_ng_STATUS> (ENOMEMcxx . . $ `<external>/8987895939074593727/errno-base.h:16:9`!);
126 }
127 if (FileInMemory_freadcxx . . $ FileInMemory_fread(44d619d1f1a9ecd5).(p, 1, length, f) != length) {
128 int error = errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!;
129 FileInMemory_fclosecxx . . $ FileInMemory_fclose(18557d97a31d1c3b).(f);
130 if (fname_temp[0])
131 remove(fname_temp);
132 freecxx . . $ free(bd21765a0afc8e3c).(p);
133 return create_file_error_context(context, static_cast<espeak_ng_STATUS> (error), fname);
134 }
135 FileInMemory_fclosecxx . . $ FileInMemory_fclose(18557d97a31d1c3b).(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_tabcxx . . $ soundicon_tab.[index].length = length / 2; // length in samples
141 soundicon_tabcxx . . $ soundicon_tab.[index].data = (char *) p;
142 return ENS_OK;
143 }
144
145 int LookupSoundiconcxx . . $ LookupSoundicon(7864480464b09eea).(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_tabcxx . . $ n_soundicon_tab.; ix++) {
151 if (soundicon_tabcxx . . $ soundicon_tab.[ix].name == c) {
152 if (soundicon_tabcxx . . $ soundicon_tab.[ix].length == 0) { // not yet loaded, load now
153 if (LoadSoundFilecxx . . $ LoadSoundFile(c0e9b29066867e63).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, ix, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) != ENS_OK) {
154 return -1; // sound file is not available
155 }
156 }
157 return ix;
158 }
159 }
160 return -1;
161 }
162
163 int LoadSoundFile2cxx . . $ LoadSoundFile2(5c4306d8553b3696).(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_tabcxx . . $ n_soundicon_tab.; ix++) {
171 if (((soundicon_tabcxx . . $ soundicon_tab.[ix].filename != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) && strcmp(fname, soundicon_tabcxx . . $ soundicon_tab.[ix].filename) == 0)) {
172 // the file information is found. If length = 0 it needs to be loaded to memory
173 if (soundicon_tabcxx . . $ soundicon_tab.[ix].length == 0) {
174 if (LoadSoundFilecxx . . $ LoadSoundFile(c0e9b29066867e63).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!, ix, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) != 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 (LoadSoundFilecxx . . $ LoadSoundFile(c0e9b29066867e63).(fname, n_soundicon_tabcxx . . $ n_soundicon_tab., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) != ENS_OK)
183 return -1;
184
185 soundicon_tabcxx . . $ soundicon_tab.[n_soundicon_tabcxx . . $ n_soundicon_tab.].filename = (char *)realloccxx . . $ realloc(42b2a9dd8f084bf2).(soundicon_tabcxx . . $ soundicon_tab.[n_soundicon_tabcxx . . $ n_soundicon_tab.].filename, strlen(fname)+1);
186 strcpy(soundicon_tabcxx . . $ soundicon_tab.[n_soundicon_tabcxx . . $ n_soundicon_tab.].filename, fname);
187 n_soundicon_tabcxx . . $ n_soundicon_tab.++;
188 return n_soundicon_tabcxx . . $ n_soundicon_tab. - 1;
189 }