JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_OSCTimeTag.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29const OSCTimeTag OSCTimeTag::immediately;
30
31static const uint64 millisecondsBetweenOscAndJuceEpochs = 2208988800000ULL;
32static const uint64 rawTimeTagRepresentingImmediately = 0x0000000000000001ULL;
33
34//==============================================================================
35OSCTimeTag::OSCTimeTag() noexcept : rawTimeTag (rawTimeTagRepresentingImmediately)
36{
37}
38
39OSCTimeTag::OSCTimeTag (uint64 t) noexcept : rawTimeTag (t)
40{
41}
42
44{
45 const uint64 milliseconds = (uint64) time.toMilliseconds() + millisecondsBetweenOscAndJuceEpochs;
46
47 uint64 seconds = milliseconds / 1000;
48 uint32 fractionalPart = uint32 (4294967.296 * (milliseconds % 1000));
49
50 rawTimeTag = (seconds << 32) + fractionalPart;
51}
52
53//==============================================================================
55{
56 const uint64 seconds = rawTimeTag >> 32;
57 const uint32 fractionalPart = (rawTimeTag & 0x00000000FFFFFFFFULL);
58
59 const auto fractionalPartInMillis = (double) fractionalPart / 4294967.296;
60
61 // now using signed integer, because this is allowed to become negative:
62 const auto juceTimeInMillis = (int64) (seconds * 1000)
64 - (int64) millisecondsBetweenOscAndJuceEpochs;
65
66 return Time (juceTimeInMillis);
67}
68
69//==============================================================================
71{
72 return rawTimeTag == rawTimeTagRepresentingImmediately;
73}
74
75//==============================================================================
76//==============================================================================
77#if JUCE_UNIT_TESTS
78
79class OSCTimeTagTests final : public UnitTest
80{
81public:
83 : UnitTest ("OSCTimeTag class", UnitTestCategories::osc)
84 {}
85
86 void runTest() override
87 {
88 beginTest ("Basics");
89
90 {
91 OSCTimeTag tag;
92 expect (tag.isImmediately());
93 }
94 {
95 OSCTimeTag tag (3535653);
96 expect (! tag.isImmediately());
97
98 OSCTimeTag otherTag;
99 otherTag = tag;
100 expect (! otherTag.isImmediately());
101
102 OSCTimeTag copyTag (tag);
103 expect (! copyTag.isImmediately());
104 }
105
106 beginTest ("Conversion to/from JUCE Time");
107
108 {
109 Time time;
110 OSCTimeTag tag (time);
111 expect (! tag.isImmediately());
112 }
113 {
114 OSCTimeTag tag;
115 Time time = tag.toTime();
116 expect (time < Time::getCurrentTime());
117 }
118 {
120 double deltaInSeconds = 1.234;
121 RelativeTime delta (deltaInSeconds);
122 Time laterTime = currentTime + delta;
123
124 OSCTimeTag currentTimeTag (currentTime);
125 OSCTimeTag laterTimeTag (laterTime);
126
127 uint64 currentTimeTagRaw = currentTimeTag.getRawTimeTag();
128 uint64 laterTimeTagRaw = laterTimeTag.getRawTimeTag();
129
130 // in the raw time tag, the most significant 32 bits are seconds,
131 // so let's verify that the difference is right:
133 double acceptableErrorInSeconds = 0.000001; // definitely not audible anymore.
134
135 expect ((float) diff / float (1ULL << 32) < deltaInSeconds + acceptableErrorInSeconds );
136 expect ((float) diff / float (1ULL << 32) > deltaInSeconds - acceptableErrorInSeconds );
137
138 // round trip:
139
140 Time currentTime2 = currentTimeTag.toTime();
141 Time laterTime2 = laterTimeTag.toTime();
142 RelativeTime delta2 = laterTime2 - currentTime2;
143
144 expect (currentTime2 == currentTime);
145 expect (laterTime2 == laterTime);
146 expect (delta2 == delta);
147 }
148 }
149};
150
152
153#endif
154
155} // namespace juce
static const OSCTimeTag immediately
The special value representing "immediately".
bool isImmediately() const noexcept
Returns true if the OSCTimeTag object has the special value representing "immediately".
Time toTime() const noexcept
Returns a juce::Time object representing the same time as the OSCTimeTag.
OSCTimeTag() noexcept
Default constructor.
Holds an absolute date and time.
Definition juce_Time.h:37
static int64 currentTimeMillis() noexcept
Returns the current system time.
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Returns a Time object that is set to the current system time.
This is a base class for classes that perform a unit test.
typedef double
JUCE Namespace.
unsigned long long uint64
A platform-independent 64-bit unsigned integer type.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
unsigned int uint32
A platform-independent 32-bit unsigned integer type.
int roundToInt(const FloatType value) noexcept
Fast floating-point-to-integer conversion.
long long int64
A platform-independent 64-bit integer type.
time