OpenShot Audio Library | OpenShotAudio  0.6.0
juce_AudioProcessLoadMeasurer.cpp
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 
23 namespace juce
24 {
25 
26 AudioProcessLoadMeasurer::AudioProcessLoadMeasurer() = default;
28 
30 {
31  reset (0, 0);
32 }
33 
34 void 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 
53 void AudioProcessLoadMeasurer::registerRenderTime (double milliseconds, int numSamples)
54 {
55  const SpinLock::ScopedTryLockType lock (mutex);
56 
57  if (lock.isLocked())
58  registerRenderTimeLocked (milliseconds, numSamples);
59 }
60 
61 void 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 
76 double AudioProcessLoadMeasurer::getLoadAsProportion() const { return jlimit (0.0, 1.0, cpuUsageProportion.load()); }
78 
79 int AudioProcessLoadMeasurer::getXRunCount() const { return xruns; }
80 
81 AudioProcessLoadMeasurer::ScopedTimer::ScopedTimer (AudioProcessLoadMeasurer& p)
82  : ScopedTimer (p, p.samplesPerBlock)
83 {
84 }
85 
86 AudioProcessLoadMeasurer::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?
91  jassert (numSamplesInBlock);
92 }
93 
94 AudioProcessLoadMeasurer::ScopedTimer::~ScopedTimer()
95 {
96  owner.registerRenderTime (Time::getMillisecondCounterHiRes() - startTime, samplesInBlock);
97 }
98 
99 } // namespace juce
void registerBlockRenderTime(double millisecondsTaken)
void registerRenderTime(double millisecondsTaken, int numSamples)
bool isLocked() const noexcept
static double getMillisecondCounterHiRes() noexcept