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.

191 lines
6.2KB

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