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.

185 lines
6.1KB

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