1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 Copyright (c) 2013 Tim Blechmann
5 http://www.audiosynth.com
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #pragma once
23
24 #include "../../common/SC_Lock.h" // for chrono
25 #include "SC_Types.h"
26
27 const int32 kSECONDS_FROM_1900_to_1970 = (int32)2208988800UL; /* 17 leap years */
28 const double kOSCtoSecs = 2.328306436538696e-10;
29
30 const double kSecondsToOSCunits = 4294967296.; // pow(2,32)
31 const double kMicrosToOSCunits = 4294.967296; // pow(2,32)/1e6
32 const double kNanosToOSCunits = 4.294967296; // pow(2,32)/1e9
33
34 typedef std::chrono::system_clock::time_point HostTime;
35
getTime()36 static inline HostTime getTime() { return std::chrono::system_clock::now(); }
37
secondsSinceEpoch(TimePoint const & tp)38 template <typename TimePoint> static inline double secondsSinceEpoch(TimePoint const& tp) {
39 return std::chrono::duration_cast<std::chrono::duration<double>>(tp.time_since_epoch()).count();
40 }
41
OSCTime(TimePoint const & tp)42 template <typename TimePoint> static inline int64 OSCTime(TimePoint const& tp) {
43 using namespace std::chrono;
44 typedef typename TimePoint::duration Duration;
45 Duration sinceEpoch = tp.time_since_epoch();
46 seconds secs = duration_cast<seconds>(sinceEpoch);
47
48 nanoseconds nsecs = sinceEpoch - secs;
49
50 return ((int64)(secs.count() + kSECONDS_FROM_1900_to_1970) << 32) + (int64)(nsecs.count() * kNanosToOSCunits);
51 }
52
timeSeed()53 static inline int32 timeSeed() {
54 using namespace std::chrono;
55 static int32 count = 0;
56
57 typedef high_resolution_clock::time_point TimePoint;
58
59 typedef TimePoint::duration Duration;
60
61 TimePoint now = high_resolution_clock::now();
62 Duration sinceEpoch = now.time_since_epoch();
63 seconds secs = duration_cast<seconds>(sinceEpoch);
64
65 nanoseconds nsecs = sinceEpoch - secs;
66
67 return (int32)secs.count() ^ (int32)nsecs.count() ^ count--;
68 }
69