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.

161 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the dRowAudio JUCE module
  4. Copyright 2004-13 by dRowAudio.
  5. ------------------------------------------------------------------------------
  6. dRowAudio is provided under the terms of The MIT License (MIT):
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. ==============================================================================
  23. */
  24. #ifndef __DROWAUDIO_SEGMENTEDMETER_H__
  25. #define __DROWAUDIO_SEGMENTEDMETER_H__
  26. #include "dRowAudio_GraphicalComponent.h"
  27. #include "../utility/dRowAudio_StateVariable.h"
  28. //==============================================================================
  29. /** A segmented graphical VU meter.
  30. This class is a very efficient way of creating meters as it will only repaint itself when
  31. necessarry and does all its processing on a shared background thread.
  32. It is very customisable letting you set the number of each segments, how many decibels
  33. each segment represents and the colours of the segments.
  34. To use one, register it with a TimeSliceThread and then in your audio callback
  35. push some values to it with copyValues(...).
  36. Eg. in your Component
  37. @code
  38. graphicalManager = new TimeSliceThread();
  39. graphicalManager->startThread (2);
  40. addAndMakeVisible (meter = new SegmentedMeter());
  41. graphicalManager->addTimeSliceClient (meter);
  42. @endcode
  43. and in your audioCallback
  44. @code
  45. if (currentMeter != nullptr) {
  46. currentMeter->copyValues (outputChannelData[0], numSamples);
  47. }
  48. @endcode
  49. */
  50. class SegmentedMeter : public GraphicalComponent
  51. {
  52. public:
  53. //==============================================================================
  54. /** Creates a SegmentedMeter.
  55. Initially this will do nothing as you need to register it with a
  56. TimeSliceThread then push some values to it with copyValues().
  57. */
  58. SegmentedMeter();
  59. /** Destructor.
  60. */
  61. ~SegmentedMeter();
  62. /** Calculates the number of segments currently required and triggers a repaint if necessary.
  63. */
  64. void calculateSegments();
  65. /** Returns the total number of segments the meter has.
  66. */
  67. int getTotalNumSegments() { return totalNumSegs; }
  68. /** Returns the number of segments that represent the over level.
  69. */
  70. int getNumOverSegments() { return numRedSeg; }
  71. /** Returns the total number of segments that represent the warning level.
  72. */
  73. int getNumWarningSegments() { return numYellowSeg; }
  74. /** Returns the total number of segments that represent the safe level.
  75. */
  76. int getNumSafeSegments() { return numGreenSeg; }
  77. /** Returns the number of decibels each segment represents.
  78. */
  79. float getNumDbPerSegment() { return decibelsPerSeg; }
  80. /** Sets the number of decibels each segment represents.
  81. */
  82. void setNumDecibelsPerSeg (float numDecibelsPerSegment)
  83. {
  84. decibelsPerSeg = numDecibelsPerSegment;
  85. }
  86. /** Forces the meter to repaint itself.
  87. You may need to use this if a container component moves without moving
  88. or resizing its parent directly, eg. if you are housing your component
  89. in a tabbed component.
  90. */
  91. void flagForRepaint()
  92. {
  93. needsRepaint = true;
  94. repaint();
  95. }
  96. //==============================================================================
  97. /** Draws the meter. */
  98. void paint (Graphics &g);
  99. /** @internal
  100. */
  101. void timerCallback();
  102. /** @internal
  103. */
  104. void resized();
  105. /** @internal
  106. */
  107. void moved()
  108. {
  109. needsRepaint = true;
  110. }
  111. /** Processes the channel data for the value to display.
  112. */
  113. virtual void process();
  114. private:
  115. //==============================================================================
  116. int numRedSeg, numYellowSeg, numGreenSeg, totalNumSegs;
  117. float decibelsPerSeg;
  118. StateVariable<int> numSegs;
  119. int sampleCount, samplesToCount;
  120. float sampleMax;
  121. StateVariable<float> level;
  122. bool needsRepaint;
  123. Image onImage, offImage;
  124. //==============================================================================
  125. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SegmentedMeter);
  126. };
  127. #endif //__DROWAUDIO_SEGMENTEDMETER_H__