Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. AudioProcessLoadMeasurer::AudioProcessLoadMeasurer() = default;
  20. AudioProcessLoadMeasurer::~AudioProcessLoadMeasurer() = default;
  21. void AudioProcessLoadMeasurer::reset()
  22. {
  23. reset (0, 0);
  24. }
  25. void AudioProcessLoadMeasurer::reset (double sampleRate, int blockSize)
  26. {
  27. const SpinLock::ScopedLockType lock (mutex);
  28. cpuUsageProportion = 0;
  29. xruns = 0;
  30. samplesPerBlock = blockSize;
  31. msPerSample = (sampleRate > 0.0 && blockSize > 0) ? 1000.0 / sampleRate : 0;
  32. }
  33. void AudioProcessLoadMeasurer::registerBlockRenderTime (double milliseconds)
  34. {
  35. const SpinLock::ScopedTryLockType lock (mutex);
  36. if (lock.isLocked())
  37. registerRenderTimeLocked (milliseconds, samplesPerBlock);
  38. }
  39. void AudioProcessLoadMeasurer::registerRenderTime (double milliseconds, int numSamples)
  40. {
  41. const SpinLock::ScopedTryLockType lock (mutex);
  42. if (lock.isLocked())
  43. registerRenderTimeLocked (milliseconds, numSamples);
  44. }
  45. void AudioProcessLoadMeasurer::registerRenderTimeLocked (double milliseconds, int numSamples)
  46. {
  47. if (msPerSample == 0)
  48. return;
  49. const auto maxMilliseconds = numSamples * msPerSample;
  50. const auto usedProportion = milliseconds / maxMilliseconds;
  51. const auto filterAmount = 0.2;
  52. const auto proportion = cpuUsageProportion.load();
  53. cpuUsageProportion = proportion + filterAmount * (usedProportion - proportion);
  54. if (milliseconds > maxMilliseconds)
  55. ++xruns;
  56. }
  57. double AudioProcessLoadMeasurer::getLoadAsProportion() const { return jlimit (0.0, 1.0, cpuUsageProportion.load()); }
  58. double AudioProcessLoadMeasurer::getLoadAsPercentage() const { return 100.0 * getLoadAsProportion(); }
  59. int AudioProcessLoadMeasurer::getXRunCount() const { return xruns; }
  60. AudioProcessLoadMeasurer::ScopedTimer::ScopedTimer (AudioProcessLoadMeasurer& p)
  61. : ScopedTimer (p, p.samplesPerBlock)
  62. {
  63. }
  64. AudioProcessLoadMeasurer::ScopedTimer::ScopedTimer (AudioProcessLoadMeasurer& p, int numSamplesInBlock)
  65. : owner (p), startTime (Time::getMillisecondCounterHiRes()), samplesInBlock (numSamplesInBlock)
  66. {
  67. // numSamplesInBlock should never be zero. Did you remember to call AudioProcessLoadMeasurer::reset(),
  68. // passing the expected samples per block?
  69. jassert (numSamplesInBlock);
  70. }
  71. AudioProcessLoadMeasurer::ScopedTimer::~ScopedTimer()
  72. {
  73. owner.registerRenderTime (Time::getMillisecondCounterHiRes() - startTime, samplesInBlock);
  74. }
  75. } // namespace juce