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