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.

204 lines
6.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: ConvolutionDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Convolution demo using the DSP module.
  24. dependencies: juce_audio_basics, juce_audio_devices, juce_audio_formats,
  25. juce_audio_processors, juce_audio_utils, juce_core,
  26. juce_data_structures, juce_dsp, juce_events, juce_graphics,
  27. juce_gui_basics, juce_gui_extra
  28. exporters: xcode_mac, vs2022, linux_make
  29. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  30. type: Component
  31. mainClass: ConvolutionDemo
  32. useLocalCopy: 1
  33. END_JUCE_PIP_METADATA
  34. *******************************************************************************/
  35. #pragma once
  36. #include "../Assets/DemoUtilities.h"
  37. #include "../Assets/DSPDemos_Common.h"
  38. using namespace dsp;
  39. //==============================================================================
  40. struct ConvolutionDemoDSP
  41. {
  42. void prepare (const ProcessSpec& spec)
  43. {
  44. sampleRate = spec.sampleRate;
  45. convolution.prepare (spec);
  46. updateParameters();
  47. }
  48. void process (ProcessContextReplacing<float> context)
  49. {
  50. context.isBypassed = bypass;
  51. // Load a new IR if there's one available. Note that this doesn't lock or allocate!
  52. bufferTransfer.get ([this] (BufferWithSampleRate& buf)
  53. {
  54. convolution.loadImpulseResponse (std::move (buf.buffer),
  55. buf.sampleRate,
  56. Convolution::Stereo::yes,
  57. Convolution::Trim::yes,
  58. Convolution::Normalise::yes);
  59. });
  60. convolution.process (context);
  61. }
  62. void reset()
  63. {
  64. convolution.reset();
  65. }
  66. void updateParameters()
  67. {
  68. auto* cabinetTypeParameter = dynamic_cast<ChoiceParameter*> (parameters[0]);
  69. if (cabinetTypeParameter == nullptr)
  70. {
  71. jassertfalse;
  72. return;
  73. }
  74. if (cabinetTypeParameter->getCurrentSelectedID() == 1)
  75. {
  76. bypass = true;
  77. }
  78. else
  79. {
  80. bypass = false;
  81. auto selectedType = cabinetTypeParameter->getCurrentSelectedID();
  82. auto assetName = (selectedType == 2 ? "guitar_amp.wav" : "cassette_recorder.wav");
  83. auto assetInputStream = createAssetInputStream (assetName);
  84. if (assetInputStream == nullptr)
  85. {
  86. jassertfalse;
  87. return;
  88. }
  89. AudioFormatManager manager;
  90. manager.registerBasicFormats();
  91. std::unique_ptr<AudioFormatReader> reader { manager.createReaderFor (std::move (assetInputStream)) };
  92. if (reader == nullptr)
  93. {
  94. jassertfalse;
  95. return;
  96. }
  97. AudioBuffer<float> buffer (static_cast<int> (reader->numChannels),
  98. static_cast<int> (reader->lengthInSamples));
  99. reader->read (buffer.getArrayOfWritePointers(), buffer.getNumChannels(), 0, buffer.getNumSamples());
  100. bufferTransfer.set (BufferWithSampleRate { std::move (buffer), reader->sampleRate });
  101. }
  102. }
  103. //==============================================================================
  104. struct BufferWithSampleRate
  105. {
  106. BufferWithSampleRate() = default;
  107. BufferWithSampleRate (AudioBuffer<float>&& bufferIn, double sampleRateIn)
  108. : buffer (std::move (bufferIn)), sampleRate (sampleRateIn) {}
  109. AudioBuffer<float> buffer;
  110. double sampleRate = 0.0;
  111. };
  112. class BufferTransfer
  113. {
  114. public:
  115. void set (BufferWithSampleRate&& p)
  116. {
  117. const SpinLock::ScopedLockType lock (mutex);
  118. buffer = std::move (p);
  119. newBuffer = true;
  120. }
  121. // Call `fn` passing the new buffer, if there's one available
  122. template <typename Fn>
  123. void get (Fn&& fn)
  124. {
  125. const SpinLock::ScopedTryLockType lock (mutex);
  126. if (lock.isLocked() && newBuffer)
  127. {
  128. fn (buffer);
  129. newBuffer = false;
  130. }
  131. }
  132. private:
  133. BufferWithSampleRate buffer;
  134. bool newBuffer = false;
  135. SpinLock mutex;
  136. };
  137. double sampleRate = 0.0;
  138. bool bypass = false;
  139. MemoryBlock currentCabinetData;
  140. Convolution convolution;
  141. BufferTransfer bufferTransfer;
  142. ChoiceParameter cabinetParam { { "Bypass", "Guitar amplifier 8''", "Cassette recorder" }, 1, "Cabinet Type" };
  143. std::vector<DSPDemoParameterBase*> parameters { &cabinetParam };
  144. };
  145. struct ConvolutionDemo final : public Component
  146. {
  147. ConvolutionDemo()
  148. {
  149. addAndMakeVisible (fileReaderComponent);
  150. setSize (750, 500);
  151. }
  152. void resized() override
  153. {
  154. fileReaderComponent.setBounds (getLocalBounds());
  155. }
  156. AudioFileReaderComponent<ConvolutionDemoDSP> fileReaderComponent;
  157. };