Mercurial > hg > aimc
comparison src/Main/AIMCopy.cc @ 121:3cdaa81c3aca
- Massive refactoring to make module tree stuff work. In theory we now support configuration files again. The graphics stuff is untested as yet.
author | tomwalters |
---|---|
date | Mon, 18 Oct 2010 04:42:28 +0000 |
parents | c5f5e9569863 |
children | a9cb396529c2 |
comparison
equal
deleted
inserted
replaced
120:89e4facffd93 | 121:3cdaa81c3aca |
---|---|
41 #include <vector> | 41 #include <vector> |
42 | 42 |
43 #include <stdlib.h> | 43 #include <stdlib.h> |
44 #include <time.h> | 44 #include <time.h> |
45 | 45 |
46 #include "Modules/Input/ModuleFileInput.h" | |
47 #include "Modules/BMM/ModuleGammatone.h" | |
48 #include "Modules/BMM/ModulePZFC.h" | |
49 #include "Modules/NAP/ModuleHCL.h" | |
50 #include "Modules/Strobes/ModuleParabola.h" | |
51 #include "Modules/SAI/ModuleSAI.h" | |
52 #include "Modules/SSI/ModuleSSI.h" | |
53 #include "Modules/Profile/ModuleSlice.h" | |
54 #include "Modules/Profile/ModuleScaler.h" | |
55 #include "Modules/Features/ModuleGaussians.h" | |
56 #include "Modules/Output/FileOutputHTK.h" | |
57 #include "Support/Common.h" | 46 #include "Support/Common.h" |
58 #include "Support/FileList.h" | 47 #include "Support/FileList.h" |
48 #include "Support/ModuleTree.h" | |
59 #include "Support/Parameters.h" | 49 #include "Support/Parameters.h" |
60 | 50 |
51 namespace aimc { | |
61 using std::ofstream; | 52 using std::ofstream; |
62 using std::pair; | 53 using std::pair; |
63 using std::vector; | 54 using std::vector; |
64 using std::string; | 55 using std::string; |
56 class AIMCopy { | |
57 public: | |
58 AIMCopy(); | |
59 | |
60 bool Initialize(string script_filename, | |
61 string config_filename); | |
62 | |
63 bool WriteConfig(string config_dump_filename, | |
64 string config_graph_filename); | |
65 | |
66 bool Process(); | |
67 | |
68 private: | |
69 bool initialized_; | |
70 Parameters global_parameters_; | |
71 ModuleTree tree_; | |
72 vector<pair<string, string> > script_; | |
73 }; | |
74 | |
75 | |
76 AIMCopy::AIMCopy() : initialized_(false) { | |
77 | |
78 } | |
79 | |
80 bool AIMCopy::Initialize(string script_filename, | |
81 string config_filename) { | |
82 | |
83 LOG_INFO("AIMCopy: Loading script"); | |
84 script_ = FileList::Load(script_filename); | |
85 if (script_.size() == 0) { | |
86 LOG_ERROR("No data read from script file %s", script_filename.c_str()); | |
87 return false; | |
88 } | |
89 | |
90 LOG_INFO("AIMCopy: Loading configuration"); | |
91 if (!tree_.LoadConfigFile(config_filename)) { | |
92 LOG_ERROR(_T("Failed to load configuration file")); | |
93 return false; | |
94 } | |
95 LOG_INFO("AIMCopy: Successfully loaded configuration"); | |
96 initialized_ = true; | |
97 return true; | |
98 } | |
99 | |
100 bool AIMCopy::WriteConfig(string config_dump_filename, | |
101 string config_graph_filename) { | |
102 if (!initialized_) { | |
103 return false; | |
104 } | |
105 | |
106 if (script_.size() > 0) { | |
107 global_parameters_.SetString("input_filename", script_[0].first.c_str()); | |
108 global_parameters_.SetString("output_filename_base", script_[0].second.c_str()); | |
109 LOG_INFO("AIMCopy: Initializing tree for initial parameter write."); | |
110 if (!tree_.Initialize(&global_parameters_)) { | |
111 LOG_ERROR(_T("Failed to initialize tree.")); | |
112 return false; | |
113 } | |
114 } else { | |
115 LOG_ERROR(_T("No input files in script.")); | |
116 return false; | |
117 } | |
118 | |
119 if (!config_dump_filename.empty()) { | |
120 LOG_INFO("AIMCopy: Dumping configuration."); | |
121 ofstream output_stream; | |
122 output_stream.open(config_dump_filename.c_str()); | |
123 if (output_stream.fail()) { | |
124 LOG_ERROR(_T("Failed to open configuration file %s for writing."), | |
125 config_dump_filename.c_str()); | |
126 return false; | |
127 } | |
128 | |
129 time_t rawtime; | |
130 struct tm * timeinfo; | |
131 time(&rawtime); | |
132 timeinfo = localtime(&rawtime); | |
133 output_stream << "# AIM-C AIMCopy\n"; | |
134 output_stream << "# Run at: " << asctime(timeinfo); | |
135 char * descr = getenv("USER"); | |
136 if (descr) { | |
137 output_stream << "# By user: " << descr <<"\n"; | |
138 } | |
139 tree_.PrintConfiguration(output_stream); | |
140 output_stream.close(); | |
141 } | |
142 | |
143 if (!config_graph_filename.empty()) { | |
144 ofstream output_stream; | |
145 output_stream.open(config_graph_filename.c_str()); | |
146 if (output_stream.fail()) { | |
147 LOG_ERROR(_T("Failed to open graph file %s for writing."), | |
148 config_graph_filename.c_str()); | |
149 return false; | |
150 } | |
151 tree_.MakeDotGraph(output_stream); | |
152 output_stream.close(); | |
153 } | |
154 return true; | |
155 } | |
156 | |
157 bool AIMCopy::Process() { | |
158 if (!initialized_) { | |
159 return false; | |
160 } | |
161 for (unsigned int i = 0; i < script_.size(); ++i) { | |
162 global_parameters_.SetString("input_filename", script_[i].first.c_str()); | |
163 global_parameters_.SetString("output_filename_base", script_[i].second.c_str()); | |
164 if (!tree_.Initialize(&global_parameters_)) { | |
165 return false; | |
166 } | |
167 aimc::LOG_INFO(_T("%s -> %s"), | |
168 script_[i].first.c_str(), | |
169 script_[i].second.c_str()); | |
170 tree_.Reset(); | |
171 tree_.Process(); | |
172 } | |
173 return true; | |
174 } | |
175 | |
176 } // namespace aimc | |
177 | |
65 int main(int argc, char* argv[]) { | 178 int main(int argc, char* argv[]) { |
66 string sound_file; | 179 std::string data_file; |
67 string data_file; | 180 std::string dot_file; |
68 string config_file; | 181 std::string config_file; |
69 string script_file; | 182 std::string script_file; |
70 bool write_data = false; | 183 |
71 bool print_version = false; | 184 const std::string version_string( |
72 | |
73 string version_string( | |
74 " AIM-C AIMCopy\n" | 185 " AIM-C AIMCopy\n" |
75 " (c) 2006-2010, Thomas Walters and Willem van Engen\n" | 186 " (c) 2006-2010, Thomas Walters and Willem van Engen\n" |
76 " http://www.acoustiscale.org/AIMC/\n" | 187 " http://www.acoustiscale.org/AIMC/\n" |
77 "\n"); | 188 "\n"); |
189 | |
190 const std::string usage_string( | |
191 "AIMCopy is intended as a drop-in replacement for HTK's HCopy\n" | |
192 "command. It is used for making features from audio files for\n" | |
193 "use with HTK.\n" | |
194 "Usage: \n" | |
195 " <flag> <meaning> <default>\n" | |
196 " -A Print command line arguments off\n" | |
197 " -C cf Set config file to cf none\n" | |
198 " -S f Set script file to f none\n" | |
199 " -V Print version information off\n" | |
200 " -D d Write complete parameter set to file d none\n" | |
201 " -G g Write graph to file g none\n"); | |
78 | 202 |
79 if (argc < 2) { | 203 if (argc < 2) { |
80 printf("%s", version_string.c_str()); | 204 std::cout << version_string.c_str(); |
81 printf("AIMCopy is intended as a drop-in replacement for HTK's HCopy\n"); | 205 std::cout << usage_string.c_str(); |
82 printf("command. It is used for making features from audio files for\n"); | |
83 printf("use with HTK.\n"); | |
84 printf("Usage: \n"); | |
85 printf(" -A Print command line arguments off\n"); | |
86 printf(" -C cf Set config file to cf none\n"); | |
87 printf(" -S f Set script file to f none\n"); | |
88 printf(" -V Print version information off\n"); | |
89 printf(" -D g Write configuration data to g none\n"); | |
90 return -1; | 206 return -1; |
91 } | 207 } |
92 | 208 |
93 // Parse command-line arguments | 209 // Parse command-line arguments |
94 for (int i = 1; i < argc; i++) { | 210 for (int i = 1; i < argc; i++) { |
119 if (++i >= argc) { | 235 if (++i >= argc) { |
120 aimc::LOG_ERROR(_T("Data file name expected after -D")); | 236 aimc::LOG_ERROR(_T("Data file name expected after -D")); |
121 return(-1); | 237 return(-1); |
122 } | 238 } |
123 data_file = argv[i]; | 239 data_file = argv[i]; |
124 write_data = true; | 240 continue; |
125 continue; | 241 } |
126 } | 242 if (strcmp(argv[i],"-G") == 0) { |
127 if (strcmp(argv[i],"-V") == 0) { | 243 if (++i >= argc) { |
128 print_version = true; | 244 aimc::LOG_ERROR(_T("Graph file name expected after -D")); |
245 return(-1); | |
246 } | |
247 dot_file = argv[i]; | |
248 continue; | |
249 } | |
250 if (strcmp(argv[i],"-V") == 0) { | |
251 std::cout << version_string; | |
129 continue; | 252 continue; |
130 } | 253 } |
131 aimc::LOG_ERROR(_T("Unrecognized command-line argument: %s"), argv[i]); | 254 aimc::LOG_ERROR(_T("Unrecognized command-line argument: %s"), argv[i]); |
132 } | 255 } |
133 | 256 |
134 if (print_version) | 257 std::cout << "Configuration file: " << config_file << std::endl; |
135 printf("%s", version_string.c_str()); | 258 std::cout << "Script file: " << script_file << std::endl; |
136 | 259 std::cout << "Data file: " << data_file << std::endl; |
137 aimc::Parameters params; | 260 std::cout << "Graph file: " << dot_file << std::endl; |
138 | 261 |
139 if (!params.Load(config_file.c_str())) { | 262 aimc::AIMCopy processor; |
140 aimc::LOG_ERROR(_T("Couldn't load parameters from file %s"), | 263 aimc::LOG_INFO("main: Initializing..."); |
141 config_file.c_str()); | 264 if (!processor.Initialize(script_file, config_file)) { |
142 return -1; | 265 return -1; |
143 } | 266 } |
144 | 267 |
145 vector<pair<string, string> > file_list = aimc::FileList::Load(script_file); | 268 aimc::LOG_INFO("main: Writing confg..."); |
146 if (file_list.size() == 0) { | 269 if (!processor.WriteConfig(data_file, dot_file)) { |
147 aimc::LOG_ERROR("No data read from file %s", script_file.c_str()); | 270 return -1; |
148 return -1; | 271 } |
149 } | 272 |
150 | 273 aimc::LOG_INFO("main: Processing..."); |
151 // Set up AIM-C processor here | 274 if (!processor.Process()) { |
152 aimc::ModuleFileInput input(¶ms); | 275 return -1; |
153 aimc::ModuleGammatone bmm(¶ms); | |
154 aimc::ModuleHCL nap(¶ms); | |
155 aimc::ModuleSlice profile(¶ms); | |
156 aimc::ModuleScaler scaler(¶ms); | |
157 aimc::ModuleGaussians features(¶ms); | |
158 aimc::FileOutputHTK output(¶ms); | |
159 | |
160 input.AddTarget(&bmm); | |
161 bmm.AddTarget(&nap); | |
162 nap.AddTarget(&profile); | |
163 profile.AddTarget(&scaler); | |
164 scaler.AddTarget(&features); | |
165 features.AddTarget(&output); | |
166 | |
167 if (write_data) { | |
168 ofstream outfile(data_file.c_str()); | |
169 if (outfile.fail()) { | |
170 aimc::LOG_ERROR("Couldn't open data file %s for writing", | |
171 data_file.c_str()); | |
172 return -1; | |
173 } | |
174 time_t rawtime; | |
175 struct tm * timeinfo; | |
176 time(&rawtime); | |
177 timeinfo = localtime(&rawtime); | |
178 | |
179 | |
180 outfile << "# AIM-C AIMCopy\n"; | |
181 outfile << "# Run on: " << asctime(timeinfo); | |
182 char * descr = getenv("USER"); | |
183 if (descr) { | |
184 outfile << "# By user: " << descr <<"\n"; | |
185 } | |
186 outfile << "# Module chain: file_input->gt->hcl->slice->scaler->"; | |
187 outfile << "gaussians->out_htk\n"; | |
188 outfile << "# Module versions:\n"; | |
189 outfile << "# " << input.id() << " : " << input.version() << "\n"; | |
190 outfile << "# " << bmm.id() << " : " << bmm.version() << "\n"; | |
191 outfile << "# " << nap.id() << " : " << nap.version() << "\n"; | |
192 outfile << "# " << profile.id() << " : " << profile.version() << "\n"; | |
193 outfile << "# " << scaler.id() << " : " << scaler.version() << "\n"; | |
194 outfile << "# " << features.id() << " : " << features.version() << "\n"; | |
195 outfile << "# " << output.id() << " : " << output.version() << "\n"; | |
196 outfile << "#\n"; | |
197 outfile << "# Parameters:\n"; | |
198 outfile << params.WriteString(); | |
199 outfile.close(); | |
200 } | |
201 | |
202 for (unsigned int i = 0; i < file_list.size(); ++i) { | |
203 aimc::LOG_INFO(_T("In: %s"), file_list[i].first.c_str()); | |
204 aimc::LOG_INFO(_T("Out: %s"), file_list[i].second.c_str()); | |
205 | |
206 output.OpenFile(file_list[i].second.c_str(), 10.0f); | |
207 if (input.LoadFile(file_list[i].first.c_str())) { | |
208 input.Process(); | |
209 } else { | |
210 printf("LoadFile failed for file %s\n", file_list[i].first.c_str()); | |
211 } | |
212 input.Reset(); | |
213 } | 276 } |
214 | 277 |
215 return 0; | 278 return 0; |
216 } | 279 } |