svcore  1.9
RealTime.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 /*
16  This is a modified version of a source file from the
17  Rosegarden MIDI and audio sequencer and notation editor.
18  This file copyright 2000-2006 Chris Cannam.
19 */
20 
21 #ifndef _REAL_TIME_H_
22 #define _REAL_TIME_H_
23 
24 #include <iostream>
25 #include <string>
26 
27 struct timeval;
28 
29 
35 struct RealTime
36 {
37  int sec;
38  int nsec;
39 
40  int usec() const { return nsec / 1000; }
41  int msec() const { return nsec / 1000000; }
42 
43  RealTime(): sec(0), nsec(0) {}
44  RealTime(int s, int n);
45 
46  RealTime(const RealTime &r) :
47  sec(r.sec), nsec(r.nsec) { }
48 
49  static RealTime fromSeconds(double sec);
50  static RealTime fromMilliseconds(int msec);
51  static RealTime fromTimeval(const struct timeval &);
52  static RealTime fromXsdDuration(std::string xsdd);
53 
54  double toDouble() const;
55 
57  sec = r.sec; nsec = r.nsec; return *this;
58  }
59 
60  RealTime operator+(const RealTime &r) const {
61  return RealTime(sec + r.sec, nsec + r.nsec);
62  }
63  RealTime operator-(const RealTime &r) const {
64  return RealTime(sec - r.sec, nsec - r.nsec);
65  }
66  RealTime operator-() const {
67  return RealTime(-sec, -nsec);
68  }
69 
70  bool operator <(const RealTime &r) const {
71  if (sec == r.sec) return nsec < r.nsec;
72  else return sec < r.sec;
73  }
74 
75  bool operator >(const RealTime &r) const {
76  if (sec == r.sec) return nsec > r.nsec;
77  else return sec > r.sec;
78  }
79 
80  bool operator==(const RealTime &r) const {
81  return (sec == r.sec && nsec == r.nsec);
82  }
83 
84  bool operator!=(const RealTime &r) const {
85  return !(r == *this);
86  }
87 
88  bool operator>=(const RealTime &r) const {
89  if (sec == r.sec) return nsec >= r.nsec;
90  else return sec >= r.sec;
91  }
92 
93  bool operator<=(const RealTime &r) const {
94  if (sec == r.sec) return nsec <= r.nsec;
95  else return sec <= r.sec;
96  }
97 
98  RealTime operator*(int m) const;
99  RealTime operator/(int d) const;
100 
101  RealTime operator*(double m) const;
102  RealTime operator/(double d) const;
103 
107  double operator/(const RealTime &r) const;
108 
115  std::string toString(bool align = false) const;
116 
121  static RealTime fromString(std::string);
122 
127  std::string toText(bool fixedDp = false) const;
128 
136  std::string toFrameText(int fps) const;
137 
142  std::string toSecText() const;
143 
147  std::string toXsdDuration() const;
148 
152  static long realTime2Frame(const RealTime &r, unsigned int sampleRate);
153 
157  static RealTime frame2RealTime(long frame, unsigned int sampleRate);
158 
159  static const RealTime zeroTime;
160 };
161 
162 std::ostream &operator<<(std::ostream &out, const RealTime &rt);
163 
164 #endif
RealTime operator+(const RealTime &r) const
Definition: RealTime.h:60
static RealTime fromTimeval(const struct timeval &)
Definition: RealTime.cpp:72
static RealTime fromString(std::string)
Convert a string as obtained from toString back to a RealTime object.
Definition: RealTime.cpp:208
static RealTime frame2RealTime(long frame, unsigned int sampleRate)
Convert a sample frame at the given sample rate into a RealTime.
Definition: RealTime.cpp:450
std::string toText(bool fixedDp=false) const
Return a user-readable string to the nearest millisecond, in a form like HH:MM:SS....
Definition: RealTime.cpp:252
std::string toXsdDuration() const
Return a string in xsd:duration format.
Definition: RealTime.cpp:390
static RealTime fromSeconds(double sec)
Definition: RealTime.cpp:60
int nsec
Definition: RealTime.h:38
bool operator<(const RealTime &r) const
Definition: RealTime.h:70
RealTime operator-(const RealTime &r) const
Definition: RealTime.h:63
RealTime operator/(int d) const
Definition: RealTime.cpp:405
bool operator<=(const RealTime &r) const
Definition: RealTime.h:93
bool operator!=(const RealTime &r) const
Definition: RealTime.h:84
bool operator >(const RealTime &r) const
Definition: RealTime.h:75
std::string toSecText() const
Return a user-readable string to the nearest second, in a form like "6s" (for less than a minute) or ...
Definition: RealTime.cpp:360
RealTime(const RealTime &r)
Definition: RealTime.h:46
static RealTime fromMilliseconds(int msec)
Definition: RealTime.cpp:66
int sec
Definition: RealTime.h:37
RealTime()
Definition: RealTime.h:43
std::ostream & operator<<(std::ostream &out, const RealTime &rt)
Definition: RealTime.cpp:166
static long realTime2Frame(const RealTime &r, unsigned int sampleRate)
Convert a RealTime into a sample frame at the given sample rate.
Definition: RealTime.cpp:442
double toDouble() const
Definition: RealTime.cpp:159
int usec() const
Definition: RealTime.h:40
RealTime & operator=(const RealTime &r)
Definition: RealTime.h:56
static const RealTime zeroTime
Definition: RealTime.h:159
bool operator==(const RealTime &r) const
Definition: RealTime.h:80
int msec() const
Definition: RealTime.h:41
std::string toString(bool align=false) const
Return a human-readable debug-type string to full precision (probably not a format to show to a user ...
Definition: RealTime.cpp:191
static RealTime fromXsdDuration(std::string xsdd)
Definition: RealTime.cpp:78
RealTime operator-() const
Definition: RealTime.h:66
bool operator>=(const RealTime &r) const
Definition: RealTime.h:88
std::string toFrameText(int fps) const
Return a user-readable string in which seconds are divided into frames (presumably at a lower frame r...
Definition: RealTime.cpp:314
RealTime operator *(int m) const
Definition: RealTime.cpp:397
RealTime represents time values to nanosecond precision with accurate arithmetic and frame-rate conve...
Definition: RealTime.h:35