OpenShot Audio Library | OpenShotAudio: juce_AudioProcessLoadMeasurer.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
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
23namespace juce
24{
25
26AudioProcessLoadMeasurer::AudioProcessLoadMeasurer() = default;
27 AudioProcessLoadMeasurer::~AudioProcessLoadMeasurer() = default;
28
29 void AudioProcessLoadMeasurer::reset()
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
45 void AudioProcessLoadMeasurer::registerBlockRenderTime (double milliseconds)
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
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
76 double AudioProcessLoadMeasurer::getLoadAsProportion() const { return jlimit (0.0, 1.0, cpuUsageProportion.load()); }
77 double AudioProcessLoadMeasurer::getLoadAsPercentage() const { return 100.0 * getLoadAsProportion(); }
78
79 int 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?
91 jassert (numSamplesInBlock);
92}
93
94AudioProcessLoadMeasurer::ScopedTimer::~ScopedTimer()
95{
96 owner.registerRenderTime (Time::getMillisecondCounterHiRes() - startTime, samplesInBlock);
97}
98
99} // namespace juce
juce::AudioProcessLoadMeasurer
Definition juce_AudioProcessLoadMeasurer.h:34
juce::AudioProcessLoadMeasurer::~AudioProcessLoadMeasurer
~AudioProcessLoadMeasurer()
juce::AudioProcessLoadMeasurer::getLoadAsPercentage
double getLoadAsPercentage() const
Definition juce_AudioProcessLoadMeasurer.cpp:77
juce::AudioProcessLoadMeasurer::registerBlockRenderTime
void registerBlockRenderTime(double millisecondsTaken)
Definition juce_AudioProcessLoadMeasurer.cpp:45
juce::AudioProcessLoadMeasurer::registerRenderTime
void registerRenderTime(double millisecondsTaken, int numSamples)
Definition juce_AudioProcessLoadMeasurer.cpp:53
juce::AudioProcessLoadMeasurer::getXRunCount
int getXRunCount() const
Definition juce_AudioProcessLoadMeasurer.cpp:79
juce::AudioProcessLoadMeasurer::reset
void reset()
Definition juce_AudioProcessLoadMeasurer.cpp:29
juce::AudioProcessLoadMeasurer::getLoadAsProportion
double getLoadAsProportion() const
Definition juce_AudioProcessLoadMeasurer.cpp:76
juce::GenericScopedLock
Definition juce_ScopedLock.h:55
juce::GenericScopedTryLock
Definition juce_ScopedLock.h:198
juce::GenericScopedTryLock::isLocked
bool isLocked() const noexcept
Definition juce_ScopedLock.h:231
juce::Time::getMillisecondCounterHiRes
static double getMillisecondCounterHiRes() noexcept