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 namespace Vamp {
00055 
00063 struct RealTime
00064 {
00065     int sec;
00066     int nsec;
00067 
00068     int usec() const { return nsec / 1000; }
00069     int msec() const { return nsec / 1000000; }
00070 
00071     RealTime(): sec(0), nsec(0) {}
00072     RealTime(int s, int n);
00073 
00074     RealTime(const RealTime &r) :
00075         sec(r.sec), nsec(r.nsec) { }
00076 
00077     static RealTime fromSeconds(double sec);
00078     static RealTime fromMilliseconds(int msec);
00079 
00080 #ifndef _WIN32
00081     static RealTime fromTimeval(const struct timeval &);
00082 #endif
00083 
00084     RealTime &operator=(const RealTime &r) {
00085         sec = r.sec; nsec = r.nsec; return *this;
00086     }
00087 
00088     RealTime operator+(const RealTime &r) const {
00089         return RealTime(sec + r.sec, nsec + r.nsec);
00090     }
00091     RealTime operator-(const RealTime &r) const {
00092         return RealTime(sec - r.sec, nsec - r.nsec);
00093     }
00094     RealTime operator-() const {
00095         return RealTime(-sec, -nsec);
00096     }
00097 
00098     bool operator <(const RealTime &r) const {
00099         if (sec == r.sec) return nsec < r.nsec;
00100         else return sec < r.sec;
00101     }
00102 
00103     bool operator >(const RealTime &r) const {
00104         if (sec == r.sec) return nsec > r.nsec;
00105         else return sec > r.sec;
00106     }
00107 
00108     bool operator==(const RealTime &r) const {
00109         return (sec == r.sec && nsec == r.nsec);
00110     }
00111  
00112     bool operator!=(const RealTime &r) const {
00113         return !(r == *this);
00114     }
00115  
00116     bool operator>=(const RealTime &r) const {
00117         if (sec == r.sec) return nsec >= r.nsec;
00118         else return sec >= r.sec;
00119     }
00120 
00121     bool operator<=(const RealTime &r) const {
00122         if (sec == r.sec) return nsec <= r.nsec;
00123         else return sec <= r.sec;
00124     }
00125 
00126     RealTime operator/(int d) const;
00127 
00131     double operator/(const RealTime &r) const;
00132 
00137     std::string toString() const;
00138 
00143     std::string toText(bool fixedDp = false) const;
00144 
00148     static long realTime2Frame(const RealTime &r, unsigned int sampleRate);
00149 
00153     static RealTime frame2RealTime(long frame, unsigned int sampleRate);
00154 
00155     static const RealTime zeroTime;
00156 };
00157 
00158 std::ostream &operator<<(std::ostream &out, const RealTime &rt);
00159 
00160 }
00161     
00162 #endif

Generated on Thu Jun 19 14:01:48 2008 for VampPluginSDK by  doxygen 1.5.5