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.

79 lines
2.6KB

  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 "wavetable_keyframe.h"
  19. #include "wavetable_component_factory.h"
  20. #include "json/json.h"
  21. using json = nlohmann::json;
  22. namespace vital {
  23. class WaveFrame;
  24. } // namespace vital
  25. class WavetableComponent {
  26. public:
  27. enum InterpolationStyle {
  28. kNone,
  29. kLinear,
  30. kCubic,
  31. kNumInterpolationStyles
  32. };
  33. WavetableComponent() : interpolation_style_(kLinear) { }
  34. virtual ~WavetableComponent() { }
  35. virtual WavetableKeyframe* createKeyframe(int position) = 0;
  36. virtual void render(vital::WaveFrame* wave_frame, float position) = 0;
  37. virtual WavetableComponentFactory::ComponentType getType() = 0;
  38. virtual json stateToJson();
  39. virtual void jsonToState(json data);
  40. virtual void prerender() { }
  41. virtual bool hasKeyframes() { return true; }
  42. void reset();
  43. void interpolate(WavetableKeyframe* dest, float position);
  44. WavetableKeyframe* insertNewKeyframe(int position);
  45. void reposition(WavetableKeyframe* keyframe);
  46. void remove(WavetableKeyframe* keyframe);
  47. inline int numFrames() const { return static_cast<int>(keyframes_.size()); }
  48. inline int indexOf(WavetableKeyframe* keyframe) const {
  49. for (int i = 0; i < keyframes_.size(); ++i) {
  50. if (keyframes_[i].get() == keyframe)
  51. return i;
  52. }
  53. return -1;
  54. }
  55. inline WavetableKeyframe* getFrameAt(int index) const { return keyframes_[index].get(); }
  56. int getIndexFromPosition(int position) const;
  57. WavetableKeyframe* getFrameAtPosition(int position);
  58. int getLastKeyframePosition();
  59. void setInterpolationStyle(InterpolationStyle type) { interpolation_style_ = type; }
  60. InterpolationStyle getInterpolationStyle() const { return interpolation_style_; }
  61. protected:
  62. std::vector<std::unique_ptr<WavetableKeyframe>> keyframes_;
  63. InterpolationStyle interpolation_style_;
  64. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(WavetableComponent)
  65. };