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.

437 lines
16KB

  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. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. template <typename Value>
  21. struct ChannelInfo
  22. {
  23. ChannelInfo() = default;
  24. ChannelInfo (Value** dataIn, int numChannelsIn)
  25. : data (dataIn), numChannels (numChannelsIn) {}
  26. Value** data = nullptr;
  27. int numChannels = 0;
  28. };
  29. /** Sets up `channels` so that it contains channel pointers suitable for passing to
  30. an AudioProcessor's processBlock.
  31. On return, `channels` will hold `max (processorIns, processorOuts)` entries.
  32. The first `processorIns` entries will point to buffers holding input data.
  33. Any entries after the first `processorIns` entries will point to zeroed buffers.
  34. In the case that the system only provides a single input channel, but the processor
  35. has been initialised with multiple input channels, the system input will be copied
  36. to all processor inputs.
  37. In the case that the system provides no input channels, but the processor has
  38. been initialise with multiple input channels, the processor's input channels will
  39. all be zeroed.
  40. @param ins the system inputs.
  41. @param outs the system outputs.
  42. @param numSamples the number of samples in the system buffers.
  43. @param processorIns the number of input channels requested by the processor.
  44. @param processorOuts the number of output channels requested by the processor.
  45. @param tempBuffer temporary storage for inputs that don't have a corresponding output.
  46. @param channels holds pointers to each of the processor's audio channels.
  47. */
  48. static void initialiseIoBuffers (ChannelInfo<const float> ins,
  49. ChannelInfo<float> outs,
  50. const int numSamples,
  51. int processorIns,
  52. int processorOuts,
  53. AudioBuffer<float>& tempBuffer,
  54. std::vector<float*>& channels)
  55. {
  56. jassert ((int) channels.size() >= jmax (processorIns, processorOuts));
  57. size_t totalNumChans = 0;
  58. const auto numBytes = (size_t) numSamples * sizeof (float);
  59. const auto prepareInputChannel = [&] (int index)
  60. {
  61. if (ins.numChannels == 0)
  62. zeromem (channels[totalNumChans], numBytes);
  63. else
  64. memcpy (channels[totalNumChans], ins.data[index % ins.numChannels], numBytes);
  65. };
  66. if (processorIns > processorOuts)
  67. {
  68. // If there aren't enough output channels for the number of
  69. // inputs, we need to use some temporary extra ones (can't
  70. // use the input data in case it gets written to).
  71. jassert (tempBuffer.getNumChannels() >= processorIns - processorOuts);
  72. jassert (tempBuffer.getNumSamples() >= numSamples);
  73. for (int i = 0; i < processorOuts; ++i)
  74. {
  75. channels[totalNumChans] = outs.data[i];
  76. prepareInputChannel (i);
  77. ++totalNumChans;
  78. }
  79. for (auto i = processorOuts; i < processorIns; ++i)
  80. {
  81. channels[totalNumChans] = tempBuffer.getWritePointer (i - outs.numChannels);
  82. prepareInputChannel (i);
  83. ++totalNumChans;
  84. }
  85. }
  86. else
  87. {
  88. for (int i = 0; i < processorIns; ++i)
  89. {
  90. channels[totalNumChans] = outs.data[i];
  91. prepareInputChannel (i);
  92. ++totalNumChans;
  93. }
  94. for (auto i = processorIns; i < processorOuts; ++i)
  95. {
  96. channels[totalNumChans] = outs.data[i];
  97. zeromem (channels[totalNumChans], (size_t) numSamples * sizeof (float));
  98. ++totalNumChans;
  99. }
  100. }
  101. }
  102. //==============================================================================
  103. AudioProcessorPlayer::AudioProcessorPlayer (bool doDoublePrecisionProcessing)
  104. : isDoublePrecision (doDoublePrecisionProcessing)
  105. {
  106. }
  107. AudioProcessorPlayer::~AudioProcessorPlayer()
  108. {
  109. setProcessor (nullptr);
  110. }
  111. //==============================================================================
  112. AudioProcessorPlayer::NumChannels AudioProcessorPlayer::findMostSuitableLayout (const AudioProcessor& proc) const
  113. {
  114. if (proc.isMidiEffect())
  115. return {};
  116. std::vector<NumChannels> layouts { deviceChannels };
  117. if (deviceChannels.ins == 0 || deviceChannels.ins == 1)
  118. {
  119. layouts.emplace_back (defaultProcessorChannels.ins, deviceChannels.outs);
  120. layouts.emplace_back (deviceChannels.outs, deviceChannels.outs);
  121. }
  122. const auto it = std::find_if (layouts.begin(), layouts.end(), [&] (const NumChannels& chans)
  123. {
  124. return proc.checkBusesLayoutSupported (chans.toLayout());
  125. });
  126. return it != std::end (layouts) ? *it : layouts[0];
  127. }
  128. void AudioProcessorPlayer::resizeChannels()
  129. {
  130. const auto maxChannels = jmax (deviceChannels.ins,
  131. deviceChannels.outs,
  132. actualProcessorChannels.ins,
  133. actualProcessorChannels.outs);
  134. channels.resize ((size_t) maxChannels);
  135. tempBuffer.setSize (maxChannels, blockSize);
  136. }
  137. void AudioProcessorPlayer::setProcessor (AudioProcessor* const processorToPlay)
  138. {
  139. const ScopedLock sl (lock);
  140. if (processor == processorToPlay)
  141. return;
  142. if (processorToPlay != nullptr && sampleRate > 0 && blockSize > 0)
  143. {
  144. defaultProcessorChannels = NumChannels { processorToPlay->getBusesLayout() };
  145. actualProcessorChannels = findMostSuitableLayout (*processorToPlay);
  146. if (processorToPlay->isMidiEffect())
  147. processorToPlay->setRateAndBufferSizeDetails (sampleRate, blockSize);
  148. else
  149. processorToPlay->setPlayConfigDetails (actualProcessorChannels.ins,
  150. actualProcessorChannels.outs,
  151. sampleRate,
  152. blockSize);
  153. auto supportsDouble = processorToPlay->supportsDoublePrecisionProcessing() && isDoublePrecision;
  154. processorToPlay->setProcessingPrecision (supportsDouble ? AudioProcessor::doublePrecision
  155. : AudioProcessor::singlePrecision);
  156. processorToPlay->prepareToPlay (sampleRate, blockSize);
  157. }
  158. AudioProcessor* oldOne = nullptr;
  159. oldOne = isPrepared ? processor : nullptr;
  160. processor = processorToPlay;
  161. isPrepared = true;
  162. resizeChannels();
  163. if (oldOne != nullptr)
  164. oldOne->releaseResources();
  165. }
  166. void AudioProcessorPlayer::setDoublePrecisionProcessing (bool doublePrecision)
  167. {
  168. if (doublePrecision != isDoublePrecision)
  169. {
  170. const ScopedLock sl (lock);
  171. if (processor != nullptr)
  172. {
  173. processor->releaseResources();
  174. auto supportsDouble = processor->supportsDoublePrecisionProcessing() && doublePrecision;
  175. processor->setProcessingPrecision (supportsDouble ? AudioProcessor::doublePrecision
  176. : AudioProcessor::singlePrecision);
  177. processor->prepareToPlay (sampleRate, blockSize);
  178. }
  179. isDoublePrecision = doublePrecision;
  180. }
  181. }
  182. void AudioProcessorPlayer::setMidiOutput (MidiOutput* midiOutputToUse)
  183. {
  184. if (midiOutput != midiOutputToUse)
  185. {
  186. const ScopedLock sl (lock);
  187. midiOutput = midiOutputToUse;
  188. }
  189. }
  190. //==============================================================================
  191. void AudioProcessorPlayer::audioDeviceIOCallback (const float** const inputChannelData,
  192. const int numInputChannels,
  193. float** const outputChannelData,
  194. const int numOutputChannels,
  195. const int numSamples)
  196. {
  197. const ScopedLock sl (lock);
  198. // These should have been prepared by audioDeviceAboutToStart()...
  199. jassert (sampleRate > 0 && blockSize > 0);
  200. incomingMidi.clear();
  201. messageCollector.removeNextBlockOfMessages (incomingMidi, numSamples);
  202. initialiseIoBuffers ({ inputChannelData, numInputChannels },
  203. { outputChannelData, numOutputChannels },
  204. numSamples,
  205. actualProcessorChannels.ins,
  206. actualProcessorChannels.outs,
  207. tempBuffer,
  208. channels);
  209. const auto totalNumChannels = jmax (actualProcessorChannels.ins, actualProcessorChannels.outs);
  210. AudioBuffer<float> buffer (channels.data(), (int) totalNumChannels, numSamples);
  211. if (processor != nullptr)
  212. {
  213. // The processor should be prepared to deal with the same number of output channels
  214. // as our output device.
  215. jassert (processor->isMidiEffect() || numOutputChannels == actualProcessorChannels.outs);
  216. const ScopedLock sl2 (processor->getCallbackLock());
  217. if (! processor->isSuspended())
  218. {
  219. if (processor->isUsingDoublePrecision())
  220. {
  221. conversionBuffer.makeCopyOf (buffer, true);
  222. processor->processBlock (conversionBuffer, incomingMidi);
  223. buffer.makeCopyOf (conversionBuffer, true);
  224. }
  225. else
  226. {
  227. processor->processBlock (buffer, incomingMidi);
  228. }
  229. if (midiOutput != nullptr)
  230. {
  231. if (midiOutput->isBackgroundThreadRunning())
  232. {
  233. midiOutput->sendBlockOfMessages (incomingMidi,
  234. Time::getMillisecondCounterHiRes(),
  235. sampleRate);
  236. }
  237. else
  238. {
  239. midiOutput->sendBlockOfMessagesNow (incomingMidi);
  240. }
  241. }
  242. return;
  243. }
  244. }
  245. for (int i = 0; i < numOutputChannels; ++i)
  246. FloatVectorOperations::clear (outputChannelData[i], numSamples);
  247. }
  248. void AudioProcessorPlayer::audioDeviceAboutToStart (AudioIODevice* const device)
  249. {
  250. auto newSampleRate = device->getCurrentSampleRate();
  251. auto newBlockSize = device->getCurrentBufferSizeSamples();
  252. auto numChansIn = device->getActiveInputChannels().countNumberOfSetBits();
  253. auto numChansOut = device->getActiveOutputChannels().countNumberOfSetBits();
  254. const ScopedLock sl (lock);
  255. sampleRate = newSampleRate;
  256. blockSize = newBlockSize;
  257. deviceChannels = { numChansIn, numChansOut };
  258. resizeChannels();
  259. messageCollector.reset (sampleRate);
  260. if (processor != nullptr)
  261. {
  262. if (isPrepared)
  263. processor->releaseResources();
  264. auto* oldProcessor = processor;
  265. setProcessor (nullptr);
  266. setProcessor (oldProcessor);
  267. }
  268. }
  269. void AudioProcessorPlayer::audioDeviceStopped()
  270. {
  271. const ScopedLock sl (lock);
  272. if (processor != nullptr && isPrepared)
  273. processor->releaseResources();
  274. sampleRate = 0.0;
  275. blockSize = 0;
  276. isPrepared = false;
  277. tempBuffer.setSize (1, 1);
  278. }
  279. void AudioProcessorPlayer::handleIncomingMidiMessage (MidiInput*, const MidiMessage& message)
  280. {
  281. messageCollector.addMessageToQueue (message);
  282. }
  283. //==============================================================================
  284. //==============================================================================
  285. #if JUCE_UNIT_TESTS
  286. struct AudioProcessorPlayerTests : public UnitTest
  287. {
  288. AudioProcessorPlayerTests()
  289. : UnitTest ("AudioProcessorPlayer", UnitTestCategories::audio) {}
  290. void runTest() override
  291. {
  292. struct Layout
  293. {
  294. int numIns, numOuts;
  295. };
  296. const Layout processorLayouts[] { Layout { 0, 0 },
  297. Layout { 1, 1 },
  298. Layout { 4, 4 },
  299. Layout { 4, 8 },
  300. Layout { 8, 4 } };
  301. beginTest ("Buffers are prepared correctly for a variety of channel layouts");
  302. {
  303. for (const auto& layout : processorLayouts)
  304. {
  305. for (const auto numSystemInputs : { 0, 1, layout.numIns })
  306. {
  307. const int numSamples = 256;
  308. const auto systemIns = getTestBuffer (numSystemInputs, numSamples);
  309. auto systemOuts = getTestBuffer (layout.numOuts, numSamples);
  310. AudioBuffer<float> tempBuffer (jmax (layout.numIns, layout.numOuts), numSamples);
  311. std::vector<float*> channels ((size_t) jmax (layout.numIns, layout.numOuts), nullptr);
  312. initialiseIoBuffers ({ systemIns.getArrayOfReadPointers(), systemIns.getNumChannels() },
  313. { systemOuts.getArrayOfWritePointers(), systemOuts.getNumChannels() },
  314. numSamples,
  315. layout.numIns,
  316. layout.numOuts,
  317. tempBuffer,
  318. channels);
  319. int channelIndex = 0;
  320. for (const auto& channel : channels)
  321. {
  322. const auto value = [&]
  323. {
  324. // Any channels past the number of inputs should be silent.
  325. if (layout.numIns <= channelIndex)
  326. return 0.0f;
  327. // If there's no input, all input channels should be silent.
  328. if (numSystemInputs == 0) return 0.0f;
  329. // If there's one input, all input channels should copy from that input.
  330. if (numSystemInputs == 1) return 1.0f;
  331. // Otherwise, each processor input should match the corresponding system input.
  332. return (float) (channelIndex + 1);
  333. }();
  334. expect (FloatVectorOperations::findMinAndMax (channel, numSamples) == Range<float> (value, value));
  335. channelIndex += 1;
  336. }
  337. }
  338. }
  339. }
  340. }
  341. static AudioBuffer<float> getTestBuffer (int numChannels, int numSamples)
  342. {
  343. AudioBuffer<float> result (numChannels, numSamples);
  344. for (int i = 0; i < result.getNumChannels(); ++i)
  345. FloatVectorOperations::fill (result.getWritePointer (i), (float) i + 1, result.getNumSamples());
  346. return result;
  347. }
  348. };
  349. static AudioProcessorPlayerTests audioProcessorPlayerTests;
  350. #endif
  351. } // namespace juce