svcore  1.9
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     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     
00008     This program is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU General Public License as
00010     published by the Free Software Foundation; either version 2 of the
00011     License, or (at your option) any later version.  See the file
00012     COPYING included with this distribution for more information.
00013 */
00014 
00015 /*
00016    This is a modified version of a source file from the 
00017    Rosegarden MIDI and audio sequencer and notation editor.
00018    This file copyright 2000-2006 Chris Cannam.
00019 */
00020 
00021 #ifndef _REAL_TIME_H_
00022 #define _REAL_TIME_H_
00023 
00024 #include <iostream>
00025 #include <string>
00026 
00027 struct timeval;
00028 
00029 
00035 struct RealTime
00036 {
00037     int sec;
00038     int nsec;
00039 
00040     int usec() const { return nsec / 1000; }
00041     int msec() const { return nsec / 1000000; }
00042 
00043     RealTime(): sec(0), nsec(0) {}
00044     RealTime(int s, int n);
00045 
00046     RealTime(const RealTime &r) :
00047         sec(r.sec), nsec(r.nsec) { }
00048 
00049     static RealTime fromSeconds(double sec);
00050     static RealTime fromMilliseconds(int msec);
00051     static RealTime fromTimeval(const struct timeval &);
00052     static RealTime fromXsdDuration(std::string xsdd);
00053 
00054     double toDouble() const;
00055 
00056     RealTime &operator=(const RealTime &r) {
00057         sec = r.sec; nsec = r.nsec; return *this;
00058     }
00059 
00060     RealTime operator+(const RealTime &r) const {
00061         return RealTime(sec + r.sec, nsec + r.nsec);
00062     }
00063     RealTime operator-(const RealTime &r) const {
00064         return RealTime(sec - r.sec, nsec - r.nsec);
00065     }
00066     RealTime operator-() const {
00067         return RealTime(-sec, -nsec);
00068     }
00069 
00070     bool operator <(const RealTime &r) const {
00071         if (sec == r.sec) return nsec < r.nsec;
00072         else return sec < r.sec;
00073     }
00074 
00075     bool operator >(const RealTime &r) const {
00076         if (sec == r.sec) return nsec > r.nsec;
00077         else return sec > r.sec;
00078     }
00079 
00080     bool operator==(const RealTime &r) const {
00081         return (sec == r.sec && nsec == r.nsec);
00082     }
00083  
00084     bool operator!=(const RealTime &r) const {
00085         return !(r == *this);
00086     }
00087  
00088     bool operator>=(const RealTime &r) const {
00089         if (sec == r.sec) return nsec >= r.nsec;
00090         else return sec >= r.sec;
00091     }
00092 
00093     bool operator<=(const RealTime &r) const {
00094         if (sec == r.sec) return nsec <= r.nsec;
00095         else return sec <= r.sec;
00096     }
00097 
00098     RealTime operator*(int m) const;
00099     RealTime operator/(int d) const;
00100 
00101     RealTime operator*(double m) const;
00102     RealTime operator/(double d) const;
00103 
00107     double operator/(const RealTime &r) const;
00108 
00115     std::string toString(bool align = false) const;
00116 
00121     static RealTime fromString(std::string);
00122 
00127     std::string toText(bool fixedDp = false) const;
00128 
00136     std::string toFrameText(int fps) const;
00137 
00142     std::string toSecText() const;
00143 
00147     std::string toXsdDuration() const;
00148 
00152     static long realTime2Frame(const RealTime &r, unsigned int sampleRate);
00153 
00157     static RealTime frame2RealTime(long frame, unsigned int sampleRate);
00158 
00159     static const RealTime zeroTime;
00160 };
00161 
00162 std::ostream &operator<<(std::ostream &out, const RealTime &rt);
00163     
00164 #endif