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 
00053     RealTime &operator=(const RealTime &r) {
00054         sec = r.sec; nsec = r.nsec; return *this;
00055     }
00056 
00057     RealTime operator+(const RealTime &r) const {
00058         return RealTime(sec + r.sec, nsec + r.nsec);
00059     }
00060     RealTime operator-(const RealTime &r) const {
00061         return RealTime(sec - r.sec, nsec - r.nsec);
00062     }
00063     RealTime operator-() const {
00064         return RealTime(-sec, -nsec);
00065     }
00066 
00067     bool operator <(const RealTime &r) const {
00068         if (sec == r.sec) return nsec < r.nsec;
00069         else return sec < r.sec;
00070     }
00071 
00072     bool operator >(const RealTime &r) const {
00073         if (sec == r.sec) return nsec > r.nsec;
00074         else return sec > r.sec;
00075     }
00076 
00077     bool operator==(const RealTime &r) const {
00078         return (sec == r.sec && nsec == r.nsec);
00079     }
00080  
00081     bool operator!=(const RealTime &r) const {
00082         return !(r == *this);
00083     }
00084  
00085     bool operator>=(const RealTime &r) const {
00086         if (sec == r.sec) return nsec >= r.nsec;
00087         else return sec >= r.sec;
00088     }
00089 
00090     bool operator<=(const RealTime &r) const {
00091         if (sec == r.sec) return nsec <= r.nsec;
00092         else return sec <= r.sec;
00093     }
00094 
00095     RealTime operator*(int m) const;
00096     RealTime operator/(int d) const;
00097 
00098     RealTime operator*(double m) const;
00099     RealTime operator/(double d) const;
00100 
00104     double operator/(const RealTime &r) const;
00105 
00112     std::string toString(bool align = false) const;
00113 
00118     static RealTime fromString(std::string);
00119 
00124     std::string toText(bool fixedDp = false) const;
00125 
00130     std::string toSecText() const;
00131 
00135     static long realTime2Frame(const RealTime &r, unsigned int sampleRate);
00136 
00140     static RealTime frame2RealTime(long frame, unsigned int sampleRate);
00141 
00142     static const RealTime zeroTime;
00143 };
00144 
00145 std::ostream &operator<<(std::ostream &out, const RealTime &rt);
00146     
00147 #endif

Generated on Wed Feb 20 15:45:27 2008 for SonicVisualiser by  doxygen 1.5.1