00001 /* 00002 ** Copyright (C) 1998-2005 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 00020 #include "TmVirtualTime.h" 00021 #include "MarSystem.h" 00022 #include "Scheduler.h" 00023 00024 using namespace std; 00025 using namespace Marsyas; 00026 00027 TmVirtualTime::TmVirtualTime() : TmTimer("TmVirtualTime","Virtual") 00028 { 00029 setSource(NULL); 00030 } 00031 00032 TmVirtualTime::TmVirtualTime(std::string name) : TmTimer("TmVirtualTime",name) 00033 { 00034 setSource(NULL); 00035 } 00036 00037 TmVirtualTime::TmVirtualTime(std::string name, MarSystem* ms) : TmTimer("TmVirtualTime",name) 00038 { 00039 setSource(ms); 00040 } 00041 00042 TmVirtualTime::TmVirtualTime(const TmVirtualTime& s) : TmTimer(s) 00043 { 00044 setSource(s.read_src_); 00045 } 00046 00047 TmVirtualTime::~TmVirtualTime() {} 00048 00049 void 00050 TmVirtualTime::setSource(MarSystem* ms) 00051 { 00052 // start at 0 00053 previous_tick_interval_=0; 00054 read_src_=ms; 00055 error_term_=0.0; 00056 if(read_src_!=NULL) { 00057 nsamples_=read_src_->getctrl("mrs_natural/onSamples"); 00058 srate_=read_src_->getctrl("mrs_real/osrate"); 00059 } 00060 } 00061 00062 mrs_natural 00063 TmVirtualTime::readTimeSrc() 00064 { 00065 if (read_src_==NULL) { 00066 MRSWARN("TmVirtualTime::readTimeSrc() time source is not defined."); 00067 return 0; 00068 } 00069 // this is the width of the last tick - elapsed time since last tick 00070 mrs_natural ret = previous_tick_interval_; 00071 mrs_real srate = srate_->to<mrs_real>(); 00072 if (srate<1.0) 00073 return 0; 00074 mrs_real interval_width = nsamples_->to<mrs_natural>() / srate; 00075 mrs_real microseconds = (interval_width * 1000000.0) + error_term_; 00076 error_term_ = microseconds - ((long)microseconds); 00077 // mrs_natural count = (mrs_natural)microseconds; 00078 // cout << "us=" << ((mrs_natural)microseconds) << endl; 00079 previous_tick_interval_ = (mrs_natural)microseconds; 00080 return ret; 00081 } 00082 00083 mrs_natural 00084 TmVirtualTime::intervalsize(std::string interval) 00085 { 00086 return time2usecs(interval); 00087 // return (read_src_==NULL) ? 0 : 00088 // time2samples(interval,read_src_->getctrl("mrs_real/osrate")->to<mrs_real>()); 00089 } 00090 00091 void 00092 TmVirtualTime::updtimer(std::string cname, TmControlValue value) 00093 { 00094 bool type_error=false; 00095 if (cname=="MarSystem/source") { 00096 if (value.getType()==tmcv_marsystem) { 00097 setSource(value.toMarSystem()); 00098 } 00099 else type_error=true; 00100 } 00101 else 00102 MRSWARN("TmVirtualTime::updtimer(string,TmControlValue) unsupported control"); 00103 if (type_error) 00104 MRSWARN("TmVirtualTime::updtimer(string,TmControlValue) wrong type to "+cname); 00105 } 00106
1.5.6