annotate src/Main/AIMCopy.cc @ 611:0fbaf443ec82

Carfac C++ revision 3, indluding more style improvements. The output structs are now classes again, and have separate storage methods for each output structure along with flags in the Run and RunSegment methods to allow for only storing NAPs if desired.
author alexbrandmeyer
date Fri, 17 May 2013 19:52:45 +0000
parents d3968c3149b0
children
rev   line source
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@232 48 #include "Support/ModuleTree.h"
tomwalters@23 49 #include "Support/Parameters.h"
tomwalters@23 50
tomwalters@232 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@232 56 class AIMCopy {
tomwalters@232 57 public:
tomwalters@232 58 AIMCopy();
tomwalters@232 59
tomwalters@232 60 bool Initialize(string script_filename,
tomwalters@232 61 string config_filename);
tomwalters@232 62
tomwalters@232 63 bool WriteConfig(string config_dump_filename,
tomwalters@232 64 string config_graph_filename);
tomwalters@232 65
tomwalters@232 66 bool Process();
tomwalters@232 67
tomwalters@232 68 private:
tomwalters@232 69 bool initialized_;
tomwalters@232 70 Parameters global_parameters_;
tomwalters@232 71 ModuleTree tree_;
tomwalters@232 72 vector<pair<string, string> > script_;
tomwalters@232 73 };
tomwalters@232 74
tomwalters@232 75
tomwalters@232 76 AIMCopy::AIMCopy() : initialized_(false) {
tomwalters@232 77
tomwalters@232 78 }
tomwalters@232 79
tomwalters@232 80 bool AIMCopy::Initialize(string script_filename,
tomwalters@232 81 string config_filename) {
tomwalters@232 82
tomwalters@232 83 LOG_INFO("AIMCopy: Loading script");
tomwalters@232 84 script_ = FileList::Load(script_filename);
tomwalters@232 85 if (script_.size() == 0) {
tomwalters@232 86 LOG_ERROR("No data read from script file %s", script_filename.c_str());
tomwalters@232 87 return false;
tomwalters@232 88 }
tomwalters@232 89
tomwalters@232 90 LOG_INFO("AIMCopy: Loading configuration");
tomwalters@232 91 if (!tree_.LoadConfigFile(config_filename)) {
tomwalters@232 92 LOG_ERROR(_T("Failed to load configuration file"));
tomwalters@232 93 return false;
tomwalters@232 94 }
tomwalters@232 95 LOG_INFO("AIMCopy: Successfully loaded configuration");
tomwalters@232 96 initialized_ = true;
tomwalters@232 97 return true;
tomwalters@232 98 }
tomwalters@232 99
tomwalters@232 100 bool AIMCopy::WriteConfig(string config_dump_filename,
tomwalters@232 101 string config_graph_filename) {
tomwalters@232 102 if (!initialized_) {
tomwalters@232 103 return false;
tomwalters@232 104 }
tomwalters@232 105
tomwalters@232 106 if (script_.size() > 0) {
tomwalters@232 107 global_parameters_.SetString("input_filename", script_[0].first.c_str());
tomwalters@232 108 global_parameters_.SetString("output_filename_base", script_[0].second.c_str());
tomwalters@232 109 LOG_INFO("AIMCopy: Initializing tree for initial parameter write.");
tomwalters@232 110 if (!tree_.Initialize(&global_parameters_)) {
tomwalters@232 111 LOG_ERROR(_T("Failed to initialize tree."));
tomwalters@232 112 return false;
tomwalters@232 113 }
tomwalters@237 114 tree_.Reset();
tomwalters@232 115 } else {
tomwalters@232 116 LOG_ERROR(_T("No input files in script."));
tomwalters@232 117 return false;
tomwalters@232 118 }
tomwalters@232 119
tomwalters@232 120 if (!config_dump_filename.empty()) {
tomwalters@232 121 LOG_INFO("AIMCopy: Dumping configuration.");
tomwalters@232 122 ofstream output_stream;
tomwalters@232 123 output_stream.open(config_dump_filename.c_str());
tomwalters@232 124 if (output_stream.fail()) {
tomwalters@232 125 LOG_ERROR(_T("Failed to open configuration file %s for writing."),
tomwalters@232 126 config_dump_filename.c_str());
tomwalters@232 127 return false;
tomwalters@232 128 }
tomwalters@232 129
tomwalters@232 130 time_t rawtime;
tomwalters@232 131 struct tm * timeinfo;
tomwalters@232 132 time(&rawtime);
tomwalters@232 133 timeinfo = localtime(&rawtime);
tomwalters@232 134 output_stream << "# AIM-C AIMCopy\n";
tomwalters@232 135 output_stream << "# Run at: " << asctime(timeinfo);
tomwalters@232 136 char * descr = getenv("USER");
tomwalters@232 137 if (descr) {
tomwalters@232 138 output_stream << "# By user: " << descr <<"\n";
tomwalters@232 139 }
tomwalters@232 140 tree_.PrintConfiguration(output_stream);
tomwalters@232 141 output_stream.close();
tomwalters@232 142 }
tomwalters@232 143
tomwalters@232 144 if (!config_graph_filename.empty()) {
tomwalters@232 145 ofstream output_stream;
tomwalters@232 146 output_stream.open(config_graph_filename.c_str());
tomwalters@232 147 if (output_stream.fail()) {
tomwalters@232 148 LOG_ERROR(_T("Failed to open graph file %s for writing."),
tomwalters@232 149 config_graph_filename.c_str());
tomwalters@232 150 return false;
tomwalters@232 151 }
tomwalters@232 152 tree_.MakeDotGraph(output_stream);
tomwalters@232 153 output_stream.close();
tomwalters@232 154 }
tomwalters@232 155 return true;
tomwalters@232 156 }
tomwalters@232 157
tomwalters@232 158 bool AIMCopy::Process() {
tomwalters@232 159 if (!initialized_) {
tomwalters@232 160 return false;
tomwalters@232 161 }
tomwalters@244 162 bool tree_initialized = false;
tomwalters@232 163 for (unsigned int i = 0; i < script_.size(); ++i) {
tomwalters@232 164 global_parameters_.SetString("input_filename", script_[i].first.c_str());
tomwalters@232 165 global_parameters_.SetString("output_filename_base", script_[i].second.c_str());
tomwalters@244 166 if (!tree_initialized) {
tomwalters@244 167 if (!tree_.Initialize(&global_parameters_)) {
tomwalters@244 168 return false;
tomwalters@244 169 }
tomwalters@244 170 tree_initialized = true;
tomwalters@244 171 } else {
tomwalters@244 172 tree_.Reset();
tomwalters@232 173 }
tomwalters@232 174 aimc::LOG_INFO(_T("%s -> %s"),
tomwalters@232 175 script_[i].first.c_str(),
tomwalters@232 176 script_[i].second.c_str());
tomwalters@237 177 tree_.Process();
tomwalters@232 178 }
tomwalters@244 179 // A final call to Reset() is required to close any open files.
tomwalters@244 180 global_parameters_.SetString("input_filename", "");
tomwalters@244 181 global_parameters_.SetString("output_filename_base", "");
tomwalters@244 182 tree_.Reset();
tomwalters@232 183 return true;
tomwalters@232 184 }
tomwalters@232 185
tomwalters@232 186 } // namespace aimc
tomwalters@232 187
tomwalters@162 188 int main(int argc, char* argv[]) {
tomwalters@232 189 std::string data_file;
tomwalters@232 190 std::string dot_file;
tomwalters@232 191 std::string config_file;
tomwalters@232 192 std::string script_file;
tomwalters@121 193
tomwalters@232 194 const std::string version_string(
tomwalters@23 195 " AIM-C AIMCopy\n"
tomwalters@23 196 " (c) 2006-2010, Thomas Walters and Willem van Engen\n"
tomwalters@23 197 " http://www.acoustiscale.org/AIMC/\n"
tomwalters@232 198 "\n");
tomwalters@232 199
tomwalters@232 200 const std::string usage_string(
tomwalters@232 201 "AIMCopy is intended as a drop-in replacement for HTK's HCopy\n"
tomwalters@232 202 "command. It is used for making features from audio files for\n"
tomwalters@232 203 "use with HTK.\n"
tomwalters@232 204 "Usage: \n"
tomwalters@232 205 " <flag> <meaning> <default>\n"
tomwalters@232 206 " -A Print command line arguments off\n"
tomwalters@232 207 " -C cf Set config file to cf none\n"
tomwalters@232 208 " -S f Set script file to f none\n"
tomwalters@232 209 " -V Print version information off\n"
tomwalters@232 210 " -D d Write complete parameter set to file d none\n"
tomwalters@232 211 " -G g Write graph to file g none\n");
tomwalters@23 212
tomwalters@23 213 if (argc < 2) {
tomwalters@232 214 std::cout << version_string.c_str();
tomwalters@232 215 std::cout << usage_string.c_str();
tomwalters@23 216 return -1;
tomwalters@23 217 }
tomwalters@23 218
tomwalters@23 219 // Parse command-line arguments
tomwalters@23 220 for (int i = 1; i < argc; i++) {
tomwalters@23 221 if (strcmp(argv[i],"-A") == 0) {
tomwalters@23 222 for (int j = 0; j < argc; j++)
tomwalters@23 223 printf("%s ",argv[j]);
tomwalters@23 224 printf("\n");
tomwalters@23 225 fflush(stdout);
tomwalters@23 226 continue;
tomwalters@23 227 }
tomwalters@23 228 if (strcmp(argv[i],"-C") == 0) {
tomwalters@23 229 if (++i >= argc) {
tomwalters@23 230 aimc::LOG_ERROR(_T("Configuration file name expected after -C"));
tomwalters@23 231 return(-1);
tomwalters@23 232 }
tomwalters@23 233 config_file = argv[i];
tomwalters@23 234 continue;
tomwalters@23 235 }
tomwalters@23 236 if (strcmp(argv[i],"-S") == 0) {
tomwalters@23 237 if (++i >= argc) {
tomwalters@23 238 aimc::LOG_ERROR(_T("Script file name expected after -S"));
tomwalters@23 239 return(-1);
tomwalters@23 240 }
tomwalters@23 241 script_file = argv[i];
tomwalters@23 242 continue;
tomwalters@23 243 }
tomwalters@23 244 if (strcmp(argv[i],"-D") == 0) {
tomwalters@23 245 if (++i >= argc) {
tomwalters@23 246 aimc::LOG_ERROR(_T("Data file name expected after -D"));
tomwalters@23 247 return(-1);
tomwalters@23 248 }
tomwalters@23 249 data_file = argv[i];
tomwalters@23 250 continue;
tomwalters@23 251 }
tomwalters@232 252 if (strcmp(argv[i],"-G") == 0) {
tomwalters@232 253 if (++i >= argc) {
tomwalters@232 254 aimc::LOG_ERROR(_T("Graph file name expected after -D"));
tomwalters@232 255 return(-1);
tomwalters@232 256 }
tomwalters@232 257 dot_file = argv[i];
tomwalters@232 258 continue;
tomwalters@232 259 }
tomwalters@232 260 if (strcmp(argv[i],"-V") == 0) {
tomwalters@232 261 std::cout << version_string;
tomwalters@23 262 continue;
tomwalters@23 263 }
tomwalters@23 264 aimc::LOG_ERROR(_T("Unrecognized command-line argument: %s"), argv[i]);
tomwalters@23 265 }
tomwalters@23 266
tomwalters@232 267 std::cout << "Configuration file: " << config_file << std::endl;
tomwalters@232 268 std::cout << "Script file: " << script_file << std::endl;
tomwalters@232 269 std::cout << "Data file: " << data_file << std::endl;
tomwalters@232 270 std::cout << "Graph file: " << dot_file << std::endl;
tomwalters@232 271
tomwalters@232 272 aimc::AIMCopy processor;
tomwalters@232 273 aimc::LOG_INFO("main: Initializing...");
tomwalters@232 274 if (!processor.Initialize(script_file, config_file)) {
tomwalters@23 275 return -1;
tomwalters@23 276 }
tomwalters@232 277
tomwalters@232 278 aimc::LOG_INFO("main: Writing confg...");
tomwalters@232 279 if (!processor.WriteConfig(data_file, dot_file)) {
tomwalters@23 280 return -1;
tomwalters@23 281 }
tomwalters@232 282
tomwalters@232 283 aimc::LOG_INFO("main: Processing...");
tomwalters@232 284 if (!processor.Process()) {
tomwalters@232 285 return -1;
tomwalters@23 286 }
tomwalters@23 287
tomwalters@23 288 return 0;
tomwalters@23 289 }