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.

hylia.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 setStartStopSyncEnabled(const bool enabled)
  56. {
  57. engine.setStartStopSyncEnabled(enabled);
  58. }
  59. void startPlaying()
  60. {
  61. engine.startPlaying();
  62. }
  63. void stopPlaying()
  64. {
  65. engine.stopPlaying();
  66. }
  67. void process(const uint32_t frames, LinkTimeInfo* const info)
  68. {
  69. const std::chrono::microseconds hostTime = hostTimeFilter.sampleTimeToHostTime(sampleTime)
  70. + std::chrono::microseconds(outputLatency);
  71. engine.timelineCallback(hostTime, info);
  72. sampleTime += frames;
  73. }
  74. private:
  75. ableton::Link link;
  76. ableton::link::AudioEngine engine;
  77. ableton::link::HostTimeFilter<ableton::link::platform::Clock> hostTimeFilter;
  78. uint32_t outputLatency, sampleTime;
  79. };
  80. hylia_t* hylia_create(void)
  81. {
  82. HyliaTransport* t;
  83. try {
  84. t = new HyliaTransport();
  85. } catch (...) {
  86. return nullptr;
  87. }
  88. return (hylia_t*)t;
  89. }
  90. void hylia_enable(hylia_t* link, bool on)
  91. {
  92. ((HyliaTransport*)link)->setEnabled(on);
  93. }
  94. void hylia_set_beats_per_bar(hylia_t* link, double beatsPerBar)
  95. {
  96. ((HyliaTransport*)link)->setQuantum(beatsPerBar);
  97. }
  98. void hylia_set_beats_per_minute(hylia_t* link, double beatsPerMinute)
  99. {
  100. ((HyliaTransport*)link)->setTempo(beatsPerMinute);
  101. }
  102. void hylia_set_output_latency(hylia_t* link, uint32_t latency)
  103. {
  104. ((HyliaTransport*)link)->setOutputLatency(latency);
  105. }
  106. void hylia_set_start_stop_sync_enabled(hylia_t* link, bool enabled)
  107. {
  108. ((HyliaTransport*)link)->setStartStopSyncEnabled(enabled);
  109. }
  110. void hylia_start_playing(hylia_t* link)
  111. {
  112. ((HyliaTransport*)link)->startPlaying();
  113. }
  114. void hylia_stop_playing(hylia_t* link)
  115. {
  116. ((HyliaTransport*)link)->stopPlaying();
  117. }
  118. void hylia_process(hylia_t* link, uint32_t frames, hylia_time_info_t* info)
  119. {
  120. ((HyliaTransport*)link)->process(frames, (LinkTimeInfo*)info);
  121. }
  122. void hylia_cleanup(hylia_t* link)
  123. {
  124. delete (HyliaTransport*)link;
  125. }
  126. #include "AudioEngine.cpp"