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.

166 lines
4.6KB

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