annotate trunk/src/Main/AIMCopy_SSI_Features_v5_smooth_nap.cc @ 706:f8e90b5d85fd tip

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