VampPluginSDK  2.1
vamp-sdk/RealTime.h
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Vamp
00005 
00006     An API for audio analysis and feature extraction plugins.
00007 
00008     Centre for Digital Music, Queen Mary, University of London.
00009     Copyright 2006 Chris Cannam.
00010   
00011     Permission is hereby granted, free of charge, to any person
00012     obtaining a copy of this software and associated documentation
00013     files (the "Software"), to deal in the Software without
00014     restriction, including without limitation the rights to use, copy,
00015     modify, merge, publish, distribute, sublicense, and/or sell copies
00016     of the Software, and to permit persons to whom the Software is
00017     furnished to do so, subject to the following conditions:
00018 
00019     The above copyright notice and this permission notice shall be
00020     included in all copies or substantial portions of the Software.
00021 
00022     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00023     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00024     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00025     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
00026     ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00027     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00028     WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00029 
00030     Except as contained in this notice, the names of the Centre for
00031     Digital Music; Queen Mary, University of London; and Chris Cannam
00032     shall not be used in advertising or otherwise to promote the sale,
00033     use or other dealings in this Software without prior written
00034     authorization.
00035 */
00036 
00037 /*
00038    This is a modified version of a source file from the 
00039    Rosegarden MIDI and audio sequencer and notation editor.
00040    This file copyright 2000-2006 Chris Cannam.
00041    Relicensed by the author as detailed above.
00042 */
00043 
00044 #ifndef _VAMP_REAL_TIME_H_
00045 #define _VAMP_REAL_TIME_H_
00046 
00047 #include <iostream>
00048 #include <string>
00049 
00050 #ifndef _WIN32
00051 struct timeval;
00052 #endif
00053 
00054 #include "plugguard.h"
00055 _VAMP_SDK_PLUGSPACE_BEGIN(RealTime.h)
00056 
00057 namespace Vamp {
00058 
00066 struct RealTime
00067 {
00068     int sec;
00069     int nsec;
00070 
00071     int usec() const { return nsec / 1000; }
00072     int msec() const { return nsec / 1000000; }
00073 
00074     RealTime(): sec(0), nsec(0) {}
00075     RealTime(int s, int n);
00076 
00077     RealTime(const RealTime &r) :
00078         sec(r.sec), nsec(r.nsec) { }
00079 
00080     static RealTime fromSeconds(double sec);
00081     static RealTime fromMilliseconds(int msec);
00082 
00083 #ifndef _WIN32
00084     static RealTime fromTimeval(const struct timeval &);
00085 #endif
00086 
00087     RealTime &operator=(const RealTime &r) {
00088         sec = r.sec; nsec = r.nsec; return *this;
00089     }
00090 
00091     RealTime operator+(const RealTime &r) const {
00092         return RealTime(sec + r.sec, nsec + r.nsec);
00093     }
00094     RealTime operator-(const RealTime &r) const {
00095         return RealTime(sec - r.sec, nsec - r.nsec);
00096     }
00097     RealTime operator-() const {
00098         return RealTime(-sec, -nsec);
00099     }
00100 
00101     bool operator <(const RealTime &r) const {
00102         if (sec == r.sec) return nsec < r.nsec;
00103         else return sec < r.sec;
00104     }
00105 
00106     bool operator >(const RealTime &r) const {
00107         if (sec == r.sec) return nsec > r.nsec;
00108         else return sec > r.sec;
00109     }
00110 
00111     bool operator==(const RealTime &r) const {
00112         return (sec == r.sec && nsec == r.nsec);
00113     }
00114  
00115     bool operator!=(const RealTime &r) const {
00116         return !(r == *this);
00117     }
00118  
00119     bool operator>=(const RealTime &r) const {
00120         if (sec == r.sec) return nsec >= r.nsec;
00121         else return sec >= r.sec;
00122     }
00123 
00124     bool operator<=(const RealTime &r) const {
00125         if (sec == r.sec) return nsec <= r.nsec;
00126         else return sec <= r.sec;
00127     }
00128 
00129     RealTime operator/(int d) const;
00130 
00134     double operator/(const RealTime &r) const;
00135 
00140     std::string toString() const;
00141 
00146     std::string toText(bool fixedDp = false) const;
00147 
00151     static long realTime2Frame(const RealTime &r, unsigned int sampleRate);
00152 
00156     static RealTime frame2RealTime(long frame, unsigned int sampleRate);
00157 
00158     static const RealTime zeroTime;
00159 };
00160 
00161 std::ostream &operator<<(std::ostream &out, const RealTime &rt);
00162 
00163 }
00164 
00165 _VAMP_SDK_PLUGSPACE_END(RealTime.h)
00166     
00167 #endif