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 ? roundToInt (oversampling->getLatencyInSamples()) : 0);
  96. if (audioCurrentlyOversampled)
  97. oversampledBlock = oversampling->processSamplesUp (context.getInputBlock());
  98. auto waveshaperContext = audioCurrentlyOversampled ? dsp::ProcessContextReplacing<float> (oversampledBlock)
  99. : context;
  100. // Waveshaper processing, for distortion generation, thanks to the input gain
  101. // The fast tanh can be used instead of std::tanh to reduce the CPU load
  102. auto waveshaperIndex = waveshaperParam->getIndex();
  103. if (isPositiveAndBelow (waveshaperIndex, numWaveShapers) )
  104. {
  105. waveShapers[waveshaperIndex].process (waveshaperContext);
  106. if (waveshaperIndex == 1)
  107. clipping.process (waveshaperContext);
  108. waveshaperContext.getOutputBlock() *= 0.7f;
  109. }
  110. // Downsampling
  111. if (audioCurrentlyOversampled)
  112. oversampling->processSamplesDown (context.getOutputBlock());
  113. // Post-lowpass filtering
  114. lowPassFilter.process (context);
  115. // Convolution with the impulse response of a guitar cabinet
  116. auto wasBypassed = context.isBypassed;
  117. context.isBypassed = context.isBypassed || cabinetIsBypassed;
  118. convolution.process (context);
  119. context.isBypassed = wasBypassed;
  120. // Output volume applied with a LinearSmoothedValue
  121. outputVolume.process (context);
  122. }
  123. void DspModulePluginDemoAudioProcessor::processBlock (AudioBuffer<float>& inoutBuffer, MidiBuffer&)
  124. {
  125. auto totalNumInputChannels = getTotalNumInputChannels();
  126. auto totalNumOutputChannels = getTotalNumOutputChannels();
  127. auto numSamples = inoutBuffer.getNumSamples();
  128. for (auto i = jmin (2, totalNumInputChannels); i < totalNumOutputChannels; ++i)
  129. inoutBuffer.clear (i, 0, numSamples);
  130. updateParameters();
  131. dsp::AudioBlock<float> block (inoutBuffer);
  132. if (stereoParam->getIndex() == 1)
  133. {
  134. // Stereo processing mode:
  135. if (block.getNumChannels() > 2)
  136. block = block.getSubsetChannelBlock (0, 2);
  137. process (dsp::ProcessContextReplacing<float> (block));
  138. }
  139. else
  140. {
  141. // Mono processing mode:
  142. auto firstChan = block.getSingleChannelBlock (0);
  143. process (dsp::ProcessContextReplacing<float> (firstChan));
  144. for (size_t chan = 1; chan < block.getNumChannels(); ++chan)
  145. block.getSingleChannelBlock (chan).copy (firstChan);
  146. }
  147. }
  148. //==============================================================================
  149. bool DspModulePluginDemoAudioProcessor::hasEditor() const
  150. {
  151. return true;
  152. }
  153. AudioProcessorEditor* DspModulePluginDemoAudioProcessor::createEditor()
  154. {
  155. return new DspModulePluginDemoAudioProcessorEditor (*this);
  156. }
  157. //==============================================================================
  158. bool DspModulePluginDemoAudioProcessor::acceptsMidi() const
  159. {
  160. #if JucePlugin_WantsMidiInput
  161. return true;
  162. #else
  163. return false;
  164. #endif
  165. }
  166. bool DspModulePluginDemoAudioProcessor::producesMidi() const
  167. {
  168. #if JucePlugin_ProducesMidiOutput
  169. return true;
  170. #else
  171. return false;
  172. #endif
  173. }
  174. //==============================================================================
  175. void DspModulePluginDemoAudioProcessor::updateParameters()
  176. {
  177. auto newOversampling = oversamplingParam->get();
  178. if (newOversampling != audioCurrentlyOversampled)
  179. {
  180. audioCurrentlyOversampled = newOversampling;
  181. oversampling->reset();
  182. }
  183. //==============================================================================
  184. auto inputdB = Decibels::decibelsToGain (inputVolumeParam->get());
  185. auto outputdB = Decibels::decibelsToGain (outputVolumeParam->get());
  186. if (inputVolume.getGainLinear() != inputdB) inputVolume.setGainLinear (inputdB);
  187. if (outputVolume.getGainLinear() != outputdB) outputVolume.setGainLinear (outputdB);
  188. auto newSlopeType = slopeParam->getIndex();
  189. if (newSlopeType == 0)
  190. {
  191. *lowPassFilter.state = *dsp::IIR::Coefficients<float>::makeFirstOrderLowPass (getSampleRate(), lowPassFilterFreqParam->get());
  192. *highPassFilter.state = *dsp::IIR::Coefficients<float>::makeFirstOrderHighPass (getSampleRate(), highPassFilterFreqParam->get());
  193. }
  194. else
  195. {
  196. *lowPassFilter.state = *dsp::IIR::Coefficients<float>::makeLowPass (getSampleRate(), lowPassFilterFreqParam->get());
  197. *highPassFilter.state = *dsp::IIR::Coefficients<float>::makeHighPass (getSampleRate(), highPassFilterFreqParam->get());
  198. }
  199. //==============================================================================
  200. auto type = cabinetTypeParam->getIndex();
  201. auto currentType = cabinetType.get();
  202. if (type != currentType)
  203. {
  204. cabinetType.set(type);
  205. auto maxSize = static_cast<size_t> (roundToInt (getSampleRate() * (8192.0 / 44100.0)));
  206. if (type == 0)
  207. convolution.loadImpulseResponse (BinaryData::Impulse1_wav, BinaryData::Impulse1_wavSize, false, true, maxSize);
  208. else
  209. convolution.loadImpulseResponse (BinaryData::Impulse2_wav, BinaryData::Impulse2_wavSize, false, true, maxSize);
  210. }
  211. cabinetIsBypassed = ! cabinetSimParam->get();
  212. }
  213. //==============================================================================
  214. // This creates new instances of the plugin..
  215. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  216. {
  217. return new DspModulePluginDemoAudioProcessor();
  218. }