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@121: #include "Support/ModuleTree.h" tomwalters@23: #include "Support/Parameters.h" tomwalters@23: tomwalters@121: namespace aimc { tomwalters@23: using std::ofstream; tomwalters@23: using std::pair; tomwalters@23: using std::vector; tomwalters@23: using std::string; tomwalters@121: class AIMCopy { tomwalters@121: public: tomwalters@121: AIMCopy(); tomwalters@121: tomwalters@121: bool Initialize(string script_filename, tomwalters@121: string config_filename); tomwalters@121: tomwalters@121: bool WriteConfig(string config_dump_filename, tomwalters@121: string config_graph_filename); tomwalters@121: tomwalters@121: bool Process(); tomwalters@121: tomwalters@121: private: tomwalters@121: bool initialized_; tomwalters@121: Parameters global_parameters_; tomwalters@121: ModuleTree tree_; tomwalters@121: vector > script_; tomwalters@121: }; tomwalters@121: tomwalters@121: tomwalters@121: AIMCopy::AIMCopy() : initialized_(false) { tomwalters@121: tomwalters@121: } tomwalters@121: tomwalters@121: bool AIMCopy::Initialize(string script_filename, tomwalters@121: string config_filename) { tomwalters@121: tomwalters@121: LOG_INFO("AIMCopy: Loading script"); tomwalters@121: script_ = FileList::Load(script_filename); tomwalters@121: if (script_.size() == 0) { tomwalters@121: LOG_ERROR("No data read from script file %s", script_filename.c_str()); tomwalters@121: return false; tomwalters@121: } tomwalters@121: tomwalters@121: LOG_INFO("AIMCopy: Loading configuration"); tomwalters@121: if (!tree_.LoadConfigFile(config_filename)) { tomwalters@121: LOG_ERROR(_T("Failed to load configuration file")); tomwalters@121: return false; tomwalters@121: } tomwalters@121: LOG_INFO("AIMCopy: Successfully loaded configuration"); tomwalters@121: initialized_ = true; tomwalters@121: return true; tomwalters@121: } tomwalters@121: tomwalters@121: bool AIMCopy::WriteConfig(string config_dump_filename, tomwalters@121: string config_graph_filename) { tomwalters@121: if (!initialized_) { tomwalters@121: return false; tomwalters@121: } tomwalters@121: tomwalters@121: if (script_.size() > 0) { tomwalters@121: global_parameters_.SetString("input_filename", script_[0].first.c_str()); tomwalters@121: global_parameters_.SetString("output_filename_base", script_[0].second.c_str()); tomwalters@121: LOG_INFO("AIMCopy: Initializing tree for initial parameter write."); tomwalters@121: if (!tree_.Initialize(&global_parameters_)) { tomwalters@121: LOG_ERROR(_T("Failed to initialize tree.")); tomwalters@121: return false; tomwalters@121: } tomwalters@121: } else { tomwalters@121: LOG_ERROR(_T("No input files in script.")); tomwalters@121: return false; tomwalters@121: } tomwalters@121: tomwalters@121: if (!config_dump_filename.empty()) { tomwalters@121: LOG_INFO("AIMCopy: Dumping configuration."); tomwalters@121: ofstream output_stream; tomwalters@121: output_stream.open(config_dump_filename.c_str()); tomwalters@121: if (output_stream.fail()) { tomwalters@121: LOG_ERROR(_T("Failed to open configuration file %s for writing."), tomwalters@121: config_dump_filename.c_str()); tomwalters@121: return false; tomwalters@121: } tomwalters@121: tomwalters@121: time_t rawtime; tomwalters@121: struct tm * timeinfo; tomwalters@121: time(&rawtime); tomwalters@121: timeinfo = localtime(&rawtime); tomwalters@121: output_stream << "# AIM-C AIMCopy\n"; tomwalters@121: output_stream << "# Run at: " << asctime(timeinfo); tomwalters@121: char * descr = getenv("USER"); tomwalters@121: if (descr) { tomwalters@121: output_stream << "# By user: " << descr <<"\n"; tomwalters@121: } tomwalters@121: tree_.PrintConfiguration(output_stream); tomwalters@121: output_stream.close(); tomwalters@121: } tomwalters@121: tomwalters@121: if (!config_graph_filename.empty()) { tomwalters@121: ofstream output_stream; tomwalters@121: output_stream.open(config_graph_filename.c_str()); tomwalters@121: if (output_stream.fail()) { tomwalters@121: LOG_ERROR(_T("Failed to open graph file %s for writing."), tomwalters@121: config_graph_filename.c_str()); tomwalters@121: return false; tomwalters@121: } tomwalters@121: tree_.MakeDotGraph(output_stream); tomwalters@121: output_stream.close(); tomwalters@121: } tomwalters@121: return true; tomwalters@121: } tomwalters@121: tomwalters@121: bool AIMCopy::Process() { tomwalters@121: if (!initialized_) { tomwalters@121: return false; tomwalters@121: } tomwalters@121: for (unsigned int i = 0; i < script_.size(); ++i) { tomwalters@121: global_parameters_.SetString("input_filename", script_[i].first.c_str()); tomwalters@121: global_parameters_.SetString("output_filename_base", script_[i].second.c_str()); tomwalters@121: if (!tree_.Initialize(&global_parameters_)) { tomwalters@121: return false; tomwalters@121: } tomwalters@121: aimc::LOG_INFO(_T("%s -> %s"), tomwalters@121: script_[i].first.c_str(), tomwalters@121: script_[i].second.c_str()); tomwalters@121: tree_.Reset(); tomwalters@121: tree_.Process(); tomwalters@121: } tomwalters@121: return true; tomwalters@121: } tomwalters@121: tomwalters@121: } // namespace aimc tomwalters@121: tomwalters@23: int main(int argc, char* argv[]) { tomwalters@121: std::string data_file; tomwalters@121: std::string dot_file; tomwalters@121: std::string config_file; tomwalters@121: std::string script_file; tomwalters@23: tomwalters@121: 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@121: "\n"); tomwalters@121: tomwalters@121: const std::string usage_string( tomwalters@121: "AIMCopy is intended as a drop-in replacement for HTK's HCopy\n" tomwalters@121: "command. It is used for making features from audio files for\n" tomwalters@121: "use with HTK.\n" tomwalters@121: "Usage: \n" tomwalters@121: " \n" tomwalters@121: " -A Print command line arguments off\n" tomwalters@121: " -C cf Set config file to cf none\n" tomwalters@121: " -S f Set script file to f none\n" tomwalters@121: " -V Print version information off\n" tomwalters@121: " -D d Write complete parameter set to file d none\n" tomwalters@121: " -G g Write graph to file g none\n"); tomwalters@23: tomwalters@23: if (argc < 2) { tomwalters@121: std::cout << version_string.c_str(); tomwalters@121: 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@121: if (strcmp(argv[i],"-G") == 0) { tomwalters@121: if (++i >= argc) { tomwalters@121: aimc::LOG_ERROR(_T("Graph file name expected after -D")); tomwalters@121: return(-1); tomwalters@121: } tomwalters@121: dot_file = argv[i]; tomwalters@121: continue; tomwalters@121: } tomwalters@121: if (strcmp(argv[i],"-V") == 0) { tomwalters@121: 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@121: std::cout << "Configuration file: " << config_file << std::endl; tomwalters@121: std::cout << "Script file: " << script_file << std::endl; tomwalters@121: std::cout << "Data file: " << data_file << std::endl; tomwalters@121: std::cout << "Graph file: " << dot_file << std::endl; tomwalters@121: tomwalters@121: aimc::AIMCopy processor; tomwalters@121: aimc::LOG_INFO("main: Initializing..."); tomwalters@121: if (!processor.Initialize(script_file, config_file)) { tomwalters@23: return -1; tomwalters@23: } tomwalters@121: tomwalters@121: aimc::LOG_INFO("main: Writing confg..."); tomwalters@121: if (!processor.WriteConfig(data_file, dot_file)) { tomwalters@23: return -1; tomwalters@23: } tomwalters@121: tomwalters@121: aimc::LOG_INFO("main: Processing..."); tomwalters@121: if (!processor.Process()) { tomwalters@121: return -1; tomwalters@23: } tomwalters@23: tomwalters@23: return 0; tomwalters@23: }