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 SV_REAL_TIME_H
22 #define SV_REAL_TIME_H
23 
24 #include "BaseTypes.h"
25 
26 #include <iostream>
27 #include <string>
28 
29 #include <vamp-hostsdk/RealTime.h>
30 
31 #ifdef _MSC_VER
32 #include "winsock.h" // struct timeval is in here
33 #else
34 #include "sys/time.h"
35 #endif
36 
42 struct RealTime
43 {
44  int sec;
45  int nsec;
46 
47  int usec() const { return nsec / 1000; }
48  int msec() const { return nsec / 1000000; }
49 
50  RealTime(): sec(0), nsec(0) {}
51  RealTime(int s, int n);
52 
53  RealTime(const RealTime &r) :
54  sec(r.sec), nsec(r.nsec) { }
55 
56  RealTime(const Vamp::RealTime &r) :
57  sec(r.sec), nsec(r.nsec) { }
58 
59  static RealTime fromSeconds(double sec);
60  static RealTime fromMilliseconds(int64_t msec);
61  static RealTime fromMicroseconds(int64_t usec);
62  static RealTime fromTimeval(const struct timeval &);
63  static RealTime fromXsdDuration(std::string xsdd);
64 
65  double toDouble() const;
66  Vamp::RealTime toVampRealTime() const { return Vamp::RealTime(sec, nsec); }
67 
69  sec = r.sec; nsec = r.nsec; return *this;
70  }
71 
72  RealTime operator+(const RealTime &r) const {
73  return RealTime(sec + r.sec, nsec + r.nsec);
74  }
75  RealTime operator-(const RealTime &r) const {
76  return RealTime(sec - r.sec, nsec - r.nsec);
77  }
78  RealTime operator-() const {
79  return RealTime(-sec, -nsec);
80  }
81 
82  bool operator <(const RealTime &r) const {
83  if (sec == r.sec) return nsec < r.nsec;
84  else return sec < r.sec;
85  }
86 
87  bool operator >(const RealTime &r) const {
88  if (sec == r.sec) return nsec > r.nsec;
89  else return sec > r.sec;
90  }
91 
92  bool operator==(const RealTime &r) const {
93  return (sec == r.sec && nsec == r.nsec);
94  }
95 
96  bool operator!=(const RealTime &r) const {
97  return !(r == *this);
98  }
99 
100  bool operator>=(const RealTime &r) const {
101  if (sec == r.sec) return nsec >= r.nsec;
102  else return sec >= r.sec;
103  }
104 
105  bool operator<=(const RealTime &r) const {
106  if (sec == r.sec) return nsec <= r.nsec;
107  else return sec <= r.sec;
108  }
109 
110  RealTime operator*(int m) const;
111  RealTime operator/(int d) const;
112 
113  RealTime operator*(double m) const;
114  RealTime operator/(double d) const;
115 
119  double operator/(const RealTime &r) const;
120 
127  std::string toString(bool align = false) const;
128 
133  static RealTime fromString(std::string);
134 
147  std::string toText(bool fixedDp = false) const;
148 
163  std::string toMSText(bool fixedDp, bool hms) const;
164 
175  std::string toFrameText(int fps, bool hms,
176  std::string frameDelimiter = ":") const;
177 
187  std::string toSecText() const;
188 
192  std::string toXsdDuration() const;
193 
197  static sv_frame_t realTime2Frame(const RealTime &r, sv_samplerate_t sampleRate);
198 
202  static RealTime frame2RealTime(sv_frame_t frame, sv_samplerate_t sampleRate);
203 
204  static const RealTime zeroTime;
205 };
206 
207 std::ostream &operator<<(std::ostream &out, const RealTime &rt);
208 
209 #endif
RealTime(const Vamp::RealTime &r)
Definition: RealTime.h:56
double sv_samplerate_t
Sample rate.
Definition: BaseTypes.h:51
RealTime operator+(const RealTime &r) const
Definition: RealTime.h:72
static RealTime fromTimeval(const struct timeval &)
Definition: RealTimeSV.cpp:90
static RealTime fromString(std::string)
Convert a string as obtained from toString back to a RealTime object.
Definition: RealTimeSV.cpp:230
int64_t sv_frame_t
Frame index, the unit of our time axis.
Definition: BaseTypes.h:31
static RealTime fromMilliseconds(int64_t msec)
Definition: RealTimeSV.cpp:64
static RealTime frame2RealTime(sv_frame_t frame, sv_samplerate_t sampleRate)
Convert a sample frame at the given sample rate into a RealTime.
Definition: RealTimeSV.cpp:498
static RealTime fromSeconds(double sec)
Definition: RealTimeSV.cpp:54
int nsec
Definition: RealTime.h:45
std::string toXsdDuration() const
Return a string in xsd:duration format.
Definition: RealTimeSV.cpp:424
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: RealTimeSV.cpp:213
bool operator>(const RealTime &r) const
Definition: RealTime.h:87
std::string toMSText(bool fixedDp, bool hms) const
Return a user-readable string to the nearest millisecond.
Definition: RealTimeSV.cpp:332
double toDouble() const
Definition: RealTimeSV.cpp:181
RealTime(const RealTime &r)
Definition: RealTime.h:53
RealTime operator/(int d) const
Definition: RealTimeSV.cpp:439
int sec
Definition: RealTime.h:44
RealTime()
Definition: RealTime.h:50
bool operator==(const RealTime &r) const
Definition: RealTime.h:92
std::string toSecText() const
Return a user-readable string to the nearest second, in H:M:S form.
Definition: RealTimeSV.cpp:406
bool operator<(const RealTime &r) const
Definition: RealTime.h:82
int msec() const
Definition: RealTime.h:48
static sv_frame_t realTime2Frame(const RealTime &r, sv_samplerate_t sampleRate)
Convert a RealTime into a sample frame at the given sample rate.
Definition: RealTimeSV.cpp:490
std::ostream & operator<<(std::ostream &out, const RealTime &rt)
Definition: RealTimeSV.cpp:188
RealTime operator-() const
Definition: RealTime.h:78
RealTime operator-(const RealTime &r) const
Definition: RealTime.h:75
bool operator>=(const RealTime &r) const
Definition: RealTime.h:100
int usec() const
Definition: RealTime.h:47
bool operator<=(const RealTime &r) const
Definition: RealTime.h:105
std::string toFrameText(int fps, bool hms, std::string frameDelimiter=":") const
Return a user-readable string in which seconds are divided into frames (presumably at a lower frame r...
Definition: RealTimeSV.cpp:367
RealTime & operator=(const RealTime &r)
Definition: RealTime.h:68
std::string toText(bool fixedDp=false) const
Return a user-readable string to the nearest millisecond, typically in a form like HH:MM:SS...
Definition: RealTimeSV.cpp:274
static RealTime fromMicroseconds(int64_t usec)
Definition: RealTimeSV.cpp:77
static const RealTime zeroTime
Definition: RealTime.h:204
Vamp::RealTime toVampRealTime() const
Definition: RealTime.h:66
static RealTime fromXsdDuration(std::string xsdd)
Definition: RealTimeSV.cpp:96
bool operator!=(const RealTime &r) const
Definition: RealTime.h:96
RealTime represents time values to nanosecond precision with accurate arithmetic and frame-rate conve...
Definition: RealTime.h:42
RealTime operator*(int m) const
Definition: RealTimeSV.cpp:431