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.

140 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCETICE project - Copyright 2009 by Lucio Asnaghi.
  4. JUCETICE is based around the JUCE library - "Jules' Utility Class Extensions"
  5. Copyright 2007 by Julian Storer.
  6. ------------------------------------------------------------------------------
  7. JUCE and JUCETICE can be redistributed and/or modified under the terms of
  8. the GNU General Public License, as published by the Free Software Foundation;
  9. either version 2 of the License, or (at your option) any later version.
  10. JUCE and JUCETICE are distributed in the hope that they will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with JUCE and JUCETICE; if not, visit www.gnu.org/licenses or write to
  16. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. Boston, MA 02111-1307 USA
  18. ==============================================================================
  19. @author Rui Nuno Capela
  20. @tweaker Lucio Asnaghi
  21. ==============================================================================
  22. */
  23. BEGIN_JUCE_NAMESPACE
  24. //==============================================================================
  25. /*
  26. // Meter level limits (in dB).
  27. #define HQ_METER_MAXDB (+3.0f)
  28. #define HQ_METER_MINDB (-70.0f)
  29. // The decay rates (magic goes here :).
  30. // - value decay rate (faster)
  31. #define HQ_METER_DECAY_RATE1 (1.0f - 3E-2f)
  32. // - peak decay rate (slower)
  33. #define HQ_METER_DECAY_RATE2 (1.0f - 3E-6f)
  34. // Number of cycles the peak stays on hold before fall-off.
  35. #define HQ_METER_PEAK_FALLOFF 16
  36. */
  37. //==============================================================================
  38. DecibelScaleComponent::DecibelScaleComponent ()
  39. : font (7.0f, Font::plain),
  40. scale (0.0f),
  41. lastY (0)
  42. {
  43. zeromem (levels, sizeof (int) * LevelCount);
  44. }
  45. DecibelScaleComponent::~DecibelScaleComponent ()
  46. {
  47. }
  48. void DecibelScaleComponent::paint (Graphics& g)
  49. {
  50. g.setFont (font);
  51. g.setColour (Colours::black);
  52. lastY = 0;
  53. drawLabel (g, iecLevel (Level0dB), "0");
  54. drawLabel (g, iecLevel (Level3dB), "3");
  55. drawLabel (g, iecLevel (Level6dB), "6");
  56. drawLabel (g, iecLevel (Level10dB), "10");
  57. for (float dB = -20.0f; dB > -70.0f; dB -= 10.0f)
  58. drawLabel (g, DecibelScaleComponent::iecScale (dB), String ((int) -dB));
  59. }
  60. void DecibelScaleComponent::resized ()
  61. {
  62. scale = 0.85f * getHeight();
  63. levels [Level0dB] = iecScale( 0.0f);
  64. levels [Level3dB] = iecScale( -3.0f);
  65. levels [Level6dB] = iecScale( -6.0f);
  66. levels [Level10dB] = iecScale(-10.0f);
  67. }
  68. void DecibelScaleComponent::drawLabel (Graphics& g, const int y, const String& label)
  69. {
  70. int iCurrY = getHeight() - y;
  71. int iWidth = getWidth();
  72. int iMidHeight = (int) (font.getHeight () * 0.5f);
  73. if (font.getStringWidth (label) < iWidth - 5)
  74. {
  75. g.drawLine (0, iCurrY, 2, iCurrY);
  76. g.drawLine (iWidth - 3, iCurrY, iWidth - 1, iCurrY);
  77. }
  78. if (iCurrY < iMidHeight || iCurrY > lastY + iMidHeight)
  79. {
  80. g.drawText (label,
  81. 2, iCurrY - iMidHeight, iWidth - 3, (int) font.getHeight (),
  82. Justification::centred,
  83. false);
  84. lastY = iCurrY + 1;
  85. }
  86. }
  87. int DecibelScaleComponent::iecScale (const float dB) const
  88. {
  89. float fDef = 1.0;
  90. if (dB < -70.0)
  91. fDef = 0.0;
  92. else if (dB < -60.0)
  93. fDef = (dB + 70.0) * 0.0025;
  94. else if (dB < -50.0)
  95. fDef = (dB + 60.0) * 0.005 + 0.025;
  96. else if (dB < -40.0)
  97. fDef = (dB + 50.0) * 0.0075 + 0.075;
  98. else if (dB < -30.0)
  99. fDef = (dB + 40.0) * 0.015 + 0.15;
  100. else if (dB < -20.0)
  101. fDef = (dB + 30.0) * 0.02 + 0.3;
  102. else // if (dB < 0.0)
  103. fDef = (dB + 20.0) * 0.025 + 0.5;
  104. return (int) (fDef * scale);
  105. }
  106. int DecibelScaleComponent::iecLevel (const int index) const
  107. {
  108. return levels [index];
  109. }
  110. END_JUCE_NAMESPACE