annotate trunk/src/Main/AIMCopy.cc @ 349:89b49df39ce6

- AWS
author tomwalters
date Wed, 11 Aug 2010 11:27:07 +0000
parents 30dde71d0230
children 69466da9745e
rev   line source
tomwalters@296 1 // Copyright 2008-2010, Thomas Walters
tomwalters@296 2 //
tomwalters@296 3 // AIM-C: A C++ implementation of the Auditory Image Model
tomwalters@296 4 // http://www.acousticscale.org/AIMC
tomwalters@296 5 //
tomwalters@318 6 // Licensed under the Apache License, Version 2.0 (the "License");
tomwalters@318 7 // you may not use this file except in compliance with the License.
tomwalters@318 8 // You may obtain a copy of the License at
tomwalters@296 9 //
tomwalters@318 10 // http://www.apache.org/licenses/LICENSE-2.0
tomwalters@296 11 //
tomwalters@318 12 // Unless required by applicable law or agreed to in writing, software
tomwalters@318 13 // distributed under the License is distributed on an "AS IS" BASIS,
tomwalters@318 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tomwalters@318 15 // See the License for the specific language governing permissions and
tomwalters@318 16 // limitations under the License.
tomwalters@296 17
tomwalters@296 18 /*!
tomwalters@296 19 * \file AIMCopy.cpp
tomwalters@296 20 * \brief AIM-C replacement for HTK's HCopy
tomwalters@296 21 *
tomwalters@296 22 * The following subset of the command-line flags
tomwalters@296 23 * should be implemented from HCopy:
tomwalters@296 24 * -A Print command line arguments off
tomwalters@296 25 * -C cf Set config file to cf default
tomwalters@296 26 * (should be able to take multiple config files)
tomwalters@296 27 * -S f Set script file to f none
tomwalters@296 28 * //! \todo -T N Set trace flags to N 0
tomwalters@296 29 * -V Print version information off
tomwalters@296 30 * -D of Write configuration data to of none
tomwalters@296 31 *
tomwalters@296 32 * \author Thomas Walters <tom@acousticscale.org>
tomwalters@296 33 * \date created 2008/05/08
tomwalters@296 34 * \version \$Id$
tomwalters@296 35 */
tomwalters@296 36
tomwalters@296 37 #include <fstream>
tomwalters@296 38 #include <iostream>
tomwalters@296 39 #include <string>
tomwalters@296 40 #include <utility>
tomwalters@296 41 #include <vector>
tomwalters@296 42
tomwalters@296 43 #include <stdlib.h>
tomwalters@297 44 #include <time.h>
tomwalters@296 45
tomwalters@296 46 #include "Modules/Input/ModuleFileInput.h"
tomwalters@296 47 #include "Modules/BMM/ModuleGammatone.h"
tomwalters@296 48 #include "Modules/BMM/ModulePZFC.h"
tomwalters@296 49 #include "Modules/NAP/ModuleHCL.h"
tomwalters@296 50 #include "Modules/Strobes/ModuleParabola.h"
tomwalters@296 51 #include "Modules/SAI/ModuleSAI.h"
tomwalters@296 52 #include "Modules/SSI/ModuleSSI.h"
tomwalters@296 53 #include "Modules/Profile/ModuleSlice.h"
tomwalters@296 54 #include "Modules/Profile/ModuleScaler.h"
tomwalters@296 55 #include "Modules/Features/ModuleGaussians.h"
tomwalters@296 56 #include "Modules/Output/FileOutputHTK.h"
tomwalters@296 57 #include "Support/Common.h"
tomwalters@296 58 #include "Support/FileList.h"
tomwalters@296 59 #include "Support/Parameters.h"
tomwalters@296 60
tomwalters@296 61 using std::ofstream;
tomwalters@296 62 using std::pair;
tomwalters@296 63 using std::vector;
tomwalters@296 64 using std::string;
tomwalters@296 65 int main(int argc, char* argv[]) {
tomwalters@296 66 string sound_file;
tomwalters@296 67 string data_file;
tomwalters@296 68 string config_file;
tomwalters@296 69 string script_file;
tomwalters@296 70 bool write_data = false;
tomwalters@296 71 bool print_version = false;
tomwalters@296 72
tomwalters@296 73 string version_string(
tomwalters@296 74 " AIM-C AIMCopy\n"
tomwalters@296 75 " (c) 2006-2010, Thomas Walters and Willem van Engen\n"
tomwalters@296 76 " http://www.acoustiscale.org/AIMC/\n"
tomwalters@296 77 "\n");
tomwalters@296 78
tomwalters@296 79 if (argc < 2) {
tomwalters@296 80 printf("%s", version_string.c_str());
tomwalters@296 81 printf("AIMCopy is intended as a drop-in replacement for HTK's HCopy\n");
tomwalters@296 82 printf("command. It is used for making features from audio files for\n");
tomwalters@296 83 printf("use with HTK.\n");
tomwalters@296 84 printf("Usage: \n");
tomwalters@296 85 printf(" -A Print command line arguments off\n");
tomwalters@296 86 printf(" -C cf Set config file to cf none\n");
tomwalters@296 87 printf(" -S f Set script file to f none\n");
tomwalters@296 88 printf(" -V Print version information off\n");
tomwalters@296 89 printf(" -D g Write configuration data to g none\n");
tomwalters@296 90 return -1;
tomwalters@296 91 }
tomwalters@296 92
tomwalters@296 93 // Parse command-line arguments
tomwalters@296 94 for (int i = 1; i < argc; i++) {
tomwalters@296 95 if (strcmp(argv[i],"-A") == 0) {
tomwalters@296 96 for (int j = 0; j < argc; j++)
tomwalters@296 97 printf("%s ",argv[j]);
tomwalters@296 98 printf("\n");
tomwalters@296 99 fflush(stdout);
tomwalters@296 100 continue;
tomwalters@296 101 }
tomwalters@296 102 if (strcmp(argv[i],"-C") == 0) {
tomwalters@296 103 if (++i >= argc) {
tomwalters@296 104 aimc::LOG_ERROR(_T("Configuration file name expected after -C"));
tomwalters@296 105 return(-1);
tomwalters@296 106 }
tomwalters@296 107 config_file = argv[i];
tomwalters@296 108 continue;
tomwalters@296 109 }
tomwalters@296 110 if (strcmp(argv[i],"-S") == 0) {
tomwalters@296 111 if (++i >= argc) {
tomwalters@296 112 aimc::LOG_ERROR(_T("Script file name expected after -S"));
tomwalters@296 113 return(-1);
tomwalters@296 114 }
tomwalters@296 115 script_file = argv[i];
tomwalters@296 116 continue;
tomwalters@296 117 }
tomwalters@296 118 if (strcmp(argv[i],"-D") == 0) {
tomwalters@296 119 if (++i >= argc) {
tomwalters@296 120 aimc::LOG_ERROR(_T("Data file name expected after -D"));
tomwalters@296 121 return(-1);
tomwalters@296 122 }
tomwalters@296 123 data_file = argv[i];
tomwalters@296 124 write_data = true;
tomwalters@296 125 continue;
tomwalters@296 126 }
tomwalters@296 127 if (strcmp(argv[i],"-V") == 0) {
tomwalters@296 128 print_version = true;
tomwalters@296 129 continue;
tomwalters@296 130 }
tomwalters@296 131 aimc::LOG_ERROR(_T("Unrecognized command-line argument: %s"), argv[i]);
tomwalters@296 132 }
tomwalters@296 133
tomwalters@296 134 if (print_version)
tomwalters@296 135 printf("%s", version_string.c_str());
tomwalters@296 136
tomwalters@296 137 aimc::Parameters params;
tomwalters@296 138
tomwalters@296 139 if (!params.Load(config_file.c_str())) {
tomwalters@296 140 aimc::LOG_ERROR(_T("Couldn't load parameters from file %s"),
tomwalters@296 141 config_file.c_str());
tomwalters@296 142 return -1;
tomwalters@296 143 }
tomwalters@296 144
tomwalters@296 145 vector<pair<string, string> > file_list = aimc::FileList::Load(script_file);
tomwalters@296 146 if (file_list.size() == 0) {
tomwalters@296 147 aimc::LOG_ERROR("No data read from file %s", script_file.c_str());
tomwalters@296 148 return -1;
tomwalters@296 149 }
tomwalters@296 150
tomwalters@296 151 // Set up AIM-C processor here
tomwalters@296 152 aimc::ModuleFileInput input(&params);
tomwalters@296 153 aimc::ModuleGammatone bmm(&params);
tomwalters@296 154 aimc::ModuleHCL nap(&params);
tomwalters@296 155 aimc::ModuleSlice profile(&params);
tomwalters@296 156 aimc::ModuleScaler scaler(&params);
tomwalters@296 157 aimc::ModuleGaussians features(&params);
tomwalters@296 158 aimc::FileOutputHTK output(&params);
tomwalters@296 159
tomwalters@296 160 input.AddTarget(&bmm);
tomwalters@296 161 bmm.AddTarget(&nap);
tomwalters@296 162 nap.AddTarget(&profile);
tomwalters@296 163 profile.AddTarget(&scaler);
tomwalters@296 164 scaler.AddTarget(&features);
tomwalters@296 165 features.AddTarget(&output);
tomwalters@296 166
tomwalters@296 167 if (write_data) {
tomwalters@296 168 ofstream outfile(data_file.c_str());
tomwalters@296 169 if (outfile.fail()) {
tomwalters@296 170 aimc::LOG_ERROR("Couldn't open data file %s for writing",
tomwalters@296 171 data_file.c_str());
tomwalters@296 172 return -1;
tomwalters@296 173 }
tomwalters@297 174 time_t rawtime;
tomwalters@297 175 struct tm * timeinfo;
tomwalters@297 176 time(&rawtime);
tomwalters@297 177 timeinfo = localtime(&rawtime);
tomwalters@297 178
tomwalters@297 179
tomwalters@296 180 outfile << "# AIM-C AIMCopy\n";
tomwalters@297 181 outfile << "# Run on: " << asctime(timeinfo);
tomwalters@297 182 char * descr = getenv("USER");
tomwalters@297 183 if (descr) {
tomwalters@297 184 outfile << "# By user: " << descr <<"\n";
tomwalters@297 185 }
tomwalters@297 186 outfile << "# Module chain: file_input->gt->hcl->slice->scaler->";
tomwalters@297 187 outfile << "gaussians->out_htk\n";
tomwalters@296 188 outfile << "# Module versions:\n";
tomwalters@296 189 outfile << "# " << input.id() << " : " << input.version() << "\n";
tomwalters@296 190 outfile << "# " << bmm.id() << " : " << bmm.version() << "\n";
tomwalters@296 191 outfile << "# " << nap.id() << " : " << nap.version() << "\n";
tomwalters@296 192 outfile << "# " << profile.id() << " : " << profile.version() << "\n";
tomwalters@296 193 outfile << "# " << scaler.id() << " : " << scaler.version() << "\n";
tomwalters@296 194 outfile << "# " << features.id() << " : " << features.version() << "\n";
tomwalters@296 195 outfile << "# " << output.id() << " : " << output.version() << "\n";
tomwalters@296 196 outfile << "#\n";
tomwalters@296 197 outfile << "# Parameters:\n";
tomwalters@296 198 outfile << params.WriteString();
tomwalters@296 199 outfile.close();
tomwalters@296 200 }
tomwalters@296 201
tomwalters@296 202 for (unsigned int i = 0; i < file_list.size(); ++i) {
tomwalters@296 203 aimc::LOG_INFO(_T("In: %s"), file_list[i].first.c_str());
tomwalters@296 204 aimc::LOG_INFO(_T("Out: %s"), file_list[i].second.c_str());
tomwalters@296 205
tomwalters@296 206 output.OpenFile(file_list[i].second.c_str(), 10.0f);
tomwalters@296 207 if (input.LoadFile(file_list[i].first.c_str())) {
tomwalters@296 208 input.Process();
tomwalters@296 209 } else {
tomwalters@296 210 printf("LoadFile failed for file %s\n", file_list[i].first.c_str());
tomwalters@296 211 }
tomwalters@296 212 input.Reset();
tomwalters@296 213 }
tomwalters@296 214
tomwalters@296 215 return 0;
tomwalters@296 216 }