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.

257 lines
9.6KB

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