Audio plugin host https://kx.studio/carla
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.

128 lines
3.0KB

  1. /*
  2. * This file is part of Hylia.
  3. *
  4. * Hylia is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * Hylia is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with Hylia. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "hylia.h"
  18. #if defined(__clang_major__) && __clang_major__ >= 4
  19. # pragma clang diagnostic push
  20. # pragma clang diagnostic ignored "-Wunused-local-typedef"
  21. #endif
  22. #include "AudioEngine.hpp"
  23. #if defined(__clang_major__) && __clang_major__ >= 4
  24. # pragma clang diagnostic pop
  25. #endif
  26. #include "ableton/link/HostTimeFilter.hpp"
  27. #include <chrono>
  28. class HyliaTransport {
  29. public:
  30. HyliaTransport()
  31. : link(120.0),
  32. engine(link),
  33. outputLatency(0),
  34. sampleTime(0)
  35. {
  36. }
  37. void setEnabled(const bool enabled)
  38. {
  39. if (enabled)
  40. sampleTime = 0;
  41. link.enable(enabled);
  42. }
  43. void setQuantum(const double quantum)
  44. {
  45. engine.setQuantum(quantum);
  46. }
  47. void setTempo(const double tempo)
  48. {
  49. engine.setTempo(tempo);
  50. }
  51. void setOutputLatency(const uint32_t latency) noexcept
  52. {
  53. outputLatency = latency;
  54. }
  55. void process(const uint32_t frames, LinkTimeInfo* const info)
  56. {
  57. const std::chrono::microseconds hostTime = hostTimeFilter.sampleTimeToHostTime(sampleTime)
  58. + std::chrono::microseconds(outputLatency);
  59. engine.timelineCallback(hostTime, info);
  60. sampleTime += frames;
  61. }
  62. private:
  63. ableton::Link link;
  64. ableton::link::AudioEngine engine;
  65. ableton::link::HostTimeFilter<ableton::link::platform::Clock> hostTimeFilter;
  66. uint32_t outputLatency, sampleTime;
  67. };
  68. hylia_t* hylia_create(void)
  69. {
  70. HyliaTransport* t;
  71. try {
  72. t = new HyliaTransport();
  73. } catch (...) {
  74. return nullptr;
  75. }
  76. return (hylia_t*)t;
  77. }
  78. void hylia_enable(hylia_t* link, bool on)
  79. {
  80. ((HyliaTransport*)link)->setEnabled(on);
  81. }
  82. void hylia_set_beats_per_bar(hylia_t* link, double beatsPerBar)
  83. {
  84. ((HyliaTransport*)link)->setQuantum(beatsPerBar);
  85. }
  86. void hylia_set_beats_per_minute(hylia_t* link, double beatsPerMinute)
  87. {
  88. ((HyliaTransport*)link)->setTempo(beatsPerMinute);
  89. }
  90. void hylia_set_output_latency(hylia_t* link, uint32_t latency)
  91. {
  92. ((HyliaTransport*)link)->setOutputLatency(latency);
  93. }
  94. void hylia_process(hylia_t* link, uint32_t frames, hylia_time_info_t* info)
  95. {
  96. ((HyliaTransport*)link)->process(frames, (LinkTimeInfo*)info);
  97. }
  98. void hylia_cleanup(hylia_t* link)
  99. {
  100. delete (HyliaTransport*)link;
  101. }
  102. #include "AudioEngine.cpp"