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.

127 lines
4.0KB

  1. /* Copyright 2013-2019 Matt Tytel
  2. *
  3. * vital is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * vital is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with vital. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include "JuceHeader.h"
  18. #include "common.h"
  19. #include "json/json.h"
  20. using json = nlohmann::json;
  21. class LineGenerator {
  22. public:
  23. static constexpr int kMaxPoints = 100;
  24. static constexpr int kDefaultResolution = 2048;
  25. static constexpr int kExtraValues = 3;
  26. static force_inline float smoothTransition(float t) {
  27. return 0.5f * sinf((t - 0.5f) * vital::kPi) + 0.5f;
  28. }
  29. LineGenerator(int resolution = kDefaultResolution);
  30. virtual ~LineGenerator() { }
  31. void setLoop(bool loop) { loop_ = loop; render(); }
  32. void setName(const std::string& name) { name_ = name; }
  33. void setLastBrowsedFile(const std::string& path) { last_browsed_file_ = path; }
  34. void setSmooth(bool smooth) { smooth_ = smooth; checkLineIsLinear(); render(); }
  35. void initLinear();
  36. void initTriangle();
  37. void initSquare();
  38. void initSin();
  39. void initSawUp();
  40. void initSawDown();
  41. void render();
  42. json stateToJson();
  43. static bool isValidJson(json data);
  44. void jsonToState(json data);
  45. float valueAtPhase(float phase);
  46. void checkLineIsLinear();
  47. float getValueBetweenPoints(float x, int index_from, int index_to);
  48. float getValueAtPhase(float phase);
  49. std::string getName() const { return name_; }
  50. std::string getLastBrowsedFile() const { return last_browsed_file_; }
  51. void addPoint(int index, std::pair<float, float> position);
  52. void addMiddlePoint(int index);
  53. void removePoint(int index);
  54. void flipHorizontal();
  55. void flipVertical();
  56. std::pair<float, float> lastPoint() const { return points_[num_points_ - 1]; }
  57. float lastPower() const { return powers_[num_points_ - 1]; }
  58. force_inline int resolution() const { return resolution_; }
  59. force_inline bool linear() const { return linear_; }
  60. force_inline bool smooth() const { return smooth_; }
  61. force_inline vital::mono_float* getBuffer() const { return buffer_.get() + 1; }
  62. force_inline vital::mono_float* getCubicInterpolationBuffer() const { return buffer_.get(); }
  63. force_inline std::pair<float, float> getPoint(int index) const {
  64. VITAL_ASSERT(index < kMaxPoints && index >= 0);
  65. return points_[index];
  66. }
  67. force_inline float getPower(int index) const {
  68. VITAL_ASSERT(index < kMaxPoints && index >= 0);
  69. return powers_[index];
  70. }
  71. force_inline int getNumPoints() const {
  72. return num_points_;
  73. }
  74. force_inline void setPoint(int index, std::pair<float, float> point) {
  75. VITAL_ASSERT(index < kMaxPoints && index >= 0);
  76. points_[index] = point;
  77. checkLineIsLinear();
  78. }
  79. force_inline void setPower(int index, float power) {
  80. VITAL_ASSERT(index < kMaxPoints && index >= 0);
  81. powers_[index] = power;
  82. checkLineIsLinear();
  83. }
  84. force_inline void setNumPoints(int num_points) {
  85. VITAL_ASSERT(num_points <= kMaxPoints && num_points >= 0);
  86. num_points_ = num_points;
  87. checkLineIsLinear();
  88. }
  89. int getRenderCount() const { return render_count_; }
  90. protected:
  91. std::string name_;
  92. std::string last_browsed_file_;
  93. std::pair<float, float> points_[kMaxPoints];
  94. float powers_[kMaxPoints];
  95. int num_points_;
  96. int resolution_;
  97. std::unique_ptr<vital::mono_float[]> buffer_;
  98. bool loop_;
  99. bool smooth_;
  100. bool linear_;
  101. int render_count_;
  102. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LineGenerator)
  103. };