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.

162 lines
4.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. MixerAudioSource::MixerAudioSource()
  24. : currentSampleRate (0.0), bufferSizeExpected (0)
  25. {
  26. }
  27. MixerAudioSource::~MixerAudioSource()
  28. {
  29. removeAllInputs();
  30. }
  31. //==============================================================================
  32. void MixerAudioSource::addInputSource (AudioSource* input, const bool deleteWhenRemoved)
  33. {
  34. if (input != nullptr && ! inputs.contains (input))
  35. {
  36. double localRate;
  37. int localBufferSize;
  38. {
  39. const ScopedLock sl (lock);
  40. localRate = currentSampleRate;
  41. localBufferSize = bufferSizeExpected;
  42. }
  43. if (localRate > 0.0)
  44. input->prepareToPlay (localBufferSize, localRate);
  45. const ScopedLock sl (lock);
  46. inputsToDelete.setBit (inputs.size(), deleteWhenRemoved);
  47. inputs.add (input);
  48. }
  49. }
  50. void MixerAudioSource::removeInputSource (AudioSource* const input)
  51. {
  52. if (input != nullptr)
  53. {
  54. ScopedPointer<AudioSource> toDelete;
  55. {
  56. const ScopedLock sl (lock);
  57. const int index = inputs.indexOf (input);
  58. if (index < 0)
  59. return;
  60. if (inputsToDelete [index])
  61. toDelete = input;
  62. inputsToDelete.shiftBits (-1, index);
  63. inputs.remove (index);
  64. }
  65. input->releaseResources();
  66. }
  67. }
  68. void MixerAudioSource::removeAllInputs()
  69. {
  70. OwnedArray<AudioSource> toDelete;
  71. {
  72. const ScopedLock sl (lock);
  73. for (int i = inputs.size(); --i >= 0;)
  74. if (inputsToDelete[i])
  75. toDelete.add (inputs.getUnchecked(i));
  76. inputs.clear();
  77. }
  78. for (int i = toDelete.size(); --i >= 0;)
  79. toDelete.getUnchecked(i)->releaseResources();
  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 (&tempBuffer, 0, info.numSamples);
  110. for (int i = 1; i < inputs.size(); ++i)
  111. {
  112. inputs.getUnchecked(i)->getNextAudioBlock (info2);
  113. for (int chan = 0; chan < info.buffer->getNumChannels(); ++chan)
  114. info.buffer->addFrom (chan, info.startSample, tempBuffer, chan, 0, info.numSamples);
  115. }
  116. }
  117. }
  118. else
  119. {
  120. info.clearActiveBufferRegion();
  121. }
  122. }