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.

186 lines
6.2KB

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