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.

158 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. MixerAudioSource::MixerAudioSource()
  18. : tempBuffer (2, 0),
  19. currentSampleRate (0.0),
  20. bufferSizeExpected (0)
  21. {
  22. }
  23. MixerAudioSource::~MixerAudioSource()
  24. {
  25. removeAllInputs();
  26. }
  27. //==============================================================================
  28. void MixerAudioSource::addInputSource (AudioSource* input, const bool deleteWhenRemoved)
  29. {
  30. if (input != nullptr && ! inputs.contains (input))
  31. {
  32. double localRate;
  33. int localBufferSize;
  34. {
  35. const ScopedLock sl (lock);
  36. localRate = currentSampleRate;
  37. localBufferSize = bufferSizeExpected;
  38. }
  39. if (localRate > 0.0)
  40. input->prepareToPlay (localBufferSize, localRate);
  41. const ScopedLock sl (lock);
  42. inputsToDelete.setBit (inputs.size(), deleteWhenRemoved);
  43. inputs.add (input);
  44. }
  45. }
  46. void MixerAudioSource::removeInputSource (AudioSource* const input)
  47. {
  48. if (input != nullptr)
  49. {
  50. ScopedPointer<AudioSource> toDelete;
  51. {
  52. const ScopedLock sl (lock);
  53. const int index = inputs.indexOf (input);
  54. if (index < 0)
  55. return;
  56. if (inputsToDelete [index])
  57. toDelete = input;
  58. inputsToDelete.shiftBits (-1, index);
  59. inputs.remove (index);
  60. }
  61. input->releaseResources();
  62. }
  63. }
  64. void MixerAudioSource::removeAllInputs()
  65. {
  66. OwnedArray<AudioSource> toDelete;
  67. {
  68. const ScopedLock sl (lock);
  69. for (int i = inputs.size(); --i >= 0;)
  70. if (inputsToDelete[i])
  71. toDelete.add (inputs.getUnchecked(i));
  72. inputs.clear();
  73. }
  74. for (int i = toDelete.size(); --i >= 0;)
  75. toDelete.getUnchecked(i)->releaseResources();
  76. }
  77. void MixerAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
  78. {
  79. tempBuffer.setSize (2, samplesPerBlockExpected);
  80. const ScopedLock sl (lock);
  81. currentSampleRate = sampleRate;
  82. bufferSizeExpected = samplesPerBlockExpected;
  83. for (int i = inputs.size(); --i >= 0;)
  84. inputs.getUnchecked(i)->prepareToPlay (samplesPerBlockExpected, sampleRate);
  85. }
  86. void MixerAudioSource::releaseResources()
  87. {
  88. const ScopedLock sl (lock);
  89. for (int i = inputs.size(); --i >= 0;)
  90. inputs.getUnchecked(i)->releaseResources();
  91. tempBuffer.setSize (2, 0);
  92. currentSampleRate = 0;
  93. bufferSizeExpected = 0;
  94. }
  95. void MixerAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  96. {
  97. const ScopedLock sl (lock);
  98. if (inputs.size() > 0)
  99. {
  100. inputs.getUnchecked(0)->getNextAudioBlock (info);
  101. if (inputs.size() > 1)
  102. {
  103. tempBuffer.setSize (jmax (1, info.buffer->getNumChannels()),
  104. info.buffer->getNumSamples());
  105. AudioSourceChannelInfo info2 (&tempBuffer, 0, info.numSamples);
  106. for (int i = 1; i < inputs.size(); ++i)
  107. {
  108. inputs.getUnchecked(i)->getNextAudioBlock (info2);
  109. for (int chan = 0; chan < info.buffer->getNumChannels(); ++chan)
  110. info.buffer->addFrom (chan, info.startSample, tempBuffer, chan, 0, info.numSamples);
  111. }
  112. }
  113. }
  114. else
  115. {
  116. info.clearActiveBufferRegion();
  117. }
  118. }