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.

132 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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: ConvolutionDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Convolution demo using the DSP module.
  24. dependencies: juce_audio_basics, juce_audio_devices, juce_audio_formats,
  25. juce_audio_processors, juce_audio_utils, juce_core,
  26. juce_data_structures, juce_dsp, juce_events, juce_graphics,
  27. juce_gui_basics, juce_gui_extra
  28. exporters: xcode_mac, vs2019, linux_make
  29. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  30. type: Component
  31. mainClass: ConvolutionDemo
  32. useLocalCopy: 1
  33. END_JUCE_PIP_METADATA
  34. *******************************************************************************/
  35. #pragma once
  36. #include "../Assets/DemoUtilities.h"
  37. #include "../Assets/DSPDemos_Common.h"
  38. using namespace dsp;
  39. //==============================================================================
  40. struct ConvolutionDemoDSP
  41. {
  42. void prepare (const ProcessSpec& spec)
  43. {
  44. sampleRate = spec.sampleRate;
  45. convolution.prepare (spec);
  46. updateParameters();
  47. }
  48. void process (ProcessContextReplacing<float> context)
  49. {
  50. context.isBypassed = bypass;
  51. convolution.process (context);
  52. }
  53. void reset()
  54. {
  55. convolution.reset();
  56. }
  57. void updateParameters()
  58. {
  59. if (auto* cabinetTypeParameter = dynamic_cast<ChoiceParameter*> (parameters[0]))
  60. {
  61. if (cabinetTypeParameter->getCurrentSelectedID() == 1)
  62. {
  63. bypass = true;
  64. }
  65. else
  66. {
  67. bypass = false;
  68. auto maxSize = static_cast<size_t> (roundToInt (sampleRate * (8192.0 / 44100.0)));
  69. auto selectedType = cabinetTypeParameter->getCurrentSelectedID();
  70. auto assetName = (selectedType == 2 ? "guitar_amp.wav" : "cassette_recorder.wav");
  71. if (auto assetInputStream = createAssetInputStream (assetName))
  72. {
  73. currentCabinetData.reset();
  74. assetInputStream->readIntoMemoryBlock (currentCabinetData);
  75. convolution.loadImpulseResponse (currentCabinetData.getData(), currentCabinetData.getSize(),
  76. false, true, maxSize);
  77. }
  78. }
  79. }
  80. }
  81. //==============================================================================
  82. double sampleRate = 0.0;
  83. bool bypass = false;
  84. MemoryBlock currentCabinetData;
  85. Convolution convolution;
  86. ChoiceParameter cabinetParam { { "Bypass", "Guitar amplifier 8''", "Cassette recorder" }, 1, "Cabinet Type" };
  87. std::vector<DSPDemoParameterBase*> parameters { &cabinetParam };
  88. };
  89. struct ConvolutionDemo : public Component
  90. {
  91. ConvolutionDemo()
  92. {
  93. addAndMakeVisible (fileReaderComponent);
  94. setSize (750, 500);
  95. }
  96. void resized() override
  97. {
  98. fileReaderComponent.setBounds (getLocalBounds());
  99. }
  100. AudioFileReaderComponent<ConvolutionDemoDSP> fileReaderComponent;
  101. };