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.

287 lines
11KB

  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. #include "PluginProcessor.h"
  20. #include "PluginEditor.h"
  21. //==============================================================================
  22. DspModulePluginDemoAudioProcessor::DspModulePluginDemoAudioProcessor()
  23. : AudioProcessor (BusesProperties()
  24. .withInput ("Input", AudioChannelSet::stereo(), true)
  25. .withOutput ("Output", AudioChannelSet::stereo(), true)),
  26. lowPassFilter (dsp::IIR::Coefficients<float>::makeFirstOrderLowPass (48000.0, 20000.f)),
  27. highPassFilter (dsp::IIR::Coefficients<float>::makeFirstOrderHighPass (48000.0, 20.0f)),
  28. waveShapers { {std::tanh}, {dsp::FastMathApproximations::tanh} },
  29. clipping { clip }
  30. {
  31. // Oversampling 2 times with IIR filtering
  32. oversampling = new dsp::Oversampling<float> (2, 1, dsp::Oversampling<float>::filterHalfBandPolyphaseIIR, false);
  33. addParameter (inputVolumeParam = new AudioParameterFloat ("INPUT", "Input Volume", { 0.f, 60.f, 0.f, 1.0f }, 0.f, "dB"));
  34. addParameter (highPassFilterFreqParam = new AudioParameterFloat ("HPFREQ", "Pre Highpass Freq.", { 20.f, 20000.f, 0.f, 0.5f }, 20.f, "Hz"));
  35. addParameter (lowPassFilterFreqParam = new AudioParameterFloat ("LPFREQ", "Post Lowpass Freq.", { 20.f, 20000.f, 0.f, 0.5f }, 20000.f, "Hz"));
  36. addParameter (stereoParam = new AudioParameterChoice ("STEREO", "Stereo Processing", { "Always mono", "Yes" }, 1));
  37. addParameter (slopeParam = new AudioParameterChoice ("SLOPE", "Slope", { "-6 dB / octave", "-12 dB / octave" }, 0));
  38. addParameter (waveshaperParam = new AudioParameterChoice ("WVSHP", "Waveshaper", { "std::tanh", "Fast tanh approx." }, 0));
  39. addParameter (cabinetTypeParam = new AudioParameterChoice ("CABTYPE", "Cabinet Type", { "Guitar amplifier 8'' cabinet ",
  40. "Cassette recorder cabinet" }, 0));
  41. addParameter (cabinetSimParam = new AudioParameterBool ("CABSIM", "Cabinet Sim", false));
  42. addParameter (oversamplingParam = new AudioParameterBool ("OVERS", "Oversampling", false));
  43. addParameter (outputVolumeParam = new AudioParameterFloat ("OUTPUT", "Output Volume", { -40.f, 40.f, 0.f, 1.0f }, 0.f, "dB"));
  44. cabinetType.set (0);
  45. }
  46. DspModulePluginDemoAudioProcessor::~DspModulePluginDemoAudioProcessor()
  47. {
  48. }
  49. //==============================================================================
  50. bool DspModulePluginDemoAudioProcessor::isBusesLayoutSupported (const BusesLayout& layouts) const
  51. {
  52. // This is the place where you check if the layout is supported.
  53. // In this template code we only support mono or stereo.
  54. if (layouts.getMainOutputChannelSet() != AudioChannelSet::mono() && layouts.getMainOutputChannelSet() != AudioChannelSet::stereo())
  55. return false;
  56. // This checks if the input layout matches the output layout
  57. if (layouts.getMainOutputChannelSet() != layouts.getMainInputChannelSet())
  58. return false;
  59. return true;
  60. }
  61. void DspModulePluginDemoAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
  62. {
  63. auto channels = static_cast<uint32> (jmin (getMainBusNumInputChannels(), getMainBusNumOutputChannels()));
  64. dsp::ProcessSpec spec { sampleRate, static_cast<uint32> (samplesPerBlock), channels };
  65. lowPassFilter.prepare (spec);
  66. highPassFilter.prepare (spec);
  67. inputVolume.prepare (spec);
  68. outputVolume.prepare (spec);
  69. convolution.prepare (spec);
  70. cabinetType.set (-1);
  71. oversampling->initProcessing (static_cast<size_t> (samplesPerBlock));
  72. updateParameters();
  73. reset();
  74. }
  75. void DspModulePluginDemoAudioProcessor::reset()
  76. {
  77. lowPassFilter.reset();
  78. highPassFilter.reset();
  79. convolution.reset();
  80. oversampling->reset();
  81. }
  82. void DspModulePluginDemoAudioProcessor::releaseResources()
  83. {
  84. }
  85. void DspModulePluginDemoAudioProcessor::process (dsp::ProcessContextReplacing<float> context) noexcept
  86. {
  87. ScopedNoDenormals noDenormals;
  88. // Input volume applied with a LinearSmoothedValue
  89. inputVolume.process (context);
  90. // Pre-highpass filtering, very useful for distortion audio effects
  91. // Note : try frequencies around 700 Hz
  92. highPassFilter.process (context);
  93. // Upsampling
  94. dsp::AudioBlock<float> oversampledBlock;
  95. setLatencySamples (audioCurrentlyOversampled ? roundFloatToInt (oversampling->getLatencyInSamples()) : 0);
  96. if (audioCurrentlyOversampled)
  97. oversampledBlock = oversampling->processSamplesUp (context.getInputBlock());
  98. dsp::ProcessContextReplacing<float> waveshaperContext = audioCurrentlyOversampled ? dsp::ProcessContextReplacing<float> (oversampledBlock) : context;
  99. // Waveshaper processing, for distortion generation, thanks to the input gain
  100. // The fast tanh can be used instead of std::tanh to reduce the CPU load
  101. auto waveshaperIndex = waveshaperParam->getIndex();
  102. if (isPositiveAndBelow (waveshaperIndex, numWaveShapers) )
  103. {
  104. waveShapers[waveshaperIndex].process (waveshaperContext);
  105. if (waveshaperIndex == 1)
  106. clipping.process (waveshaperContext);
  107. waveshaperContext.getOutputBlock() *= 0.7f;
  108. }
  109. // Downsampling
  110. if (audioCurrentlyOversampled)
  111. oversampling->processSamplesDown (context.getOutputBlock());
  112. // Post-lowpass filtering
  113. lowPassFilter.process (context);
  114. // Convolution with the impulse response of a guitar cabinet
  115. auto wasBypassed = context.isBypassed;
  116. context.isBypassed = context.isBypassed || cabinetIsBypassed;
  117. convolution.process (context);
  118. context.isBypassed = wasBypassed;
  119. // Output volume applied with a LinearSmoothedValue
  120. outputVolume.process (context);
  121. }
  122. void DspModulePluginDemoAudioProcessor::processBlock (AudioBuffer<float>& inoutBuffer, MidiBuffer&)
  123. {
  124. auto totalNumInputChannels = getTotalNumInputChannels();
  125. auto totalNumOutputChannels = getTotalNumOutputChannels();
  126. auto numSamples = inoutBuffer.getNumSamples();
  127. for (auto i = jmin (2, totalNumInputChannels); i < totalNumOutputChannels; ++i)
  128. inoutBuffer.clear (i, 0, numSamples);
  129. updateParameters();
  130. dsp::AudioBlock<float> block (inoutBuffer);
  131. if (stereoParam->getIndex() == 1)
  132. {
  133. // Stereo processing mode:
  134. if (block.getNumChannels() > 2)
  135. block = block.getSubsetChannelBlock (0, 2);
  136. process (dsp::ProcessContextReplacing<float> (block));
  137. }
  138. else
  139. {
  140. // Mono processing mode:
  141. auto firstChan = block.getSingleChannelBlock (0);
  142. process (dsp::ProcessContextReplacing<float> (firstChan));
  143. for (size_t chan = 1; chan < block.getNumChannels(); ++chan)
  144. block.getSingleChannelBlock (chan).copy (firstChan);
  145. }
  146. }
  147. //==============================================================================
  148. bool DspModulePluginDemoAudioProcessor::hasEditor() const
  149. {
  150. return true;
  151. }
  152. AudioProcessorEditor* DspModulePluginDemoAudioProcessor::createEditor()
  153. {
  154. return new DspModulePluginDemoAudioProcessorEditor (*this);
  155. }
  156. //==============================================================================
  157. bool DspModulePluginDemoAudioProcessor::acceptsMidi() const
  158. {
  159. #if JucePlugin_WantsMidiInput
  160. return true;
  161. #else
  162. return false;
  163. #endif
  164. }
  165. bool DspModulePluginDemoAudioProcessor::producesMidi() const
  166. {
  167. #if JucePlugin_ProducesMidiOutput
  168. return true;
  169. #else
  170. return false;
  171. #endif
  172. }
  173. //==============================================================================
  174. void DspModulePluginDemoAudioProcessor::updateParameters()
  175. {
  176. auto newOversampling = oversamplingParam->get();
  177. if (newOversampling != audioCurrentlyOversampled)
  178. {
  179. audioCurrentlyOversampled = newOversampling;
  180. oversampling->reset();
  181. }
  182. //==============================================================================
  183. auto inputdB = Decibels::decibelsToGain (inputVolumeParam->get());
  184. auto outputdB = Decibels::decibelsToGain (outputVolumeParam->get());
  185. if (inputVolume.getGainLinear() != inputdB) inputVolume.setGainLinear (inputdB);
  186. if (outputVolume.getGainLinear() != outputdB) outputVolume.setGainLinear (outputdB);
  187. auto newSlopeType = slopeParam->getIndex();
  188. if (newSlopeType == 0)
  189. {
  190. *lowPassFilter.state = *dsp::IIR::Coefficients<float>::makeFirstOrderLowPass (getSampleRate(), lowPassFilterFreqParam->get());
  191. *highPassFilter.state = *dsp::IIR::Coefficients<float>::makeFirstOrderHighPass (getSampleRate(), highPassFilterFreqParam->get());
  192. }
  193. else
  194. {
  195. *lowPassFilter.state = *dsp::IIR::Coefficients<float>::makeLowPass (getSampleRate(), lowPassFilterFreqParam->get());
  196. *highPassFilter.state = *dsp::IIR::Coefficients<float>::makeHighPass (getSampleRate(), highPassFilterFreqParam->get());
  197. }
  198. //==============================================================================
  199. auto type = cabinetTypeParam->getIndex();
  200. auto currentType = cabinetType.get();
  201. if (type != currentType)
  202. {
  203. cabinetType.set(type);
  204. auto maxSize = static_cast<size_t> (roundDoubleToInt (8192 * getSampleRate() / 44100));
  205. if (type == 0)
  206. convolution.loadImpulseResponse (BinaryData::Impulse1_wav, BinaryData::Impulse1_wavSize, false, true, maxSize);
  207. else
  208. convolution.loadImpulseResponse (BinaryData::Impulse2_wav, BinaryData::Impulse2_wavSize, false, true, maxSize);
  209. }
  210. cabinetIsBypassed = ! cabinetSimParam->get();
  211. }
  212. //==============================================================================
  213. // This creates new instances of the plugin..
  214. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  215. {
  216. return new DspModulePluginDemoAudioProcessor();
  217. }