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.

1111 lines
42KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. /** This class is the convolution engine itself, processing only one channel at
  20. a time of input signal.
  21. */
  22. struct ConvolutionEngine
  23. {
  24. ConvolutionEngine() = default;
  25. //==============================================================================
  26. struct ProcessingInformation
  27. {
  28. enum class SourceType
  29. {
  30. sourceBinaryData,
  31. sourceAudioFile,
  32. sourceAudioBuffer,
  33. sourceNone
  34. };
  35. SourceType sourceType = SourceType::sourceNone;
  36. const void* sourceData;
  37. size_t sourceDataSize;
  38. File fileImpulseResponse;
  39. double bufferSampleRate;
  40. AudioBuffer<float>* buffer;
  41. double sampleRate = 0;
  42. bool wantsStereo;
  43. size_t impulseResponseSize;
  44. size_t maximumBufferSize = 0;
  45. };
  46. //==============================================================================
  47. void reset()
  48. {
  49. bufferInput.clear();
  50. bufferOverlap.clear();
  51. bufferTempOutput.clear();
  52. for (auto i = 0; i < buffersInputSegments.size(); ++i)
  53. buffersInputSegments.getReference (i).clear();
  54. currentSegment = 0;
  55. inputDataPos = 0;
  56. }
  57. /** Initalize all the states and objects to perform the convolution. */
  58. void initializeConvolutionEngine (ProcessingInformation& info, int channel)
  59. {
  60. blockSize = (size_t) nextPowerOfTwo ((int) info.maximumBufferSize);
  61. FFTSize = blockSize > 128 ? 2 * blockSize
  62. : 4 * blockSize;
  63. numSegments = ((size_t) info.buffer->getNumSamples()) / (FFTSize - blockSize) + 1;
  64. numInputSegments = (blockSize > 128 ? numSegments : 3 * numSegments);
  65. FFTobject = new FFT (roundDoubleToInt (log2 (FFTSize)));
  66. bufferInput.setSize (1, static_cast<int> (FFTSize));
  67. bufferOutput.setSize (1, static_cast<int> (FFTSize * 2));
  68. bufferTempOutput.setSize (1, static_cast<int> (FFTSize * 2));
  69. bufferOverlap.setSize (1, static_cast<int> (FFTSize));
  70. buffersInputSegments.clear();
  71. buffersImpulseSegments.clear();
  72. for (size_t i = 0; i < numInputSegments; ++i)
  73. {
  74. AudioBuffer<float> newInputSegment;
  75. newInputSegment.setSize (1, static_cast<int> (FFTSize * 2));
  76. buffersInputSegments.add (newInputSegment);
  77. }
  78. for (auto i = 0u; i < numSegments; ++i)
  79. {
  80. AudioBuffer<float> newImpulseSegment;
  81. newImpulseSegment.setSize (1, static_cast<int> (FFTSize * 2));
  82. buffersImpulseSegments.add (newImpulseSegment);
  83. }
  84. ScopedPointer<FFT> FFTTempObject = new FFT (roundDoubleToInt (log2 (FFTSize)));
  85. auto numChannels = (info.wantsStereo && info.buffer->getNumChannels() >= 2 ? 2 : 1);
  86. if (channel < numChannels)
  87. {
  88. auto* channelData = info.buffer->getWritePointer (channel);
  89. for (size_t n = 0; n < numSegments; ++n)
  90. {
  91. buffersImpulseSegments.getReference (static_cast<int> (n)).clear();
  92. auto* impulseResponse = buffersImpulseSegments.getReference (static_cast<int> (n)).getWritePointer (0);
  93. if (n == 0)
  94. impulseResponse[0] = 1.0f;
  95. for (size_t i = 0; i < FFTSize - blockSize; ++i)
  96. if (i + n * (FFTSize - blockSize) < (size_t) info.buffer->getNumSamples())
  97. impulseResponse[i] = channelData[i + n * (FFTSize - blockSize)];
  98. FFTTempObject->performRealOnlyForwardTransform (impulseResponse);
  99. prepareForConvolution (impulseResponse);
  100. }
  101. }
  102. reset();
  103. isReady = true;
  104. }
  105. /** Copy the states of another engine. */
  106. void copyStateFromOtherEngine (const ConvolutionEngine& other)
  107. {
  108. if (FFTSize != other.FFTSize)
  109. {
  110. FFTobject = new FFT (roundDoubleToInt (log2 (other.FFTSize)));
  111. FFTSize = other.FFTSize;
  112. }
  113. currentSegment = other.currentSegment;
  114. numInputSegments = other.numInputSegments;
  115. numSegments = other.numSegments;
  116. blockSize = other.blockSize;
  117. inputDataPos = other.inputDataPos;
  118. bufferInput = other.bufferInput;
  119. bufferTempOutput = other.bufferTempOutput;
  120. bufferOutput = other.bufferOutput;
  121. buffersInputSegments = other.buffersInputSegments;
  122. buffersImpulseSegments = other.buffersImpulseSegments;
  123. bufferOverlap = other.bufferOverlap;
  124. isReady = true;
  125. }
  126. /** Performs the uniform partitioned convolution using FFT. */
  127. void processSamples (const float* input, float* output, size_t numSamples)
  128. {
  129. if (! isReady)
  130. return;
  131. // Overlap-add, zero latency convolution algorithm with uniform partitioning
  132. size_t numSamplesProcessed = 0;
  133. auto indexStep = numInputSegments / numSegments;
  134. auto* inputData = bufferInput.getWritePointer (0);
  135. auto* outputTempData = bufferTempOutput.getWritePointer (0);
  136. auto* outputData = bufferOutput.getWritePointer (0);
  137. auto* overlapData = bufferOverlap.getWritePointer (0);
  138. while (numSamplesProcessed < numSamples)
  139. {
  140. const bool inputDataWasEmpty = (inputDataPos == 0);
  141. auto numSamplesToProcess = jmin (numSamples - numSamplesProcessed, blockSize - inputDataPos);
  142. // copy the input samples
  143. FloatVectorOperations::copy (inputData + inputDataPos, input + numSamplesProcessed, static_cast<int> (numSamplesToProcess));
  144. auto* inputSegmentData = buffersInputSegments.getReference (static_cast<int> (currentSegment)).getWritePointer (0);
  145. FloatVectorOperations::copy (inputSegmentData, inputData, static_cast<int> (FFTSize));
  146. // Forward FFT
  147. FFTobject->performRealOnlyForwardTransform (inputSegmentData);
  148. prepareForConvolution (inputSegmentData);
  149. // Complex multiplication
  150. if (inputDataWasEmpty)
  151. {
  152. FloatVectorOperations::fill (outputTempData, 0, static_cast<int> (FFTSize + 1));
  153. auto index = currentSegment;
  154. for (size_t i = 1; i < numSegments; ++i)
  155. {
  156. index += indexStep;
  157. if (index >= numInputSegments)
  158. index -= numInputSegments;
  159. convolutionProcessingAndAccumulate (buffersInputSegments.getReference (static_cast<int> (index)).getWritePointer (0),
  160. buffersImpulseSegments.getReference (static_cast<int> (i)).getWritePointer (0),
  161. outputTempData);
  162. }
  163. }
  164. FloatVectorOperations::copy (outputData, outputTempData, static_cast<int> (FFTSize + 1));
  165. convolutionProcessingAndAccumulate (buffersInputSegments.getReference (static_cast<int> (currentSegment)).getWritePointer (0),
  166. buffersImpulseSegments.getReference (0).getWritePointer (0),
  167. outputData);
  168. // Inverse FFT
  169. updateSymmetricFrequencyDomainData (outputData);
  170. FFTobject->performRealOnlyInverseTransform (outputData);
  171. // Add overlap
  172. for (size_t i = 0; i < numSamplesToProcess; ++i)
  173. output[i + numSamplesProcessed] = outputData[inputDataPos + i] + overlapData[inputDataPos + i];
  174. // Input buffer full => Next block
  175. inputDataPos += numSamplesToProcess;
  176. if (inputDataPos == blockSize)
  177. {
  178. // Input buffer is empty again now
  179. FloatVectorOperations::fill (inputData, 0.0f, static_cast<int> (FFTSize));
  180. inputDataPos = 0;
  181. // Extra step for segSize > blockSize
  182. FloatVectorOperations::add (&(outputData[blockSize]), &(overlapData[blockSize]), static_cast<int> (FFTSize - 2 * blockSize));
  183. // Save the overlap
  184. FloatVectorOperations::copy (overlapData, &(outputData[blockSize]), static_cast<int> (FFTSize - blockSize));
  185. // Update current segment
  186. currentSegment = (currentSegment > 0) ? (currentSegment - 1) : (numInputSegments - 1);
  187. }
  188. numSamplesProcessed += numSamplesToProcess;
  189. }
  190. }
  191. /** After each FFT, this function is called to allow convolution to be performed with only 4 SIMD functions calls. */
  192. void prepareForConvolution (float *samples) noexcept
  193. {
  194. auto FFTSizeDiv2 = FFTSize / 2;
  195. for (size_t i = 0; i < FFTSizeDiv2; i++)
  196. samples[i] = samples[2 * i];
  197. samples[FFTSizeDiv2] = 0;
  198. for (size_t i = 1; i < FFTSizeDiv2; i++)
  199. samples[i + FFTSizeDiv2] = -samples[2 * (FFTSize - i) + 1];
  200. }
  201. /** Does the convolution operation itself only on half of the frequency domain samples. */
  202. void convolutionProcessingAndAccumulate (const float *input, const float *impulse, float *output)
  203. {
  204. auto FFTSizeDiv2 = FFTSize / 2;
  205. FloatVectorOperations::addWithMultiply (output, input, impulse, static_cast<int> (FFTSizeDiv2));
  206. FloatVectorOperations::subtractWithMultiply (output, &(input[FFTSizeDiv2]), &(impulse[FFTSizeDiv2]), static_cast<int> (FFTSizeDiv2));
  207. FloatVectorOperations::addWithMultiply (&(output[FFTSizeDiv2]), input, &(impulse[FFTSizeDiv2]), static_cast<int> (FFTSizeDiv2));
  208. FloatVectorOperations::addWithMultiply (&(output[FFTSizeDiv2]), &(input[FFTSizeDiv2]), impulse, static_cast<int> (FFTSizeDiv2));
  209. }
  210. /** Undo the re-organization of samples from the function prepareForConvolution.
  211. Then, takes the conjugate of the frequency domain first half of samples, to fill the
  212. second half, so that the inverse transform will return real samples in the time domain.
  213. */
  214. void updateSymmetricFrequencyDomainData (float* samples) noexcept
  215. {
  216. auto FFTSizeDiv2 = FFTSize / 2;
  217. for (size_t i = 1; i < FFTSizeDiv2; i++)
  218. {
  219. samples[2 * (FFTSize - i)] = samples[i];
  220. samples[2 * (FFTSize - i) + 1] = -samples[FFTSizeDiv2 + i];
  221. }
  222. samples[1] = 0.f;
  223. for (size_t i = 1; i < FFTSizeDiv2; i++)
  224. {
  225. samples[2 * i] = samples[2 * (FFTSize - i)];
  226. samples[2 * i + 1] = -samples[2 * (FFTSize - i) + 1];
  227. }
  228. }
  229. //==============================================================================
  230. ScopedPointer<FFT> FFTobject;
  231. size_t FFTSize = 0;
  232. size_t currentSegment = 0, numInputSegments = 0, numSegments = 0, blockSize = 0, inputDataPos = 0;
  233. AudioBuffer<float> bufferInput, bufferOutput, bufferTempOutput, bufferOverlap;
  234. Array<AudioBuffer<float>> buffersInputSegments, buffersImpulseSegments;
  235. bool isReady = false;
  236. //==============================================================================
  237. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConvolutionEngine)
  238. };
  239. //==============================================================================
  240. /** Manages all the changes requested by the main convolution engine, to minimize
  241. the number of calls of the convolution engine initialization, and the potential
  242. consequences of multiple quick calls to the function Convolution::loadImpulseResponse.
  243. */
  244. struct Convolution::Pimpl : private Thread
  245. {
  246. public:
  247. enum class ChangeRequest
  248. {
  249. changeEngine = 0,
  250. changeSampleRate,
  251. changeMaximumBufferSize,
  252. changeSource,
  253. changeImpulseResponseSize,
  254. changeStereo,
  255. numChangeRequestTypes
  256. };
  257. using SourceType = ConvolutionEngine::ProcessingInformation::SourceType;
  258. //==============================================================================
  259. Pimpl() : Thread ("Convolution"), abstractFifo (fifoSize)
  260. {
  261. abstractFifo.reset();
  262. requestsType.resize (fifoSize);
  263. requestsParameter.resize (fifoSize);
  264. for (auto i = 0u; i < 4; ++i)
  265. engines.add (new ConvolutionEngine());
  266. currentInfo.maximumBufferSize = 0;
  267. currentInfo.buffer = &impulseResponse;
  268. }
  269. ~Pimpl()
  270. {
  271. stopThread (10000);
  272. }
  273. //==============================================================================
  274. /** Adds a new change request. */
  275. void addToFifo (ChangeRequest type, juce::var parameter)
  276. {
  277. int start1, size1, start2, size2;
  278. abstractFifo.prepareToWrite (1, start1, size1, start2, size2);
  279. if (size1 > 0)
  280. {
  281. requestsType.setUnchecked (start1, type);
  282. requestsParameter.setUnchecked (start1, parameter);
  283. }
  284. if (size2 > 0)
  285. {
  286. requestsType.setUnchecked (start2, type);
  287. requestsParameter.setUnchecked (start2, parameter);
  288. }
  289. abstractFifo.finishedWrite (size1 + size2);
  290. }
  291. /** Adds a new array of change requests. */
  292. void addToFifo (ChangeRequest* types, juce::var* parameters, int numEntries)
  293. {
  294. int start1, size1, start2, size2;
  295. abstractFifo.prepareToWrite (numEntries, start1, size1, start2, size2);
  296. if (size1 > 0)
  297. {
  298. for (int i = 0; i < size1; ++i)
  299. {
  300. requestsType.setUnchecked (start1 + i, types[i]);
  301. requestsParameter.setUnchecked (start1 + i, parameters[i]);
  302. }
  303. }
  304. if (size2 > 0)
  305. {
  306. for (int i = 0; i < size2; ++i)
  307. {
  308. requestsType.setUnchecked (start2 + i, types[i + size1]);
  309. requestsParameter.setUnchecked (start2 + i, parameters[i + size1]);
  310. }
  311. }
  312. abstractFifo.finishedWrite (size1 + size2);
  313. }
  314. /** Reads requests from the fifo */
  315. void readFromFifo (ChangeRequest& type, juce::var& parameter)
  316. {
  317. int start1, size1, start2, size2;
  318. abstractFifo.prepareToRead (1, start1, size1, start2, size2);
  319. if (size1 > 0)
  320. {
  321. type = requestsType[start1];
  322. parameter = requestsParameter[start1];
  323. }
  324. if (size2 > 0)
  325. {
  326. type = requestsType[start2];
  327. parameter = requestsParameter[start2];
  328. }
  329. abstractFifo.finishedRead (size1 + size2);
  330. }
  331. /** Returns the number of requests that still need to be processed */
  332. int getNumRemainingEntries() const noexcept
  333. {
  334. return abstractFifo.getNumReady();
  335. }
  336. //==============================================================================
  337. /** This function processes all the change requests to remove all the the
  338. redundant ones, and to tell what kind of initialization must be done.
  339. Depending on the results, the convolution engines might be reset, or
  340. simply updated, or they might not need any change at all.
  341. */
  342. void processFifo()
  343. {
  344. if (getNumRemainingEntries() == 0 || isThreadRunning() || mustInterpolate)
  345. return;
  346. // retrieve the information from the FIFO for processing
  347. Array<ChangeRequest> requests;
  348. Array<juce::var> requestParameters;
  349. while (getNumRemainingEntries() > 0)
  350. {
  351. ChangeRequest type = ChangeRequest::changeEngine;
  352. juce::var parameter;
  353. readFromFifo (type, parameter);
  354. requests.add (type);
  355. requestParameters.add (parameter);
  356. }
  357. // remove any useless messages
  358. for (int i = 0; i < (int) ChangeRequest::numChangeRequestTypes; ++i)
  359. {
  360. bool exists = false;
  361. for (int n = requests.size(); --n >= 0;)
  362. {
  363. if (requests[n] == (ChangeRequest) i)
  364. {
  365. if (! exists)
  366. {
  367. exists = true;
  368. }
  369. else
  370. {
  371. requests.remove (n);
  372. requestParameters.remove (n);
  373. }
  374. }
  375. }
  376. }
  377. changeLevel = 0;
  378. for (int n = 0; n < requests.size(); ++n)
  379. {
  380. switch (requests[n])
  381. {
  382. case ChangeRequest::changeEngine:
  383. changeLevel = 3;
  384. break;
  385. case ChangeRequest::changeSampleRate:
  386. {
  387. double newSampleRate = requestParameters[n];
  388. if (currentInfo.sampleRate != newSampleRate)
  389. changeLevel = 3;
  390. currentInfo.sampleRate = newSampleRate;
  391. }
  392. break;
  393. case ChangeRequest::changeMaximumBufferSize:
  394. {
  395. int newMaximumBufferSize = requestParameters[n];
  396. if (currentInfo.maximumBufferSize != (size_t) newMaximumBufferSize)
  397. changeLevel = 3;
  398. currentInfo.maximumBufferSize = (size_t) newMaximumBufferSize;
  399. }
  400. break;
  401. case ChangeRequest::changeSource:
  402. {
  403. auto* arrayParameters = requestParameters[n].getArray();
  404. auto newSourceType = static_cast<SourceType> (static_cast<int> (arrayParameters->getUnchecked (0)));
  405. if (currentInfo.sourceType != newSourceType)
  406. changeLevel = jmax (2, changeLevel);
  407. if (newSourceType == SourceType::sourceBinaryData)
  408. {
  409. auto& prm = arrayParameters->getRawDataPointer()[1];
  410. auto* newMemoryBlock = prm.getBinaryData();
  411. auto* newPtr = newMemoryBlock->getData();
  412. auto newSize = newMemoryBlock->getSize();
  413. if (currentInfo.sourceData != newPtr || currentInfo.sourceDataSize != newSize)
  414. changeLevel = jmax (2, changeLevel);
  415. currentInfo.sourceType = SourceType::sourceBinaryData;
  416. currentInfo.sourceData = newPtr;
  417. currentInfo.sourceDataSize = newSize;
  418. currentInfo.fileImpulseResponse = File();
  419. }
  420. else if (newSourceType == SourceType::sourceAudioFile)
  421. {
  422. File newFile (arrayParameters->getUnchecked (1).toString());
  423. if (currentInfo.fileImpulseResponse != newFile)
  424. changeLevel = jmax (2, changeLevel);
  425. currentInfo.sourceType = SourceType::sourceAudioFile;
  426. currentInfo.fileImpulseResponse = newFile;
  427. currentInfo.sourceData = nullptr;
  428. currentInfo.sourceDataSize = 0;
  429. }
  430. else if (newSourceType == SourceType::sourceAudioBuffer)
  431. {
  432. double bufferSampleRate (arrayParameters->getUnchecked (1));
  433. changeLevel = jmax (2, changeLevel);
  434. currentInfo.sourceType = SourceType::sourceAudioBuffer;
  435. currentInfo.bufferSampleRate = bufferSampleRate;
  436. currentInfo.fileImpulseResponse = File();
  437. currentInfo.sourceData = nullptr;
  438. currentInfo.sourceDataSize = 0;
  439. }
  440. }
  441. break;
  442. case ChangeRequest::changeImpulseResponseSize:
  443. {
  444. int64 newSize = requestParameters[n];
  445. if (currentInfo.impulseResponseSize != (size_t) newSize)
  446. changeLevel = jmax (1, changeLevel);
  447. currentInfo.impulseResponseSize = (size_t) newSize;
  448. }
  449. break;
  450. case ChangeRequest::changeStereo:
  451. {
  452. bool newWantsStereo = requestParameters[n];
  453. if (currentInfo.wantsStereo != newWantsStereo)
  454. changeLevel = jmax (1, changeLevel);
  455. currentInfo.wantsStereo = newWantsStereo;
  456. }
  457. break;
  458. default:
  459. jassertfalse;
  460. break;
  461. }
  462. }
  463. if (currentInfo.sourceType == SourceType::sourceNone)
  464. {
  465. currentInfo.sourceType = SourceType::sourceAudioBuffer;
  466. if (currentInfo.sampleRate == 0)
  467. currentInfo.sampleRate = 44100;
  468. if (currentInfo.maximumBufferSize == 0)
  469. currentInfo.maximumBufferSize = 128;
  470. currentInfo.bufferSampleRate = currentInfo.sampleRate;
  471. currentInfo.impulseResponseSize = 1;
  472. currentInfo.fileImpulseResponse = File();
  473. currentInfo.sourceData = nullptr;
  474. currentInfo.sourceDataSize = 0;
  475. AudioBuffer<float> newBuffer;
  476. newBuffer.setSize (1, 1);
  477. newBuffer.setSample (0, 0, 1.f);
  478. copyBufferToTemporaryLocation (newBuffer);
  479. }
  480. // action depending on the change level
  481. if (changeLevel == 3)
  482. {
  483. interpolationBuffer.setSize (2, static_cast<int> (currentInfo.maximumBufferSize));
  484. processImpulseResponse();
  485. initializeConvolutionEngines();
  486. }
  487. else if (changeLevel == 2)
  488. {
  489. startThread();
  490. }
  491. else if (changeLevel == 1)
  492. {
  493. startThread();
  494. }
  495. }
  496. //==============================================================================
  497. void copyBufferToTemporaryLocation (const AudioBuffer<float>& buffer)
  498. {
  499. const SpinLock::ScopedLockType sl (processLock);
  500. auto numChannels = buffer.getNumChannels() > 1 ? 2 : 1;
  501. temporaryBuffer.setSize (numChannels, buffer.getNumSamples(), false, false, true);
  502. for (auto channel = 0; channel < numChannels; ++channel)
  503. temporaryBuffer.copyFrom (channel, 0, buffer, channel, 0, buffer.getNumSamples());
  504. }
  505. /** Copies a buffer from a temporary location to the impulseResponseOriginal
  506. buffer for the sourceAudioBuffer. */
  507. void copyBufferFromTemporaryLocation()
  508. {
  509. const SpinLock::ScopedLockType sl (processLock);
  510. impulseResponseOriginal.setSize (2, temporaryBuffer.getNumSamples(), false, false, true);
  511. for (auto channel = 0; channel < temporaryBuffer.getNumChannels(); ++channel)
  512. impulseResponseOriginal.copyFrom (channel, 0, temporaryBuffer, channel, 0, temporaryBuffer.getNumSamples());
  513. }
  514. //==============================================================================
  515. void reset()
  516. {
  517. for (auto* e : engines)
  518. e->reset();
  519. }
  520. /** Convolution processing handling interpolation between previous and new states
  521. of the convolution engines.
  522. */
  523. void processSamples (const AudioBlock<float>& input, AudioBlock<float>& output)
  524. {
  525. processFifo();
  526. size_t numChannels = input.getNumChannels();
  527. size_t numSamples = jmin (input.getNumSamples(), output.getNumSamples());
  528. if (mustInterpolate == false)
  529. {
  530. for (size_t channel = 0; channel < numChannels; ++channel)
  531. engines[(int) channel]->processSamples (input.getChannelPointer (channel), output.getChannelPointer (channel), numSamples);
  532. }
  533. else
  534. {
  535. auto interpolated = AudioBlock<float> (interpolationBuffer).getSubBlock (0, numSamples);
  536. for (size_t channel = 0; channel < numChannels; ++channel)
  537. {
  538. auto&& buffer = output.getSingleChannelBlock (channel);
  539. interpolationBuffer.copyFrom ((int) channel, 0, input.getChannelPointer (channel), (int) numSamples);
  540. engines[(int) channel]->processSamples (input.getChannelPointer (channel), buffer.getChannelPointer (0), numSamples);
  541. changeVolumes[channel].applyGain (buffer.getChannelPointer (0), (int) numSamples);
  542. auto* interPtr = interpolationBuffer.getWritePointer ((int) channel);
  543. engines[(int) channel + 2]->processSamples (interPtr, interPtr, numSamples);
  544. changeVolumes[channel + 2].applyGain (interPtr, (int) numSamples);
  545. buffer += interpolated.getSingleChannelBlock (channel);
  546. }
  547. if (changeVolumes[0].isSmoothing() == false)
  548. {
  549. mustInterpolate = false;
  550. for (auto channel = 0; channel < 2; ++channel)
  551. engines[channel]->copyStateFromOtherEngine (*engines[channel + 2]);
  552. }
  553. }
  554. }
  555. private:
  556. //==============================================================================
  557. void run() override
  558. {
  559. if (changeLevel == 2)
  560. {
  561. processImpulseResponse();
  562. if (isThreadRunning() && threadShouldExit())
  563. return;
  564. initializeConvolutionEngines();
  565. }
  566. else if (changeLevel == 1)
  567. {
  568. initializeConvolutionEngines();
  569. }
  570. }
  571. void processImpulseResponse()
  572. {
  573. if (currentInfo.sourceType == SourceType::sourceBinaryData)
  574. {
  575. copyAudioStreamInAudioBuffer (new MemoryInputStream (currentInfo.sourceData, currentInfo.sourceDataSize, false));
  576. }
  577. else if (currentInfo.sourceType == SourceType::sourceAudioFile)
  578. {
  579. copyAudioStreamInAudioBuffer (new FileInputStream (currentInfo.fileImpulseResponse));
  580. }
  581. else if (currentInfo.sourceType == SourceType::sourceAudioBuffer)
  582. {
  583. copyBufferFromTemporaryLocation();
  584. trimAndResampleImpulseResponse (temporaryBuffer.getNumChannels(), currentInfo.bufferSampleRate);
  585. }
  586. if (isThreadRunning() && threadShouldExit())
  587. return;
  588. if (currentInfo.wantsStereo)
  589. {
  590. normalizeImpulseResponse (currentInfo.buffer->getWritePointer(0), currentInfo.buffer->getNumSamples());
  591. normalizeImpulseResponse (currentInfo.buffer->getWritePointer(1), currentInfo.buffer->getNumSamples());
  592. }
  593. else
  594. {
  595. normalizeImpulseResponse (currentInfo.buffer->getWritePointer (0), currentInfo.buffer->getNumSamples());
  596. }
  597. }
  598. /** Converts the data from an audio file into a stereo audio buffer of floats, and
  599. performs resampling if necessary.
  600. */
  601. void copyAudioStreamInAudioBuffer (InputStream* stream)
  602. {
  603. AudioFormatManager manager;
  604. manager.registerBasicFormats();
  605. if (ScopedPointer<AudioFormatReader> formatReader = manager.createReaderFor (stream))
  606. {
  607. auto maximumTimeInSeconds = 10.0;
  608. int64 maximumLength = static_cast<int64> (roundDoubleToInt (maximumTimeInSeconds * formatReader->sampleRate));
  609. auto numChannels = formatReader->numChannels > 1 ? 2 : 1;
  610. impulseResponseOriginal.setSize (2, static_cast<int> (jmin (maximumLength, formatReader->lengthInSamples)), false, false, true);
  611. impulseResponseOriginal.clear();
  612. formatReader->read (&(impulseResponseOriginal), 0, impulseResponseOriginal.getNumSamples(), 0, true, numChannels > 1);
  613. trimAndResampleImpulseResponse (numChannels, formatReader->sampleRate);
  614. }
  615. }
  616. void trimAndResampleImpulseResponse (int numChannels, double bufferSampleRate)
  617. {
  618. auto thresholdTrim = Decibels::decibelsToGain (-80.0f);
  619. auto indexStart = impulseResponseOriginal.getNumSamples() - 1;
  620. auto indexEnd = 0;
  621. for (auto channel = 0; channel < numChannels; ++channel)
  622. {
  623. auto localIndexStart = 0;
  624. auto localIndexEnd = impulseResponseOriginal.getNumSamples() - 1;
  625. auto* channelData = impulseResponseOriginal.getReadPointer (channel);
  626. while (localIndexStart < impulseResponseOriginal.getNumSamples() - 1
  627. && channelData[localIndexStart] <= thresholdTrim
  628. && channelData[localIndexStart] >= -thresholdTrim)
  629. ++localIndexStart;
  630. while (localIndexEnd >= 0
  631. && channelData[localIndexEnd] <= thresholdTrim
  632. && channelData[localIndexEnd] >= -thresholdTrim)
  633. --localIndexEnd;
  634. indexStart = jmin (indexStart, localIndexStart);
  635. indexEnd = jmax (indexEnd, localIndexEnd);
  636. }
  637. if (indexStart > 0)
  638. {
  639. for (auto channel = 0; channel < numChannels; ++channel)
  640. {
  641. auto* channelData = impulseResponseOriginal.getWritePointer (channel);
  642. for (auto i = 0; i < indexEnd - indexStart + 1; ++i)
  643. channelData[i] = channelData[i + indexStart];
  644. for (auto i = indexEnd - indexStart + 1; i < impulseResponseOriginal.getNumSamples() - 1; ++i)
  645. channelData[i] = 0.0f;
  646. }
  647. }
  648. if (currentInfo.sampleRate == bufferSampleRate)
  649. {
  650. // No resampling
  651. auto impulseSize = jmin (static_cast<int> (currentInfo.impulseResponseSize), indexEnd - indexStart + 1);
  652. impulseResponse.setSize (2, impulseSize);
  653. impulseResponse.clear();
  654. for (auto channel = 0; channel < numChannels; ++channel)
  655. impulseResponse.copyFrom (channel, 0, impulseResponseOriginal, channel, 0, impulseSize);
  656. }
  657. else
  658. {
  659. // Resampling
  660. auto factorReading = bufferSampleRate / currentInfo.sampleRate;
  661. auto impulseSize = jmin (static_cast<int> (currentInfo.impulseResponseSize), roundDoubleToInt ((indexEnd - indexStart + 1) / factorReading));
  662. impulseResponse.setSize (2, impulseSize);
  663. impulseResponse.clear();
  664. MemoryAudioSource memorySource (impulseResponseOriginal, false);
  665. ResamplingAudioSource resamplingSource (&memorySource, false, numChannels);
  666. resamplingSource.setResamplingRatio (factorReading);
  667. resamplingSource.prepareToPlay (impulseSize, currentInfo.sampleRate);
  668. AudioSourceChannelInfo info;
  669. info.startSample = 0;
  670. info.numSamples = impulseSize;
  671. info.buffer = &impulseResponse;
  672. resamplingSource.getNextAudioBlock (info);
  673. }
  674. // Filling the second channel with the first if necessary
  675. if (numChannels == 1)
  676. impulseResponse.copyFrom (1, 0, impulseResponse, 0, 0, impulseResponse.getNumSamples());
  677. }
  678. void normalizeImpulseResponse (float* samples, int numSamples) const
  679. {
  680. auto magnitude = 0.0f;
  681. for (int i = 0; i < numSamples; ++i)
  682. magnitude += samples[i] * samples[i];
  683. auto magnitudeInv = 1.0f / (4.0f * std::sqrt (magnitude));
  684. for (int i = 0; i < numSamples; ++i)
  685. samples[i] *= magnitudeInv;
  686. }
  687. void initializeConvolutionEngines()
  688. {
  689. if (currentInfo.maximumBufferSize == 0)
  690. return;
  691. auto numChannels = (currentInfo.wantsStereo ? 2 : 1);
  692. if (changeLevel == 3)
  693. {
  694. for (int i = 0; i < numChannels; ++i)
  695. engines[i]->initializeConvolutionEngine (currentInfo, i);
  696. if (numChannels == 1)
  697. engines[1]->copyStateFromOtherEngine (*engines[0]);
  698. mustInterpolate = false;
  699. }
  700. else
  701. {
  702. for (int i = 0; i < numChannels; ++i)
  703. {
  704. engines[i + 2]->initializeConvolutionEngine (currentInfo, i);
  705. engines[i + 2]->reset();
  706. if (isThreadRunning() && threadShouldExit())
  707. return;
  708. }
  709. if (numChannels == 1)
  710. engines[3]->copyStateFromOtherEngine (*engines[2]);
  711. for (size_t i = 0; i < 2; ++i)
  712. {
  713. changeVolumes[i].setValue (1.0f);
  714. changeVolumes[i].reset (currentInfo.sampleRate, 0.05);
  715. changeVolumes[i].setValue (0.0f);
  716. changeVolumes[i + 2].setValue (0.0f);
  717. changeVolumes[i + 2].reset (currentInfo.sampleRate, 0.05);
  718. changeVolumes[i + 2].setValue (1.0f);
  719. }
  720. mustInterpolate = true;
  721. }
  722. }
  723. //==============================================================================
  724. static constexpr int fifoSize = 256; // the size of the fifo which handles all the change requests
  725. AbstractFifo abstractFifo; // the abstract fifo
  726. Array<ChangeRequest> requestsType; // an array of ChangeRequest
  727. Array<juce::var> requestsParameter; // an array of change parameters
  728. int changeLevel = 0; // the current level of requested change in the convolution engine
  729. //==============================================================================
  730. ConvolutionEngine::ProcessingInformation currentInfo; // the information about the impulse response to load
  731. AudioBuffer<float> temporaryBuffer; // a temporary buffer that is used when the function copyAndLoadImpulseResponse is called in the main API
  732. SpinLock processLock; // a necessary lock to use with this temporary buffer
  733. AudioBuffer<float> impulseResponseOriginal; // a buffer with the original impulse response
  734. AudioBuffer<float> impulseResponse; // a buffer with the impulse response trimmed, resampled, resized and normalized
  735. //==============================================================================
  736. OwnedArray<ConvolutionEngine> engines; // the 4 convolution engines being used
  737. AudioBuffer<float> interpolationBuffer; // a buffer to do the interpolation between the convolution engines 0-1 and 2-3
  738. LinearSmoothedValue<float> changeVolumes[4]; // the volumes for each convolution engine during interpolation
  739. bool mustInterpolate = false; // tells if the convolution engines outputs must be currently interpolated
  740. //==============================================================================
  741. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  742. };
  743. //==============================================================================
  744. Convolution::Convolution()
  745. {
  746. pimpl = new Pimpl();
  747. pimpl->addToFifo (Convolution::Pimpl::ChangeRequest::changeEngine, juce::var (0));
  748. }
  749. Convolution::~Convolution()
  750. {
  751. }
  752. void Convolution::loadImpulseResponse (const void* sourceData, size_t sourceDataSize, bool wantsStereo, size_t size)
  753. {
  754. if (sourceData == nullptr)
  755. return;
  756. Pimpl::ChangeRequest types[] = { Pimpl::ChangeRequest::changeSource,
  757. Pimpl::ChangeRequest::changeImpulseResponseSize,
  758. Pimpl::ChangeRequest::changeStereo };
  759. Array<juce::var> sourceParameter;
  760. sourceParameter.add (juce::var ((int) ConvolutionEngine::ProcessingInformation::SourceType::sourceBinaryData));
  761. sourceParameter.add (juce::var (sourceData, sourceDataSize));
  762. juce::var parameters[] = { juce::var (sourceParameter),
  763. juce::var (static_cast<int64> (size)),
  764. juce::var (wantsStereo) };
  765. pimpl->addToFifo (types, parameters, 3);
  766. }
  767. void Convolution::loadImpulseResponse (const File& fileImpulseResponse, bool wantsStereo, size_t size)
  768. {
  769. if (! fileImpulseResponse.existsAsFile())
  770. return;
  771. Pimpl::ChangeRequest types[] = { Pimpl::ChangeRequest::changeSource,
  772. Pimpl::ChangeRequest::changeImpulseResponseSize,
  773. Pimpl::ChangeRequest::changeStereo };
  774. Array<juce::var> sourceParameter;
  775. sourceParameter.add (juce::var ((int) ConvolutionEngine::ProcessingInformation::SourceType::sourceAudioFile));
  776. sourceParameter.add (juce::var (fileImpulseResponse.getFullPathName()));
  777. juce::var parameters[] = { juce::var (sourceParameter),
  778. juce::var (static_cast<int64> (size)),
  779. juce::var (wantsStereo) };
  780. pimpl->addToFifo (types, parameters, 3);
  781. }
  782. void Convolution::copyAndLoadImpulseResponseFromBuffer (const AudioBuffer<float>& buffer,
  783. double bufferSampleRate, bool wantsStereo, size_t size)
  784. {
  785. jassert (bufferSampleRate > 0);
  786. if (buffer.getNumSamples() == 0)
  787. return;
  788. pimpl->copyBufferToTemporaryLocation (buffer);
  789. Pimpl::ChangeRequest types[] = { Pimpl::ChangeRequest::changeSource,
  790. Pimpl::ChangeRequest::changeImpulseResponseSize,
  791. Pimpl::ChangeRequest::changeStereo };
  792. Array<juce::var> sourceParameter;
  793. sourceParameter.add (juce::var ((int) ConvolutionEngine::ProcessingInformation::SourceType::sourceAudioBuffer));
  794. sourceParameter.add (juce::var (bufferSampleRate));
  795. juce::var parameters[] = { juce::var (sourceParameter),
  796. juce::var (static_cast<int64> (size)),
  797. juce::var (wantsStereo) };
  798. pimpl->addToFifo (types, parameters, 3);
  799. }
  800. void Convolution::prepare (const ProcessSpec& spec)
  801. {
  802. jassert (isPositiveAndBelow (spec.numChannels, static_cast<uint32> (3))); // only mono and stereo is supported
  803. Pimpl::ChangeRequest types[] = { Pimpl::ChangeRequest::changeSampleRate,
  804. Pimpl::ChangeRequest::changeMaximumBufferSize };
  805. juce::var parameters[] = { juce::var (spec.sampleRate),
  806. juce::var (static_cast<int> (spec.maximumBlockSize)) };
  807. pimpl->addToFifo (types, parameters, 2);
  808. for (size_t channel = 0; channel < spec.numChannels; ++channel)
  809. {
  810. volumeDry[channel].reset (spec.sampleRate, 0.05);
  811. volumeWet[channel].reset (spec.sampleRate, 0.05);
  812. }
  813. sampleRate = spec.sampleRate;
  814. dryBuffer = AudioBlock<float> (dryBufferStorage,
  815. jmin (spec.numChannels, 2u),
  816. spec.maximumBlockSize);
  817. }
  818. void Convolution::reset() noexcept
  819. {
  820. dryBuffer.clear();
  821. pimpl->reset();
  822. }
  823. void Convolution::processSamples (const AudioBlock<float>& input, AudioBlock<float>& output, bool isBypassed) noexcept
  824. {
  825. jassert (input.getNumChannels() == output.getNumChannels());
  826. jassert (isPositiveAndBelow (input.getNumChannels(), static_cast<size_t> (3))); // only mono and stereo is supported
  827. auto numChannels = input.getNumChannels();
  828. auto numSamples = jmin (input.getNumSamples(), output.getNumSamples());
  829. auto dry = dryBuffer.getSubsetChannelBlock (0, numChannels);
  830. if (volumeDry[0].isSmoothing())
  831. {
  832. dry.copy (input);
  833. for (size_t channel = 0; channel < numChannels; ++channel)
  834. volumeDry[channel].applyGain (dry.getChannelPointer (channel), (int) numSamples);
  835. pimpl->processSamples (input, output);
  836. for (size_t channel = 0; channel < numChannels; ++channel)
  837. volumeWet[channel].applyGain (output.getChannelPointer (channel), (int) numSamples);
  838. output += dry;
  839. }
  840. else
  841. {
  842. if (! currentIsBypassed)
  843. pimpl->processSamples (input, output);
  844. if (isBypassed != currentIsBypassed)
  845. {
  846. currentIsBypassed = isBypassed;
  847. for (size_t channel = 0; channel < numChannels; ++channel)
  848. {
  849. volumeDry[channel].setValue (isBypassed ? 0.0f : 1.0f);
  850. volumeDry[channel].reset (sampleRate, 0.05);
  851. volumeDry[channel].setValue (isBypassed ? 1.0f : 0.0f);
  852. volumeWet[channel].setValue (isBypassed ? 1.0f : 0.0f);
  853. volumeWet[channel].reset (sampleRate, 0.05);
  854. volumeWet[channel].setValue (isBypassed ? 0.0f : 1.0f);
  855. }
  856. }
  857. }
  858. }