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.

174 lines
6.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - 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: SIMDRegisterDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: SIMD register 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: SIMDRegisterDemo
  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. template <typename T>
  40. static T* toBasePointer (SIMDRegister<T>* r) noexcept
  41. {
  42. return reinterpret_cast<T*> (r);
  43. }
  44. constexpr auto registerSize = dsp::SIMDRegister<float>::size();
  45. //==============================================================================
  46. struct SIMDRegisterDemoDSP
  47. {
  48. void prepare (const ProcessSpec& spec)
  49. {
  50. sampleRate = spec.sampleRate;
  51. iirCoefficients = IIR::Coefficients<float>::makeLowPass (sampleRate, 440.0f);
  52. iir.reset (new IIR::Filter<SIMDRegister<float>> (iirCoefficients));
  53. interleaved = AudioBlock<SIMDRegister<float>> (interleavedBlockData, 1, spec.maximumBlockSize);
  54. zero = AudioBlock<float> (zeroData, SIMDRegister<float>::size(), spec.maximumBlockSize);
  55. zero.clear();
  56. auto monoSpec = spec;
  57. monoSpec.numChannels = 1;
  58. iir->prepare (monoSpec);
  59. }
  60. template <typename SampleType>
  61. auto prepareChannelPointers (const AudioBlock<SampleType>& block)
  62. {
  63. std::array<SampleType*, registerSize> result {};
  64. for (size_t ch = 0; ch < result.size(); ++ch)
  65. result[ch] = (ch < block.getNumChannels() ? block.getChannelPointer (ch) : zero.getChannelPointer (ch));
  66. return result;
  67. }
  68. void process (const ProcessContextReplacing<float>& context)
  69. {
  70. jassert (context.getInputBlock().getNumSamples() == context.getOutputBlock().getNumSamples());
  71. jassert (context.getInputBlock().getNumChannels() == context.getOutputBlock().getNumChannels());
  72. const auto& input = context.getInputBlock();
  73. const auto numSamples = (int) input.getNumSamples();
  74. auto inChannels = prepareChannelPointers (input);
  75. using Format = AudioData::Format<AudioData::Float32, AudioData::NativeEndian>;
  76. AudioData::interleaveSamples (AudioData::NonInterleavedSource<Format> { inChannels.data(), registerSize, },
  77. AudioData::InterleavedDest<Format> { toBasePointer (interleaved.getChannelPointer (0)), registerSize },
  78. numSamples);
  79. iir->process (ProcessContextReplacing<SIMDRegister<float>> (interleaved));
  80. auto outChannels = prepareChannelPointers (context.getOutputBlock());
  81. AudioData::deinterleaveSamples (AudioData::InterleavedSource<Format> { toBasePointer (interleaved.getChannelPointer (0)), registerSize },
  82. AudioData::NonInterleavedDest<Format> { outChannels.data(), registerSize },
  83. numSamples);
  84. }
  85. void reset()
  86. {
  87. iir.reset();
  88. }
  89. void updateParameters()
  90. {
  91. if (sampleRate != 0.0)
  92. {
  93. auto cutoff = static_cast<float> (cutoffParam.getCurrentValue());
  94. auto qVal = static_cast<float> (qParam.getCurrentValue());
  95. switch (typeParam.getCurrentSelectedID())
  96. {
  97. case 1: *iirCoefficients = IIR::ArrayCoefficients<float>::makeLowPass (sampleRate, cutoff, qVal); break;
  98. case 2: *iirCoefficients = IIR::ArrayCoefficients<float>::makeHighPass (sampleRate, cutoff, qVal); break;
  99. case 3: *iirCoefficients = IIR::ArrayCoefficients<float>::makeBandPass (sampleRate, cutoff, qVal); break;
  100. default: break;
  101. }
  102. }
  103. }
  104. //==============================================================================
  105. IIR::Coefficients<float>::Ptr iirCoefficients;
  106. std::unique_ptr<IIR::Filter<SIMDRegister<float>>> iir;
  107. AudioBlock<SIMDRegister<float>> interleaved;
  108. AudioBlock<float> zero;
  109. HeapBlock<char> interleavedBlockData, zeroData;
  110. ChoiceParameter typeParam { { "Low-pass", "High-pass", "Band-pass" }, 1, "Type" };
  111. SliderParameter cutoffParam { { 20.0, 20000.0 }, 0.5, 440.0f, "Cutoff", "Hz" };
  112. SliderParameter qParam { { 0.3, 20.0 }, 0.5, 0.7, "Q" };
  113. std::vector<DSPDemoParameterBase*> parameters { &typeParam, &cutoffParam, &qParam };
  114. double sampleRate = 0.0;
  115. };
  116. struct SIMDRegisterDemo : public Component
  117. {
  118. SIMDRegisterDemo()
  119. {
  120. addAndMakeVisible (fileReaderComponent);
  121. setSize (750, 500);
  122. }
  123. void resized() override
  124. {
  125. fileReaderComponent.setBounds (getLocalBounds());
  126. }
  127. AudioFileReaderComponent<SIMDRegisterDemoDSP> fileReaderComponent;
  128. };