view include/VirtualClock.h @ 150:ebbfb154351a ClockSync

Now leveraging BBB's lock between xenomai clock and audio clock for ultra-accurate, low-latency clocking. CAVEAT: fractions of samples drifts will occurr every time the clock is changed
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 22 Sep 2015 04:09:13 +0100
parents 134bff10e561
children
line wrap: on
line source
#ifndef VIRTUAL_CLOCK_H_INCLUDED
#define VIRTUAL_CLOCK_H_INCLUDED

#include "math.h"
#include "stats.hpp"
#include "Clock.h"
#include "IirFilter.h"
#include "Kalman.h"
#ifdef USE_JUCE
#else
#include <BeagleRT.h>
#include <I2c_Codec.h>
extern I2c_Codec* gAudioCodec;
#endif /* USE_JUCE */

class VirtualClock{
private:
	myClock_t startTime;
	myClock_t startTimeOffset;
	myClock_t lastSyncTime;
//	myClock_t lastSyncEstimatedTime;
	double lastSyncEstimatedTime;
	bool firstRun;
	double elapsedPeriods;
	double elapsedPeriodsOffset;
	double blockPeriod;
	double period;
	MovingAverage<double> movingAverage;
	IirFilter iir;
	KalmanOne kalman;
public:
	void init(float initialValueUs);
	VirtualClock();
/**
	Call this method at regular intervals to sync che virtual clock
*/
	void sync();
/**
	Call this method asynchronously, passing a number of equally spaced events that have elapsed since the last call.
*/
	void sync(double numPeriods);
/**
	Get the current time according to the VirtualClock.
	
	@return Time elapsed since the first call to sync(), in period units. 
*/
	double getNow();
/** 
	Get the length of the period.
	
	Get the length of the period (difference between calls to sync() after various filtering operations)
*/
	double getPeriod();
/**
 * Add an offset to the number of elapsed periods.
 *
 * Add an offset to the number of elapsed periods. It also compensates for the corresponding time offset.
 */
	void addOffset(double periodOffset);
};

#endif /* VIRTUAL_CLOCK_H_INCLUDED */