Encoder.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/AudioBuffer.hpp>
  21. #include <ableton/link_audio/Buffer.hpp>
  22. #include <ableton/link_audio/Id.hpp>
  23. #include <ableton/link_audio/PCMCodec.hpp>
  24. #include <ableton/link_audio/Resizer.hpp>
  25. #include <ableton/util/Injected.hpp>
  26. #include <array>
  27. #include <memory>
  28. #include <stdexcept>
  29. namespace ableton
  30. {
  31. namespace link_audio
  32. {
  33. template <typename Sender, typename SampleFormat>
  34. struct Encoder
  35. {
  36. // TODO: Find the best size for audio buffer messages
  37. // For now we take RFC 791 as a reference. Nodes must be able to process IP messages of
  38. // at least 576 bytes.
  39. static constexpr uint32_t kMaxAudioBytes =
  40. 576 - v1::kHeaderSize - AudioBuffer::kNonAudioBytes;
  41. static_assert(kMaxAudioBytes <= v1::kMaxPayloadSize);
  42. Encoder(util::Injected<Sender> sender, Id channelId)
  43. : mProcessor(
  44. util::injectVal(PCMEncoder<SampleFormat, Sender>(std::move(sender), channelId)))
  45. {
  46. }
  47. void operator()(const Buffer<SampleFormat>& input)
  48. {
  49. mProcessor(input.mSamples.data(),
  50. input.mNumFrames,
  51. input.mNumChannels,
  52. input.mSampleRate,
  53. input.mBeginBeats,
  54. input.mTempo,
  55. input.mSessionId);
  56. }
  57. private:
  58. Resizer<SampleFormat, PCMEncoder<SampleFormat, Sender>, kMaxAudioBytes> mProcessor;
  59. };
  60. } // namespace link_audio
  61. } // namespace ableton