annotate base/RealTime.h @ 124:4ab844784152

* 1507728: Crash when creating spectrogram of small soundfiles
author Chris Cannam
date Mon, 19 Jun 2006 14:19:55 +0000
parents 7c3e1bc51080
children 3c1d5ef43baa
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@0 21 #ifndef _REAL_TIME_H_
Chris@0 22 #define _REAL_TIME_H_
Chris@0 23
Chris@121 24 //#include "vamp-sdk/RealTime.h"
Chris@121 25 //using Vamp::RealTime;
Chris@66 26
Chris@121 27 //#ifdef NOT_DEFINED
Chris@66 28
Chris@0 29 #include <iostream>
Chris@0 30 #include <string>
Chris@0 31
Chris@26 32 struct timeval;
Chris@26 33
Chris@26 34
Chris@0 35 /**
Chris@0 36 * RealTime represents time values to nanosecond precision
Chris@0 37 * with accurate arithmetic and frame-rate conversion functions.
Chris@0 38 */
Chris@0 39
Chris@0 40 struct RealTime
Chris@0 41 {
Chris@0 42 int sec;
Chris@0 43 int nsec;
Chris@0 44
Chris@0 45 int usec() const { return nsec / 1000; }
Chris@0 46 int msec() const { return nsec / 1000000; }
Chris@0 47
Chris@0 48 RealTime(): sec(0), nsec(0) {}
Chris@0 49 RealTime(int s, int n);
Chris@0 50
Chris@0 51 RealTime(const RealTime &r) :
Chris@0 52 sec(r.sec), nsec(r.nsec) { }
Chris@0 53
Chris@26 54 static RealTime fromSeconds(double sec);
Chris@26 55 static RealTime fromMilliseconds(int msec);
Chris@26 56 static RealTime fromTimeval(const struct timeval &);
Chris@26 57
Chris@0 58 RealTime &operator=(const RealTime &r) {
Chris@0 59 sec = r.sec; nsec = r.nsec; return *this;
Chris@0 60 }
Chris@0 61
Chris@0 62 RealTime operator+(const RealTime &r) const {
Chris@0 63 return RealTime(sec + r.sec, nsec + r.nsec);
Chris@0 64 }
Chris@0 65 RealTime operator-(const RealTime &r) const {
Chris@0 66 return RealTime(sec - r.sec, nsec - r.nsec);
Chris@0 67 }
Chris@0 68 RealTime operator-() const {
Chris@0 69 return RealTime(-sec, -nsec);
Chris@0 70 }
Chris@0 71
Chris@0 72 bool operator <(const RealTime &r) const {
Chris@0 73 if (sec == r.sec) return nsec < r.nsec;
Chris@0 74 else return sec < r.sec;
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 return (sec == r.sec && nsec == r.nsec);
Chris@0 84 }
Chris@0 85
Chris@0 86 bool operator!=(const RealTime &r) const {
Chris@0 87 return !(r == *this);
Chris@0 88 }
Chris@0 89
Chris@0 90 bool operator>=(const RealTime &r) const {
Chris@0 91 if (sec == r.sec) return nsec >= r.nsec;
Chris@0 92 else return sec >= r.sec;
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 RealTime operator/(int d) const;
Chris@0 101
Chris@0 102 // Find the fractional difference between times
Chris@0 103 //
Chris@0 104 double operator/(const RealTime &r) const;
Chris@0 105
Chris@0 106 // Return a human-readable debug-type string to full precision
Chris@121 107 // (probably not a format to show to a user directly). If align
Chris@121 108 // is true, prepend " " to the start of positive values so that
Chris@121 109 // they line up with negative ones (which start with "-").
Chris@0 110 //
Chris@121 111 std::string toString(bool align = false) const;
Chris@0 112
Chris@0 113 // Return a user-readable string to the nearest millisecond
Chris@0 114 // in a form like HH:MM:SS.mmm
Chris@0 115 //
Chris@0 116 std::string toText(bool fixedDp = false) const;
Chris@0 117
Chris@0 118 // Convenience functions for handling sample frames
Chris@0 119 //
Chris@0 120 static long realTime2Frame(const RealTime &r, unsigned int sampleRate);
Chris@0 121 static RealTime frame2RealTime(long frame, unsigned int sampleRate);
Chris@0 122
Chris@0 123 static const RealTime zeroTime;
Chris@0 124 };
Chris@0 125
Chris@0 126 std::ostream &operator<<(std::ostream &out, const RealTime &rt);
Chris@66 127
Chris@121 128 //#endif
Chris@0 129
Chris@0 130 #endif