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.

52 lines
1.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. #include "wavetable_keyframe.h"
  17. #include "utils.h"
  18. #include "wavetable_component.h"
  19. float WavetableKeyframe::linearTween(float point_from, float point_to, float t) {
  20. return vital::utils::interpolate(point_from, point_to, t);
  21. }
  22. float WavetableKeyframe::cubicTween(float point_prev, float point_from, float point_to, float point_next,
  23. float range_prev, float range, float range_next, float t) {
  24. float slope_from = 0.0f;
  25. float slope_to = 0.0f;
  26. if (range_prev > 0.0f)
  27. slope_from = (point_to - point_prev) / (1.0f + range_prev / range);
  28. if (range_next > 0.0f)
  29. slope_to = (point_next - point_from) / (1.0f + range_next / range);
  30. float delta = point_to - point_from;
  31. float movement = linearTween(point_from, point_to, t);
  32. float smooth = t * (1.0f - t) * ((1.0f - t) * (slope_from - delta) + t * (delta - slope_to));
  33. return movement + smooth;
  34. }
  35. int WavetableKeyframe::index() {
  36. return owner()->indexOf(this);
  37. }
  38. json WavetableKeyframe::stateToJson() {
  39. return { { "position", position_ } };
  40. }
  41. void WavetableKeyframe::jsonToState(json data) {
  42. position_ = data["position"];
  43. }