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_AudioProcessLoadMeasurer.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 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26AudioProcessLoadMeasurer::AudioProcessLoadMeasurer() = default;
28
30{
31 reset (0, 0);
32}
33
34void AudioProcessLoadMeasurer::reset (double sampleRate, int blockSize)
35{
36 const SpinLock::ScopedLockType lock (mutex);
37
38 cpuUsageProportion = 0;
39 xruns = 0;
40
41 samplesPerBlock = blockSize;
42 msPerSample = (sampleRate > 0.0 && blockSize > 0) ? 1000.0 / sampleRate : 0;
43}
44
46{
47 const SpinLock::ScopedTryLockType lock (mutex);
48
49 if (lock.isLocked())
50 registerRenderTimeLocked (milliseconds, samplesPerBlock);
51}
52
53void AudioProcessLoadMeasurer::registerRenderTime (double milliseconds, int numSamples)
54{
55 const SpinLock::ScopedTryLockType lock (mutex);
56
57 if (lock.isLocked())
58 registerRenderTimeLocked (milliseconds, numSamples);
59}
60
61void AudioProcessLoadMeasurer::registerRenderTimeLocked (double milliseconds, int numSamples)
62{
63 if (approximatelyEqual (msPerSample, 0.0))
64 return;
65
66 const auto maxMilliseconds = numSamples * msPerSample;
67 const auto usedProportion = milliseconds / maxMilliseconds;
68 const auto filterAmount = 0.2;
69 const auto proportion = cpuUsageProportion.load();
70 cpuUsageProportion = proportion + filterAmount * (usedProportion - proportion);
71
72 if (milliseconds > maxMilliseconds)
73 ++xruns;
74}
75
76double AudioProcessLoadMeasurer::getLoadAsProportion() const { return jlimit (0.0, 1.0, cpuUsageProportion.load()); }
78
79int AudioProcessLoadMeasurer::getXRunCount() const { return xruns; }
80
81AudioProcessLoadMeasurer::ScopedTimer::ScopedTimer (AudioProcessLoadMeasurer& p)
82 : ScopedTimer (p, p.samplesPerBlock)
83{
84}
85
86AudioProcessLoadMeasurer::ScopedTimer::ScopedTimer (AudioProcessLoadMeasurer& p, int numSamplesInBlock)
87 : owner (p), startTime (Time::getMillisecondCounterHiRes()), samplesInBlock (numSamplesInBlock)
88{
89 // numSamplesInBlock should never be zero. Did you remember to call AudioProcessLoadMeasurer::reset(),
90 // passing the expected samples per block?
92}
93
94AudioProcessLoadMeasurer::ScopedTimer::~ScopedTimer()
95{
96 owner.registerRenderTime (Time::getMillisecondCounterHiRes() - startTime, samplesInBlock);
97}
98
99} // namespace juce
Maintains an ongoing measurement of the proportion of time which is being spent inside an audio callb...
~AudioProcessLoadMeasurer()
Destructor.
double getLoadAsPercentage() const
Returns the current load as a percentage 0 to 100.0.
void registerBlockRenderTime(double millisecondsTaken)
Can be called manually to add the time of a callback to the stats.
void registerRenderTime(double millisecondsTaken, int numSamples)
Can be called manually to add the time of a callback to the stats.
int getXRunCount() const
Returns the number of over- (or under-) runs recorded since the state was reset.
double getLoadAsProportion() const
Returns the current load as a proportion 0 to 1.0.
Automatically locks and unlocks a mutex object.
Automatically locks and unlocks a mutex object.
static double getMillisecondCounterHiRes() noexcept
Returns the number of millisecs since a fixed event (usually system startup).
#define jassert(expression)
Platform-independent assertion macro.
T load(T... args)
JUCE Namespace.
constexpr bool approximatelyEqual(Type a, Type b, Tolerance< Type > tolerance=Tolerance< Type >{} .withAbsolute(std::numeric_limits< Type >::min()) .withRelative(std::numeric_limits< Type >::epsilon()))
Returns true if the two floating-point numbers are approximately equal.
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Constrains a value to keep it within a given range.
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