The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

86 lines
2.2KB

  1. /*!
  2. @file AudioUnitSDK/AUInputElement.cpp
  3. @copyright © 2000-2021 Apple Inc. All rights reserved.
  4. */
  5. #include <AudioUnitSDK/AUBase.h>
  6. namespace ausdk {
  7. constexpr bool HasGoodBufferPointers(const AudioBufferList& abl, UInt32 nBytes) noexcept
  8. {
  9. const AudioBuffer* buf = abl.mBuffers; // NOLINT
  10. for (UInt32 i = abl.mNumberBuffers; i-- > 0; ++buf) { // NOLINT
  11. if (buf->mData == nullptr || buf->mDataByteSize < nBytes) {
  12. return false;
  13. }
  14. }
  15. return true;
  16. }
  17. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  18. // AUInputElement::SetConnection
  19. //
  20. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. void AUInputElement::SetConnection(const AudioUnitConnection& conn)
  22. {
  23. if (conn.sourceAudioUnit == nullptr) {
  24. Disconnect();
  25. return;
  26. }
  27. mInputType = EInputType::FromConnection;
  28. mConnection = conn;
  29. AllocateBuffer();
  30. }
  31. void AUInputElement::Disconnect()
  32. {
  33. mInputType = EInputType::NoInput;
  34. IOBuffer().Deallocate();
  35. }
  36. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  37. // AUInputElement::SetInputCallback
  38. //
  39. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  40. void AUInputElement::SetInputCallback(AURenderCallback proc, void* refCon)
  41. {
  42. if (proc == nullptr) {
  43. Disconnect();
  44. } else {
  45. mInputType = EInputType::FromCallback;
  46. mInputProc = proc;
  47. mInputProcRefCon = refCon;
  48. AllocateBuffer();
  49. }
  50. }
  51. OSStatus AUInputElement::SetStreamFormat(const AudioStreamBasicDescription& fmt)
  52. {
  53. const OSStatus err = AUIOElement::SetStreamFormat(fmt);
  54. if (err == noErr) {
  55. AllocateBuffer();
  56. }
  57. return err;
  58. }
  59. OSStatus AUInputElement::PullInput(AudioUnitRenderActionFlags& ioActionFlags,
  60. const AudioTimeStamp& inTimeStamp, AudioUnitElement inElement, UInt32 nFrames)
  61. {
  62. if (!IsActive()) {
  63. return kAudioUnitErr_NoConnection;
  64. }
  65. auto& iob = IOBuffer();
  66. AudioBufferList& pullBuffer = (HasConnection() || !WillAllocateBuffer())
  67. ? iob.PrepareNullBuffer(GetStreamFormat(), nFrames)
  68. : iob.PrepareBuffer(GetStreamFormat(), nFrames);
  69. return PullInputWithBufferList(ioActionFlags, inTimeStamp, inElement, nFrames, pullBuffer);
  70. }
  71. } // namespace ausdk