Mercurial > hg > svcore
comparison base/RealTime.cpp @ 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 |
comparison
equal
deleted
inserted
replaced
25:7dad8a310963 | 26:090c22aa726a |
---|---|
24 | 24 |
25 using std::cerr; | 25 using std::cerr; |
26 using std::endl; | 26 using std::endl; |
27 | 27 |
28 #include "base/RealTime.h" | 28 #include "base/RealTime.h" |
29 #include "sys/time.h" | |
29 | 30 |
30 // A RealTime consists of two ints that must be at least 32 bits each. | 31 // A RealTime consists of two ints that must be at least 32 bits each. |
31 // A signed 32-bit int can store values exceeding +/- 2 billion. This | 32 // A signed 32-bit int can store values exceeding +/- 2 billion. This |
32 // means we can safely use our lower int for nanoseconds, as there are | 33 // means we can safely use our lower int for nanoseconds, as there are |
33 // 1 billion nanoseconds in a second and we need to handle double that | 34 // 1 billion nanoseconds in a second and we need to handle double that |
51 while (nsec >= ONE_BILLION) { nsec -= ONE_BILLION; ++sec; } | 52 while (nsec >= ONE_BILLION) { nsec -= ONE_BILLION; ++sec; } |
52 while (nsec < 0) { nsec += ONE_BILLION; --sec; } | 53 while (nsec < 0) { nsec += ONE_BILLION; --sec; } |
53 } | 54 } |
54 } | 55 } |
55 | 56 |
57 RealTime | |
58 RealTime::fromSeconds(double sec) | |
59 { | |
60 return RealTime(int(sec), int((sec - int(sec)) * ONE_BILLION)); | |
61 } | |
62 | |
63 RealTime | |
64 RealTime::fromMilliseconds(int msec) | |
65 { | |
66 return RealTime(msec / 1000, (msec % 1000) * 1000000); | |
67 } | |
68 | |
69 RealTime | |
70 RealTime::fromTimeval(const struct timeval &tv) | |
71 { | |
72 return RealTime(tv.tv_sec, tv.tv_usec * 1000); | |
73 } | |
56 | 74 |
57 std::ostream &operator<<(std::ostream &out, const RealTime &rt) | 75 std::ostream &operator<<(std::ostream &out, const RealTime &rt) |
58 { | 76 { |
59 if (rt < RealTime::zeroTime) { | 77 if (rt < RealTime::zeroTime) { |
60 out << "-"; | 78 out << "-"; |