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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include "AudioEngine.hpp"
  19. #include "ableton/link/HostTimeFilter.hpp"
  20. #include <chrono>
  21. class HyliaTransport {
  22. public:
  23. HyliaTransport()
  24. : link(120.0),
  25. engine(link),
  26. outputLatency(0),
  27. sampleTime(0)
  28. {
  29. }
  30. void setEnabled(const bool enabled)
  31. {
  32. if (enabled)
  33. sampleTime = 0;
  34. link.enable(enabled);
  35. }
  36. void setQuantum(const double quantum)
  37. {
  38. engine.setQuantum(quantum);
  39. }
  40. void setTempo(const double tempo)
  41. {
  42. engine.setTempo(tempo);
  43. }
  44. void setOutputLatency(const uint32_t latency) noexcept
  45. {
  46. outputLatency = latency;
  47. }
  48. void process(const uint32_t frames, LinkTimeInfo* const info)
  49. {
  50. const std::chrono::microseconds hostTime = hostTimeFilter.sampleTimeToHostTime(sampleTime)
  51. + std::chrono::microseconds(outputLatency);
  52. engine.timelineCallback(hostTime, info);
  53. sampleTime += frames;
  54. }
  55. private:
  56. ableton::Link link;
  57. ableton::link::AudioEngine engine;
  58. ableton::link::HostTimeFilter<ableton::link::platform::Clock> hostTimeFilter;
  59. uint32_t outputLatency, sampleTime;
  60. };
  61. hylia_t* hylia_create(void)
  62. {
  63. HyliaTransport* t;
  64. try {
  65. t = new HyliaTransport();
  66. } catch (...) {
  67. return nullptr;
  68. }
  69. return (hylia_t*)t;
  70. }
  71. void hylia_enable(hylia_t* link, bool on)
  72. {
  73. ((HyliaTransport*)link)->setEnabled(on);
  74. }
  75. void hylia_set_beats_per_bar(hylia_t* link, double beatsPerBar)
  76. {
  77. ((HyliaTransport*)link)->setQuantum(beatsPerBar);
  78. }
  79. void hylia_set_beats_per_minute(hylia_t* link, double beatsPerMinute)
  80. {
  81. ((HyliaTransport*)link)->setTempo(beatsPerMinute);
  82. }
  83. void hylia_set_output_latency(hylia_t* link, uint32_t latency)
  84. {
  85. ((HyliaTransport*)link)->setOutputLatency(latency);
  86. }
  87. void hylia_process(hylia_t* link, uint32_t frames, hylia_time_info_t* info)
  88. {
  89. ((HyliaTransport*)link)->process(frames, (LinkTimeInfo*)info);
  90. }
  91. void hylia_cleanup(hylia_t* link)
  92. {
  93. delete (HyliaTransport*)link;
  94. }
  95. #include "AudioEngine.cpp"