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.

564 lines
25KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  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: DSPModulePluginDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Audio plugin using the DSP module.
  24. dependencies: juce_audio_basics, juce_audio_devices, juce_audio_formats,
  25. juce_audio_plugin_client, juce_audio_processors,
  26. juce_audio_utils, juce_core, juce_data_structures, juce_dsp,
  27. juce_events, juce_graphics, juce_gui_basics, juce_gui_extra
  28. exporters: xcode_mac, vs2019, linux_make
  29. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  30. type: AudioProcessor
  31. mainClass: DspModulePluginDemoAudioProcessor
  32. useLocalCopy: 1
  33. END_JUCE_PIP_METADATA
  34. *******************************************************************************/
  35. #pragma once
  36. #include "../Assets/DemoUtilities.h"
  37. //==============================================================================
  38. struct ParameterSlider : public Slider,
  39. public Timer
  40. {
  41. ParameterSlider (AudioProcessorParameter& p)
  42. : Slider (p.getName (256)), param (p)
  43. {
  44. setRange (0.0, 1.0, 0.0);
  45. startTimerHz (30);
  46. updateSliderPos();
  47. }
  48. void valueChanged() override
  49. {
  50. if (isMouseButtonDown())
  51. param.setValueNotifyingHost ((float) Slider::getValue());
  52. else
  53. param.setValue ((float) Slider::getValue());
  54. }
  55. void timerCallback() override { updateSliderPos(); }
  56. void startedDragging() override { param.beginChangeGesture(); }
  57. void stoppedDragging() override { param.endChangeGesture(); }
  58. double getValueFromText (const String& text) override { return param.getValueForText (text); }
  59. String getTextFromValue (double value) override { return param.getText ((float) value, 1024) + " " + param.getLabel(); }
  60. void updateSliderPos()
  61. {
  62. auto newValue = param.getValue();
  63. if (newValue != (float) Slider::getValue() && ! isMouseButtonDown())
  64. Slider::setValue (newValue);
  65. }
  66. AudioProcessorParameter& param;
  67. };
  68. //==============================================================================
  69. /**
  70. This class handles the audio processing for the DSP module plugin demo.
  71. */
  72. class DspModulePluginDemoAudioProcessor : public AudioProcessor
  73. {
  74. public:
  75. //==============================================================================
  76. DspModulePluginDemoAudioProcessor()
  77. : AudioProcessor (BusesProperties().withInput ("Input", AudioChannelSet::stereo(), true)
  78. .withOutput ("Output", AudioChannelSet::stereo(), true)),
  79. lowPassFilter (dsp::IIR::Coefficients<float>::makeFirstOrderLowPass (48000.0, 20000.0f)),
  80. highPassFilter (dsp::IIR::Coefficients<float>::makeFirstOrderHighPass (48000.0, 20.0f)),
  81. waveShapers { { std::tanh }, { dsp::FastMathApproximations::tanh } },
  82. clipping { clip }
  83. {
  84. // Oversampling 2 times with IIR filtering
  85. oversampling.reset (new dsp::Oversampling<float> (2, 1, dsp::Oversampling<float>::filterHalfBandPolyphaseIIR, false));
  86. addParameter (inputVolumeParam = new AudioParameterFloat ("INPUT", "Input Volume", { 0.0f, 60.0f, 0.0f, 1.0f }, 0.0f, "dB"));
  87. addParameter (highPassFilterFreqParam = new AudioParameterFloat ("HPFREQ", "Pre Highpass Freq.", { 20.0f, 20000.0f, 0.0f, 0.5f }, 20.0f, "Hz"));
  88. addParameter (lowPassFilterFreqParam = new AudioParameterFloat ("LPFREQ", "Post Lowpass Freq.", { 20.0f, 20000.0f, 0.0f, 0.5f }, 20000.0f, "Hz"));
  89. addParameter (stereoParam = new AudioParameterChoice ("STEREO", "Stereo Processing", { "Always mono", "Yes" }, 1));
  90. addParameter (slopeParam = new AudioParameterChoice ("SLOPE", "Slope", { "-6 dB / octave", "-12 dB / octave" }, 0));
  91. addParameter (waveshaperParam = new AudioParameterChoice ("WVSHP", "Waveshaper", { "std::tanh", "Fast tanh approx." }, 0));
  92. addParameter (cabinetTypeParam = new AudioParameterChoice ("CABTYPE", "Cabinet Type", { "Guitar amplifier 8'' cabinet ",
  93. "Cassette recorder cabinet" }, 0));
  94. addParameter (cabinetSimParam = new AudioParameterBool ("CABSIM", "Cabinet Sim", false));
  95. addParameter (oversamplingParam = new AudioParameterBool ("OVERS", "Oversampling", false));
  96. addParameter (outputVolumeParam = new AudioParameterFloat ("OUTPUT", "Output Volume", { -40.0f, 40.0f, 0.0f, 1.0f }, 0.0f, "dB"));
  97. cabinetType.set (0);
  98. }
  99. //==============================================================================
  100. bool isBusesLayoutSupported (const BusesLayout& layouts) const override
  101. {
  102. // This is the place where you check if the layout is supported.
  103. // In this template code we only support mono or stereo.
  104. if (layouts.getMainOutputChannelSet() != AudioChannelSet::mono() && layouts.getMainOutputChannelSet() != AudioChannelSet::stereo())
  105. return false;
  106. // This checks if the input layout matches the output layout
  107. if (layouts.getMainOutputChannelSet() != layouts.getMainInputChannelSet())
  108. return false;
  109. return true;
  110. }
  111. void prepareToPlay (double sampleRate, int samplesPerBlock) override
  112. {
  113. auto channels = static_cast<uint32> (jmin (getMainBusNumInputChannels(), getMainBusNumOutputChannels()));
  114. dsp::ProcessSpec spec { sampleRate, static_cast<uint32> (samplesPerBlock), channels };
  115. lowPassFilter .prepare (spec);
  116. highPassFilter.prepare (spec);
  117. inputVolume .prepare (spec);
  118. outputVolume.prepare (spec);
  119. convolution.prepare (spec);
  120. cabinetType.set (-1);
  121. oversampling->initProcessing (static_cast<size_t> (samplesPerBlock));
  122. updateParameters();
  123. reset();
  124. }
  125. void releaseResources() override {}
  126. void processBlock (AudioBuffer<float>& inoutBuffer, MidiBuffer&) override
  127. {
  128. auto totalNumInputChannels = getTotalNumInputChannels();
  129. auto totalNumOutputChannels = getTotalNumOutputChannels();
  130. auto numSamples = inoutBuffer.getNumSamples();
  131. for (auto i = jmin (2, totalNumInputChannels); i < totalNumOutputChannels; ++i)
  132. inoutBuffer.clear (i, 0, numSamples);
  133. updateParameters();
  134. dsp::AudioBlock<float> block (inoutBuffer);
  135. if (stereoParam->getIndex() == 1)
  136. {
  137. // Stereo processing mode:
  138. if (block.getNumChannels() > 2)
  139. block = block.getSubsetChannelBlock (0, 2);
  140. process (dsp::ProcessContextReplacing<float> (block));
  141. }
  142. else
  143. {
  144. // Mono processing mode:
  145. auto firstChan = block.getSingleChannelBlock (0);
  146. process (dsp::ProcessContextReplacing<float> (firstChan));
  147. for (size_t chan = 1; chan < block.getNumChannels(); ++chan)
  148. block.getSingleChannelBlock (chan).copyFrom (firstChan);
  149. }
  150. }
  151. void reset() override
  152. {
  153. lowPassFilter .reset();
  154. highPassFilter.reset();
  155. convolution .reset();
  156. oversampling->reset();
  157. }
  158. //==============================================================================
  159. bool hasEditor() const override { return true; }
  160. AudioProcessorEditor* createEditor() override
  161. {
  162. return new DspModulePluginDemoAudioProcessorEditor (*this);
  163. }
  164. //==============================================================================
  165. bool acceptsMidi() const override { return false; }
  166. bool producesMidi() const override { return false; }
  167. const String getName() const override { return JucePlugin_Name; }
  168. double getTailLengthSeconds() const override { return 0.0; }
  169. //==============================================================================
  170. int getNumPrograms() override { return 1; }
  171. int getCurrentProgram() override { return 0; }
  172. void setCurrentProgram (int) override {}
  173. const String getProgramName (int) override { return {}; }
  174. void changeProgramName (int, const String&) override {}
  175. //==============================================================================
  176. void getStateInformation (MemoryBlock&) override {}
  177. void setStateInformation (const void*, int) override {}
  178. //==============================================================================
  179. void updateParameters()
  180. {
  181. auto newOversampling = oversamplingParam->get();
  182. if (newOversampling != audioCurrentlyOversampled)
  183. {
  184. audioCurrentlyOversampled = newOversampling;
  185. oversampling->reset();
  186. }
  187. //==============================================================================
  188. auto inputdB = Decibels::decibelsToGain (inputVolumeParam->get());
  189. auto outputdB = Decibels::decibelsToGain (outputVolumeParam->get());
  190. if (inputVolume .getGainLinear() != inputdB) inputVolume.setGainLinear (inputdB);
  191. if (outputVolume.getGainLinear() != outputdB) outputVolume.setGainLinear (outputdB);
  192. auto newSlopeType = slopeParam->getIndex();
  193. if (newSlopeType == 0)
  194. {
  195. *lowPassFilter .state = *dsp::IIR::Coefficients<float>::makeFirstOrderLowPass (getSampleRate(), lowPassFilterFreqParam->get());
  196. *highPassFilter.state = *dsp::IIR::Coefficients<float>::makeFirstOrderHighPass (getSampleRate(), highPassFilterFreqParam->get());
  197. }
  198. else
  199. {
  200. *lowPassFilter .state = *dsp::IIR::Coefficients<float>::makeLowPass (getSampleRate(), lowPassFilterFreqParam->get());
  201. *highPassFilter.state = *dsp::IIR::Coefficients<float>::makeHighPass (getSampleRate(), highPassFilterFreqParam->get());
  202. }
  203. //==============================================================================
  204. auto type = cabinetTypeParam->getIndex();
  205. auto currentType = cabinetType.get();
  206. if (type != currentType)
  207. {
  208. cabinetType.set (type);
  209. auto maxSize = static_cast<size_t> (roundToInt (getSampleRate() * (8192.0 / 44100.0)));
  210. auto assetName = (type == 0 ? "Impulse1.wav" : "Impulse2.wav");
  211. if (auto assetInputStream = createAssetInputStream (assetName))
  212. {
  213. currentCabinetData.reset();
  214. assetInputStream->readIntoMemoryBlock (currentCabinetData);
  215. convolution.loadImpulseResponse (currentCabinetData.getData(), currentCabinetData.getSize(),
  216. false, true, maxSize);
  217. }
  218. }
  219. cabinetIsBypassed = ! cabinetSimParam->get();
  220. }
  221. static inline float clip (float x) { return jmax (-1.0f, jmin (1.0f, x)); }
  222. //==============================================================================
  223. AudioParameterFloat* inputVolumeParam;
  224. AudioParameterFloat* outputVolumeParam;
  225. AudioParameterFloat* lowPassFilterFreqParam;
  226. AudioParameterFloat* highPassFilterFreqParam;
  227. AudioParameterChoice* stereoParam;
  228. AudioParameterChoice* slopeParam;
  229. AudioParameterChoice* waveshaperParam;
  230. AudioParameterChoice* cabinetTypeParam;
  231. AudioParameterBool* cabinetSimParam;
  232. AudioParameterBool* oversamplingParam;
  233. private:
  234. //==============================================================================
  235. /**
  236. This is the editor component that will be displayed.
  237. */
  238. class DspModulePluginDemoAudioProcessorEditor : public AudioProcessorEditor
  239. {
  240. public:
  241. //==============================================================================
  242. DspModulePluginDemoAudioProcessorEditor (DspModulePluginDemoAudioProcessor& p)
  243. : AudioProcessorEditor (&p),
  244. dspProcessor (p),
  245. inputVolumeLabel ({}, dspProcessor.inputVolumeParam->name),
  246. outputVolumeLabel ({}, dspProcessor.outputVolumeParam->name),
  247. lowPassFilterFreqLabel ({}, dspProcessor.lowPassFilterFreqParam->name),
  248. highPassFilterFreqLabel ({}, dspProcessor.highPassFilterFreqParam->name),
  249. stereoLabel ({}, dspProcessor.stereoParam->name),
  250. slopeLabel ({}, dspProcessor.slopeParam->name),
  251. waveshaperLabel ({}, dspProcessor.waveshaperParam->name),
  252. cabinetTypeLabel ({}, dspProcessor.cabinetTypeParam->name)
  253. {
  254. //==============================================================================
  255. inputVolumeSlider .reset (new ParameterSlider (*dspProcessor.inputVolumeParam));
  256. outputVolumeSlider .reset (new ParameterSlider (*dspProcessor.outputVolumeParam));
  257. lowPassFilterFreqSlider .reset (new ParameterSlider (*dspProcessor.lowPassFilterFreqParam));
  258. highPassFilterFreqSlider.reset (new ParameterSlider (*dspProcessor.highPassFilterFreqParam));
  259. addAndMakeVisible (inputVolumeSlider .get());
  260. addAndMakeVisible (outputVolumeSlider .get());
  261. addAndMakeVisible (lowPassFilterFreqSlider .get());
  262. addAndMakeVisible (highPassFilterFreqSlider.get());
  263. addAndMakeVisible (inputVolumeLabel);
  264. inputVolumeLabel.setJustificationType (Justification::centredLeft);
  265. inputVolumeLabel.attachToComponent (inputVolumeSlider.get(), true);
  266. addAndMakeVisible (outputVolumeLabel);
  267. outputVolumeLabel.setJustificationType (Justification::centredLeft);
  268. outputVolumeLabel.attachToComponent (outputVolumeSlider.get(), true);
  269. addAndMakeVisible (lowPassFilterFreqLabel);
  270. lowPassFilterFreqLabel.setJustificationType (Justification::centredLeft);
  271. lowPassFilterFreqLabel.attachToComponent (lowPassFilterFreqSlider.get(), true);
  272. addAndMakeVisible (highPassFilterFreqLabel);
  273. highPassFilterFreqLabel.setJustificationType (Justification::centredLeft);
  274. highPassFilterFreqLabel.attachToComponent (highPassFilterFreqSlider.get(), true);
  275. //==============================================================================
  276. addAndMakeVisible (stereoBox);
  277. auto i = 1;
  278. for (auto choice : dspProcessor.stereoParam->choices)
  279. stereoBox.addItem (choice, i++);
  280. stereoBox.onChange = [this] { dspProcessor.stereoParam->operator= (stereoBox.getSelectedItemIndex()); };
  281. stereoBox.setSelectedId (dspProcessor.stereoParam->getIndex() + 1);
  282. addAndMakeVisible (stereoLabel);
  283. stereoLabel.setJustificationType (Justification::centredLeft);
  284. stereoLabel.attachToComponent (&stereoBox, true);
  285. //==============================================================================
  286. addAndMakeVisible(slopeBox);
  287. i = 1;
  288. for (auto choice : dspProcessor.slopeParam->choices)
  289. slopeBox.addItem(choice, i++);
  290. slopeBox.onChange = [this] { dspProcessor.slopeParam->operator= (slopeBox.getSelectedItemIndex()); };
  291. slopeBox.setSelectedId(dspProcessor.slopeParam->getIndex() + 1);
  292. addAndMakeVisible(slopeLabel);
  293. slopeLabel.setJustificationType(Justification::centredLeft);
  294. slopeLabel.attachToComponent(&slopeBox, true);
  295. //==============================================================================
  296. addAndMakeVisible (waveshaperBox);
  297. i = 1;
  298. for (auto choice : dspProcessor.waveshaperParam->choices)
  299. waveshaperBox.addItem (choice, i++);
  300. waveshaperBox.onChange = [this] { dspProcessor.waveshaperParam->operator= (waveshaperBox.getSelectedItemIndex()); };
  301. waveshaperBox.setSelectedId (dspProcessor.waveshaperParam->getIndex() + 1);
  302. addAndMakeVisible (waveshaperLabel);
  303. waveshaperLabel.setJustificationType (Justification::centredLeft);
  304. waveshaperLabel.attachToComponent (&waveshaperBox, true);
  305. //==============================================================================
  306. addAndMakeVisible (cabinetTypeBox);
  307. i = 1;
  308. for (auto choice : dspProcessor.cabinetTypeParam->choices)
  309. cabinetTypeBox.addItem (choice, i++);
  310. cabinetTypeBox.onChange = [this] { dspProcessor.cabinetTypeParam->operator= (cabinetTypeBox.getSelectedItemIndex()); };
  311. cabinetTypeBox.setSelectedId (dspProcessor.cabinetTypeParam->getIndex() + 1);
  312. addAndMakeVisible (cabinetTypeLabel);
  313. cabinetTypeLabel.setJustificationType (Justification::centredLeft);
  314. cabinetTypeLabel.attachToComponent (&cabinetTypeBox, true);
  315. //==============================================================================
  316. addAndMakeVisible (cabinetSimButton);
  317. cabinetSimButton.onClick = [this] { dspProcessor.cabinetSimParam->operator= (cabinetSimButton.getToggleState()); };
  318. cabinetSimButton.setButtonText (dspProcessor.cabinetSimParam->name);
  319. cabinetSimButton.setToggleState (dspProcessor.cabinetSimParam->get(), NotificationType::dontSendNotification);
  320. addAndMakeVisible (oversamplingButton);
  321. oversamplingButton.onClick = [this] { dspProcessor.oversamplingParam->operator= (oversamplingButton.getToggleState()); };
  322. oversamplingButton.setButtonText (dspProcessor.oversamplingParam->name);
  323. oversamplingButton.setToggleState (dspProcessor.oversamplingParam->get(), NotificationType::dontSendNotification);
  324. //==============================================================================
  325. setSize (600, 400);
  326. }
  327. //==============================================================================
  328. void paint (Graphics& g) override
  329. {
  330. g.setColour (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
  331. g.fillAll();
  332. }
  333. void resized() override
  334. {
  335. auto bounds = getLocalBounds().reduced (10);
  336. bounds.removeFromTop (10);
  337. bounds.removeFromLeft (125);
  338. //==============================================================================
  339. inputVolumeSlider->setBounds (bounds.removeFromTop (30));
  340. bounds.removeFromTop (5);
  341. outputVolumeSlider->setBounds (bounds.removeFromTop (30));
  342. bounds.removeFromTop (15);
  343. highPassFilterFreqSlider->setBounds (bounds.removeFromTop (30));
  344. bounds.removeFromTop (5);
  345. lowPassFilterFreqSlider->setBounds (bounds.removeFromTop (30));
  346. bounds.removeFromTop (15);
  347. //==============================================================================
  348. stereoBox.setBounds (bounds.removeFromTop(30));
  349. bounds.removeFromTop (5);
  350. slopeBox.setBounds (bounds.removeFromTop (30));
  351. bounds.removeFromTop (5);
  352. waveshaperBox.setBounds (bounds.removeFromTop (30));
  353. bounds.removeFromTop (5);
  354. cabinetTypeBox.setBounds (bounds.removeFromTop (30));
  355. bounds.removeFromTop (15);
  356. //==============================================================================
  357. auto buttonSlice = bounds.removeFromTop (30);
  358. cabinetSimButton.setSize (200, buttonSlice.getHeight());
  359. cabinetSimButton.setCentrePosition (buttonSlice.getCentre());
  360. bounds.removeFromTop(5);
  361. buttonSlice = bounds.removeFromTop (30);
  362. oversamplingButton.setSize(200, buttonSlice.getHeight());
  363. oversamplingButton.setCentrePosition(buttonSlice.getCentre());
  364. }
  365. private:
  366. //==============================================================================
  367. DspModulePluginDemoAudioProcessor& dspProcessor;
  368. std::unique_ptr<ParameterSlider> inputVolumeSlider, outputVolumeSlider,
  369. lowPassFilterFreqSlider, highPassFilterFreqSlider;
  370. ComboBox stereoBox, slopeBox, waveshaperBox, cabinetTypeBox;
  371. ToggleButton cabinetSimButton, oversamplingButton;
  372. Label inputVolumeLabel, outputVolumeLabel, lowPassFilterFreqLabel,
  373. highPassFilterFreqLabel, stereoLabel, slopeLabel, waveshaperLabel,
  374. cabinetTypeLabel;
  375. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DspModulePluginDemoAudioProcessorEditor)
  376. };
  377. //==============================================================================
  378. void process (dsp::ProcessContextReplacing<float> context) noexcept
  379. {
  380. ScopedNoDenormals noDenormals;
  381. // Input volume applied with a SmoothedValue
  382. inputVolume.process (context);
  383. // Pre-highpass filtering, very useful for distortion audio effects
  384. // Note : try frequencies around 700 Hz
  385. highPassFilter.process (context);
  386. // Upsampling
  387. dsp::AudioBlock<float> oversampledBlock;
  388. setLatencySamples (audioCurrentlyOversampled ? roundToInt (oversampling->getLatencyInSamples()) : 0);
  389. if (audioCurrentlyOversampled)
  390. oversampledBlock = oversampling->processSamplesUp (context.getOutputBlock());
  391. auto waveshaperContext = audioCurrentlyOversampled ? dsp::ProcessContextReplacing<float> (oversampledBlock)
  392. : context;
  393. // Waveshaper processing, for distortion generation, thanks to the input gain
  394. // The fast tanh can be used instead of std::tanh to reduce the CPU load
  395. auto waveshaperIndex = waveshaperParam->getIndex();
  396. if (isPositiveAndBelow (waveshaperIndex, numWaveShapers) )
  397. {
  398. waveShapers[waveshaperIndex].process (waveshaperContext);
  399. if (waveshaperIndex == 1)
  400. clipping.process (waveshaperContext);
  401. waveshaperContext.getOutputBlock() *= 0.7f;
  402. }
  403. // Downsampling
  404. if (audioCurrentlyOversampled)
  405. oversampling->processSamplesDown (context.getOutputBlock());
  406. // Post-lowpass filtering
  407. lowPassFilter.process (context);
  408. // Convolution with the impulse response of a guitar cabinet
  409. auto wasBypassed = context.isBypassed;
  410. context.isBypassed = context.isBypassed || cabinetIsBypassed;
  411. convolution.process (context);
  412. context.isBypassed = wasBypassed;
  413. // Output volume applied with a SmoothedValue
  414. outputVolume.process (context);
  415. }
  416. //==============================================================================
  417. dsp::ProcessorDuplicator<dsp::IIR::Filter<float>, dsp::IIR::Coefficients<float>> lowPassFilter, highPassFilter;
  418. dsp::Convolution convolution;
  419. MemoryBlock currentCabinetData;
  420. static constexpr size_t numWaveShapers = 2;
  421. dsp::WaveShaper<float> waveShapers[numWaveShapers];
  422. dsp::WaveShaper<float> clipping;
  423. dsp::Gain<float> inputVolume, outputVolume;
  424. std::unique_ptr<dsp::Oversampling<float>> oversampling;
  425. bool audioCurrentlyOversampled = false;
  426. Atomic<int> cabinetType;
  427. bool cabinetIsBypassed = false;
  428. //==============================================================================
  429. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DspModulePluginDemoAudioProcessor)
  430. };