The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

93 lines
3.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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. cpuUsageProportion = 0;
  28. xruns = 0;
  29. samplesPerBlock = blockSize;
  30. if (sampleRate > 0.0 && blockSize > 0)
  31. {
  32. msPerSample = 1000.0 / sampleRate;
  33. timeToCpuScale = (msPerSample > 0.0) ? (1.0 / msPerSample) : 0.0;
  34. }
  35. else
  36. {
  37. msPerSample = 0;
  38. timeToCpuScale = 0;
  39. }
  40. }
  41. void AudioProcessLoadMeasurer::registerBlockRenderTime (double milliseconds)
  42. {
  43. registerRenderTime (milliseconds, samplesPerBlock);
  44. }
  45. void AudioProcessLoadMeasurer::registerRenderTime (double milliseconds, int numSamples)
  46. {
  47. const auto maxMilliseconds = numSamples * msPerSample;
  48. const auto usedProportion = milliseconds / maxMilliseconds;
  49. const auto filterAmount = 0.2;
  50. cpuUsageProportion += filterAmount * (usedProportion - cpuUsageProportion);
  51. if (milliseconds > maxMilliseconds)
  52. ++xruns;
  53. }
  54. double AudioProcessLoadMeasurer::getLoadAsProportion() const { return jlimit (0.0, 1.0, cpuUsageProportion); }
  55. double AudioProcessLoadMeasurer::getLoadAsPercentage() const { return 100.0 * getLoadAsProportion(); }
  56. int AudioProcessLoadMeasurer::getXRunCount() const { return xruns; }
  57. AudioProcessLoadMeasurer::ScopedTimer::ScopedTimer (AudioProcessLoadMeasurer& p)
  58. : ScopedTimer (p, p.samplesPerBlock)
  59. {
  60. }
  61. AudioProcessLoadMeasurer::ScopedTimer::ScopedTimer (AudioProcessLoadMeasurer& p, int numSamplesInBlock)
  62. : owner (p), startTime (Time::getMillisecondCounterHiRes()), samplesInBlock (numSamplesInBlock)
  63. {
  64. // numSamplesInBlock should never be zero. Did you remember to call AudioProcessLoadMeasurer::reset(),
  65. // passing the expected samples per block?
  66. jassert (numSamplesInBlock);
  67. }
  68. AudioProcessLoadMeasurer::ScopedTimer::~ScopedTimer()
  69. {
  70. owner.registerRenderTime (Time::getMillisecondCounterHiRes() - startTime, samplesInBlock);
  71. }
  72. } // namespace juce