ControlTrack.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "sekai/ControlTrack.h"
  2. #include <assert.h>
  3. #if 1
  4. //move this logic to sinsy
  5. void ControlTrack::addNote(float start, float length, float f0) {
  6. if (time.size() == 0) {
  7. time.push_back(start);
  8. pitch.push_back(f0);
  9. interp.startPos = start;
  10. interp.endPos = start + length;
  11. time.push_back(start + 0.8 * length);
  12. pitch.push_back(f0);
  13. } else {
  14. time.push_back(start + 0.2 * length);
  15. pitch.push_back(f0);
  16. time.push_back(start + 0.5 * length);
  17. pitch.push_back(f0);
  18. time.push_back(start + 0.8 * length);
  19. pitch.push_back(f0);
  20. interp.endPos = start + length;
  21. }
  22. int count = time.size();
  23. fprintf(stderr,"ControlTrack::addNote() count=%i\n",count);
  24. }
  25. #endif
  26. void ControlTrack::append(float delta)
  27. {
  28. int count = time.size();
  29. float lastf0 = pitch[count - 1];
  30. pitch.push_back(lastf0);
  31. time.push_back(interp.endPos+delta);
  32. interp.endPos= interp.endPos+delta;
  33. count++;
  34. }
  35. void ControlTrack::addPoint(float pos,float f0)
  36. {
  37. if(time.size() == 0)
  38. interp.startPos = pos;
  39. else
  40. interp.endPos= pos;
  41. if(time.size()==0 || pos>lastpos)
  42. {
  43. printf("addPoint %f %f\n",pos,lastpos);
  44. time.push_back(pos);
  45. pitch.push_back(f0);
  46. lastpos=pos;
  47. }
  48. }
  49. void ControlTrack::fix() {
  50. append(0.1);
  51. append(0.1);
  52. append(0.1);
  53. assert(time.size() == pitch.size());
  54. interp.acc = gsl_interp_accel_alloc();
  55. interp.cspline = gsl_spline_alloc(gsl_interp_cspline, time.size());
  56. gsl_spline_init(interp.cspline, time.data(), pitch.data(), time.size());
  57. }
  58. float ControlTrack::getF0atTime(float time) {
  59. if (time >= interp.startPos && time <= interp.endPos) {
  60. return gsl_spline_eval(interp.cspline, (double)time, interp.acc);
  61. }
  62. return 0.0;
  63. }
  64. float ControlTrack::getDynamicsAtTime(float time)
  65. {
  66. for(const auto& d: dyn) {
  67. if(time>=d.start && time<d.end)
  68. {
  69. float tmp = (time - d.start) / (d.end - d.start);
  70. return d.dyn0 * (1 - tmp) + d.dyn1 * tmp;
  71. }
  72. }
  73. return 1.0;
  74. }
  75. float ControlTrack::getLength() { return interp.endPos; }