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.

158 lines
5.9KB

  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: 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, vs2017, linux_make
  29. type: Component
  30. mainClass: SIMDRegisterDemo
  31. useLocalCopy: 1
  32. END_JUCE_PIP_METADATA
  33. *******************************************************************************/
  34. #pragma once
  35. #include "../Assets/DemoUtilities.h"
  36. #include "../Assets/DSPDemos_Common.h"
  37. using namespace dsp;
  38. //==============================================================================
  39. struct SIMDRegisterDemoDSP
  40. {
  41. void prepare (const ProcessSpec& spec)
  42. {
  43. sampleRate = spec.sampleRate;
  44. iirCoefficients = IIR::Coefficients<float>::makeLowPass (sampleRate, 440.0f);
  45. iir.reset (new IIR::Filter<SIMDRegister<float>> (iirCoefficients));
  46. interleaved = AudioBlock<SIMDRegister<float>> (interleavedBlockData, 1, spec.maximumBlockSize);
  47. zero = AudioBlock<float> (zeroData, SIMDRegister<float>::size(), spec.maximumBlockSize);
  48. zero.clear();
  49. auto monoSpec = spec;
  50. monoSpec.numChannels = 1;
  51. iir->prepare (monoSpec);
  52. }
  53. void process (const ProcessContextReplacing<float>& context)
  54. {
  55. jassert (context.getInputBlock().getNumSamples() == context.getOutputBlock().getNumSamples());
  56. jassert (context.getInputBlock().getNumChannels() == context.getOutputBlock().getNumChannels());
  57. auto& input = context.getInputBlock();
  58. auto& output = context.getOutputBlock();
  59. auto n = input.getNumSamples();
  60. auto* inout = channelPointers.getData();
  61. for (size_t ch = 0; ch < SIMDRegister<float>::size(); ++ch)
  62. inout[ch] = (ch < input.getNumChannels() ? const_cast<float*> (input.getChannelPointer (ch)) : zero.getChannelPointer (ch));
  63. AudioDataConverters::interleaveSamples (inout, reinterpret_cast<float*> (interleaved.getChannelPointer (0)),
  64. static_cast<int> (n), static_cast<int> (SIMDRegister<float>::size()));
  65. iir->process (ProcessContextReplacing<SIMDRegister<float>> (interleaved));
  66. for (size_t ch = 0; ch < input.getNumChannels(); ++ch)
  67. inout[ch] = output.getChannelPointer (ch);
  68. AudioDataConverters::deinterleaveSamples (reinterpret_cast<float*> (interleaved.getChannelPointer (0)),
  69. const_cast<float**> (inout),
  70. static_cast<int> (n), static_cast<int> (SIMDRegister<float>::size()));
  71. }
  72. void reset()
  73. {
  74. iir.reset();
  75. }
  76. void updateParameters()
  77. {
  78. if (sampleRate != 0.0)
  79. {
  80. auto cutoff = static_cast<float> (cutoffParam.getCurrentValue());
  81. auto qVal = static_cast<float> (qParam.getCurrentValue());
  82. switch (typeParam.getCurrentSelectedID())
  83. {
  84. case 1: *iirCoefficients = *IIR::Coefficients<float>::makeLowPass (sampleRate, cutoff, qVal); break;
  85. case 2: *iirCoefficients = *IIR::Coefficients<float>::makeHighPass (sampleRate, cutoff, qVal); break;
  86. case 3: *iirCoefficients = *IIR::Coefficients<float>::makeBandPass (sampleRate, cutoff, qVal); break;
  87. default: break;
  88. }
  89. }
  90. }
  91. //==============================================================================
  92. IIR::Coefficients<float>::Ptr iirCoefficients;
  93. ScopedPointer<IIR::Filter<SIMDRegister<float>>> iir;
  94. AudioBlock<SIMDRegister<float>> interleaved;
  95. AudioBlock<float> zero;
  96. HeapBlock<char> interleavedBlockData, zeroData;
  97. HeapBlock<const float*> channelPointers { SIMDRegister<float>::size() };
  98. ChoiceParameter typeParam { { "Low-pass", "High-pass", "Band-pass" }, 1, "Type" };
  99. SliderParameter cutoffParam { { 20.0, 20000.0 }, 0.5, 440.0f, "Cutoff", "Hz" };
  100. SliderParameter qParam { { 0.3, 20.0 }, 0.5, 0.7, "Q" };
  101. std::vector<DSPDemoParameterBase*> parameters { &typeParam, &cutoffParam, &qParam };
  102. double sampleRate = 0.0;
  103. };
  104. struct SIMDRegisterDemo : public Component
  105. {
  106. SIMDRegisterDemo()
  107. {
  108. addAndMakeVisible (fileReaderComponent);
  109. setSize (750, 500);
  110. }
  111. void resized() override
  112. {
  113. fileReaderComponent.setBounds (getLocalBounds());
  114. }
  115. AudioFileReaderComponent<SIMDRegisterDemoDSP> fileReaderComponent;
  116. };