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.

186 lines
5.7KB

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