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.

142 lines
5.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. //==============================================================================
  20. /**
  21. An AudioSource that takes the audio from another source, and re-maps its
  22. input and output channels to a different arrangement.
  23. You can use this to increase or decrease the number of channels that an
  24. audio source uses, or to re-order those channels.
  25. Call the reset() method before using it to set up a default mapping, and then
  26. the setInputChannelMapping() and setOutputChannelMapping() methods to
  27. create an appropriate mapping, otherwise no channels will be connected and
  28. it'll produce silence.
  29. @see AudioSource
  30. @tags{Audio}
  31. */
  32. class ChannelRemappingAudioSource : public AudioSource
  33. {
  34. public:
  35. //==============================================================================
  36. /** Creates a remapping source that will pass on audio from the given input.
  37. @param source the input source to use. Make sure that this doesn't
  38. get deleted before the ChannelRemappingAudioSource object
  39. @param deleteSourceWhenDeleted if true, the input source will be deleted
  40. when this object is deleted, if false, the caller is
  41. responsible for its deletion
  42. */
  43. ChannelRemappingAudioSource (AudioSource* source,
  44. bool deleteSourceWhenDeleted);
  45. /** Destructor. */
  46. ~ChannelRemappingAudioSource() override;
  47. //==============================================================================
  48. /** Specifies a number of channels that this audio source must produce from its
  49. getNextAudioBlock() callback.
  50. */
  51. void setNumberOfChannelsToProduce (int requiredNumberOfChannels);
  52. /** Clears any mapped channels.
  53. After this, no channels are mapped, so this object will produce silence. Create
  54. some mappings with setInputChannelMapping() and setOutputChannelMapping().
  55. */
  56. void clearAllMappings();
  57. /** Creates an input channel mapping.
  58. When the getNextAudioBlock() method is called, the data in channel sourceChannelIndex of the incoming
  59. data will be sent to destChannelIndex of our input source.
  60. @param destChannelIndex the index of an input channel in our input audio source (i.e. the
  61. source specified when this object was created).
  62. @param sourceChannelIndex the index of the input channel in the incoming audio data buffer
  63. during our getNextAudioBlock() callback
  64. */
  65. void setInputChannelMapping (int destChannelIndex,
  66. int sourceChannelIndex);
  67. /** Creates an output channel mapping.
  68. When the getNextAudioBlock() method is called, the data returned in channel sourceChannelIndex by
  69. our input audio source will be copied to channel destChannelIndex of the final buffer.
  70. @param sourceChannelIndex the index of an output channel coming from our input audio source
  71. (i.e. the source specified when this object was created).
  72. @param destChannelIndex the index of the output channel in the incoming audio data buffer
  73. during our getNextAudioBlock() callback
  74. */
  75. void setOutputChannelMapping (int sourceChannelIndex,
  76. int destChannelIndex);
  77. /** Returns the channel from our input that will be sent to channel inputChannelIndex of
  78. our input audio source.
  79. */
  80. int getRemappedInputChannel (int inputChannelIndex) const;
  81. /** Returns the output channel to which channel outputChannelIndex of our input audio
  82. source will be sent to.
  83. */
  84. int getRemappedOutputChannel (int outputChannelIndex) const;
  85. //==============================================================================
  86. /** Returns an XML object to encapsulate the state of the mappings.
  87. @see restoreFromXml
  88. */
  89. std::unique_ptr<XmlElement> createXml() const;
  90. /** Restores the mappings from an XML object created by createXML().
  91. @see createXml
  92. */
  93. void restoreFromXml (const XmlElement&);
  94. //==============================================================================
  95. void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
  96. void releaseResources() override;
  97. void getNextAudioBlock (const AudioSourceChannelInfo&) override;
  98. private:
  99. //==============================================================================
  100. OptionalScopedPointer<AudioSource> source;
  101. Array<int> remappedInputs, remappedOutputs;
  102. int requiredNumberOfChannels;
  103. AudioBuffer<float> buffer;
  104. AudioSourceChannelInfo remappedInfo;
  105. CriticalSection lock;
  106. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChannelRemappingAudioSource)
  107. };
  108. } // namespace juce