Subversion Repositories DIN Is Noise

Rev

Rev 315 | Rev 964 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
* chrono.cc
* DIN Is Noise is copyright (c) 2006-2017 Jagannathan Sampath
* For more information, please visit http://dinisnoise.org/
*/


#include "chrono.h"
#include "log.h"

using namespace boost::gregorian;
using namespace boost::posix_time;

audio_clock::audio_clock () {
  secs = delta_secs = 0;
  ticks = delta_ticks = 0;
  dlog << "+++ created audio clock +++" << endl;
}

audio_clock::~audio_clock () {
  dlog << "--- destroyed audio clock ---" << endl;
}

audio_clock& audio_clock::operator++ () {
  ticks += delta_ticks;
  secs += delta_secs;
  return *this;
}

ui_clock::ui_clock () {
  reset ();
  dlog << "+++ created UI clock +++" << endl;
}

ui_clock::~ui_clock () {
  dlog << "--- destroyed UI clock ---" << endl;
}

void ui_clock::reset () {
  start = microsec_clock::local_time ();
  elapsed = seconds (0);
}

double ui_clock::operator() () { // advances clock and returns seconds elapsed from last reset
  now = microsec_clock::local_time ();
  time_duration delta_elapsed (now - start);
  double delta_secs = delta_elapsed.total_microseconds () / 1000000.0;
  elapsed += delta_elapsed;
  secs_ += delta_secs;
  start = now;
  return secs_;
}

alarm_t::alarm_t (double tt) {
  triggert = tt;
  startt = 0;
  active = 0;
}

int alarm_t::operator() (double t) {
  float elapsed = t - startt;
  float dt = elapsed - triggert;
  if (dt >= 0)  {
    startt = t;
    return 1;
  } else return 0;
}

void alarm_t::start () {
  startt = ui_clk ();
  active = 1;
}

void alarm_t::stop () {
  active = 0;
}