tomwalters@23: // Copyright 2008-2010, Thomas Walters tomwalters@23: // tomwalters@23: // AIM-C: A C++ implementation of the Auditory Image Model tomwalters@23: // http://www.acousticscale.org/AIMC tomwalters@23: // tomwalters@45: // Licensed under the Apache License, Version 2.0 (the "License"); tomwalters@45: // you may not use this file except in compliance with the License. tomwalters@45: // You may obtain a copy of the License at tomwalters@23: // tomwalters@45: // http://www.apache.org/licenses/LICENSE-2.0 tomwalters@23: // tomwalters@45: // Unless required by applicable law or agreed to in writing, software tomwalters@45: // distributed under the License is distributed on an "AS IS" BASIS, tomwalters@45: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tomwalters@45: // See the License for the specific language governing permissions and tomwalters@45: // limitations under the License. tomwalters@23: tomwalters@23: /*! tomwalters@23: * \file AIMCopy.cpp tomwalters@23: * \brief AIM-C replacement for HTK's HCopy tomwalters@23: * tomwalters@23: * The following subset of the command-line flags tomwalters@23: * should be implemented from HCopy: tomwalters@23: * -A Print command line arguments off tomwalters@23: * -C cf Set config file to cf default tomwalters@23: * (should be able to take multiple config files) tomwalters@23: * -S f Set script file to f none tomwalters@23: * //! \todo -T N Set trace flags to N 0 tomwalters@23: * -V Print version information off tomwalters@23: * -D of Write configuration data to of none tomwalters@23: * tomwalters@23: * \author Thomas Walters tomwalters@23: * \date created 2008/05/08 tomwalters@23: * \version \$Id$ tomwalters@23: */ tomwalters@23: tomwalters@23: #include tomwalters@23: #include tomwalters@23: #include tomwalters@23: #include tomwalters@23: #include tomwalters@23: tomwalters@23: #include tomwalters@24: #include tomwalters@23: tomwalters@23: #include "Support/Common.h" tomwalters@23: #include "Support/FileList.h" tomwalters@232: #include "Support/ModuleTree.h" tomwalters@23: #include "Support/Parameters.h" tomwalters@23: tomwalters@232: namespace aimc { tomwalters@23: using std::ofstream; tomwalters@23: using std::pair; tomwalters@23: using std::vector; tomwalters@23: using std::string; tomwalters@232: class AIMCopy { tomwalters@232: public: tomwalters@232: AIMCopy(); tomwalters@232: tomwalters@232: bool Initialize(string script_filename, tomwalters@232: string config_filename); tomwalters@232: tomwalters@232: bool WriteConfig(string config_dump_filename, tomwalters@232: string config_graph_filename); tomwalters@232: tomwalters@232: bool Process(); tomwalters@232: tomwalters@232: private: tomwalters@232: bool initialized_; tomwalters@232: Parameters global_parameters_; tomwalters@232: ModuleTree tree_; tomwalters@232: vector > script_; tomwalters@232: }; tomwalters@232: tomwalters@232: tomwalters@232: AIMCopy::AIMCopy() : initialized_(false) { tomwalters@232: tomwalters@232: } tomwalters@232: tomwalters@232: bool AIMCopy::Initialize(string script_filename, tomwalters@232: string config_filename) { tomwalters@232: tomwalters@232: LOG_INFO("AIMCopy: Loading script"); tomwalters@232: script_ = FileList::Load(script_filename); tomwalters@232: if (script_.size() == 0) { tomwalters@232: LOG_ERROR("No data read from script file %s", script_filename.c_str()); tomwalters@232: return false; tomwalters@232: } tomwalters@232: tomwalters@232: LOG_INFO("AIMCopy: Loading configuration"); tomwalters@232: if (!tree_.LoadConfigFile(config_filename)) { tomwalters@232: LOG_ERROR(_T("Failed to load configuration file")); tomwalters@232: return false; tomwalters@232: } tomwalters@232: LOG_INFO("AIMCopy: Successfully loaded configuration"); tomwalters@232: initialized_ = true; tomwalters@232: return true; tomwalters@232: } tomwalters@232: tomwalters@232: bool AIMCopy::WriteConfig(string config_dump_filename, tomwalters@232: string config_graph_filename) { tomwalters@232: if (!initialized_) { tomwalters@232: return false; tomwalters@232: } tomwalters@232: tomwalters@232: if (script_.size() > 0) { tomwalters@232: global_parameters_.SetString("input_filename", script_[0].first.c_str()); tomwalters@232: global_parameters_.SetString("output_filename_base", script_[0].second.c_str()); tomwalters@232: LOG_INFO("AIMCopy: Initializing tree for initial parameter write."); tomwalters@232: if (!tree_.Initialize(&global_parameters_)) { tomwalters@232: LOG_ERROR(_T("Failed to initialize tree.")); tomwalters@232: return false; tomwalters@232: } tomwalters@237: tree_.Reset(); tomwalters@232: } else { tomwalters@232: LOG_ERROR(_T("No input files in script.")); tomwalters@232: return false; tomwalters@232: } tomwalters@232: tomwalters@232: if (!config_dump_filename.empty()) { tomwalters@232: LOG_INFO("AIMCopy: Dumping configuration."); tomwalters@232: ofstream output_stream; tomwalters@232: output_stream.open(config_dump_filename.c_str()); tomwalters@232: if (output_stream.fail()) { tomwalters@232: LOG_ERROR(_T("Failed to open configuration file %s for writing."), tomwalters@232: config_dump_filename.c_str()); tomwalters@232: return false; tomwalters@232: } tomwalters@232: tomwalters@232: time_t rawtime; tomwalters@232: struct tm * timeinfo; tomwalters@232: time(&rawtime); tomwalters@232: timeinfo = localtime(&rawtime); tomwalters@232: output_stream << "# AIM-C AIMCopy\n"; tomwalters@232: output_stream << "# Run at: " << asctime(timeinfo); tomwalters@232: char * descr = getenv("USER"); tomwalters@232: if (descr) { tomwalters@232: output_stream << "# By user: " << descr <<"\n"; tomwalters@232: } tomwalters@232: tree_.PrintConfiguration(output_stream); tomwalters@232: output_stream.close(); tomwalters@232: } tomwalters@232: tomwalters@232: if (!config_graph_filename.empty()) { tomwalters@232: ofstream output_stream; tomwalters@232: output_stream.open(config_graph_filename.c_str()); tomwalters@232: if (output_stream.fail()) { tomwalters@232: LOG_ERROR(_T("Failed to open graph file %s for writing."), tomwalters@232: config_graph_filename.c_str()); tomwalters@232: return false; tomwalters@232: } tomwalters@232: tree_.MakeDotGraph(output_stream); tomwalters@232: output_stream.close(); tomwalters@232: } tomwalters@232: return true; tomwalters@232: } tomwalters@232: tomwalters@232: bool AIMCopy::Process() { tomwalters@232: if (!initialized_) { tomwalters@232: return false; tomwalters@232: } tomwalters@244: bool tree_initialized = false; tomwalters@232: for (unsigned int i = 0; i < script_.size(); ++i) { tomwalters@232: global_parameters_.SetString("input_filename", script_[i].first.c_str()); tomwalters@232: global_parameters_.SetString("output_filename_base", script_[i].second.c_str()); tomwalters@244: if (!tree_initialized) { tomwalters@244: if (!tree_.Initialize(&global_parameters_)) { tomwalters@244: return false; tomwalters@244: } tomwalters@244: tree_initialized = true; tomwalters@244: } else { tomwalters@244: tree_.Reset(); tomwalters@232: } tomwalters@232: aimc::LOG_INFO(_T("%s -> %s"), tomwalters@232: script_[i].first.c_str(), tomwalters@232: script_[i].second.c_str()); tomwalters@237: tree_.Process(); tomwalters@232: } tomwalters@244: // A final call to Reset() is required to close any open files. tomwalters@244: global_parameters_.SetString("input_filename", ""); tomwalters@244: global_parameters_.SetString("output_filename_base", ""); tomwalters@244: tree_.Reset(); tomwalters@232: return true; tomwalters@232: } tomwalters@232: tomwalters@232: } // namespace aimc tomwalters@232: tomwalters@162: int main(int argc, char* argv[]) { tomwalters@232: std::string data_file; tomwalters@232: std::string dot_file; tomwalters@232: std::string config_file; tomwalters@232: std::string script_file; tomwalters@121: tomwalters@232: const std::string version_string( tomwalters@23: " AIM-C AIMCopy\n" tomwalters@23: " (c) 2006-2010, Thomas Walters and Willem van Engen\n" tomwalters@23: " http://www.acoustiscale.org/AIMC/\n" tomwalters@232: "\n"); tomwalters@232: tomwalters@232: const std::string usage_string( tomwalters@232: "AIMCopy is intended as a drop-in replacement for HTK's HCopy\n" tomwalters@232: "command. It is used for making features from audio files for\n" tomwalters@232: "use with HTK.\n" tomwalters@232: "Usage: \n" tomwalters@232: " \n" tomwalters@232: " -A Print command line arguments off\n" tomwalters@232: " -C cf Set config file to cf none\n" tomwalters@232: " -S f Set script file to f none\n" tomwalters@232: " -V Print version information off\n" tomwalters@232: " -D d Write complete parameter set to file d none\n" tomwalters@232: " -G g Write graph to file g none\n"); tomwalters@23: tomwalters@23: if (argc < 2) { tomwalters@232: std::cout << version_string.c_str(); tomwalters@232: std::cout << usage_string.c_str(); tomwalters@23: return -1; tomwalters@23: } tomwalters@23: tomwalters@23: // Parse command-line arguments tomwalters@23: for (int i = 1; i < argc; i++) { tomwalters@23: if (strcmp(argv[i],"-A") == 0) { tomwalters@23: for (int j = 0; j < argc; j++) tomwalters@23: printf("%s ",argv[j]); tomwalters@23: printf("\n"); tomwalters@23: fflush(stdout); tomwalters@23: continue; tomwalters@23: } tomwalters@23: if (strcmp(argv[i],"-C") == 0) { tomwalters@23: if (++i >= argc) { tomwalters@23: aimc::LOG_ERROR(_T("Configuration file name expected after -C")); tomwalters@23: return(-1); tomwalters@23: } tomwalters@23: config_file = argv[i]; tomwalters@23: continue; tomwalters@23: } tomwalters@23: if (strcmp(argv[i],"-S") == 0) { tomwalters@23: if (++i >= argc) { tomwalters@23: aimc::LOG_ERROR(_T("Script file name expected after -S")); tomwalters@23: return(-1); tomwalters@23: } tomwalters@23: script_file = argv[i]; tomwalters@23: continue; tomwalters@23: } tomwalters@23: if (strcmp(argv[i],"-D") == 0) { tomwalters@23: if (++i >= argc) { tomwalters@23: aimc::LOG_ERROR(_T("Data file name expected after -D")); tomwalters@23: return(-1); tomwalters@23: } tomwalters@23: data_file = argv[i]; tomwalters@23: continue; tomwalters@23: } tomwalters@232: if (strcmp(argv[i],"-G") == 0) { tomwalters@232: if (++i >= argc) { tomwalters@232: aimc::LOG_ERROR(_T("Graph file name expected after -D")); tomwalters@232: return(-1); tomwalters@232: } tomwalters@232: dot_file = argv[i]; tomwalters@232: continue; tomwalters@232: } tomwalters@232: if (strcmp(argv[i],"-V") == 0) { tomwalters@232: std::cout << version_string; tomwalters@23: continue; tomwalters@23: } tomwalters@23: aimc::LOG_ERROR(_T("Unrecognized command-line argument: %s"), argv[i]); tomwalters@23: } tomwalters@23: tomwalters@232: std::cout << "Configuration file: " << config_file << std::endl; tomwalters@232: std::cout << "Script file: " << script_file << std::endl; tomwalters@232: std::cout << "Data file: " << data_file << std::endl; tomwalters@232: std::cout << "Graph file: " << dot_file << std::endl; tomwalters@232: tomwalters@232: aimc::AIMCopy processor; tomwalters@232: aimc::LOG_INFO("main: Initializing..."); tomwalters@232: if (!processor.Initialize(script_file, config_file)) { tomwalters@23: return -1; tomwalters@23: } tomwalters@232: tomwalters@232: aimc::LOG_INFO("main: Writing confg..."); tomwalters@232: if (!processor.WriteConfig(data_file, dot_file)) { tomwalters@23: return -1; tomwalters@23: } tomwalters@232: tomwalters@232: aimc::LOG_INFO("main: Processing..."); tomwalters@232: if (!processor.Process()) { tomwalters@232: return -1; tomwalters@23: } tomwalters@23: tomwalters@23: return 0; tomwalters@23: }