Marsyas
0.6.0-alpha
|
00001 /* 00002 ** Copyright (C) 1998-2010 George Tzanetakis <gtzan@cs.uvic.ca> 00003 ** 00004 ** This program is free software; you can redistribute it and/or modify 00005 ** it under the terms of the GNU General Public License as published by 00006 ** the Free Software Foundation; either version 2 of the License, or 00007 ** (at your option) any later version. 00008 ** 00009 ** This program is distributed in the hope that it will be useful, 00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 ** GNU General Public License for more details. 00013 ** 00014 ** You should have received a copy of the GNU General Public License 00015 ** along with this program; if not, write to the Free Software 00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 */ 00018 00019 #include <marsyas/system/MarControlManager.h> 00020 #include "MarSystemTemplateAdvanced.h" 00021 #include "../common_source.h" 00022 00023 using std::ostringstream; 00024 using std::ofstream; 00025 using std::endl; 00026 using std::cout; 00027 using std::ios; 00028 00029 using namespace Marsyas; 00030 00031 MarSystemTemplateAdvanced::MarSystemTemplateAdvanced(mrs_string name):MarSystem("MarSystemTemplateAdvanced", name) 00032 { 00033 // Add any specific controls needed by this MarSystem 00034 // (default controls all MarSystems should have 00035 // were already added by MarSystem::addControl(), 00036 // called by :MarSystem(name) constructor). 00037 // If no specific controls are needed by a MarSystem 00038 // there is no need to implement and call this addControl() 00039 // method (see for e.g. Rms.cpp) 00040 addControls(); 00041 } 00042 00043 MarSystemTemplateAdvanced::MarSystemTemplateAdvanced(const MarSystemTemplateAdvanced& a) : MarSystem(a) 00044 { 00045 // IMPORTANT! 00046 // All member "pointers" to controls have to be 00047 // explicitly reassigned here, at the copy ctor. 00048 // Otherwise these member "pointers" would be invalid! 00049 ctrl_header_ = getctrl("mrs_myheader/hdrname"); 00050 } 00051 00052 MarSystemTemplateAdvanced::~MarSystemTemplateAdvanced() 00053 { 00054 } 00055 00056 MarSystem* 00057 MarSystemTemplateAdvanced::clone() const 00058 { 00059 return new MarSystemTemplateAdvanced(*this); 00060 } 00061 00062 void 00063 MarSystemTemplateAdvanced::addControls() 00064 { 00065 someString_ = ""; 00066 00067 // register new custom control in MarControlManager 00068 MarControlManager *mcm = MarControlManager::getManager(); 00069 if (!mcm->isRegistered("mrs_myheader")) 00070 { 00071 mcm->registerPrototype("mrs_myheader", new MyHeaderT()); 00072 // if we do not define a typedef for our custom control 00073 // (see .h), we must then use a equivalent, but somewhat 00074 // more verbose call: 00075 // 00076 // mcm->registerPrototype("mrs_myheader", new MarControlValueT<MyHeader>()); 00077 } 00078 00079 // Ask MarControlManager to create a new instance 00080 // of our "custom" myHeader control 00081 ctrl_header_ = mcm->create("mrs_myheader"); 00082 00083 // create a temporary header and fill it 00084 // with the desired parameter values 00085 MyHeader myh; 00086 myh.someString = "abcd"; 00087 myh.someValue = 50; 00088 myh.someFlag = true; 00089 myh.someVec.create(10); 00090 00091 // Alternatively, we could ask our newly created control 00092 // for a copy of it that we can then use for setting its parameters 00093 // (no advantage to get a reference, because it would const...) 00094 // MyHeader myh = ctrl_header_->to<MyHeader>(); 00095 // myh.someString = "abcd"; 00096 // myh.someValue = 50; 00097 // myh.someFlag = true; 00098 // myh.someVec.create(10); 00099 00100 // use the temporary header object to set 00101 // our custom control values 00102 ctrl_header_->setValue(myh); 00103 00104 // finally add it as control to this MarSystem, and remember 00105 // to register its member variable (third parameter) 00106 addctrl("mrs_myheader/hdrname", ctrl_header_, ctrl_header_); 00107 00108 // it's also possible to set a custom control with state! 00109 ctrl_header_->setState(true); 00110 } 00111 00112 void 00113 MarSystemTemplateAdvanced::myUpdate(MarControlPtr sender) 00114 { 00115 MRSDIAG("MarSystemTemplateAdvanced.cpp - MarSystemTemplateAdvanced:myUpdate"); 00116 00117 MarSystem::myUpdate(sender); 00118 00119 //get a reference to our custom control 00120 const MyHeader& hdr = ctrl_header_->to<MyHeader>(); 00121 00122 //e.g. write header to some place (e.g. a file) 00123 if(someString_ != hdr.someString) 00124 { 00125 ofstream out(hdr.someString.c_str()); 00126 out << hdr; 00127 someString_ = hdr.someString; 00128 } 00129 } 00130 00131 void 00132 MarSystemTemplateAdvanced::myProcess(realvec& in, realvec& out) 00133 { 00134 mrs_natural t,o; 00135 //get a reference to our custom control 00136 const MyHeader& hdr = ctrl_header_->to<MyHeader>(); 00137 00138 //reopen (for append) our example output file, 00139 //to which we already have written the header in localUpdate() 00140 ofstream outfile(hdr.someString.c_str(), ios::app); 00141 00142 // This is a dummy example where all input data is 00143 // just passed to its output unchanged, and simultaneously 00144 // written to the output file... 00145 for (o=0; o < inObservations_; o++) 00146 for (t = 0; t < inSamples_; t++) 00147 { 00148 out(o,t) = in(o,t); 00149 outfile << in(o,t) << endl; 00150 } 00151 } 00152 /************************************************************************/ 00153 /* Custom Control Operators */ 00154 /************************************************************************/ 00155 // some operators are mandatory for all controls! 00156 // so we must declare and define them for our custom controls 00157 00158 // Use a namespace block instead of "Marsyas::", or Doxygen will complain. 00159 namespace Marsyas { 00160 00161 bool 00162 operator==(const MyHeader& hdr1, const MyHeader& hdr2) 00163 { 00164 // here we consider that two headers are equal if all their 00165 // parameter values are equal 00166 return (hdr1.someString == hdr2.someString && 00167 hdr1.someValue == hdr2.someValue && 00168 hdr1.someFlag == hdr2.someFlag && 00169 hdr1.someVec == hdr2.someVec) ; 00170 } 00171 00172 bool 00173 operator!=(const MyHeader& hdr1, const MyHeader& hdr2) 00174 { 00175 // here we consider that two headers are equal if all their 00176 // parameter values are equal 00177 return (hdr1.someString != hdr2.someString || 00178 hdr1.someValue != hdr2.someValue || 00179 hdr1.someFlag != hdr2.someFlag || 00180 hdr1.someVec != hdr2.someVec) ; 00181 } 00182 00183 MyHeader 00184 operator+(MyHeader& hdr1, MyHeader& hdr2) 00185 { 00186 (void) hdr1; (void) hdr2; 00187 MRSASSERT(0); // not a valid operation for this example header control 00188 return MyHeader(); 00189 } 00190 00191 MyHeader 00192 operator-(MyHeader& hdr1, MyHeader& hdr2) 00193 { 00194 (void) hdr1; (void) hdr2; 00195 MRSASSERT(0); // not a valid operation for this example header control 00196 return MyHeader(); 00197 } 00198 00199 MyHeader 00200 operator*(MyHeader& hdr1, MyHeader& hdr2) 00201 { 00202 (void) hdr1; (void) hdr2; 00203 MRSASSERT(0); // not a valid operation for this example header control 00204 return MyHeader(); 00205 } 00206 00207 MyHeader 00208 operator/(MyHeader& hdr1, MyHeader& hdr2) 00209 { 00210 (void) hdr1; (void) hdr2; 00211 MRSASSERT(0); // not a valid operation for this example header control 00212 return MyHeader(); 00213 } 00214 00215 std::ostream& 00216 operator<<(std::ostream& os, const MyHeader& hdr) 00217 { 00218 os << "# MARSYAS mrs_myHeader" << endl; 00219 os << "# someString = " << hdr.someString << endl; 00220 os << "# someValue = " << hdr.someValue << endl; 00221 os << "# someFlag = " << hdr.someFlag << endl; 00222 os << "# someVec = " << hdr.someVec << endl; 00223 00224 return os; 00225 } 00226 00227 std::istream& 00228 operator>>(std::istream& is, MyHeader& hdr) 00229 { 00230 mrs_string skip; 00231 is >> skip >> skip >> skip; 00232 00233 if(skip != "mrs_myHeader") 00234 { 00235 MRSWARN("MyHeader::operator>> error reading stream"); 00236 return is; 00237 } 00238 00239 is >> skip >> skip >> skip; 00240 is >> hdr.someString; 00241 00242 is >> skip >> skip >> skip; 00243 is >> hdr.someValue; 00244 00245 is >> skip >> skip >> skip; 00246 is >> hdr.someFlag; 00247 00248 is >> skip >> skip >> skip; 00249 is >> hdr.someVec; 00250 00251 return is; 00252 } 00253 00254 } // namespace Marsyas