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.

182 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. AudioProcessorPlayer::AudioProcessorPlayer()
  19. : processor (nullptr),
  20. sampleRate (0),
  21. blockSize (0),
  22. isPrepared (false),
  23. numInputChans (0),
  24. numOutputChans (0),
  25. tempBuffer (1, 1)
  26. {
  27. }
  28. AudioProcessorPlayer::~AudioProcessorPlayer()
  29. {
  30. setProcessor (nullptr);
  31. }
  32. //==============================================================================
  33. void AudioProcessorPlayer::setProcessor (AudioProcessor* const processorToPlay)
  34. {
  35. if (processor != processorToPlay)
  36. {
  37. if (processorToPlay != nullptr && sampleRate > 0 && blockSize > 0)
  38. {
  39. processorToPlay->setPlayConfigDetails (numInputChans, numOutputChans,
  40. sampleRate, blockSize);
  41. processorToPlay->prepareToPlay (sampleRate, blockSize);
  42. }
  43. AudioProcessor* oldOne;
  44. {
  45. const ScopedLock sl (lock);
  46. oldOne = isPrepared ? processor : nullptr;
  47. processor = processorToPlay;
  48. isPrepared = true;
  49. }
  50. if (oldOne != nullptr)
  51. oldOne->releaseResources();
  52. }
  53. }
  54. //==============================================================================
  55. void AudioProcessorPlayer::audioDeviceIOCallback (const float** const inputChannelData,
  56. const int numInputChannels,
  57. float** const outputChannelData,
  58. const int numOutputChannels,
  59. const int numSamples)
  60. {
  61. // these should have been prepared by audioDeviceAboutToStart()...
  62. jassert (sampleRate > 0 && blockSize > 0);
  63. incomingMidi.clear();
  64. messageCollector.removeNextBlockOfMessages (incomingMidi, numSamples);
  65. int i, totalNumChans = 0;
  66. if (numInputChannels > numOutputChannels)
  67. {
  68. // if there aren't enough output channels for the number of
  69. // inputs, we need to create some temporary extra ones (can't
  70. // use the input data in case it gets written to)
  71. tempBuffer.setSize (numInputChannels - numOutputChannels, numSamples,
  72. false, false, true);
  73. for (i = 0; i < numOutputChannels; ++i)
  74. {
  75. channels[totalNumChans] = outputChannelData[i];
  76. memcpy (channels[totalNumChans], inputChannelData[i], sizeof (float) * (size_t) numSamples);
  77. ++totalNumChans;
  78. }
  79. for (i = numOutputChannels; i < numInputChannels; ++i)
  80. {
  81. channels[totalNumChans] = tempBuffer.getSampleData (i - numOutputChannels, 0);
  82. memcpy (channels[totalNumChans], inputChannelData[i], sizeof (float) * (size_t) numSamples);
  83. ++totalNumChans;
  84. }
  85. }
  86. else
  87. {
  88. for (i = 0; i < numInputChannels; ++i)
  89. {
  90. channels[totalNumChans] = outputChannelData[i];
  91. memcpy (channels[totalNumChans], inputChannelData[i], sizeof (float) * (size_t) numSamples);
  92. ++totalNumChans;
  93. }
  94. for (i = numInputChannels; i < numOutputChannels; ++i)
  95. {
  96. channels[totalNumChans] = outputChannelData[i];
  97. zeromem (channels[totalNumChans], sizeof (float) * (size_t) numSamples);
  98. ++totalNumChans;
  99. }
  100. }
  101. AudioSampleBuffer buffer (channels, totalNumChans, numSamples);
  102. const ScopedLock sl (lock);
  103. if (processor != nullptr)
  104. {
  105. const ScopedLock sl2 (processor->getCallbackLock());
  106. if (processor->isSuspended())
  107. {
  108. for (i = 0; i < numOutputChannels; ++i)
  109. zeromem (outputChannelData[i], sizeof (float) * (size_t) numSamples);
  110. }
  111. else
  112. {
  113. processor->processBlock (buffer, incomingMidi);
  114. }
  115. }
  116. }
  117. void AudioProcessorPlayer::audioDeviceAboutToStart (AudioIODevice* device)
  118. {
  119. const ScopedLock sl (lock);
  120. sampleRate = device->getCurrentSampleRate();
  121. blockSize = device->getCurrentBufferSizeSamples();
  122. numInputChans = device->getActiveInputChannels().countNumberOfSetBits();
  123. numOutputChans = device->getActiveOutputChannels().countNumberOfSetBits();
  124. messageCollector.reset (sampleRate);
  125. zeromem (channels, sizeof (channels));
  126. if (processor != nullptr)
  127. {
  128. if (isPrepared)
  129. processor->releaseResources();
  130. AudioProcessor* const oldProcessor = processor;
  131. setProcessor (nullptr);
  132. setProcessor (oldProcessor);
  133. }
  134. }
  135. void AudioProcessorPlayer::audioDeviceStopped()
  136. {
  137. const ScopedLock sl (lock);
  138. if (processor != nullptr && isPrepared)
  139. processor->releaseResources();
  140. sampleRate = 0.0;
  141. blockSize = 0;
  142. isPrepared = false;
  143. tempBuffer.setSize (1, 1);
  144. }
  145. void AudioProcessorPlayer::handleIncomingMidiMessage (MidiInput*, const MidiMessage& message)
  146. {
  147. messageCollector.addMessageToQueue (message);
  148. }