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.

185 lines
5.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. ChannelRemappingAudioSource::ChannelRemappingAudioSource (AudioSource* const source_,
  18. const bool deleteSourceWhenDeleted)
  19. : source (source_, deleteSourceWhenDeleted),
  20. requiredNumberOfChannels (2)
  21. {
  22. remappedInfo.buffer = &buffer;
  23. remappedInfo.startSample = 0;
  24. }
  25. ChannelRemappingAudioSource::~ChannelRemappingAudioSource() {}
  26. //==============================================================================
  27. void ChannelRemappingAudioSource::setNumberOfChannelsToProduce (const int requiredNumberOfChannels_)
  28. {
  29. const ScopedLock sl (lock);
  30. requiredNumberOfChannels = requiredNumberOfChannels_;
  31. }
  32. void ChannelRemappingAudioSource::clearAllMappings()
  33. {
  34. const ScopedLock sl (lock);
  35. remappedInputs.clear();
  36. remappedOutputs.clear();
  37. }
  38. void ChannelRemappingAudioSource::setInputChannelMapping (const int destIndex, const int sourceIndex)
  39. {
  40. const ScopedLock sl (lock);
  41. while (remappedInputs.size() < destIndex)
  42. remappedInputs.add (-1);
  43. remappedInputs.set (destIndex, sourceIndex);
  44. }
  45. void ChannelRemappingAudioSource::setOutputChannelMapping (const int sourceIndex, const int destIndex)
  46. {
  47. const ScopedLock sl (lock);
  48. while (remappedOutputs.size() < sourceIndex)
  49. remappedOutputs.add (-1);
  50. remappedOutputs.set (sourceIndex, destIndex);
  51. }
  52. int ChannelRemappingAudioSource::getRemappedInputChannel (const int inputChannelIndex) const
  53. {
  54. const ScopedLock sl (lock);
  55. if (inputChannelIndex >= 0 && inputChannelIndex < remappedInputs.size())
  56. return remappedInputs.getUnchecked (inputChannelIndex);
  57. return -1;
  58. }
  59. int ChannelRemappingAudioSource::getRemappedOutputChannel (const int outputChannelIndex) const
  60. {
  61. const ScopedLock sl (lock);
  62. if (outputChannelIndex >= 0 && outputChannelIndex < remappedOutputs.size())
  63. return remappedOutputs .getUnchecked (outputChannelIndex);
  64. return -1;
  65. }
  66. //==============================================================================
  67. void ChannelRemappingAudioSource::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
  68. {
  69. source->prepareToPlay (samplesPerBlockExpected, sampleRate);
  70. }
  71. void ChannelRemappingAudioSource::releaseResources()
  72. {
  73. source->releaseResources();
  74. }
  75. void ChannelRemappingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
  76. {
  77. const ScopedLock sl (lock);
  78. buffer.setSize (requiredNumberOfChannels, bufferToFill.numSamples, false, false, true);
  79. const int numChans = bufferToFill.buffer->getNumChannels();
  80. for (int i = 0; i < buffer.getNumChannels(); ++i)
  81. {
  82. const int remappedChan = getRemappedInputChannel (i);
  83. if (remappedChan >= 0 && remappedChan < numChans)
  84. {
  85. buffer.copyFrom (i, 0, *bufferToFill.buffer,
  86. remappedChan,
  87. bufferToFill.startSample,
  88. bufferToFill.numSamples);
  89. }
  90. else
  91. {
  92. buffer.clear (i, 0, bufferToFill.numSamples);
  93. }
  94. }
  95. remappedInfo.numSamples = bufferToFill.numSamples;
  96. source->getNextAudioBlock (remappedInfo);
  97. bufferToFill.clearActiveBufferRegion();
  98. for (int i = 0; i < requiredNumberOfChannels; ++i)
  99. {
  100. const int remappedChan = getRemappedOutputChannel (i);
  101. if (remappedChan >= 0 && remappedChan < numChans)
  102. {
  103. bufferToFill.buffer->addFrom (remappedChan, bufferToFill.startSample,
  104. buffer, i, 0, bufferToFill.numSamples);
  105. }
  106. }
  107. }
  108. //==============================================================================
  109. XmlElement* ChannelRemappingAudioSource::createXml() const
  110. {
  111. XmlElement* e = new XmlElement ("MAPPINGS");
  112. String ins, outs;
  113. const ScopedLock sl (lock);
  114. for (int i = 0; i < remappedInputs.size(); ++i)
  115. ins << remappedInputs.getUnchecked(i) << ' ';
  116. for (int i = 0; i < remappedOutputs.size(); ++i)
  117. outs << remappedOutputs.getUnchecked(i) << ' ';
  118. e->setAttribute ("inputs", ins.trimEnd());
  119. e->setAttribute ("outputs", outs.trimEnd());
  120. return e;
  121. }
  122. void ChannelRemappingAudioSource::restoreFromXml (const XmlElement& e)
  123. {
  124. if (e.hasTagName ("MAPPINGS"))
  125. {
  126. const ScopedLock sl (lock);
  127. clearAllMappings();
  128. StringArray ins, outs;
  129. ins.addTokens (e.getStringAttribute ("inputs"), false);
  130. outs.addTokens (e.getStringAttribute ("outputs"), false);
  131. for (int i = 0; i < ins.size(); ++i)
  132. remappedInputs.add (ins[i].getIntValue());
  133. for (int i = 0; i < outs.size(); ++i)
  134. remappedOutputs.add (outs[i].getIntValue());
  135. }
  136. }