Source.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Copyright 2025, Ableton AG, Berlin. All rights reserved.
  2. *
  3. * This program 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 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * If you would like to incorporate Link into a proprietary software application,
  17. * please contact <link-devs@ableton.com>.
  18. */
  19. #pragma once
  20. #include <ableton/link_audio/Buffer.hpp>
  21. #include <ableton/link_audio/Id.hpp>
  22. #include <ableton/link_audio/Queue.hpp>
  23. #include <ableton/util/Locked.hpp>
  24. #include <atomic>
  25. #include <optional>
  26. namespace ableton
  27. {
  28. namespace link_audio
  29. {
  30. static constexpr auto kSourceQueueSize = std::chrono::milliseconds(10000);
  31. struct Source
  32. {
  33. using Callback = std::function<void(BufferCallbackHandle<Buffer<int16_t>>)>;
  34. Source(Id id, Callback callback)
  35. : mId(std::move(id))
  36. , mCallback(std::move(callback))
  37. {
  38. }
  39. const Id& id() const { return mId; }
  40. void setCallback(Callback newCallback)
  41. {
  42. mCallback.update([newCallback_ = std::move(newCallback)](auto& callback)
  43. { callback = std::move(newCallback_); });
  44. }
  45. void callback(BufferCallbackHandle<Buffer<int16_t>> buffer)
  46. {
  47. mCallback.update([&](auto& callback) { callback(buffer); });
  48. }
  49. private:
  50. Id mId;
  51. util::Locked<Callback> mCallback;
  52. };
  53. } // namespace link_audio
  54. } // namespace ableton