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.

128 lines
3.7KB

  1. #include "Visualisation.h"
  2. Visualisation::Visualisation (const RefineDsp& dsp)
  3. : sepDsp (dsp),
  4. minMag (-30.f),
  5. maxMag (0.f)
  6. {
  7. setOpaque(true);
  8. startTimer(100);
  9. }
  10. Visualisation::~Visualisation()
  11. {
  12. }
  13. void Visualisation::paint (Graphics& g)
  14. {
  15. g.fillAll(Colour(0xFF101010));
  16. const int w = jmin(rmsData.size(), colourData.size(), getWidth());
  17. const float yBottom = getHeight() - 1.f;
  18. for (int i = 0; i<w; ++i)
  19. {
  20. const int idx = w - 1 - i;
  21. const float mag = jlimit(0.f, 1.f, rmsData.getReference(idx));
  22. const float y = magToY(mag);
  23. g.setColour(Colour(colourData.getReference(idx)));
  24. g.drawVerticalLine(i, y, yBottom);
  25. }
  26. }
  27. void Visualisation::resized()
  28. {
  29. const int w = getWidth();
  30. rmsData.clearQuick();
  31. colourData.clearQuick();
  32. rmsData.ensureStorageAllocated(w);
  33. colourData.ensureStorageAllocated(w);
  34. for (int i = 0; i < w; ++i)
  35. {
  36. rmsData.add(0.f);
  37. colourData.add(0);
  38. }
  39. }
  40. void Visualisation::timerCallback()
  41. {
  42. if (sepDsp.getRmsData(rmsData, colourData))
  43. {
  44. float newMin = 10;
  45. float newMax = 0;
  46. float newMean = 0;
  47. int meanSize = 0;
  48. const Colour red(191, 0, 48);
  49. const Colour green(0, 191, 44);
  50. const Colour blue(0, 96, 191);
  51. for (int i = 0; i<rmsData.size(); ++i)
  52. {
  53. if (rmsData.getReference(i) < 1e-8f)
  54. {
  55. rmsData.getReference(i) = -80.f;
  56. continue;
  57. }
  58. newMean += rmsData.getReference(i);
  59. ++meanSize;
  60. const float curMean = newMean / meanSize;
  61. const float x = rmsData.getReference(i);
  62. const float weight = 0.2f + 0.8f * sqrt(1.f - i / (rmsData.size() - 1.f));
  63. const float xMin = curMean - weight * (curMean - x);
  64. const float xMax = curMean + weight * (x - curMean);
  65. newMin = jmin(newMin, xMin);
  66. newMax = jmax(newMax, xMax);
  67. rmsData.getReference(i) = 10 * log10(rmsData.getReference(i));
  68. {
  69. const float mulBlue = ((colourData.getReference(i) >> 16) & 0xFF) / 255.f;
  70. const float mulGreen = ((colourData.getReference(i) >> 8) & 0xFF) / 255.f;
  71. const float mulRed = ((colourData.getReference(i)) & 0xFF) / 255.f;
  72. uint8_t r = uint8_t(blue.getRed()*mulBlue + green.getRed()*mulGreen + red.getRed()*mulRed);
  73. uint8_t g = uint8_t(blue.getGreen()*mulBlue + green.getGreen()*mulGreen + red.getGreen()*mulRed);
  74. uint8_t b = uint8_t(blue.getBlue()*mulBlue + green.getBlue()*mulGreen + red.getBlue()*mulRed);
  75. r = 64 + r / 2;
  76. g = 64 + g / 2;
  77. b = 64 + b / 2;
  78. colourData.getReference(i) = (255 << 24) | (r << 16) | (g << 8) | (b << 0);
  79. }
  80. }
  81. if (newMin < 10 && newMin > 0)
  82. {
  83. newMean /= meanSize;
  84. const float dist = jmax(0.01f, jmin(newMax - newMean, newMean - newMin));
  85. newMin = jmax(1e-8f, jmin(0.9f*newMin, newMean - dist));
  86. newMax = jmax(1.1f*newMax, newMean + dist);
  87. const float minDb = 10 * std::log10(newMin);
  88. const float maxDb = 10 * std::log10(newMax);
  89. minMag = minMag*0.95f + 0.05f*minDb;
  90. maxMag = maxMag*0.95f + 0.05f*maxDb;
  91. }
  92. for (int i = 0; i<rmsData.size(); ++i)
  93. rmsData.getReference(i) = jlimit(0.f, 1.f, float((rmsData.getReference(i) - minMag) / (maxMag - minMag)));
  94. repaint();
  95. }
  96. }
  97. float Visualisation::magToY (float mag) const
  98. {
  99. return 1.f + (getHeight() - 2.f) * (1.f - mag);
  100. }