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.

150 lines
5.1KB

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