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.

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