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.

152 lines
5.1KB

  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: OscillatorDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Oscillator 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, vs2022, linux_make
  29. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  30. type: Component
  31. mainClass: OscillatorDemo
  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 OscillatorDemoDSP
  41. {
  42. void prepare (const ProcessSpec& spec)
  43. {
  44. gain.setGainDecibels (-6.0f);
  45. for (auto&& oscillator : oscillators)
  46. {
  47. oscillator.setFrequency (440.f);
  48. oscillator.prepare (spec);
  49. }
  50. updateParameters();
  51. tempBuffer = AudioBlock<float> (tempBufferMemory, spec.numChannels, spec.maximumBlockSize);
  52. }
  53. void process (const ProcessContextReplacing<float>& context)
  54. {
  55. tempBuffer.copyFrom (context.getInputBlock());
  56. tempBuffer.multiplyBy (static_cast<float> (fileMix));
  57. oscillators[currentOscillatorIdx].process (context);
  58. context.getOutputBlock().multiplyBy (static_cast<float> (1.0 - fileMix));
  59. context.getOutputBlock().add (tempBuffer);
  60. gain.process (context);
  61. }
  62. void reset()
  63. {
  64. oscillators[currentOscillatorIdx].reset();
  65. }
  66. void updateParameters()
  67. {
  68. currentOscillatorIdx = jmin (numElementsInArray (oscillators),
  69. 3 * (accuracy.getCurrentSelectedID() - 1) + (typeParam.getCurrentSelectedID() - 1));
  70. auto freq = static_cast<float> (freqParam.getCurrentValue());
  71. for (auto&& oscillator : oscillators)
  72. oscillator.setFrequency (freq);
  73. gain.setGainDecibels (static_cast<float> (gainParam.getCurrentValue()));
  74. fileMix = mixParam.getCurrentValue();
  75. }
  76. //==============================================================================
  77. Oscillator<float> oscillators[6] =
  78. {
  79. // No Approximation
  80. {[] (float x) { return std::sin (x); }}, // sine
  81. {[] (float x) { return x / MathConstants<float>::pi; }}, // saw
  82. {[] (float x) { return x < 0.0f ? -1.0f : 1.0f; }}, // square
  83. // Approximated by a wave-table
  84. {[] (float x) { return std::sin (x); }, 100}, // sine
  85. {[] (float x) { return x / MathConstants<float>::pi; }, 100}, // saw
  86. {[] (float x) { return x < 0.0f ? -1.0f : 1.0f; }, 100} // square
  87. };
  88. int currentOscillatorIdx = 0;
  89. Gain<float> gain;
  90. ChoiceParameter typeParam { {"sine", "saw", "square"}, 1, "Type" };
  91. ChoiceParameter accuracy { {"No Approximation", "Use Wavetable"}, 1, "Accuracy" };
  92. SliderParameter freqParam { { 20.0, 24000.0 }, 0.4, 440.0, "Frequency", "Hz" };
  93. SliderParameter gainParam { { -100.0, 20.0 }, 3.0, -20.0, "Gain", "dB" };
  94. SliderParameter mixParam { { 0.0, 1.0 }, 1.0, 0.0, "File mix" };
  95. HeapBlock<char> tempBufferMemory;
  96. AudioBlock<float> tempBuffer;
  97. double fileMix;
  98. std::vector<DSPDemoParameterBase*> parameters { &typeParam, &accuracy, &freqParam, &gainParam, &mixParam };
  99. };
  100. struct OscillatorDemo final : public Component
  101. {
  102. OscillatorDemo()
  103. {
  104. addAndMakeVisible (fileReaderComponent);
  105. setSize (750, 500);
  106. }
  107. void resized() override
  108. {
  109. fileReaderComponent.setBounds (getLocalBounds());
  110. }
  111. AudioFileReaderComponent<OscillatorDemoDSP> fileReaderComponent;
  112. };