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