Debian Codegraph @

external/espeak/espeak_praat.cpp

raw symbols SCIP
1 /* espeak_praat.cpp
2 *
3 * Copyright (C) 2017-2021 David Weenink, 2024,2025 Paul Boersma
4 *
5 * This code 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 (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "espeak__config.h"
20
21 #include "espeak_praat.h"
22
23 #include "NUM2.h" // for strstr_regexp()
24
25 #include "espeak_ng.h"
26 #include "speech.h"
27 #include "synthesize.h"
28 #include "voice.h"
29 #include <errno.h>
30
31 /*
32 The espeak-ng data files have been written with little-endian byte order. To be able to use these files on big-endian hardware
33 we have to change these files as if they were written on a big-endian machine.
34 The following routines were modeled after espeak-phonemedata.c by Jonathan Duddington.
35 A serious bug in his code for the phontab_to_bigendian procedure has been corrected.
36 A better solution would be:
37 espeak-ng should read a little-endian int32 as 4 unsigned bytes:
38 int32 i = (ch[0]<<0) | (ch[1]<<8) | (ch[2]<<16) | (ch[3]<<24);
39 and an int16 (short) as 2 unsigned bytes:
40 int16 i = (ch[0]<<0) | (ch[1]<<8);
41 Then no conversion of data files would be necessary.
42 */
43
44 static /*constexpr*/ bool nativeIsBigEndian () { // FIXME: C++-20 will have a constexpr function for this
45 const int test = 1;
46 if /*constexpr*/ (* ((char *) & test) == 1)
47 return false;
48 else
49 return true;
50 }
51
52 static void littleEndian2ToNativeEndian (uint8 *twoBytes) {
53 if (nativeIsBigEndian ())
54 std::swap (twoBytes [0], twoBytes [1]);
55 }
56
57 static void littleEndian4ToNativeEndian (uint8 *fourBytes) {
58 if (nativeIsBigEndian ()) {
59 std::swap (fourBytes [0], fourBytes [3]);
60 std::swap (fourBytes [1], fourBytes [2]);
61 }
62 }
63
64 static void phondata_makeNativeEndian (FileInMemory me, FileInMemory manifest) {
65 //TRACE
66 try {
67 uint8 *const data = my d_data.asArgumentToFunctionThatExpectsZeroBasedArray();
68 FileInMemory phondataf = FileInMemorySet_fopen (theEspeakPraatFileInMemorySet(), Melder_peek32to8_fileSystem (my string.get()), "r");
69 Melder_assert (phondataf);
70 FileInMemory manifestf = FileInMemorySet_fopen (theEspeakPraatFileInMemorySet(), Melder_peek32to8_fileSystem (manifest -> string.get()), "r");
71 Melder_assert (manifestf);
72 char line [1024];
73 // copy 4 bytes: version number
74 // copy 4 bytes: sample rate
75 integer totalLengthOfComments = 0, totalLengthOfS = 0, totalLengthOfW = 0, totalLengthOfE = 0;
76 while (FileInMemory_fgets (line, sizeof (line), manifestf)) {
77 trace (line [0]);
78 if (line [0] == '#') { // comment
79 totalLengthOfComments += strlen (line);
80 continue;
81 }
82 unsigned int index;
83 sscanf (& line [2], "%x", & index);
84 trace (U"index ", index, U" (", Melder_hexadecimal (index, 8), U")");
85 FileInMemory_fseek (phondataf, index, SEEK_SET);
86 integer i1 = index;
87 if (line [0] == 'S') { //
88 /*
89 typedef struct {
90 short length;
91 unsigned char n_frames;
92 unsigned char sqflags;
93 frame_t frame[N_SEQ_FRAMES];
94 } SPECT_SEQ;
95 */
96
97 littleEndian2ToNativeEndian (& data [i1]); // nativize `short length`
98 index += 2; // skip `short length`
99 const integer numberOfFrames = data [index]; // interpret `unsigned char n_frames`
100 trace (U"S ", numberOfFrames, U" frames");
101 index += 2; // skip `unsigned char n_frames` and `unsigned char sqflags`
102 totalLengthOfS += 4;
103
104 for (integer n = 1; n <= numberOfFrames; n ++) {
105 /*
106 typedef struct { //64 bytes
107 short frflags;
108 short ffreq[7];
109 unsigned char length;
110 unsigned char rms;
111 unsigned char fheight[8];
112 unsigned char fwidth[6]; // width/4 f0-5
113 unsigned char fright[3]; // width/4 f0-2
114 unsigned char bw[4]; // Klatt bandwidth BNZ /2, f1,f2,f3
115 unsigned char klattp[5]; // AV, FNZ, Tilt, Aspr, Skew
116 unsigned char klattp2[5]; // continuation of klattp[], Avp, Fric, FricBP, Turb
117 unsigned char klatt_ap[7]; // Klatt parallel amplitude
118 unsigned char klatt_bp[7]; // Klatt parallel bandwidth /2
119 unsigned char spare; // pad to multiple of 4 bytes
120 } frame_t; // with extra Klatt parameters for parallel resonators
121
122 typedef struct { // 44 bytes
123 short frflags;
124 short ffreq[7];
125 unsigned char length;
126 unsigned char rms;
127 unsigned char fheight[8];
128 unsigned char fwidth[6]; // width/4 f0-5
129 unsigned char fright[3]; // width/4 f0-2
130 unsigned char bw[4]; // Klatt bandwidth BNZ /2, f1,f2,f3
131 unsigned char klattp[5]; // AV, FNZ, Tilt, Aspr, Skew
132 } frame_t2;
133 Both frame_t and frame_t2 start with 8 short's.
134 */
135 i1 = index; // both `i1` and `index` now point to the start of a `frame_t`
136 const short frflags = (int16) (uint16) (((uint32) data [i1 + 1] << 8) + (uint32) data [i1]); // save before swapping!
137 for (integer i = 1; i <= 8; i ++) {
138 littleEndian2ToNativeEndian (& data [i1]); // nativize `short frflags` and the seven `short ffreq`s
139 i1 += 2; // skip `short frflags` and the seven `short ffreq`s
140 }
141 /*
142 We will be stepping over a frame_t or frame_t2.
143 The code below assumes that one can step over such a structure by
144 progressing `sizeof (frame_t)` or `sizeof (frame_t2)` bytes,
145 but `sizeof` is about alignment, not about size per se,
146 so we check here that the two are equal. If they are ever different,
147 we will have to step by size instead of alignment
148 (we will be notified by Praat crashing at start-up; last checked 2024-08-16).
149 */
150 Melder_assert (sizeof (frame_t) == 64);
151 Melder_assert (sizeof (frame_t2) == 44);
152 /*
153 frflags signals whether the frame is a Klatt frame or not
154 20231105 changed thy d_data [i1] to thy d_data [index + 1];
155 20240816 changed thy d_data to my d_data, and then to myData [index]
156 20240904 changed myData [index] to data [index] (in order to do the byteswapping in place;
157 however, as `index` still pointed to the start of the frame, where `frflags` was,
158 this was a bug, because `frflags` had already been byteswapped;
159 so we changed data [index] to a saved version of frflags.
160 */
161 #define FRFLAG_KLATT 0x01
162 uint32 length = (frflags & FRFLAG_KLATT) ? sizeof (frame_t) : sizeof (frame_t2);
163 trace (U"S length ", length);
164 index += length;
165 totalLengthOfS += length;
166 }
167 } else if (line [0] == 'W') { // Wave data
168 uint32 length = ((uint32) data [i1 + 1] << 8) + (uint32) data [i1];
169 index += 4;
170 index += length; // char wavedata [length]
171 /*
172 Round up to a multiple of 4.
173 */
174 const integer lengthW = 4 + length + (3 - (index - 1) % 4);
175 index += 3 - (index - 1) % 4;
176 trace (U"W length ", lengthW, U" (", length, U")");
177 totalLengthOfW += lengthW;
178 /*
179 Some W chunks, namely most of those for Myanmar and Vietnam,
180 are longer than this (in the manifest file), namely 3000 to 4000 bytes or so.
181 */
182 } else if (line [0] == 'E') {
183 index += 128; // Envelope: skip 128 bytes
184 totalLengthOfE += 128;
185 } else if (line [0] == 'Q') {
186 /*
187 The following code is probably incorrect as well,
188 but we never seem to reach it.
189 */
190 uint32 length = ((uint32) data [index + 2] << 8) + (uint32) data [index + 1]; // NOT TESTABLE
191 length *= 4;
192 index += length;
193 } else
194 Melder_crash (U"Unknown tag ", line [0], U" in phondata_to_bigendian().");
195 Melder_require (index < my d_numberOfBytes ||1,
196 U"Position ", index + 1, U" is larger than file length (", my d_numberOfBytes, U").");
197 }
198 trace (U"Total length of W ", totalLengthOfW, U" S ", totalLengthOfS, U" E ", totalLengthOfE, U" comments ", totalLengthOfComments);
199 trace (U"Together ", totalLengthOfW + totalLengthOfS + totalLengthOfE + totalLengthOfComments,
200 U" in a file of ", my d_numberOfBytes, U" bytes."); // they don't add up; see the comment at case 'W'
201 } catch (MelderError) {
202 Melder_throw (U"phondata not converted to bigendian.");
203 }
204 }
205
206 static void phontab_makeNativeEndian (FileInMemory me) {
207 //TRACE
208 try {
209 uint8 *const data = my d_data.asArgumentToFunctionThatExpectsZeroBasedArray();
210 const integer numberOfPhonemeTables = data [0];
211 trace (numberOfPhonemeTables, U" phoneme tables");
212 integer index = 4; // skip first 4 bytes
213 for (integer itab = 1; itab <= numberOfPhonemeTables; itab ++) {
214 integer numberOfPhonemes = data [index];
215
216 index += 4; // this is 8 (incorrect) in the original code of eSpeak
217
218 index += N_PHONEME_TAB_NAME; // skip the name
219 const integer phonemeTableSizes = numberOfPhonemes * (integer) sizeof (PHONEME_TAB);
220 Melder_require (index + phonemeTableSizes <= my d_numberOfBytes,
221 U"Too many tables to process. (table ", itab, U" from ", numberOfPhonemeTables, U").");
222 for (integer j = 1; j <= numberOfPhonemes; j ++) {
223 /*
224 typedef struct { // 16 bytes
225 unsigned int mnemonic; // 1st char is in the l.s.byte
226 unsigned int phflags; // bits 16-19 place of articulation
227 unsigned short program;
228 unsigned char code; // the phoneme number
229 unsigned char type; // phVOWEL, phPAUSE, phSTOP etc
230 unsigned char start_type;
231 unsigned char end_type;
232 unsigned char std_length; // for vowels, in mS/2; for phSTRESS, the stress/tone type
233 unsigned char length_mod; // a length_mod group number, used to access length_mod_tab
234 } PHONEME_TAB;
235 */
236 integer i1 = index;
237 littleEndian4ToNativeEndian (& data [i1]); // `unsigned int mnemonic`
238 i1 += 4;
239 littleEndian4ToNativeEndian (& data [i1]); // `unsigned int phflags`
240 i1 += 4;
241 littleEndian2ToNativeEndian (& data [i1]); // `unsigned short program`
242 index += sizeof (PHONEME_TAB);
243 }
244 Melder_require (index <= my d_numberOfBytes, U"Position ", index, U" is larger than file length (", my d_numberOfBytes, U").");
245 }
246 trace (U"Ending up at index ", index, U" in a file with ", my d_numberOfBytes, U" bytes.");
247 Melder_assert (index == my d_numberOfBytes);
248 } catch (MelderError) {
249 Melder_throw (U"phontab not converted to bigendian.");
250 }
251 }
252
253 static void phonindex_makeNativeEndian (FileInMemory me) {
254 //TRACE
255 try {
256 uint8 *const data = my d_data.asArgumentToFunctionThatExpectsZeroBasedArray();
257 const integer numberOfShorts = (my d_numberOfBytes - 4) / 2; // there used to be an extra "- 1" here (ppgb 20240817)
258 integer index = 4; // skip first 4 bytes
259 for (integer i = 0; i < numberOfShorts; i ++) {
260 littleEndian2ToNativeEndian (& data [index]);
261 index += 2;
262 Melder_require (index <= my d_numberOfBytes, U"Position ", index, U" is larger than file length (", my d_numberOfBytes, U").");
263 }
264 trace (U"Ending up at index ", index, U" in a file with ", my d_numberOfBytes, U" bytes.");
265 Melder_assert (index == my d_numberOfBytes);
266 } catch (MelderError) {
267 Melder_throw (U"phonindex not converted to bigendian.");
268 }
269 }
270
271 static void espeak_praat_FileInMemorySet_makeNativeEndian () {
272 FileInMemorySet me = theEspeakPraatFileInMemorySet();
273
274 const integer manifestIndex = my lookUp (U"" PATH_ESPEAK_DATA "/phondata-manifest");
275 Melder_assert (manifestIndex > 0);
276 const FileInMemory manifest = my at [manifestIndex];
277 // no conversion to big-endian is necessary, because `phondata-manifest` is a text file
278 // we do need manifest here, for converting phondata
279
280 const integer phondataIndex = my lookUp (U"" PATH_ESPEAK_DATA "/phondata");
281 Melder_assert (phondataIndex > 0);
282 const FileInMemory phondata = my at [phondataIndex];
283 phondata_makeNativeEndian (phondata, manifest);
284
285 const integer phontabIndex = my lookUp (U"" PATH_ESPEAK_DATA "/phontab");
286 Melder_assert (phontabIndex > 0);
287 const FileInMemory phontab = my at [phontabIndex];
288 phontab_makeNativeEndian (phontab);
289
290 const integer phonindexIndex = my lookUp (U"" PATH_ESPEAK_DATA "/phonindex");
291 Melder_assert (phonindexIndex > 0);
292 const FileInMemory phonindex = my at [phonindexIndex];
293 phonindex_makeNativeEndian (phonindex);
294 }
295
296 FileInMemorySet theEspeakPraatFileInMemorySet() {
297 static autoFileInMemorySet me; // singleton
298 if (! me) {
299 me = FileInMemorySet_create ();
300 espeak_praat_FileInMemorySet_addPhon (me.get());
301 espeak_praat_FileInMemorySet_addDicts (me.get());
302 espeak_praat_FileInMemorySet_addLanguages (me.get());
303 espeak_praat_FileInMemorySet_addVoices (me.get());
304 if (nativeIsBigEndian ()) // OPTIMIZATION: 1 ms less start-up time
305 espeak_praat_FileInMemorySet_makeNativeEndian ();
306 }
307 return me.get();
308 }
309
310 /* End of file espeak_praat.cpp */