new audio engine backend for native CoreAudio audio I/O, and PortMIDI for MIDI.
[ardour.git] / libs / backends / wavesaudio / wavesapi / miscutils / UMicroseconds.h
1 /*
2     Copyright (C) 2013 Waves Audio Ltd.
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19 #ifndef __UMicroseconds_h__\r
20         #define __UMicroseconds_h__\r
21         \r
22 /* Copy to include\r
23 #include "UMicroseconds.h"\r
24 */\r
25 \r
26 \r
27 \r
28 #include "BasicTypes/WUDefines.h"\r
29 #include "BasicTypes/WUTypes.h"\r
30 \r
31 namespace wvNS { \r
32 // a wraper for Microseconds function from Timer.h\r
33 class DllExport UMicroseconds\r
34 {\r
35 public:\r
36 \r
37 #ifdef _WINDOWS\r
38         typedef int64_t TimeKeeper;\r
39 #endif\r
40 #ifdef __MACOS__\r
41         typedef uint64_t TimeKeeper;\r
42 #endif\r
43 #ifdef __linux__\r
44         typedef uint64_t TimeKeeper;\r
45 #endif\r
46 \r
47 private:\r
48         TimeKeeper theTime;\r
49 \r
50 public:\r
51 \r
52         UMicroseconds()\r
53         {\r
54                 ReadTime();\r
55         }\r
56 \r
57         UMicroseconds(const TimeKeeper in_initVal) : theTime(in_initVal) {}\r
58 \r
59         UMicroseconds(const UMicroseconds& inUM) : theTime(inUM.theTime) {}\r
60         UMicroseconds& operator=(const UMicroseconds& inUM) {theTime = inUM.theTime;  return *this;}\r
61         UMicroseconds& operator+=(const TimeKeeper in_timeToAdd)  {theTime += in_timeToAdd;  return *this;}\r
62 \r
63         UMicroseconds& ReadTime();\r
64   \r
65         TimeKeeper GetNativeTime() const {return theTime;}\r
66         operator uint64_t () {return static_cast<uint64_t>(theTime);}\r
67         operator double () const {return static_cast<const double>(theTime);}\r
68 \r
69         double Seconds() const {return static_cast<double>(theTime) / double(1000000);}\r
70         double MilliSeconds() const {return static_cast<double>(theTime) / double(1000);}\r
71         double MicroSeconds() const {return static_cast<double>(theTime);}\r
72 \r
73 #ifdef __MACOS__\r
74         uint32_t hi();\r
75         uint32_t lo();\r
76 #endif\r
77 };\r
78 \r
79 inline UMicroseconds operator-(const UMicroseconds& in_one, const UMicroseconds& in_two)\r
80 {\r
81         UMicroseconds retVal(in_one.GetNativeTime() - in_two.GetNativeTime());\r
82         return retVal;\r
83 }\r
84 \r
85 class UMicrosecondsAccumulator\r
86 {\r
87 public:\r
88         UMicrosecondsAccumulator() : m_start_time(0), m_accumulator(0) {}\r
89         \r
90         void Start();\r
91         void Stop();\r
92         void Clear();\r
93         \r
94         UMicroseconds GetAccumulatedTime() const;\r
95         \r
96         UMicrosecondsAccumulator& operator+=(const UMicrosecondsAccumulator&);\r
97         \r
98 protected:\r
99         UMicroseconds m_start_time;\r
100         UMicroseconds m_accumulator;\r
101 };\r
102 \r
103 inline UMicroseconds operator-(const UMicrosecondsAccumulator& in_one, const UMicrosecondsAccumulator& in_two)\r
104 {\r
105         UMicroseconds retVal(in_one.GetAccumulatedTime() - in_two.GetAccumulatedTime());\r
106         return retVal;\r
107 }\r
108 \r
109 //=========================================================================================//\r
110 inline void MicrosecondDelay(double amt)\r
111 //=========================================================================================//\r
112 {\r
113         UMicroseconds than;\r
114         UMicroseconds now;\r
115 \r
116         do\r
117         {\r
118                 now.ReadTime();\r
119         }       while ((now.MicroSeconds() - than.MicroSeconds()) < amt);\r
120 }\r
121         \r
122 } // namespace wvNS { \r
123 #endif //#ifndef __UMicroseconds_h__\r