comparison sdk/RealTime.h @ 0:6479539d1b32

* Importing first cut of Sonic Visualiser's Vamp plugin format SDK
author cannam
date Fri, 31 Mar 2006 14:21:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:6479539d1b32
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Vamp
5
6 An API for audio analysis and feature extraction plugins.
7
8 Centre for Digital Music, Queen Mary, University of London.
9 Copyright 2006 Chris Cannam.
10
11 Permission is hereby granted, free of charge, to any person
12 obtaining a copy of this software and associated documentation
13 files (the "Software"), to deal in the Software without
14 restriction, including without limitation the rights to use, copy,
15 modify, merge, publish, distribute, sublicense, and/or sell copies
16 of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be
20 included in all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 Except as contained in this notice, the names of the Centre for
31 Digital Music; Queen Mary, University of London; and Chris Cannam
32 shall not be used in advertising or otherwise to promote the sale,
33 use or other dealings in this Software without prior written
34 authorization.
35 */
36
37 /*
38 This is a modified version of a source file from the
39 Rosegarden MIDI and audio sequencer and notation editor.
40 This file copyright 2000-2006 Chris Cannam; relicensed as detailed above
41 */
42
43 #ifndef _REAL_TIME_H_
44 #define _REAL_TIME_H_
45
46 #include <iostream>
47 #include <string>
48
49 struct timeval;
50
51 namespace Vamp {
52
53 /**
54 * RealTime represents time values to nanosecond precision
55 * with accurate arithmetic and frame-rate conversion functions.
56 */
57
58 struct RealTime
59 {
60 int sec;
61 int nsec;
62
63 int usec() const { return nsec / 1000; }
64 int msec() const { return nsec / 1000000; }
65
66 RealTime(): sec(0), nsec(0) {}
67 RealTime(int s, int n);
68
69 RealTime(const RealTime &r) :
70 sec(r.sec), nsec(r.nsec) { }
71
72 static RealTime fromSeconds(double sec);
73 static RealTime fromMilliseconds(int msec);
74 static RealTime fromTimeval(const struct timeval &);
75
76 RealTime &operator=(const RealTime &r) {
77 sec = r.sec; nsec = r.nsec; return *this;
78 }
79
80 RealTime operator+(const RealTime &r) const {
81 return RealTime(sec + r.sec, nsec + r.nsec);
82 }
83 RealTime operator-(const RealTime &r) const {
84 return RealTime(sec - r.sec, nsec - r.nsec);
85 }
86 RealTime operator-() const {
87 return RealTime(-sec, -nsec);
88 }
89
90 bool operator <(const RealTime &r) const {
91 if (sec == r.sec) return nsec < r.nsec;
92 else return sec < r.sec;
93 }
94
95 bool operator >(const RealTime &r) const {
96 if (sec == r.sec) return nsec > r.nsec;
97 else return sec > r.sec;
98 }
99
100 bool operator==(const RealTime &r) const {
101 return (sec == r.sec && nsec == r.nsec);
102 }
103
104 bool operator!=(const RealTime &r) const {
105 return !(r == *this);
106 }
107
108 bool operator>=(const RealTime &r) const {
109 if (sec == r.sec) return nsec >= r.nsec;
110 else return sec >= r.sec;
111 }
112
113 bool operator<=(const RealTime &r) const {
114 if (sec == r.sec) return nsec <= r.nsec;
115 else return sec <= r.sec;
116 }
117
118 RealTime operator/(int d) const;
119
120 // Find the fractional difference between times
121 //
122 double operator/(const RealTime &r) const;
123
124 // Return a human-readable debug-type string to full precision
125 // (probably not a format to show to a user directly)
126 //
127 std::string toString() const;
128
129 // Return a user-readable string to the nearest millisecond
130 // in a form like HH:MM:SS.mmm
131 //
132 std::string toText(bool fixedDp = false) const;
133
134 // Convenience functions for handling sample frames
135 //
136 static long realTime2Frame(const RealTime &r, unsigned int sampleRate);
137 static RealTime frame2RealTime(long frame, unsigned int sampleRate);
138
139 static const RealTime zeroTime;
140 };
141
142 std::ostream &operator<<(std::ostream &out, const RealTime &rt);
143
144 }
145
146 #endif