00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "Confidence.h"
00020 #include "FileName.h"
00021
00022
00023 using std::ostringstream;
00024 using std::cout;
00025 using std::endl;
00026 using std::setprecision;
00027 using std::fixed;
00028 using std::ios;
00029
00030
00031
00032 using namespace Marsyas;
00033
00034 Confidence::Confidence(mrs_string name):MarSystem("Confidence",name)
00035 {
00036 print_ = false;
00037 forcePrint_ = false;
00038
00039 predictions_ = 0;
00040 count_ = 0;
00041 write_=0;
00042 oriName_ = "MARSYAS_EMPTY";
00043 addControls();
00044 }
00045
00046 Confidence::Confidence(const Confidence& a):MarSystem(a)
00047 {
00048 ctrl_memSize_ = getctrl("mrs_natural/memSize");
00049 ctrl_nLabels_ = getctrl("mrs_natural/nLabels");
00050 count_ = 0;
00051 print_ = false;
00052 forcePrint_ = false;
00053 write_=0;
00054 oriName_ = "MARSYAS_EMPTY";
00055 }
00056
00057 Confidence::~Confidence()
00058 {
00059 }
00060
00061
00062 MarSystem*
00063 Confidence::clone() const
00064 {
00065 return new Confidence(*this);
00066 }
00067
00068 void
00069 Confidence::addControls()
00070 {
00071 addctrl("mrs_natural/memSize", 40, ctrl_memSize_);
00072 addctrl("mrs_natural/nLabels", 2, ctrl_nLabels_);
00073 setctrlState("mrs_natural/nLabels", true);
00074 addctrl("mrs_string/labelNames", "Music,Speech");
00075 setctrlState("mrs_string/labelNames", true);
00076 addctrl("mrs_bool/print", false);
00077 setctrlState("mrs_bool/print", true);
00078 addctrl("mrs_bool/forcePrint", false);
00079 setctrlState("mrs_bool/forcePrint", true);
00080 addctrl("mrs_string/fileName", "MARSYAS_EMPTY");
00081 setctrlState("mrs_string/fileName", true);
00082 addctrl("mrs_natural/write", 0);
00083 setctrlState("mrs_natural/write", true);
00084 addctrl("mrs_natural/hopSize", 512);
00085 setctrlState("mrs_natural/hopSize", true);
00086 addctrl("mrs_bool/fileOutput", false);
00087 setctrlState("mrs_bool/fileOutput", true);
00088 }
00089
00090 void
00091 Confidence::myUpdate(MarControlPtr sender)
00092 {
00093 (void) sender;
00094 MRSDIAG("Confidence.cpp - Confidence:myUpdate");
00095 setctrl("mrs_natural/onSamples", getctrl("mrs_natural/inSamples"));
00096 setctrl("mrs_natural/onObservations", getctrl("mrs_natural/inObservations"));
00097 setctrl("mrs_real/osrate", getctrl("mrs_real/israte"));
00098
00099 confidences_.stretch(getctrl("mrs_natural/nLabels")->to<mrs_natural>());
00100 mrs_string labelNames = getctrl("mrs_string/labelNames")->to<mrs_string>();
00101
00102 labelNames_.clear();
00103
00104 print_ = getctrl("mrs_bool/print")->to<mrs_bool>();
00105 forcePrint_ = getctrl("mrs_bool/forcePrint")->to<mrs_bool>();
00106
00107 for (mrs_natural i = 0; i < getctrl("mrs_natural/nLabels")->to<mrs_natural>(); ++i)
00108 {
00109 mrs_string labelName;
00110 mrs_string temp;
00111
00112 labelName = labelNames.substr(0, labelNames.find(","));
00113 temp = labelNames.substr(labelNames.find(",")+1, labelNames.length());
00114 labelNames = temp;
00115 labelNames_.push_back(labelName);
00116 }
00117
00118
00119 if (getctrl("mrs_bool/fileOutput")->to<mrs_bool>())
00120 {
00121 if(getctrl("mrs_string/fileName")->to<mrs_string>().compare(oriName_))
00122 {
00123 if(write_)
00124 {
00125 outputFileSyn_.close();
00126 outputFileTran_.close();
00127 }
00128 oriName_ = getctrl("mrs_string/fileName")->to<mrs_string>();
00129 FileName Sfname(oriName_);
00130 mrs_string tmp = Sfname.nameNoExt() +"_synSeg.txt";
00131
00132 outputFileSyn_.open(tmp.c_str(), ios::out);
00133 tmp = Sfname.nameNoExt() +"_tranSeg.txt";
00134 outputFileTran_.open(tmp.c_str(), ios::out);
00135 write_ = 1;
00136 }
00137 }
00138 hopDuration_ = getctrl("mrs_natural/inSamples")->to<mrs_natural>() / getctrl("mrs_real/osrate")->to<mrs_real>();
00139 nbFrames_ = -getctrl("mrs_natural/memSize")->to<mrs_natural>()+1;
00140 lastLabel_ = "MARSYAS_EMPTY";
00141 }
00142
00143 void
00144 Confidence::myProcess(realvec& in, realvec& out)
00145 {
00146 mrs_natural o,t;
00147
00148 bool mute = ctrl_mute_->to<mrs_bool>();
00149 mrs_natural memSize = ctrl_memSize_->to<mrs_natural>();
00150 mrs_natural nLabels = ctrl_nLabels_->to<mrs_natural>();
00151
00152 mrs_natural label;
00153 mrs_natural l;
00154
00155
00156
00157
00158
00159 if (mute == false)
00160 {
00161 for (o=0; o < inObservations_; o++)
00162 for (t = 0; t < inSamples_; t++)
00163 {
00164 out(o,t) = in(o,t);
00165 if (o==0)
00166 {
00167 label = (mrs_natural)in(o,t);
00168
00169 confidences_(label) = confidences_(label) + 1;
00170 }
00171 }
00172 count_++;
00173 bool cond = ((count_ % memSize) == 0);
00174 if (cond || forcePrint_)
00175 {
00176 mrs_real max_conf = 0;
00177 mrs_natural max_l = 0;
00178 for (l=0; l < nLabels; l++)
00179 {
00180 mrs_real conf = ((confidences_(l)) / count_);
00181 if (conf > max_conf)
00182 {
00183 max_conf = conf;
00184 max_l = l;
00185 }
00186 }
00187 if (getctrl("mrs_bool/fileOutput")->to<mrs_bool>())
00188 {
00189 cout << "fileOutput" << endl;
00190
00191 if (write_)
00192 {
00193 outputFileSyn_ << fixed << setprecision(3) << nbFrames_*hopDuration_ << "\t";
00194 outputFileSyn_ << setprecision(0) << labelNames_[max_l] << "\t" <<
00195 ((confidences_(max_l) / count_)) * 100.0 << endl;
00196
00197 if(lastLabel_ == "MARSYAS_EMPTY" || lastLabel_ != labelNames_[max_l])
00198 {
00199 outputFileTran_ << fixed << setprecision(3) << nbFrames_*hopDuration_ << "\t" << labelNames_[max_l] << endl;
00200 lastLabel_ = labelNames_[max_l];
00201 }
00202 }
00203 }
00204 else
00205 {
00206 if (print_)
00207 {
00208 cout << fixed << setprecision(3) << nbFrames_*hopDuration_ << "\t";
00209 cout << fixed << setprecision(0) << labelNames_[max_l] << "\t" <<
00210 ((confidences_(max_l) / count_)) * 100.0 << setprecision(4) << endl;
00211 }
00212
00213 }
00214 if (cond || forcePrint_)
00215 {
00216 count_ = 0;
00217 }
00218
00219 confidences_.setval(0.0);
00220 }
00221 }
00222 nbFrames_++;
00223 }