annotate base/RealTime.h @ 1349:330bcc92507d 3.0-integration

Take a different approach to using libsndfile -- the _fd function doesn't work for me in this build, so use the wchar api
author Chris Cannam
date Fri, 06 Jan 2017 20:51:47 +0000
parents c811991a5efa
children 48e9f538e6e9
rev   line source
Chris@49 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@52 4 Sonic Visualiser
Chris@52 5 An audio file viewer and annotation editor.
Chris@52 6 Centre for Digital Music, Queen Mary, University of London.
Chris@0 7
Chris@52 8 This program is free software; you can redistribute it and/or
Chris@52 9 modify it under the terms of the GNU General Public License as
Chris@52 10 published by the Free Software Foundation; either version 2 of the
Chris@52 11 License, or (at your option) any later version. See the file
Chris@52 12 COPYING included with this distribution for more information.
Chris@0 13 */
Chris@0 14
Chris@0 15 /*
Chris@0 16 This is a modified version of a source file from the
Chris@0 17 Rosegarden MIDI and audio sequencer and notation editor.
Chris@17 18 This file copyright 2000-2006 Chris Cannam.
Chris@0 19 */
Chris@0 20
Chris@1070 21 #ifndef SV_REAL_TIME_H
Chris@1070 22 #define SV_REAL_TIME_H
Chris@0 23
Chris@1038 24 #include "BaseTypes.h"
Chris@1038 25
Chris@0 26 #include <iostream>
Chris@0 27 #include <string>
Chris@0 28
Chris@1040 29 #include <vamp-hostsdk/RealTime.h>
Chris@1040 30
Chris@1216 31 #ifdef _MSC_VER
Chris@1216 32 #include "winsock.h" // struct timeval is in here
Chris@1216 33 #else
Chris@1216 34 #include "sys/time.h"
Chris@1216 35 #endif
Chris@26 36
Chris@0 37 /**
Chris@0 38 * RealTime represents time values to nanosecond precision
Chris@0 39 * with accurate arithmetic and frame-rate conversion functions.
Chris@0 40 */
Chris@0 41
Chris@0 42 struct RealTime
Chris@0 43 {
Chris@0 44 int sec;
Chris@0 45 int nsec;
Chris@0 46
Chris@0 47 int usec() const { return nsec / 1000; }
Chris@0 48 int msec() const { return nsec / 1000000; }
Chris@0 49
Chris@0 50 RealTime(): sec(0), nsec(0) {}
Chris@0 51 RealTime(int s, int n);
Chris@0 52
Chris@0 53 RealTime(const RealTime &r) :
Chris@0 54 sec(r.sec), nsec(r.nsec) { }
Chris@0 55
Chris@1040 56 RealTime(const Vamp::RealTime &r) :
Chris@1040 57 sec(r.sec), nsec(r.nsec) { }
Chris@1040 58
Chris@26 59 static RealTime fromSeconds(double sec);
Chris@26 60 static RealTime fromMilliseconds(int msec);
Chris@26 61 static RealTime fromTimeval(const struct timeval &);
Chris@439 62 static RealTime fromXsdDuration(std::string xsdd);
Chris@439 63
Chris@439 64 double toDouble() const;
Chris@1040 65 Vamp::RealTime toVampRealTime() const { return Vamp::RealTime(sec, nsec); }
Chris@26 66
Chris@0 67 RealTime &operator=(const RealTime &r) {
Chris@0 68 sec = r.sec; nsec = r.nsec; return *this;
Chris@0 69 }
Chris@0 70
Chris@0 71 RealTime operator+(const RealTime &r) const {
Chris@0 72 return RealTime(sec + r.sec, nsec + r.nsec);
Chris@0 73 }
Chris@0 74 RealTime operator-(const RealTime &r) const {
Chris@0 75 return RealTime(sec - r.sec, nsec - r.nsec);
Chris@0 76 }
Chris@0 77 RealTime operator-() const {
Chris@0 78 return RealTime(-sec, -nsec);
Chris@0 79 }
Chris@0 80
Chris@0 81 bool operator <(const RealTime &r) const {
Chris@0 82 if (sec == r.sec) return nsec < r.nsec;
Chris@0 83 else return sec < r.sec;
Chris@0 84 }
Chris@0 85
Chris@0 86 bool operator >(const RealTime &r) const {
Chris@0 87 if (sec == r.sec) return nsec > r.nsec;
Chris@0 88 else return sec > r.sec;
Chris@0 89 }
Chris@0 90
Chris@0 91 bool operator==(const RealTime &r) const {
Chris@0 92 return (sec == r.sec && nsec == r.nsec);
Chris@0 93 }
Chris@0 94
Chris@0 95 bool operator!=(const RealTime &r) const {
Chris@0 96 return !(r == *this);
Chris@0 97 }
Chris@0 98
Chris@0 99 bool operator>=(const RealTime &r) const {
Chris@0 100 if (sec == r.sec) return nsec >= r.nsec;
Chris@0 101 else return sec >= r.sec;
Chris@0 102 }
Chris@0 103
Chris@0 104 bool operator<=(const RealTime &r) const {
Chris@0 105 if (sec == r.sec) return nsec <= r.nsec;
Chris@0 106 else return sec <= r.sec;
Chris@0 107 }
Chris@0 108
Chris@183 109 RealTime operator*(int m) const;
Chris@0 110 RealTime operator/(int d) const;
Chris@0 111
Chris@378 112 RealTime operator*(double m) const;
Chris@378 113 RealTime operator/(double d) const;
Chris@378 114
Chris@350 115 /**
Chris@350 116 * Return the ratio of two times.
Chris@350 117 */
Chris@0 118 double operator/(const RealTime &r) const;
Chris@0 119
Chris@350 120 /**
Chris@350 121 * Return a human-readable debug-type string to full precision
Chris@350 122 * (probably not a format to show to a user directly). If align
Chris@350 123 * is true, prepend " " to the start of positive values so that
Chris@350 124 * they line up with negative ones (which start with "-").
Chris@350 125 */
Chris@121 126 std::string toString(bool align = false) const;
Chris@0 127
Chris@350 128 /**
Chris@350 129 * Convert a string as obtained from toString back to a RealTime
Chris@350 130 * object.
Chris@350 131 */
Chris@350 132 static RealTime fromString(std::string);
Chris@350 133
Chris@350 134 /**
Chris@1070 135 * Return a user-readable string to the nearest millisecond,
Chris@1070 136 * typically in a form like HH:MM:SS.mmm. The exact format will
Chris@1070 137 * depend on the application preferences for time display
Chris@1070 138 * precision and hours:minutes:seconds format -- this function
Chris@1070 139 * simply dispatches to toMSText or toFrameText with appropriate
Chris@1070 140 * arguments depending on the preferences.
Chris@1070 141 *
Chris@1070 142 * If fixedDp is true, the result will be padded to 3 dp,
Chris@1070 143 * i.e. millisecond resolution, even if the number of milliseconds
Chris@1070 144 * is a multiple of 10.
Chris@350 145 */
Chris@0 146 std::string toText(bool fixedDp = false) const;
Chris@0 147
Chris@1070 148 /**
Chris@1070 149 * Return a user-readable string to the nearest millisecond.
Chris@1070 150 *
Chris@1070 151 * If fixedDp is true, the result will be padded to 3 dp,
Chris@1070 152 * i.e. millisecond resolution, even if the number of milliseconds
Chris@1070 153 * is a multiple of 10.
Chris@1070 154 *
Chris@1070 155 * If hms is true, results may be returned in the form
Chris@1070 156 * HH:MM:SS.mmm (if the time is large enough). If hms is false,
Chris@1070 157 * the result will always be a (fractional) number of seconds.
Chris@1070 158 *
Chris@1070 159 * Unlike toText, this function does not depend on the application
Chris@1070 160 * preferences.
Chris@1070 161 */
Chris@1070 162 std::string toMSText(bool fixedDp, bool hms) const;
Chris@1070 163
Chris@350 164 /**
Chris@612 165 * Return a user-readable string in which seconds are divided into
Chris@612 166 * frames (presumably at a lower frame rate than audio rate,
Chris@612 167 * e.g. 24 or 25 video frames), in a form like HH:MM:SS:FF. fps
Chris@612 168 * gives the number of frames per second, and must be integral
Chris@612 169 * (29.97 not supported).
Chris@1070 170 *
Chris@1070 171 * Unlike toText, this function does not depend on the application
Chris@1070 172 * preferences.
Chris@612 173 */
Chris@1070 174 std::string toFrameText(int fps, bool hms) const;
Chris@612 175
Chris@612 176 /**
Chris@1070 177 * Return a user-readable string to the nearest second, in H:M:S
Chris@1070 178 * form. Does not include milliseconds or frames. The result will
Chris@1070 179 * be suffixed "s" if it contains only seconds (no hours or
Chris@1070 180 * minutes).
Chris@1070 181 *
Chris@1070 182 * Unlike toText, this function does not depend on the application
Chris@1070 183 * preferences.
Chris@350 184 */
Chris@247 185 std::string toSecText() const;
Chris@247 186
Chris@350 187 /**
Chris@494 188 * Return a string in xsd:duration format.
Chris@494 189 */
Chris@494 190 std::string toXsdDuration() const;
Chris@494 191
Chris@494 192 /**
Chris@350 193 * Convert a RealTime into a sample frame at the given sample rate.
Chris@350 194 */
Chris@1040 195 static sv_frame_t realTime2Frame(const RealTime &r, sv_samplerate_t sampleRate);
Chris@350 196
Chris@350 197 /**
Chris@350 198 * Convert a sample frame at the given sample rate into a RealTime.
Chris@350 199 */
Chris@1040 200 static RealTime frame2RealTime(sv_frame_t frame, sv_samplerate_t sampleRate);
Chris@0 201
Chris@0 202 static const RealTime zeroTime;
Chris@0 203 };
Chris@0 204
Chris@0 205 std::ostream &operator<<(std::ostream &out, const RealTime &rt);
Chris@0 206
Chris@0 207 #endif