annotate base/RealTime.h @ 26:090c22aa726a

* Add the Note layer for pianoroll-type display of note-type data * Complete the MIDI file importer (well, nearly -- it would be nice to be able to import the non-note data as other sorts of models, and that's not done yet). * Minor refactoring in RealTime etc
author Chris Cannam
date Fri, 10 Feb 2006 17:51:36 +0000
parents 2fb933f88604
children 39ae3dee27b9
rev   line source
Chris@0 1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@0 4 A waveform viewer and audio annotation editor.
Chris@2 5 Chris Cannam, Queen Mary University of London, 2005-2006
Chris@0 6
Chris@0 7 This is experimental software. Not for distribution.
Chris@0 8 */
Chris@0 9
Chris@0 10 /*
Chris@0 11 This is a modified version of a source file from the
Chris@0 12 Rosegarden MIDI and audio sequencer and notation editor.
Chris@17 13 This file copyright 2000-2006 Chris Cannam.
Chris@0 14 */
Chris@0 15
Chris@0 16 #ifndef _REAL_TIME_H_
Chris@0 17 #define _REAL_TIME_H_
Chris@0 18
Chris@0 19 #include <iostream>
Chris@0 20 #include <string>
Chris@0 21
Chris@26 22 struct timeval;
Chris@26 23
Chris@26 24
Chris@0 25 /**
Chris@0 26 * RealTime represents time values to nanosecond precision
Chris@0 27 * with accurate arithmetic and frame-rate conversion functions.
Chris@0 28 */
Chris@0 29
Chris@0 30 struct RealTime
Chris@0 31 {
Chris@0 32 int sec;
Chris@0 33 int nsec;
Chris@0 34
Chris@0 35 int usec() const { return nsec / 1000; }
Chris@0 36 int msec() const { return nsec / 1000000; }
Chris@0 37
Chris@0 38 RealTime(): sec(0), nsec(0) {}
Chris@0 39 RealTime(int s, int n);
Chris@0 40
Chris@0 41 RealTime(const RealTime &r) :
Chris@0 42 sec(r.sec), nsec(r.nsec) { }
Chris@0 43
Chris@26 44 static RealTime fromSeconds(double sec);
Chris@26 45 static RealTime fromMilliseconds(int msec);
Chris@26 46 static RealTime fromTimeval(const struct timeval &);
Chris@26 47
Chris@0 48 RealTime &operator=(const RealTime &r) {
Chris@0 49 sec = r.sec; nsec = r.nsec; return *this;
Chris@0 50 }
Chris@0 51
Chris@0 52 RealTime operator+(const RealTime &r) const {
Chris@0 53 return RealTime(sec + r.sec, nsec + r.nsec);
Chris@0 54 }
Chris@0 55 RealTime operator-(const RealTime &r) const {
Chris@0 56 return RealTime(sec - r.sec, nsec - r.nsec);
Chris@0 57 }
Chris@0 58 RealTime operator-() const {
Chris@0 59 return RealTime(-sec, -nsec);
Chris@0 60 }
Chris@0 61
Chris@0 62 bool operator <(const RealTime &r) const {
Chris@0 63 if (sec == r.sec) return nsec < r.nsec;
Chris@0 64 else return sec < r.sec;
Chris@0 65 }
Chris@0 66
Chris@0 67 bool operator >(const RealTime &r) const {
Chris@0 68 if (sec == r.sec) return nsec > r.nsec;
Chris@0 69 else return sec > r.sec;
Chris@0 70 }
Chris@0 71
Chris@0 72 bool operator==(const RealTime &r) const {
Chris@0 73 return (sec == r.sec && nsec == r.nsec);
Chris@0 74 }
Chris@0 75
Chris@0 76 bool operator!=(const RealTime &r) const {
Chris@0 77 return !(r == *this);
Chris@0 78 }
Chris@0 79
Chris@0 80 bool operator>=(const RealTime &r) const {
Chris@0 81 if (sec == r.sec) return nsec >= r.nsec;
Chris@0 82 else return sec >= r.sec;
Chris@0 83 }
Chris@0 84
Chris@0 85 bool operator<=(const RealTime &r) const {
Chris@0 86 if (sec == r.sec) return nsec <= r.nsec;
Chris@0 87 else return sec <= r.sec;
Chris@0 88 }
Chris@0 89
Chris@0 90 RealTime operator/(int d) const;
Chris@0 91
Chris@0 92 // Find the fractional difference between times
Chris@0 93 //
Chris@0 94 double operator/(const RealTime &r) const;
Chris@0 95
Chris@0 96 // Return a human-readable debug-type string to full precision
Chris@0 97 // (probably not a format to show to a user directly)
Chris@0 98 //
Chris@0 99 std::string toString() const;
Chris@0 100
Chris@0 101 // Return a user-readable string to the nearest millisecond
Chris@0 102 // in a form like HH:MM:SS.mmm
Chris@0 103 //
Chris@0 104 std::string toText(bool fixedDp = false) const;
Chris@0 105
Chris@0 106 // Convenience functions for handling sample frames
Chris@0 107 //
Chris@0 108 static long realTime2Frame(const RealTime &r, unsigned int sampleRate);
Chris@0 109 static RealTime frame2RealTime(long frame, unsigned int sampleRate);
Chris@0 110
Chris@0 111 static const RealTime zeroTime;
Chris@0 112 };
Chris@0 113
Chris@0 114 std::ostream &operator<<(std::ostream &out, const RealTime &rt);
Chris@0 115
Chris@0 116 #endif