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.

159 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. MixerAudioSource::MixerAudioSource()
  20. : currentSampleRate (0.0), 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. }
  119. } // namespace juce