tomwalters@296
|
1 // Copyright 2008-2010, Thomas Walters
|
tomwalters@296
|
2 //
|
tomwalters@296
|
3 // AIM-C: A C++ implementation of the Auditory Image Model
|
tomwalters@296
|
4 // http://www.acousticscale.org/AIMC
|
tomwalters@296
|
5 //
|
tomwalters@318
|
6 // Licensed under the Apache License, Version 2.0 (the "License");
|
tomwalters@318
|
7 // you may not use this file except in compliance with the License.
|
tomwalters@318
|
8 // You may obtain a copy of the License at
|
tomwalters@296
|
9 //
|
tomwalters@318
|
10 // http://www.apache.org/licenses/LICENSE-2.0
|
tomwalters@296
|
11 //
|
tomwalters@318
|
12 // Unless required by applicable law or agreed to in writing, software
|
tomwalters@318
|
13 // distributed under the License is distributed on an "AS IS" BASIS,
|
tomwalters@318
|
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
tomwalters@318
|
15 // See the License for the specific language governing permissions and
|
tomwalters@318
|
16 // limitations under the License.
|
tomwalters@296
|
17
|
tomwalters@296
|
18 /*!
|
tomwalters@296
|
19 * \file AIMCopy.cpp
|
tomwalters@296
|
20 * \brief AIM-C replacement for HTK's HCopy
|
tomwalters@296
|
21 *
|
tomwalters@296
|
22 * The following subset of the command-line flags
|
tomwalters@296
|
23 * should be implemented from HCopy:
|
tomwalters@296
|
24 * -A Print command line arguments off
|
tomwalters@296
|
25 * -C cf Set config file to cf default
|
tomwalters@296
|
26 * (should be able to take multiple config files)
|
tomwalters@296
|
27 * -S f Set script file to f none
|
tomwalters@296
|
28 * //! \todo -T N Set trace flags to N 0
|
tomwalters@296
|
29 * -V Print version information off
|
tomwalters@296
|
30 * -D of Write configuration data to of none
|
tomwalters@296
|
31 *
|
tomwalters@296
|
32 * \author Thomas Walters <tom@acousticscale.org>
|
tomwalters@296
|
33 * \date created 2008/05/08
|
tomwalters@296
|
34 * \version \$Id$
|
tomwalters@296
|
35 */
|
tomwalters@296
|
36
|
tomwalters@296
|
37 #include <fstream>
|
tomwalters@296
|
38 #include <iostream>
|
tomwalters@296
|
39 #include <string>
|
tomwalters@296
|
40 #include <utility>
|
tomwalters@296
|
41 #include <vector>
|
tomwalters@296
|
42
|
tomwalters@296
|
43 #include <stdlib.h>
|
tomwalters@297
|
44 #include <time.h>
|
tomwalters@296
|
45
|
tomwalters@296
|
46 #include "Support/Common.h"
|
tomwalters@296
|
47 #include "Support/FileList.h"
|
tomwalters@402
|
48 #include "Support/ModuleTree.h"
|
tomwalters@296
|
49 #include "Support/Parameters.h"
|
tomwalters@296
|
50
|
tomwalters@402
|
51 namespace aimc {
|
tomwalters@296
|
52 using std::ofstream;
|
tomwalters@296
|
53 using std::pair;
|
tomwalters@296
|
54 using std::vector;
|
tomwalters@296
|
55 using std::string;
|
tomwalters@402
|
56 class AIMCopy {
|
tomwalters@402
|
57 public:
|
tomwalters@402
|
58 AIMCopy();
|
tomwalters@402
|
59
|
tomwalters@402
|
60 bool Initialize(string script_filename,
|
tomwalters@402
|
61 string config_filename);
|
tomwalters@402
|
62
|
tomwalters@402
|
63 bool WriteConfig(string config_dump_filename,
|
tomwalters@402
|
64 string config_graph_filename);
|
tomwalters@402
|
65
|
tomwalters@402
|
66 bool Process();
|
tomwalters@402
|
67
|
tomwalters@402
|
68 private:
|
tomwalters@402
|
69 bool initialized_;
|
tomwalters@402
|
70 Parameters global_parameters_;
|
tomwalters@402
|
71 ModuleTree tree_;
|
tomwalters@402
|
72 vector<pair<string, string> > script_;
|
tomwalters@402
|
73 };
|
tomwalters@402
|
74
|
tomwalters@402
|
75
|
tomwalters@402
|
76 AIMCopy::AIMCopy() : initialized_(false) {
|
tomwalters@402
|
77
|
tomwalters@402
|
78 }
|
tomwalters@402
|
79
|
tomwalters@402
|
80 bool AIMCopy::Initialize(string script_filename,
|
tomwalters@402
|
81 string config_filename) {
|
tomwalters@402
|
82
|
tomwalters@402
|
83 LOG_INFO("AIMCopy: Loading script");
|
tomwalters@402
|
84 script_ = FileList::Load(script_filename);
|
tomwalters@402
|
85 if (script_.size() == 0) {
|
tomwalters@402
|
86 LOG_ERROR("No data read from script file %s", script_filename.c_str());
|
tomwalters@402
|
87 return false;
|
tomwalters@402
|
88 }
|
tomwalters@402
|
89
|
tomwalters@402
|
90 LOG_INFO("AIMCopy: Loading configuration");
|
tomwalters@402
|
91 if (!tree_.LoadConfigFile(config_filename)) {
|
tomwalters@402
|
92 LOG_ERROR(_T("Failed to load configuration file"));
|
tomwalters@402
|
93 return false;
|
tomwalters@402
|
94 }
|
tomwalters@402
|
95 LOG_INFO("AIMCopy: Successfully loaded configuration");
|
tomwalters@402
|
96 initialized_ = true;
|
tomwalters@402
|
97 return true;
|
tomwalters@402
|
98 }
|
tomwalters@402
|
99
|
tomwalters@402
|
100 bool AIMCopy::WriteConfig(string config_dump_filename,
|
tomwalters@402
|
101 string config_graph_filename) {
|
tomwalters@402
|
102 if (!initialized_) {
|
tomwalters@402
|
103 return false;
|
tomwalters@402
|
104 }
|
tomwalters@402
|
105
|
tomwalters@402
|
106 if (script_.size() > 0) {
|
tomwalters@402
|
107 global_parameters_.SetString("input_filename", script_[0].first.c_str());
|
tomwalters@402
|
108 global_parameters_.SetString("output_filename_base", script_[0].second.c_str());
|
tomwalters@402
|
109 LOG_INFO("AIMCopy: Initializing tree for initial parameter write.");
|
tomwalters@402
|
110 if (!tree_.Initialize(&global_parameters_)) {
|
tomwalters@402
|
111 LOG_ERROR(_T("Failed to initialize tree."));
|
tomwalters@402
|
112 return false;
|
tomwalters@402
|
113 }
|
tomwalters@411
|
114 tree_.Reset();
|
tomwalters@402
|
115 } else {
|
tomwalters@402
|
116 LOG_ERROR(_T("No input files in script."));
|
tomwalters@402
|
117 return false;
|
tomwalters@402
|
118 }
|
tomwalters@402
|
119
|
tomwalters@402
|
120 if (!config_dump_filename.empty()) {
|
tomwalters@402
|
121 LOG_INFO("AIMCopy: Dumping configuration.");
|
tomwalters@402
|
122 ofstream output_stream;
|
tomwalters@402
|
123 output_stream.open(config_dump_filename.c_str());
|
tomwalters@402
|
124 if (output_stream.fail()) {
|
tomwalters@402
|
125 LOG_ERROR(_T("Failed to open configuration file %s for writing."),
|
tomwalters@402
|
126 config_dump_filename.c_str());
|
tomwalters@402
|
127 return false;
|
tomwalters@402
|
128 }
|
tomwalters@402
|
129
|
tomwalters@402
|
130 time_t rawtime;
|
tomwalters@402
|
131 struct tm * timeinfo;
|
tomwalters@402
|
132 time(&rawtime);
|
tomwalters@402
|
133 timeinfo = localtime(&rawtime);
|
tomwalters@402
|
134 output_stream << "# AIM-C AIMCopy\n";
|
tomwalters@402
|
135 output_stream << "# Run at: " << asctime(timeinfo);
|
tomwalters@402
|
136 char * descr = getenv("USER");
|
tomwalters@402
|
137 if (descr) {
|
tomwalters@402
|
138 output_stream << "# By user: " << descr <<"\n";
|
tomwalters@402
|
139 }
|
tomwalters@402
|
140 tree_.PrintConfiguration(output_stream);
|
tomwalters@402
|
141 output_stream.close();
|
tomwalters@402
|
142 }
|
tomwalters@402
|
143
|
tomwalters@402
|
144 if (!config_graph_filename.empty()) {
|
tomwalters@402
|
145 ofstream output_stream;
|
tomwalters@402
|
146 output_stream.open(config_graph_filename.c_str());
|
tomwalters@402
|
147 if (output_stream.fail()) {
|
tomwalters@402
|
148 LOG_ERROR(_T("Failed to open graph file %s for writing."),
|
tomwalters@402
|
149 config_graph_filename.c_str());
|
tomwalters@402
|
150 return false;
|
tomwalters@402
|
151 }
|
tomwalters@402
|
152 tree_.MakeDotGraph(output_stream);
|
tomwalters@402
|
153 output_stream.close();
|
tomwalters@402
|
154 }
|
tomwalters@402
|
155 return true;
|
tomwalters@402
|
156 }
|
tomwalters@402
|
157
|
tomwalters@402
|
158 bool AIMCopy::Process() {
|
tomwalters@402
|
159 if (!initialized_) {
|
tomwalters@402
|
160 return false;
|
tomwalters@402
|
161 }
|
tomwalters@418
|
162 bool tree_initialized = false;
|
tomwalters@402
|
163 for (unsigned int i = 0; i < script_.size(); ++i) {
|
tomwalters@402
|
164 global_parameters_.SetString("input_filename", script_[i].first.c_str());
|
tomwalters@402
|
165 global_parameters_.SetString("output_filename_base", script_[i].second.c_str());
|
tomwalters@418
|
166 if (!tree_initialized) {
|
tomwalters@418
|
167 if (!tree_.Initialize(&global_parameters_)) {
|
tomwalters@418
|
168 return false;
|
tomwalters@418
|
169 }
|
tomwalters@418
|
170 tree_initialized = true;
|
tomwalters@418
|
171 } else {
|
tomwalters@418
|
172 tree_.Reset();
|
tomwalters@402
|
173 }
|
tomwalters@402
|
174 aimc::LOG_INFO(_T("%s -> %s"),
|
tomwalters@402
|
175 script_[i].first.c_str(),
|
tomwalters@402
|
176 script_[i].second.c_str());
|
tomwalters@411
|
177 tree_.Process();
|
tomwalters@402
|
178 }
|
tomwalters@418
|
179 // A final call to Reset() is required to close any open files.
|
tomwalters@418
|
180 global_parameters_.SetString("input_filename", "");
|
tomwalters@418
|
181 global_parameters_.SetString("output_filename_base", "");
|
tomwalters@418
|
182 tree_.Reset();
|
tomwalters@402
|
183 return true;
|
tomwalters@402
|
184 }
|
tomwalters@402
|
185
|
tomwalters@402
|
186 } // namespace aimc
|
tomwalters@402
|
187
|
tomwalters@296
|
188 int main(int argc, char* argv[]) {
|
tomwalters@402
|
189 std::string data_file;
|
tomwalters@402
|
190 std::string dot_file;
|
tomwalters@402
|
191 std::string config_file;
|
tomwalters@402
|
192 std::string script_file;
|
tomwalters@296
|
193
|
tomwalters@402
|
194 const std::string version_string(
|
tomwalters@296
|
195 " AIM-C AIMCopy\n"
|
tomwalters@296
|
196 " (c) 2006-2010, Thomas Walters and Willem van Engen\n"
|
tomwalters@296
|
197 " http://www.acoustiscale.org/AIMC/\n"
|
tomwalters@402
|
198 "\n");
|
tomwalters@402
|
199
|
tomwalters@402
|
200 const std::string usage_string(
|
tomwalters@402
|
201 "AIMCopy is intended as a drop-in replacement for HTK's HCopy\n"
|
tomwalters@402
|
202 "command. It is used for making features from audio files for\n"
|
tomwalters@402
|
203 "use with HTK.\n"
|
tomwalters@402
|
204 "Usage: \n"
|
tomwalters@402
|
205 " <flag> <meaning> <default>\n"
|
tomwalters@402
|
206 " -A Print command line arguments off\n"
|
tomwalters@402
|
207 " -C cf Set config file to cf none\n"
|
tomwalters@402
|
208 " -S f Set script file to f none\n"
|
tomwalters@402
|
209 " -V Print version information off\n"
|
tomwalters@402
|
210 " -D d Write complete parameter set to file d none\n"
|
tomwalters@402
|
211 " -G g Write graph to file g none\n");
|
tomwalters@296
|
212
|
tomwalters@296
|
213 if (argc < 2) {
|
tomwalters@402
|
214 std::cout << version_string.c_str();
|
tomwalters@402
|
215 std::cout << usage_string.c_str();
|
tomwalters@296
|
216 return -1;
|
tomwalters@296
|
217 }
|
tomwalters@296
|
218
|
tomwalters@296
|
219 // Parse command-line arguments
|
tomwalters@296
|
220 for (int i = 1; i < argc; i++) {
|
tomwalters@296
|
221 if (strcmp(argv[i],"-A") == 0) {
|
tomwalters@296
|
222 for (int j = 0; j < argc; j++)
|
tomwalters@296
|
223 printf("%s ",argv[j]);
|
tomwalters@296
|
224 printf("\n");
|
tomwalters@296
|
225 fflush(stdout);
|
tomwalters@296
|
226 continue;
|
tomwalters@296
|
227 }
|
tomwalters@296
|
228 if (strcmp(argv[i],"-C") == 0) {
|
tomwalters@296
|
229 if (++i >= argc) {
|
tomwalters@296
|
230 aimc::LOG_ERROR(_T("Configuration file name expected after -C"));
|
tomwalters@296
|
231 return(-1);
|
tomwalters@296
|
232 }
|
tomwalters@296
|
233 config_file = argv[i];
|
tomwalters@296
|
234 continue;
|
tomwalters@296
|
235 }
|
tomwalters@296
|
236 if (strcmp(argv[i],"-S") == 0) {
|
tomwalters@296
|
237 if (++i >= argc) {
|
tomwalters@296
|
238 aimc::LOG_ERROR(_T("Script file name expected after -S"));
|
tomwalters@296
|
239 return(-1);
|
tomwalters@296
|
240 }
|
tomwalters@296
|
241 script_file = argv[i];
|
tomwalters@296
|
242 continue;
|
tomwalters@296
|
243 }
|
tomwalters@296
|
244 if (strcmp(argv[i],"-D") == 0) {
|
tomwalters@296
|
245 if (++i >= argc) {
|
tomwalters@296
|
246 aimc::LOG_ERROR(_T("Data file name expected after -D"));
|
tomwalters@296
|
247 return(-1);
|
tomwalters@296
|
248 }
|
tomwalters@296
|
249 data_file = argv[i];
|
tomwalters@296
|
250 continue;
|
tomwalters@296
|
251 }
|
tomwalters@402
|
252 if (strcmp(argv[i],"-G") == 0) {
|
tomwalters@402
|
253 if (++i >= argc) {
|
tomwalters@402
|
254 aimc::LOG_ERROR(_T("Graph file name expected after -D"));
|
tomwalters@402
|
255 return(-1);
|
tomwalters@402
|
256 }
|
tomwalters@402
|
257 dot_file = argv[i];
|
tomwalters@402
|
258 continue;
|
tomwalters@402
|
259 }
|
tomwalters@402
|
260 if (strcmp(argv[i],"-V") == 0) {
|
tomwalters@402
|
261 std::cout << version_string;
|
tomwalters@296
|
262 continue;
|
tomwalters@296
|
263 }
|
tomwalters@296
|
264 aimc::LOG_ERROR(_T("Unrecognized command-line argument: %s"), argv[i]);
|
tomwalters@296
|
265 }
|
tomwalters@296
|
266
|
tomwalters@402
|
267 std::cout << "Configuration file: " << config_file << std::endl;
|
tomwalters@402
|
268 std::cout << "Script file: " << script_file << std::endl;
|
tomwalters@402
|
269 std::cout << "Data file: " << data_file << std::endl;
|
tomwalters@402
|
270 std::cout << "Graph file: " << dot_file << std::endl;
|
tomwalters@402
|
271
|
tomwalters@402
|
272 aimc::AIMCopy processor;
|
tomwalters@402
|
273 aimc::LOG_INFO("main: Initializing...");
|
tomwalters@402
|
274 if (!processor.Initialize(script_file, config_file)) {
|
tomwalters@296
|
275 return -1;
|
tomwalters@296
|
276 }
|
tomwalters@402
|
277
|
tomwalters@402
|
278 aimc::LOG_INFO("main: Writing confg...");
|
tomwalters@402
|
279 if (!processor.WriteConfig(data_file, dot_file)) {
|
tomwalters@296
|
280 return -1;
|
tomwalters@296
|
281 }
|
tomwalters@402
|
282
|
tomwalters@402
|
283 aimc::LOG_INFO("main: Processing...");
|
tomwalters@402
|
284 if (!processor.Process()) {
|
tomwalters@402
|
285 return -1;
|
tomwalters@296
|
286 }
|
tomwalters@296
|
287
|
tomwalters@296
|
288 return 0;
|
tomwalters@296
|
289 }
|