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.

juce_MixerAudioSource.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. : currentSampleRate (0.0), bufferSizeExpected (0)
  19. {
  20. }
  21. MixerAudioSource::~MixerAudioSource()
  22. {
  23. removeAllInputs();
  24. }
  25. //==============================================================================
  26. void MixerAudioSource::addInputSource (AudioSource* input, const bool deleteWhenRemoved)
  27. {
  28. if (input != nullptr && ! inputs.contains (input))
  29. {
  30. double localRate;
  31. int localBufferSize;
  32. {
  33. const ScopedLock sl (lock);
  34. localRate = currentSampleRate;
  35. localBufferSize = bufferSizeExpected;
  36. }
  37. if (localRate > 0.0)
  38. input->prepareToPlay (localBufferSize, localRate);
  39. const ScopedLock sl (lock);
  40. inputsToDelete.setBit (inputs.size(), deleteWhenRemoved);
  41. inputs.add (input);
  42. }
  43. }
  44. void MixerAudioSource::removeInputSource (AudioSource* const input)
  45. {
  46. if (input != nullptr)
  47. {
  48. ScopedPointer<AudioSource> toDelete;
  49. {
  50. const ScopedLock sl (lock);
  51. const int index = inputs.indexOf (input);
  52. if (index < 0)
  53. return;
  54. if (inputsToDelete [index])
  55. toDelete = input;
  56. inputsToDelete.shiftBits (-1, index);
  57. inputs.remove (index);
  58. }
  59. input->releaseResources();
  60. }
  61. }
  62. void MixerAudioSource::removeAllInputs()
  63. {
  64. OwnedArray<AudioSource> toDelete;
  65. {
  66. const ScopedLock sl (lock);
  67. for (int i = inputs.size(); --i >= 0;)
  68. if (inputsToDelete[i])
  69. toDelete.add (inputs.getUnchecked(i));
  70. inputs.clear();
  71. }
  72. for (int i = toDelete.size(); --i >= 0;)
  73. toDelete.getUnchecked(i)->releaseResources();
  74. }
  75. void MixerAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
  76. {
  77. tempBuffer.setSize (2, samplesPerBlockExpected);
  78. const ScopedLock sl (lock);
  79. currentSampleRate = sampleRate;
  80. bufferSizeExpected = samplesPerBlockExpected;
  81. for (int i = inputs.size(); --i >= 0;)
  82. inputs.getUnchecked(i)->prepareToPlay (samplesPerBlockExpected, sampleRate);
  83. }
  84. void MixerAudioSource::releaseResources()
  85. {
  86. const ScopedLock sl (lock);
  87. for (int i = inputs.size(); --i >= 0;)
  88. inputs.getUnchecked(i)->releaseResources();
  89. tempBuffer.setSize (2, 0);
  90. currentSampleRate = 0;
  91. bufferSizeExpected = 0;
  92. }
  93. void MixerAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  94. {
  95. const ScopedLock sl (lock);
  96. if (inputs.size() > 0)
  97. {
  98. inputs.getUnchecked(0)->getNextAudioBlock (info);
  99. if (inputs.size() > 1)
  100. {
  101. tempBuffer.setSize (jmax (1, info.buffer->getNumChannels()),
  102. info.buffer->getNumSamples());
  103. AudioSourceChannelInfo info2 (&tempBuffer, 0, info.numSamples);
  104. for (int i = 1; i < inputs.size(); ++i)
  105. {
  106. inputs.getUnchecked(i)->getNextAudioBlock (info2);
  107. for (int chan = 0; chan < info.buffer->getNumChannels(); ++chan)
  108. info.buffer->addFrom (chan, info.startSample, tempBuffer, chan, 0, info.numSamples);
  109. }
  110. }
  111. }
  112. else
  113. {
  114. info.clearActiveBufferRegion();
  115. }
  116. }