SaSpectrumView.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* SaSpectrumView.h - declaration of SaSpectrumView class.
  2. *
  3. * Copyright (c) 2019 Martin Pavelek <he29/dot/HS/at/gmail/dot/com>
  4. *
  5. * Based partially on Eq plugin code,
  6. * Copyright (c) 2014 David French <dave/dot/french3/at/googlemail/dot/com>
  7. *
  8. * This file is part of LMMS - https://lmms.io
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program (see COPYING); if not, write to the
  22. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. * Boston, MA 02110-1301 USA.
  24. *
  25. */
  26. #ifndef SASPECTRUMVIEW_H
  27. #define SASPECTRUMVIEW_H
  28. #include <string>
  29. #include <utility>
  30. #include <QPainterPath>
  31. #include <QWidget>
  32. class QMouseEvent;
  33. class QPainter;
  34. class SaControls;
  35. class SaProcessor;
  36. //! Widget that displays a spectrum curve and frequency / amplitude grid
  37. class SaSpectrumView : public QWidget
  38. {
  39. Q_OBJECT
  40. public:
  41. explicit SaSpectrumView(SaControls *controls, SaProcessor *processor, QWidget *_parent = 0);
  42. virtual ~SaSpectrumView() {}
  43. QSize sizeHint() const override {return QSize(400, 200);}
  44. protected:
  45. void paintEvent(QPaintEvent *event) override;
  46. void mouseMoveEvent(QMouseEvent *event) override;
  47. void mousePressEvent(QMouseEvent *event) override;
  48. void resizeEvent(QResizeEvent *event) override;
  49. private slots:
  50. void periodicUpdate();
  51. private:
  52. const SaControls *m_controls;
  53. SaProcessor *m_processor;
  54. // grid labels (position, label) and methods to generate them
  55. std::vector<std::pair<int, std::string>> m_logFreqTics; // 10-20-50... Hz
  56. std::vector<std::pair<int, std::string>> m_linearFreqTics; // 2k-4k-6k... Hz
  57. std::vector<std::pair<float, std::string>> m_logAmpTics; // dB
  58. std::vector<std::pair<float, std::string>> m_linearAmpTics; // 0..1
  59. std::vector<std::pair<int, std::string>> makeLogFreqTics(int low, int high);
  60. std::vector<std::pair<int, std::string>> makeLinearFreqTics(int low, int high);
  61. std::vector<std::pair<float, std::string>> makeLogAmpTics(int low, int high);
  62. std::vector<std::pair<float, std::string>> makeLinearAmpTics(int low, int high);
  63. // currently selected ranges (see SaControls.h for enum definitions)
  64. int m_freqRangeIndex;
  65. int m_ampRangeIndex;
  66. // draw the grid and all labels based on selected ranges
  67. void drawGrid(QPainter &painter);
  68. // local buffers for frequency bin values and a method to update them
  69. // (mainly needed for averaging and to keep track of peak values)
  70. std::vector<float> m_displayBufferL;
  71. std::vector<float> m_displayBufferR;
  72. std::vector<float> m_peakBufferL;
  73. std::vector<float> m_peakBufferR;
  74. void updateBuffers(float *spectrum, float *displayBuffer, float *peakBuffer);
  75. // final paths to be drawn by QPainter and methods to build them
  76. QPainterPath m_pathL;
  77. QPainterPath m_pathR;
  78. QPainterPath m_pathPeakL;
  79. QPainterPath m_pathPeakR;
  80. void refreshPaths();
  81. QPainterPath makePath(std::vector<float> &displayBuffer, float resolution);
  82. // helper variables for path drawing
  83. float m_decaySum; // indicates if there is anything left to draw
  84. bool m_freezeRequest; // new reference should be acquired
  85. bool m_frozen; // a reference is currently stored in the peakBuffer
  86. const float m_smoothFactor = 0.15; // alpha for exponential smoothing
  87. const float m_peakDecayFactor = 0.992; // multiplier for gradual peak decay
  88. // top level: refresh buffers, make paths and draw the spectrum
  89. void drawSpectrum(QPainter &painter);
  90. // current cursor location and a method to draw it
  91. QPoint m_cursor;
  92. void drawCursor(QPainter &painter);
  93. // wrappers for most used SaProcessor conversion helpers
  94. // (to make local code more readable)
  95. float binToFreq(unsigned int bin_index);
  96. float freqToXPixel(float frequency, unsigned int width);
  97. float ampToYPixel(float amplitude, unsigned int height);
  98. // current boundaries for drawing
  99. unsigned int m_displayTop;
  100. unsigned int m_displayBottom;
  101. unsigned int m_displayLeft;
  102. unsigned int m_displayRight;
  103. unsigned int m_displayWidth;
  104. };
  105. #endif // SASPECTRUMVIEW_H