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.

113 lines
3.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../DSPDemo.h"
  20. //==============================================================================
  21. // @@ START_DEMO
  22. struct OscillatorDemo
  23. {
  24. void prepare (const ProcessSpec& spec)
  25. {
  26. gain.setGainDecibels (-6.0f);
  27. for (auto&& oscillator : oscillators)
  28. {
  29. oscillator.setFrequency (440.f);
  30. oscillator.prepare (spec);
  31. }
  32. updateParameters();
  33. tempBuffer = AudioBlock<float> (tempBufferMemory, spec.numChannels, spec.maximumBlockSize);
  34. }
  35. void process (const ProcessContextReplacing<float>& context)
  36. {
  37. tempBuffer.copy (context.getInputBlock());
  38. tempBuffer.multiply (static_cast<float> (fileMix));
  39. oscillators[currentOscillatorIdx].process (context);
  40. context.getOutputBlock().multiply (static_cast<float> (1.0 - fileMix));
  41. context.getOutputBlock().add (tempBuffer);
  42. gain.process (context);
  43. }
  44. void reset()
  45. {
  46. oscillators[currentOscillatorIdx].reset();
  47. }
  48. void updateParameters()
  49. {
  50. currentOscillatorIdx = jmin (numElementsInArray (oscillators),
  51. 3 * (accuracy.getCurrentSelectedID() - 1) + (typeParam.getCurrentSelectedID() - 1));
  52. auto freq = static_cast<float> (freqParam.getCurrentValue());
  53. for (auto&& oscillator : oscillators)
  54. oscillator.setFrequency (freq);
  55. gain.setGainDecibels (static_cast<float> (gainParam.getCurrentValue()));
  56. fileMix = mixParam.getCurrentValue();
  57. }
  58. //==============================================================================
  59. Oscillator<float> oscillators[6] =
  60. {
  61. // No Approximation
  62. {[] (float x) { return std::sin (x); }}, // sine
  63. {[] (float x) { return x / float_Pi; }}, // saw
  64. {[] (float x) { return x < 0.0f ? -1.0f : 1.0f; }}, // square
  65. // Approximated by a wave-table
  66. {[] (float x) { return std::sin (x); }, 100}, // sine
  67. {[] (float x) { return x / float_Pi; }, 100}, // saw
  68. {[] (float x) { return x < 0.0f ? -1.0f : 1.0f; }, 100} // square
  69. };
  70. int currentOscillatorIdx = 0;
  71. Gain<float> gain;
  72. ChoiceParameter typeParam { {"sine", "saw", "square"}, 1, "Type" };
  73. ChoiceParameter accuracy { {"No Approximation", "Use Wavetable"}, 1, "Accuracy" };
  74. SliderParameter freqParam { { 20.0, 24000.0 }, 0.4, 440.0, "Frequency", "Hz" };
  75. SliderParameter gainParam { { -100.0, 20.0 }, 3.0, -20.0, "Gain", "dB" };
  76. SliderParameter mixParam { { 0.0, 1.0 }, 1.0, 0.0, "File mix" };
  77. HeapBlock<char> tempBufferMemory;
  78. AudioBlock<float> tempBuffer;
  79. double fileMix;
  80. std::vector<DSPDemoParameterBase*> parameters { &typeParam, &accuracy, &freqParam, &gainParam, &mixParam };
  81. };
  82. // @@ END_DEMO
  83. RegisterDSPDemo<OscillatorDemo> oscillatorDemo ("Oscillator", BinaryData::OscillatorDemo_cpp);