The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

222 lines
7.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. AudioProcessorPlayer::AudioProcessorPlayer(bool doDoublePrecisionProcessing)
  18. : processor (nullptr),
  19. sampleRate (0),
  20. blockSize (0),
  21. isPrepared (false),
  22. isDoublePrecision (doDoublePrecisionProcessing),
  23. numInputChans (0),
  24. numOutputChans (0)
  25. {
  26. }
  27. AudioProcessorPlayer::~AudioProcessorPlayer()
  28. {
  29. setProcessor (nullptr);
  30. }
  31. //==============================================================================
  32. void AudioProcessorPlayer::setProcessor (AudioProcessor* const processorToPlay)
  33. {
  34. if (processor != processorToPlay)
  35. {
  36. if (processorToPlay != nullptr && sampleRate > 0 && blockSize > 0)
  37. {
  38. processorToPlay->setPlayConfigDetails(numInputChans, numOutputChans, sampleRate, blockSize);
  39. const bool supportsDouble = processorToPlay->supportsDoublePrecisionProcessing() && isDoublePrecision;
  40. AudioProcessor::ProcessingPrecision precision = supportsDouble ? AudioProcessor::doublePrecision
  41. : AudioProcessor::singlePrecision;
  42. processorToPlay->setProcessingPrecision (precision);
  43. processorToPlay->prepareToPlay (sampleRate, blockSize);
  44. }
  45. AudioProcessor* oldOne;
  46. {
  47. const ScopedLock sl (lock);
  48. oldOne = isPrepared ? processor : nullptr;
  49. processor = processorToPlay;
  50. isPrepared = true;
  51. }
  52. if (oldOne != nullptr)
  53. oldOne->releaseResources();
  54. }
  55. }
  56. void AudioProcessorPlayer::setDoublePrecisionProcessing (bool doublePrecision)
  57. {
  58. if (doublePrecision != isDoublePrecision)
  59. {
  60. const ScopedLock sl (lock);
  61. if (processor != nullptr)
  62. {
  63. processor->releaseResources();
  64. const bool supportsDouble = processor->supportsDoublePrecisionProcessing() && doublePrecision;
  65. AudioProcessor::ProcessingPrecision precision = supportsDouble ? AudioProcessor::doublePrecision
  66. : AudioProcessor::singlePrecision;
  67. processor->setProcessingPrecision (precision);
  68. processor->prepareToPlay (sampleRate, blockSize);
  69. }
  70. isDoublePrecision = doublePrecision;
  71. }
  72. }
  73. //==============================================================================
  74. void AudioProcessorPlayer::audioDeviceIOCallback (const float** const inputChannelData,
  75. const int numInputChannels,
  76. float** const outputChannelData,
  77. const int numOutputChannels,
  78. const int numSamples)
  79. {
  80. // these should have been prepared by audioDeviceAboutToStart()...
  81. jassert (sampleRate > 0 && blockSize > 0);
  82. incomingMidi.clear();
  83. messageCollector.removeNextBlockOfMessages (incomingMidi, numSamples);
  84. int totalNumChans = 0;
  85. if (numInputChannels > numOutputChannels)
  86. {
  87. // if there aren't enough output channels for the number of
  88. // inputs, we need to create some temporary extra ones (can't
  89. // use the input data in case it gets written to)
  90. tempBuffer.setSize (numInputChannels - numOutputChannels, numSamples,
  91. false, false, true);
  92. for (int i = 0; i < numOutputChannels; ++i)
  93. {
  94. channels[totalNumChans] = outputChannelData[i];
  95. memcpy (channels[totalNumChans], inputChannelData[i], sizeof (float) * (size_t) numSamples);
  96. ++totalNumChans;
  97. }
  98. for (int i = numOutputChannels; i < numInputChannels; ++i)
  99. {
  100. channels[totalNumChans] = tempBuffer.getWritePointer (i - numOutputChannels);
  101. memcpy (channels[totalNumChans], inputChannelData[i], sizeof (float) * (size_t) numSamples);
  102. ++totalNumChans;
  103. }
  104. }
  105. else
  106. {
  107. for (int i = 0; i < numInputChannels; ++i)
  108. {
  109. channels[totalNumChans] = outputChannelData[i];
  110. memcpy (channels[totalNumChans], inputChannelData[i], sizeof (float) * (size_t) numSamples);
  111. ++totalNumChans;
  112. }
  113. for (int i = numInputChannels; i < numOutputChannels; ++i)
  114. {
  115. channels[totalNumChans] = outputChannelData[i];
  116. zeromem (channels[totalNumChans], sizeof (float) * (size_t) numSamples);
  117. ++totalNumChans;
  118. }
  119. }
  120. AudioSampleBuffer buffer (channels, totalNumChans, numSamples);
  121. {
  122. const ScopedLock sl (lock);
  123. if (processor != nullptr)
  124. {
  125. const ScopedLock sl2 (processor->getCallbackLock());
  126. if (! processor->isSuspended())
  127. {
  128. if (processor->isUsingDoublePrecision())
  129. {
  130. conversionBuffer.makeCopyOf (buffer, true);
  131. processor->processBlock (conversionBuffer, incomingMidi);
  132. buffer.makeCopyOf (conversionBuffer, true);
  133. }
  134. else
  135. {
  136. processor->processBlock (buffer, incomingMidi);
  137. }
  138. return;
  139. }
  140. }
  141. }
  142. for (int i = 0; i < numOutputChannels; ++i)
  143. FloatVectorOperations::clear (outputChannelData[i], numSamples);
  144. }
  145. void AudioProcessorPlayer::audioDeviceAboutToStart (AudioIODevice* const device)
  146. {
  147. const double newSampleRate = device->getCurrentSampleRate();
  148. const int newBlockSize = device->getCurrentBufferSizeSamples();
  149. const int numChansIn = device->getActiveInputChannels().countNumberOfSetBits();
  150. const int numChansOut = device->getActiveOutputChannels().countNumberOfSetBits();
  151. const ScopedLock sl (lock);
  152. sampleRate = newSampleRate;
  153. blockSize = newBlockSize;
  154. numInputChans = numChansIn;
  155. numOutputChans = numChansOut;
  156. messageCollector.reset (sampleRate);
  157. channels.calloc ((size_t) jmax (numChansIn, numChansOut) + 2);
  158. if (processor != nullptr)
  159. {
  160. if (isPrepared)
  161. processor->releaseResources();
  162. AudioProcessor* const oldProcessor = processor;
  163. setProcessor (nullptr);
  164. setProcessor (oldProcessor);
  165. }
  166. }
  167. void AudioProcessorPlayer::audioDeviceStopped()
  168. {
  169. const ScopedLock sl (lock);
  170. if (processor != nullptr && isPrepared)
  171. processor->releaseResources();
  172. sampleRate = 0.0;
  173. blockSize = 0;
  174. isPrepared = false;
  175. tempBuffer.setSize (1, 1);
  176. }
  177. void AudioProcessorPlayer::handleIncomingMidiMessage (MidiInput*, const MidiMessage& message)
  178. {
  179. messageCollector.addMessageToQueue (message);
  180. }