4
0

PCMCodec.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/discovery/NetworkByteStreamSerializable.hpp>
  21. #include <ableton/link_audio/AudioBuffer.hpp>
  22. #include <ableton/link_audio/Buffer.hpp>
  23. #include <ableton/util/Injected.hpp>
  24. #include <memory>
  25. namespace ableton
  26. {
  27. namespace link_audio
  28. {
  29. template <typename SampleFormat, typename Sender>
  30. struct PCMEncoder
  31. {
  32. PCMEncoder(util::Injected<Sender> sender, Id channelId)
  33. : mSender(std::move(sender))
  34. {
  35. mOutputBuffer.channelId = channelId;
  36. }
  37. void operator()(const SampleFormat* samples,
  38. const AudioBuffer::Chunks& chunks,
  39. const uint32_t numChannels,
  40. const uint32_t sampleRate,
  41. const Id sessionId)
  42. {
  43. mOutputBuffer.chunks = chunks;
  44. mOutputBuffer.codec = Codec::kPCM_i16;
  45. mOutputBuffer.sampleRate = sampleRate;
  46. mOutputBuffer.numChannels = numChannels;
  47. mOutputBuffer.sessionId = sessionId;
  48. auto it = mOutputBuffer.bytes.begin();
  49. const auto numSamples = mOutputBuffer.numFrames() * mOutputBuffer.numChannels;
  50. assert(numSamples * sizeof(int16_t) <= AudioBuffer::kMaxAudioBytes);
  51. for (auto sample = 0u; sample < numSamples; ++sample)
  52. {
  53. it = discovery::toNetworkByteStream(samples[sample], it);
  54. }
  55. mOutputBuffer.numBytes =
  56. static_cast<uint32_t>(std::distance(mOutputBuffer.bytes.begin(), it));
  57. (*mSender)(mOutputBuffer);
  58. }
  59. AudioBuffer mOutputBuffer;
  60. util::Injected<Sender> mSender;
  61. };
  62. template <typename SampleFormat, typename Successor>
  63. struct PCMDecoder
  64. {
  65. PCMDecoder(util::Injected<Successor> successor, uint32_t cacheSize)
  66. : mBuffer(cacheSize)
  67. , mSuccessor(std::move(successor))
  68. {
  69. }
  70. void operator()(const AudioBuffer& input)
  71. {
  72. auto it = input.bytes.begin();
  73. auto numSamples = 0u;
  74. auto inputEnd = input.bytes.begin() + input.numBytes;
  75. while (it != inputEnd)
  76. {
  77. auto [sample, next] =
  78. discovery::Deserialize<SampleFormat>::fromNetworkByteStream(it, inputEnd);
  79. mBuffer.mSamples[numSamples] = sample;
  80. ++numSamples;
  81. it = next;
  82. }
  83. auto pSamples = mBuffer.mSamples.data();
  84. for (const auto& [count, numFrames, beginBeats, tempo] : input.chunks)
  85. {
  86. mBuffer.mNumFrames = numFrames;
  87. mBuffer.mNumChannels = input.numChannels;
  88. mBuffer.mSampleRate = input.sampleRate;
  89. mBuffer.mBeginBeats = beginBeats;
  90. mBuffer.mTempo = tempo;
  91. mBuffer.mCount = count;
  92. mBuffer.mSessionId = input.sessionId;
  93. (*mSuccessor)(BufferCallbackHandle<Buffer<SampleFormat>>{mBuffer, pSamples});
  94. pSamples += numFrames * input.numChannels;
  95. }
  96. }
  97. Buffer<SampleFormat> mBuffer;
  98. util::Injected<Successor> mSuccessor;
  99. };
  100. } // namespace link_audio
  101. } // namespace ableton