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