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.

98 lines
3.8KB

  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_component.h"
  19. #include "wave_frame.h"
  20. class WaveSourceKeyframe;
  21. class WaveSource : public WavetableComponent {
  22. public:
  23. enum InterpolationMode {
  24. kTime,
  25. kFrequency
  26. };
  27. WaveSource();
  28. virtual ~WaveSource();
  29. virtual WavetableKeyframe* createKeyframe(int position) override;
  30. virtual void render(vital::WaveFrame* wave_frame, float position) override;
  31. virtual WavetableComponentFactory::ComponentType getType() override;
  32. virtual json stateToJson() override;
  33. virtual void jsonToState(json data) override;
  34. vital::WaveFrame* getWaveFrame(int index);
  35. WaveSourceKeyframe* getKeyframe(int index);
  36. void setInterpolationMode(InterpolationMode mode) { interpolation_mode_ = mode; }
  37. InterpolationMode getInterpolationMode() const { return interpolation_mode_; }
  38. protected:
  39. std::unique_ptr<WaveSourceKeyframe> compute_frame_;
  40. InterpolationMode interpolation_mode_;
  41. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(WaveSource)
  42. };
  43. class WaveSourceKeyframe : public WavetableKeyframe {
  44. public:
  45. WaveSourceKeyframe() : interpolation_mode_(WaveSource::kFrequency) {
  46. wave_frame_ = std::make_unique<vital::WaveFrame>();
  47. }
  48. virtual ~WaveSourceKeyframe() { }
  49. vital::WaveFrame* wave_frame() { return wave_frame_.get(); }
  50. void copy(const WavetableKeyframe* keyframe) override;
  51. void linearTimeInterpolate(const vital::WaveFrame* from, const vital::WaveFrame* to, float t);
  52. void cubicTimeInterpolate(const vital::WaveFrame* prev, const vital::WaveFrame* from,
  53. const vital::WaveFrame* to, const vital::WaveFrame* next,
  54. float range_prev, float range, float range_next, float t);
  55. void linearFrequencyInterpolate(const vital::WaveFrame* from, const vital::WaveFrame* to, float t);
  56. void cubicFrequencyInterpolate(const vital::WaveFrame* prev, const vital::WaveFrame* from,
  57. const vital::WaveFrame* to, const vital::WaveFrame* next,
  58. float range_prev, float range, float range_next, float t);
  59. void interpolate(const WavetableKeyframe* from_keyframe,
  60. const WavetableKeyframe* to_keyframe, float t) override;
  61. void smoothInterpolate(const WavetableKeyframe* prev_keyframe,
  62. const WavetableKeyframe* from_keyframe,
  63. const WavetableKeyframe* to_keyframe,
  64. const WavetableKeyframe* next_keyframe, float t) override;
  65. void render(vital::WaveFrame* wave_frame) override {
  66. wave_frame->copy(wave_frame_.get());
  67. }
  68. json stateToJson() override;
  69. void jsonToState(json data) override;
  70. void setInterpolationMode(WaveSource::InterpolationMode mode) { interpolation_mode_ = mode; }
  71. WaveSource::InterpolationMode getInterpolationMode() const { return interpolation_mode_; }
  72. protected:
  73. std::unique_ptr<vital::WaveFrame> wave_frame_;
  74. WaveSource::InterpolationMode interpolation_mode_;
  75. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(WaveSourceKeyframe)
  76. };