VampPluginSDK 2.10
vamp-sdk/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 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 AUTHORS 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.
41 Relicensed by the author as detailed above.
42*/
43
44#ifndef _VAMP_REAL_TIME_H_
45#define _VAMP_REAL_TIME_H_
46
47#include <iostream>
48#include <string>
49
50#ifndef _WIN32
51struct timeval;
52#endif
53
54#include "plugguard.h"
56
57namespace Vamp {
58
67{
68 int sec;
69 int nsec;
70
71 int usec() const { return nsec / 1000; }
72 int msec() const { return nsec / 1000000; }
73
74 RealTime(): sec(0), nsec(0) {}
75 RealTime(int s, int n);
76
77 RealTime(const RealTime &r) :
78 sec(r.sec), nsec(r.nsec) { }
79
80 static RealTime fromSeconds(double sec);
81 static RealTime fromMilliseconds(int msec);
82
83#ifndef _WIN32
84 static RealTime fromTimeval(const struct timeval &);
85#endif
86
88 sec = r.sec; nsec = r.nsec; return *this;
89 }
90
91 RealTime operator+(const RealTime &r) const {
92 return RealTime(sec + r.sec, nsec + r.nsec);
93 }
94 RealTime operator-(const RealTime &r) const {
95 return RealTime(sec - r.sec, nsec - r.nsec);
96 }
98 return RealTime(-sec, -nsec);
99 }
100
101 bool operator <(const RealTime &r) const {
102 if (sec == r.sec) return nsec < r.nsec;
103 else return sec < r.sec;
104 }
105
106 bool operator >(const RealTime &r) const {
107 if (sec == r.sec) return nsec > r.nsec;
108 else return sec > r.sec;
109 }
110
111 bool operator==(const RealTime &r) const {
112 return (sec == r.sec && nsec == r.nsec);
113 }
114
115 bool operator!=(const RealTime &r) const {
116 return !(r == *this);
117 }
118
119 bool operator>=(const RealTime &r) const {
120 if (sec == r.sec) return nsec >= r.nsec;
121 else return sec >= r.sec;
122 }
123
124 bool operator<=(const RealTime &r) const {
125 if (sec == r.sec) return nsec <= r.nsec;
126 else return sec <= r.sec;
127 }
128
129 RealTime operator/(int d) const;
130
134 double operator/(const RealTime &r) const;
135
140 std::string toString() const;
141
146 std::string toText(bool fixedDp = false) const;
147
151 static long realTime2Frame(const RealTime &r, unsigned int sampleRate);
152
156 static RealTime frame2RealTime(long frame, unsigned int sampleRate);
157
158 static const RealTime zeroTime;
159};
160
161std::ostream &operator<<(std::ostream &out, const RealTime &rt);
162
163}
164
165_VAMP_SDK_PLUGSPACE_END(RealTime.h)
166
167#endif
Definition: FFT.h:43
std::ostream & operator<<(std::ostream &out, const RealTime &rt)
#define _VAMP_SDK_PLUGSPACE_BEGIN(h)
Definition: plugguard.h:79
#define _VAMP_SDK_PLUGSPACE_END(h)
Definition: plugguard.h:80
RealTime represents time values to nanosecond precision with accurate arithmetic and frame-rate conve...
bool operator<=(const RealTime &r) const
std::string toString() const
Return a human-readable debug-type string to full precision (probably not a format to show to a user ...
RealTime & operator=(const RealTime &r)
double operator/(const RealTime &r) const
Return the ratio of two times.
std::string toText(bool fixedDp=false) const
Return a user-readable string to the nearest millisecond in a form like HH:MM:SS.mmm.
int usec() const
static RealTime fromMilliseconds(int msec)
int msec() const
RealTime(int s, int n)
bool operator!=(const RealTime &r) const
static RealTime fromTimeval(const struct timeval &)
RealTime operator-(const RealTime &r) const
RealTime(const RealTime &r)
static RealTime frame2RealTime(long frame, unsigned int sampleRate)
Convert a sample frame at the given sample rate into a RealTime.
RealTime operator+(const RealTime &r) const
RealTime operator/(int d) const
static const RealTime zeroTime
static long realTime2Frame(const RealTime &r, unsigned int sampleRate)
Convert a RealTime into a sample frame at the given sample rate.
bool operator==(const RealTime &r) const
RealTime operator-() const
static RealTime fromSeconds(double sec)
bool operator>=(const RealTime &r) const