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 "ViconFileSource.h" 00020 00021 using namespace std; 00022 using namespace Marsyas; 00023 00024 ViconFileSource::ViconFileSource(mrs_string name):MarSystem("ViconFileSource",name) 00025 { 00026 //type_ = "ViconFileSource"; 00027 //name_ = name; 00028 00029 vfp_ = 0; 00030 00031 addControls(); 00032 } 00033 00034 00035 ViconFileSource::~ViconFileSource() 00036 { 00037 if (vfp_ != NULL) 00038 fclose(vfp_); 00039 } 00040 00041 void 00042 ViconFileSource::addControls() 00043 { 00044 addctrl("mrs_bool/hasData", true); 00045 addctrl("mrs_natural/size", 0); 00046 addctrl("mrs_string/markers", " "); 00047 addctrl("mrs_string/filename", "dviconfile"); 00048 setctrlState("mrs_string/filename", true); 00049 } 00050 00051 00052 00053 MarSystem* 00054 ViconFileSource::clone() const 00055 { 00056 return new ViconFileSource(*this); 00057 } 00058 00059 00060 00061 void 00062 ViconFileSource::getHeader(mrs_string filename) 00063 { 00064 // Need to read Vicon File Header to get number and name of markers 00065 vfp_ = fopen(filename.c_str(), "r"); 00066 if (vfp_) 00067 { 00068 // read first line from file 00069 char buffer[4096]; 00070 if ( fgets(buffer, 4096, vfp_) == NULL) { 00071 MRSERR("Problem reading Vicon file"); 00072 } 00073 stringstream line(buffer); 00074 char entry[256]; 00075 fileObs_ = 0; 00076 while (line.getline(entry, 256, ',')) 00077 { 00078 00079 fileObs_++; 00080 } 00081 setctrl("mrs_natural/onObservations", fileObs_); 00082 setctrl("mrs_string/markers", buffer); 00083 } 00084 } 00085 00086 00087 void 00088 ViconFileSource::myUpdate(MarControlPtr sender) 00089 { 00090 (void) sender; //suppress warning of unused parameter(s) 00091 inObservations_ = getctrl("mrs_natural/inObservations")->to<mrs_natural>(); 00092 israte_ = getctrl("mrs_real/israte")->to<mrs_real>(); 00093 00094 00095 if (filename_ != getctrl("mrs_string/filename")->to<mrs_string>()) 00096 { 00097 filename_ = getctrl("mrs_string/filename")->to<mrs_string>(); 00098 getHeader(filename_); 00099 00100 } 00101 00102 setctrl("mrs_natural/onSamples", inSamples_); 00103 setctrl("mrs_natural/onObservations", fileObs_); 00104 setctrl("mrs_real/osrate", israte_); 00105 00106 00107 } 00108 00109 void 00110 ViconFileSource::myProcess(realvec& in, realvec& out) 00111 { 00112 (void) in; 00113 //checkFlow(in,out); 00114 mrs_natural o,t; 00115 00116 for (t = 0; t < inSamples_; t++) 00117 { 00118 bool notValidLine = true; 00119 char buffer[4096]; 00120 while (notValidLine) 00121 { 00122 char *res; 00123 res = fgets(buffer, 4096, vfp_); 00124 if (res == NULL) 00125 { 00126 setctrl("mrs_bool/hasData",false); 00127 return; 00128 } 00129 00130 stringstream line(buffer); 00131 stringstream pline(buffer); 00132 char entry[256]; 00133 notValidLine = false; 00134 for (o=0; o < onObservations_; o++) 00135 { 00136 line.getline(entry, 256, ','); 00137 if (!strcmp(entry,"")) 00138 { 00139 for (mrs_natural j=0; j < o; j++) 00140 out(j,t) = 0.0; 00141 notValidLine = true; 00142 } 00143 else 00144 out(o,t) = (mrs_real)atof(entry); 00145 00146 if (notValidLine) break; 00147 } 00148 } 00149 } 00150 } 00151 00152 00153 00154 00155 00156