pl-nk v0.4.5
Plonk|Plink|Plank are a set of cross-platform C/C++ frameworks for audio software development
plonk_TimeStamp.h
00001 /*
00002  -------------------------------------------------------------------------------
00003  This file is part of the Plink, Plonk, Plank libraries
00004   by Martin Robinson
00005  
00006  http://code.google.com/p/pl-nk/
00007  
00008  Copyright University of the West of England, Bristol 2011-14
00009  All rights reserved.
00010  
00011  Redistribution and use in source and binary forms, with or without
00012  modification, are permitted provided that the following conditions are met:
00013  
00014  * Redistributions of source code must retain the above copyright
00015    notice, this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright
00017    notice, this list of conditions and the following disclaimer in the
00018    documentation and/or other materials provided with the distribution.
00019  * Neither the name of University of the West of England, Bristol nor 
00020    the names of its contributors may be used to endorse or promote products
00021    derived from this software without specific prior written permission.
00022  
00023  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00024  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00025  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00026  DISCLAIMED. IN NO EVENT SHALL UNIVERSITY OF THE WEST OF ENGLAND, BRISTOL BE 
00027  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00028  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
00029  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
00030  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00031  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
00032  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00033  
00034  This software makes use of third party libraries. For more information see:
00035  doc/license.txt included in the distribution.
00036  -------------------------------------------------------------------------------
00037  */
00038 
00039 #ifndef PLONK_TIMESTAMP_H
00040 #define PLONK_TIMESTAMP_H
00041 
00047 class TimeStamp
00048 {
00049 public:
00050     TimeStamp (int initialTime) throw();
00051     TimeStamp (LongLong initialTime) throw();
00052     TimeStamp (LongLong initialTime = 0, double initialFraction = 0.0) throw();
00053     
00054     TimeStamp (TimeStamp const& other) throw();
00055     TimeStamp& operator= (TimeStamp const& other) throw();
00056     
00057     bool isFinite() const throw();
00058     bool isInfinite() const throw();
00059     
00060     TimeStamp& operator+= (const double offset) throw();
00061     TimeStamp& operator+= (TimeStamp const& other) throw();
00062     TimeStamp operator+ (const double offset) const throw();
00063     TimeStamp operator+ (TimeStamp const& other) const throw();
00064     
00065     TimeStamp& operator-= (const double offset) throw();
00066     TimeStamp& operator-= (TimeStamp const& other) throw();
00067     TimeStamp operator- (const double offset) const throw();
00068     TimeStamp operator- (TimeStamp const& other) const throw();
00069     
00070     
00071     void set (LongLong newTime, double newFraction = 0.0) throw();
00072     inline LongLong getTime() const throw() { return time; }
00073     inline double getFraction() const throw() { return fraction; }
00074     inline int getFractionInTicks() const throw() { return int (fraction * getTicks()); }
00075     
00076     inline double getValue() const throw() 
00077     { 
00078         plonk_assert (((double (time) + 1.0) - (double (time))) == 1.0); // lost precision
00079         return double (time) + fraction; 
00080     }
00081     
00082     inline operator double () const throw() { return this->getValue(); }
00083     inline double toSeconds() const throw() { return this->getValue() * getReciprocalTicks(); }
00084     double toSamples (const double sampleRate) const throw();
00085     
00086     bool operator== (TimeStamp const& other) const throw();
00087     bool operator!= (TimeStamp const& other) const throw();
00088     
00089     bool operator<  (TimeStamp const& other) const throw();
00090     bool operator<= (TimeStamp const& other) const throw();
00091     bool operator>  (TimeStamp const& other) const throw();
00092     bool operator>= (TimeStamp const& other) const throw();
00093     
00094     static double getTicks() throw();
00095     static double getReciprocalTicks() throw();
00096     
00097     static TimeStamp fromSeconds (const double seconds) throw();
00098     static TimeStamp fromSamples (const double samples, const double sampleRate) throw();
00099     static const TimeStamp& getSentinel() throw();
00100     static const TimeStamp& getZero() throw();
00101     static const TimeStamp& getMaximum() throw();
00102 
00103 private:
00104     LongLong time;
00105     double fraction;
00106     
00107     static bool fractionIsValid (const double f) throw();
00108 };
00109 
00110 //------------------------------------------------------------------------------
00111 
00112 inline bool TimeStamp::isFinite() const throw()
00113 {
00114     return ! isInfinite();
00115 }
00116 
00117 inline bool TimeStamp::isInfinite() const throw()
00118 {
00119     return pl_IsInfD (fraction) != 0;
00120 }
00121 
00122 inline bool TimeStamp::operator>= (TimeStamp const& other) const throw()
00123 {
00124     plonk_assert (fractionIsValid (this->fraction));
00125     plonk_assert (fractionIsValid (other.fraction));
00126     
00127     if (this->isInfinite())
00128     {
00129         if (other.isInfinite())
00130             return false;
00131         else
00132             return true;
00133     }
00134     else return ((time > other.time) || ((time == other.time) && (fraction >= other.fraction)));
00135 }
00136 
00137 inline double TimeStamp::getTicks() throw()
00138 {
00139     return 1000000.0; // microseconds
00140 }
00141 
00142 inline double TimeStamp::getReciprocalTicks() throw()
00143 {
00144     return 1.0 / 1000000.0; // 1/microseconds
00145 }
00146 
00147 inline const TimeStamp& TimeStamp::getZero() throw()
00148 {
00149     static const TimeStamp* timeZero = new TimeStamp (0, 0.0);
00150     return *timeZero;
00151 }
00152 
00153 inline const TimeStamp& TimeStamp::getSentinel() throw()
00154 {
00155     static const TimeStamp* sentinel = new TimeStamp (-1, 0.0);
00156     return *sentinel;
00157 }
00158 
00159 inline TimeStamp& TimeStamp::operator= (TimeStamp const& other) throw()
00160 {
00161     if (&other != this)
00162     {
00163         this->time = other.time;
00164         this->fraction = other.fraction;
00165     }
00166     
00167     return *this;
00168 }
00169 
00170 inline TimeStamp& TimeStamp::operator+= (const double offset) throw()
00171 {
00172     if (pl_IsInfD (offset))
00173     {
00174         this->time = 0;
00175         this->fraction = offset;
00176     }
00177     else
00178     {
00179         const double correctedOffset = offset + this->fraction;
00180         const LongLong timeOffset = LongLong (correctedOffset);
00181         this->time += timeOffset;
00182         this->fraction = correctedOffset - timeOffset; // carry the fraction
00183         
00184         if (this->fraction < 0.0)
00185         {
00186             this->time--;
00187             this->fraction += 1.0;
00188         }
00189         
00190         plonk_assert (fractionIsValid (this->fraction));
00191     }
00192     
00193     return *this;
00194 }
00195 
00196 
00197 #endif // PLONK_TIMESTAMP_H
 All Classes Functions Typedefs Enumerations Enumerator Properties