1# espeak_ng_data_to_code.praat
2# djmw 20120117, 20120124, 20151130, 20171004
3#
4# This script is specific for my situation (althought it can be adapted easily to any directory structure
5#	and non-Linux system).
6#	My espeak-work/ has subdirectories espeak-ng-work and espeak-ng-current.
7# 	Into espeak-ng-work are copied from the original espeak-ng project only those *.c and *.h files that are
8# 	needed for the praat version of the synthesizer .
9#
10# If the upstream espeak-ng has been modified then I follow the following procedure:
11#
12# Given the following directories:
13#	A: ~/projects/praat/espeak-work/espeak-ng-work
14#	B: ~/projects/praat/espeak-work/espeak-ng-current
15#	C: ~/projects/praat/external/espeak
16#
17#	0. I pull the sources from upstream:
18#		cd ~/projects/espeak-ng
19#		- git pull https://github.com/espeak-ng/espeak-ng.git
20#		- make clean
21#		- make
22#		- Get the version of espeak-ng from the ~/projects/espeak-ng/config.h file
23#	1. I copy the necessary *.c and *.h files from ~/projects/espeak-ng/src/... to espeak-work
24#		by running this script with only the option "Copy_c_and_h_files" ON (the right version option).
25#		The *.c files are renamed as *.cpp files. Also a new file espeak-ng-version.h is created.
26#	2. I use a three-way diff program (kdiff3) to see which files in A are different from B and
27#		merge the differences into C.
28#	3. Now my *.cpp and *.h are in synchrony with upstream and we copy all files from A to B.
29#	4. I generate the new dictionaries and language files in memory by running this script
30#		with only the option "Create_FileInMemorySet" set to ON.
31#	5. I copy the file create_espeak_ng_FileInMemorySet.cpp
32#  		and espeak-ng-version.h from espeak-ng-work/ to external/espeak
33#	6. Now the praat synthesizer is up to date.
34#
35# Function of this script:
36# Generates in espeak-ng-work
37# 	1. create_espeak_ng_FileInMemorySet.cpp
38# 	2. espeak-ng-version.h
39#
40# 	and
41#
42# 	3. Copies the necessary *.c(pp) and *.h from from_dir to to_dir
43#
44
45myscriptname$ = "espeak_ng_data_to_code.praat"
46date$ = date$()
47notify$ = "This file was created automatically on " + date$ + "."
48clearinfo
49
50form Espeakdata to code
51	word Espeak_version 1.49.3-dev
52	boolean Copy_c_and_h_files 0
53	boolean Show_cp_command 1
54	boolean Create_FileInMemorySet 0
55endform
56
57gpltext$ =  " * Copyright (C) David Weenink 2012-2017" + newline$ +
58	... " *" + newline$ +
59	... " * This program is free software; you can redistribute it and/or modify " + newline$ +
60 	... " * it under the terms of the GNU General Public License as published by" + newline$ +
61 	... " * the Free Software Foundation; either version 2 of the License, or (at" + newline$ +
62 	... " * your option) any later version." + newline$ +
63  	... " *" + newline$ +
64  	... " * This program is distributed in the hope that it will be useful, but" + newline$ +
65 	... " * WITHOUT ANY WARRANTY; without even the implied warranty of" + newline$ +
66  	... " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU" + newline$ +
67 	... " * General Public License for more details." + newline$ +
68 	... " *" + newline$ +
69  	... " * You should have received a copy of the GNU General Public License" + newline$ +
70  	... " * along with this program; if not, write to the Free Software" + newline$ +
71 	... " * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA." + newline$
72
73fromdir$ = "/home/david/projects/espeak-ng"
74todir$ = "/home/david/projects/praat/espeak-work/espeak-ng-work"
75
76espeakdata_dir$ = fromdir$ + "/espeak-ng-data"
77espeakdata_voices_dir$ = espeakdata_dir$ + "/voices/!v"
78espeakdata_lang_dir$ = espeakdata_dir$ + "/lang"
79
80include_header$ = "#include "+ """" + "espeakdata_FileInMemory.h" + """" + newline$
81
82if create_FileInMemorySet
83
84	@create_phonFileInMemorySet
85
86	@create_languageFileInMemorySet
87
88	dict_fims = Create FileInMemorySet from directory contents: "list", espeakdata_dir$, "*_dict"
89
90	voice_fims = Create FileInMemorySet from directory contents: "voices", espeakdata_dir$ + "/voices/!v", "*"
91
92	selectObject: language_fims, phon_fims, dict_fims, voice_fims
93	espeak_ng_fims = Merge
94
95	.message$ = "/* espeak_ng_createFileInMemorySet.cpp" + newline$
96	... + " * This file was automatically created from files in directories in espeak-ng-data." + newline$
97	... + " * Espeak-ng version: " + espeak_version$ + "." + newline$
98	... + " * Date: " + date$() + "." + newline$
99	... + "*/" + newline$ +newline$
100	... + "#include ""espeakdata_FileInMemory.h""" + newline$
101	.cpp$ = Show as code: "espeak_ng_FileInMemorySet", 30
102	.cpp$ = .message$ + .cpp$
103	writeInfoLine: .cpp$
104	writeFile: todir$ + "/create_espeak_ng_FileInMemorySet.cpp", .cpp$
105endif
106
107procedure create_phonFileInMemorySet
108	fim1 = Create FileInMemory: espeakdata_dir$ + "/phondata"
109	fim2 = Create FileInMemory: espeakdata_dir$ + "/phonindex"
110	fim3 = Create FileInMemory: espeakdata_dir$ + "/phontab"
111	fim4 = Create FileInMemory: espeakdata_dir$ + "/intonations"
112	fim5 = Create FileInMemory: espeakdata_dir$ + "/phondata-manifest"
113	selectObject: fim1, fim2, fim3, fim4, fim5
114	phon_fims = To FileInMemorySet
115	removeObject: fim1, fim2, fim3, fim4, fim5
116endproc
117
118procedure create_languageFileInMemorySet
119	language_fims = Create FileInMemorySet from directory contents: "l", espeakdata_lang_dir$, "*"
120	.ldirs =  Create Strings as directory list: "dirs",  espeakdata_lang_dir$+ "/*"
121	.ndirs = Get number of strings
122	for .idir to .ndirs
123		selectObject: .ldirs
124		.dir$ = Get string: .idir
125		.langset = Create FileInMemorySet from directory contents: "l", espeakdata_lang_dir$ + "/" + .dir$ , "*"
126		plusObject: language_fims
127		.merged = Merge
128		removeObject: .langset, language_fims
129		language_fims = .merged
130	endfor
131	removeObject: .ldirs
132endproc
133
134# extract only the necesary files to espeak-ng-work.
135# changes to these files have to be made in espeak-ng-current
136
137if copy_c_and_h_files
138	@espeak_ng_copyfiles
139endif
140
141procedure espeak_ng_copyfiles
142	.espeakfiles_c$ = "compiledata.c compiledict.c compilembrola.c dictionary.c" +
143	... " encoding.c error.c espeak_api.c espeak_command.c" +
144	... " event.c fifo.c ieee80.c intonation.c klatt.c" +
145	... " mnemonics.c numbers.c phoneme.c phonemelist.c readclause.c setlengths.c" +
146	... " spect.c speech.c synthdata.c synthesize.c " +
147	... " synth_mbrola.c translate.c tr_languages.c voices.c wavegen.c"
148
149	.espeakfiles_h$ = "error.h espeak_command.h event.h fifo.h klatt.h phoneme.h sintab.h" +
150	... " spect.h speech.h synthesize.h translate.h voice.h synthesize.h translate.h voice.h"
151	.espeakfiles_include_h$ = "speak_lib.h espeak_ng.h encoding.h"
152	.espeakfiles_ucd_h$ = "ucd.h"
153	.espeakfiles_ucd_c$ = "case.c categories.c proplist.c"
154
155	# check version
156	.config$ = readFile$ ("~/projects/espeak-ng/config.h")
157	.version$ = extractWord$ (.config$, "#define VERSION """)
158	.version$ = replace$ (.version$, """", "", 0)
159	if .version$ <> espeak_version$
160		exitScript: "The given version (", espeak_version$, ") differs from package version ", .version$
161	endif
162
163	# rename and cp files from espeak-ng to espeak-work
164	@copy_rename: fromdir$+"/src/libespeak-ng", .espeakfiles_c$, ".c", ".cpp"
165	@copy_rename: fromdir$+"/src/libespeak-ng", .espeakfiles_h$, ".h", ".h"
166	@copy_rename: fromdir$+"/src/include/espeak-ng", .espeakfiles_include_h$, ".h", ".h"
167	@copy_rename: fromdir$+"/src/ucd-tools/src", .espeakfiles_ucd_c$, ".c", ".cpp"
168	@copy_rename: fromdir$+"/src/ucd-tools/src/include/ucd", .espeakfiles_ucd_h$, ".h", ".h"
169	.version_define$ = "#define ESPEAK_NG_VERSION " + "U""" + espeak_version$ + """" + newline$
170		... + "#define ESPEAK_NG_VERSIONX " + espeak_version$
171	writeFile: todir$ + "/espeak_ng_version.h", .version_define$
172endproc
173
174procedure copy_rename: .fromdir$, .files$, .ext$, .newext$
175	.list = Create Strings as tokens: .files$
176	.numberOfFiles = Get number of strings
177	for .ifile to .numberOfFiles
178		.name$ = Get string: .ifile
179		.newname$ = .name$ - .ext$ + .newext$
180		.command$ =  "cp " + .fromdir$ + "/" + .name$ + " " + todir$ + "/" + .newname$
181		appendInfoLine: .command$
182		if show_cp_command
183			appendInfoLine: .command$
184		endif
185		runSystem: .command$
186	endfor
187endproc
188
189