annotate base/RealTime.h @ 1188:d9698ee93659 spectrogram-minor-refactor

Extend column logic to peak frequency display as well, and correct some scopes according to whether values are per source column or per target pixel
author Chris Cannam
date Mon, 20 Jun 2016 12:00:32 +0100
parents b8a788c9a6f1
children c811991a5efa
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@26 31 struct timeval;
Chris@26 32
Chris@0 33 /**
Chris@0 34 * RealTime represents time values to nanosecond precision
Chris@0 35 * with accurate arithmetic and frame-rate conversion functions.
Chris@0 36 */
Chris@0 37
Chris@0 38 struct RealTime
Chris@0 39 {
Chris@0 40 int sec;
Chris@0 41 int nsec;
Chris@0 42
Chris@0 43 int usec() const { return nsec / 1000; }
Chris@0 44 int msec() const { return nsec / 1000000; }
Chris@0 45
Chris@0 46 RealTime(): sec(0), nsec(0) {}
Chris@0 47 RealTime(int s, int n);
Chris@0 48
Chris@0 49 RealTime(const RealTime &r) :
Chris@0 50 sec(r.sec), nsec(r.nsec) { }
Chris@0 51
Chris@1040 52 RealTime(const Vamp::RealTime &r) :
Chris@1040 53 sec(r.sec), nsec(r.nsec) { }
Chris@1040 54
Chris@26 55 static RealTime fromSeconds(double sec);
Chris@26 56 static RealTime fromMilliseconds(int msec);
Chris@26 57 static RealTime fromTimeval(const struct timeval &);
Chris@439 58 static RealTime fromXsdDuration(std::string xsdd);
Chris@439 59
Chris@439 60 double toDouble() const;
Chris@1040 61 Vamp::RealTime toVampRealTime() const { return Vamp::RealTime(sec, nsec); }
Chris@26 62
Chris@0 63 RealTime &operator=(const RealTime &r) {
Chris@0 64 sec = r.sec; nsec = r.nsec; return *this;
Chris@0 65 }
Chris@0 66
Chris@0 67 RealTime operator+(const RealTime &r) const {
Chris@0 68 return RealTime(sec + r.sec, nsec + r.nsec);
Chris@0 69 }
Chris@0 70 RealTime operator-(const RealTime &r) const {
Chris@0 71 return RealTime(sec - r.sec, nsec - r.nsec);
Chris@0 72 }
Chris@0 73 RealTime operator-() const {
Chris@0 74 return RealTime(-sec, -nsec);
Chris@0 75 }
Chris@0 76
Chris@0 77 bool operator <(const RealTime &r) const {
Chris@0 78 if (sec == r.sec) return nsec < r.nsec;
Chris@0 79 else return sec < r.sec;
Chris@0 80 }
Chris@0 81
Chris@0 82 bool operator >(const RealTime &r) const {
Chris@0 83 if (sec == r.sec) return nsec > r.nsec;
Chris@0 84 else return sec > r.sec;
Chris@0 85 }
Chris@0 86
Chris@0 87 bool operator==(const RealTime &r) const {
Chris@0 88 return (sec == r.sec && nsec == r.nsec);
Chris@0 89 }
Chris@0 90
Chris@0 91 bool operator!=(const RealTime &r) const {
Chris@0 92 return !(r == *this);
Chris@0 93 }
Chris@0 94
Chris@0 95 bool operator>=(const RealTime &r) const {
Chris@0 96 if (sec == r.sec) return nsec >= r.nsec;
Chris@0 97 else return sec >= r.sec;
Chris@0 98 }
Chris@0 99
Chris@0 100 bool operator<=(const RealTime &r) const {
Chris@0 101 if (sec == r.sec) return nsec <= r.nsec;
Chris@0 102 else return sec <= r.sec;
Chris@0 103 }
Chris@0 104
Chris@183 105 RealTime operator*(int m) const;
Chris@0 106 RealTime operator/(int d) const;
Chris@0 107
Chris@378 108 RealTime operator*(double m) const;
Chris@378 109 RealTime operator/(double d) const;
Chris@378 110
Chris@350 111 /**
Chris@350 112 * Return the ratio of two times.
Chris@350 113 */
Chris@0 114 double operator/(const RealTime &r) const;
Chris@0 115
Chris@350 116 /**
Chris@350 117 * Return a human-readable debug-type string to full precision
Chris@350 118 * (probably not a format to show to a user directly). If align
Chris@350 119 * is true, prepend " " to the start of positive values so that
Chris@350 120 * they line up with negative ones (which start with "-").
Chris@350 121 */
Chris@121 122 std::string toString(bool align = false) const;
Chris@0 123
Chris@350 124 /**
Chris@350 125 * Convert a string as obtained from toString back to a RealTime
Chris@350 126 * object.
Chris@350 127 */
Chris@350 128 static RealTime fromString(std::string);
Chris@350 129
Chris@350 130 /**
Chris@1070 131 * Return a user-readable string to the nearest millisecond,
Chris@1070 132 * typically in a form like HH:MM:SS.mmm. The exact format will
Chris@1070 133 * depend on the application preferences for time display
Chris@1070 134 * precision and hours:minutes:seconds format -- this function
Chris@1070 135 * simply dispatches to toMSText or toFrameText with appropriate
Chris@1070 136 * arguments depending on the preferences.
Chris@1070 137 *
Chris@1070 138 * If fixedDp is true, the result will be padded to 3 dp,
Chris@1070 139 * i.e. millisecond resolution, even if the number of milliseconds
Chris@1070 140 * is a multiple of 10.
Chris@350 141 */
Chris@0 142 std::string toText(bool fixedDp = false) const;
Chris@0 143
Chris@1070 144 /**
Chris@1070 145 * Return a user-readable string to the nearest millisecond.
Chris@1070 146 *
Chris@1070 147 * If fixedDp is true, the result will be padded to 3 dp,
Chris@1070 148 * i.e. millisecond resolution, even if the number of milliseconds
Chris@1070 149 * is a multiple of 10.
Chris@1070 150 *
Chris@1070 151 * If hms is true, results may be returned in the form
Chris@1070 152 * HH:MM:SS.mmm (if the time is large enough). If hms is false,
Chris@1070 153 * the result will always be a (fractional) number of seconds.
Chris@1070 154 *
Chris@1070 155 * Unlike toText, this function does not depend on the application
Chris@1070 156 * preferences.
Chris@1070 157 */
Chris@1070 158 std::string toMSText(bool fixedDp, bool hms) const;
Chris@1070 159
Chris@350 160 /**
Chris@612 161 * Return a user-readable string in which seconds are divided into
Chris@612 162 * frames (presumably at a lower frame rate than audio rate,
Chris@612 163 * e.g. 24 or 25 video frames), in a form like HH:MM:SS:FF. fps
Chris@612 164 * gives the number of frames per second, and must be integral
Chris@612 165 * (29.97 not supported).
Chris@1070 166 *
Chris@1070 167 * Unlike toText, this function does not depend on the application
Chris@1070 168 * preferences.
Chris@612 169 */
Chris@1070 170 std::string toFrameText(int fps, bool hms) const;
Chris@612 171
Chris@612 172 /**
Chris@1070 173 * Return a user-readable string to the nearest second, in H:M:S
Chris@1070 174 * form. Does not include milliseconds or frames. The result will
Chris@1070 175 * be suffixed "s" if it contains only seconds (no hours or
Chris@1070 176 * minutes).
Chris@1070 177 *
Chris@1070 178 * Unlike toText, this function does not depend on the application
Chris@1070 179 * preferences.
Chris@350 180 */
Chris@247 181 std::string toSecText() const;
Chris@247 182
Chris@350 183 /**
Chris@494 184 * Return a string in xsd:duration format.
Chris@494 185 */
Chris@494 186 std::string toXsdDuration() const;
Chris@494 187
Chris@494 188 /**
Chris@350 189 * Convert a RealTime into a sample frame at the given sample rate.
Chris@350 190 */
Chris@1040 191 static sv_frame_t realTime2Frame(const RealTime &r, sv_samplerate_t sampleRate);
Chris@350 192
Chris@350 193 /**
Chris@350 194 * Convert a sample frame at the given sample rate into a RealTime.
Chris@350 195 */
Chris@1040 196 static RealTime frame2RealTime(sv_frame_t frame, sv_samplerate_t sampleRate);
Chris@0 197
Chris@0 198 static const RealTime zeroTime;
Chris@0 199 };
Chris@0 200
Chris@0 201 std::ostream &operator<<(std::ostream &out, const RealTime &rt);
Chris@0 202
Chris@0 203 #endif