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.

225 lines
7.1KB

  1. #pragma once
  2. #include "../JuceLibraryCode/JuceHeader.h"
  3. /**
  4. Base class for oscillators
  5. */
  6. class Oscillator : public SynthesiserVoice
  7. {
  8. public:
  9. Oscillator()
  10. {
  11. amplitude.reset (getSampleRate(), 0.1);
  12. phaseIncrement.reset (getSampleRate(), 0.1);
  13. }
  14. void startNote (int midiNoteNumber, float velocity, SynthesiserSound*, int) override
  15. {
  16. frequency = MidiMessage::getMidiNoteInHertz (midiNoteNumber);
  17. phaseIncrement.setValue (((2.0 * double_Pi) * frequency) / sampleRate);
  18. amplitude.setValue (velocity);
  19. // Store the initial note and work out the maximum frequency deviations for pitch bend
  20. initialNote = midiNoteNumber;
  21. maxFreq = MidiMessage::getMidiNoteInHertz (initialNote + 4) - frequency;
  22. minFreq = frequency - MidiMessage::getMidiNoteInHertz (initialNote - 4);
  23. }
  24. void stopNote (float, bool) override
  25. {
  26. clearCurrentNote();
  27. amplitude.setValue (0.0);
  28. }
  29. void pitchWheelMoved (int newValue) override
  30. {
  31. // Change the phase increment based on pitch bend amount
  32. double frequencyOffset = ((newValue > 0 ? maxFreq : minFreq) * (newValue / 127.0));
  33. phaseIncrement.setValue (((2.0 * double_Pi) * (frequency + frequencyOffset)) / sampleRate);
  34. }
  35. void controllerMoved (int, int) override
  36. {
  37. }
  38. void channelPressureChanged (int newChannelPressureValue) override
  39. {
  40. // Set the amplitude based on pressure value
  41. amplitude.setValue (newChannelPressureValue / 127.0);
  42. }
  43. void renderNextBlock (AudioSampleBuffer& outputBuffer, int startSample, int numSamples) override
  44. {
  45. while (--numSamples >= 0)
  46. {
  47. double output = getSample() * amplitude.getNextValue();
  48. for (int i = outputBuffer.getNumChannels(); --i >= 0;)
  49. outputBuffer.addSample (i, startSample, static_cast<float> (output));
  50. ++startSample;
  51. }
  52. }
  53. /** Returns the next sample */
  54. double getSample()
  55. {
  56. double output = renderWaveShape (phasePos);
  57. phasePos += phaseIncrement.getNextValue();
  58. if (phasePos > (2.0 * double_Pi))
  59. phasePos -= (2.0 * double_Pi);
  60. return output;
  61. }
  62. /** Subclasses should override this to say whether they can play the given sound */
  63. virtual bool canPlaySound (SynthesiserSound*) override = 0;
  64. /** Subclasses should override this to render a waveshape */
  65. virtual double renderWaveShape (const double currentPhase) = 0;
  66. private:
  67. LinearSmoothedValue<double> amplitude, phaseIncrement;
  68. double frequency = 0;
  69. double phasePos = 0.0f;
  70. double sampleRate = 44100.0;
  71. int initialNote = 0;
  72. double maxFreq = 0, minFreq = 0;
  73. //==============================================================================
  74. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Oscillator)
  75. };
  76. //==============================================================================
  77. /**
  78. Sine sound struct - applies to MIDI channel 1
  79. */
  80. struct SineSound : public SynthesiserSound
  81. {
  82. SineSound () {}
  83. bool appliesToNote (int) override { return true; }
  84. bool appliesToChannel (int midiChannel) override { return (midiChannel == 1); }
  85. //==============================================================================
  86. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SineSound)
  87. };
  88. /**
  89. Sine voice struct that renders a sin waveshape
  90. */
  91. struct SineVoice : public Oscillator
  92. {
  93. SineVoice() {};
  94. bool canPlaySound (SynthesiserSound* sound) override { return dynamic_cast<SineSound*> (sound) != nullptr; }
  95. double renderWaveShape (const double currentPhase) override { return sin (currentPhase); }
  96. //==============================================================================
  97. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SineVoice)
  98. };
  99. //==============================================================================
  100. /**
  101. Square sound struct - applies to MIDI channel 2
  102. */
  103. struct SquareSound : public SynthesiserSound
  104. {
  105. SquareSound() {}
  106. bool appliesToNote (int) override { return true; }
  107. bool appliesToChannel (int midiChannel) override { return (midiChannel == 2); }
  108. //==============================================================================
  109. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SquareSound)
  110. };
  111. /**
  112. Square voice struct that renders a square waveshape
  113. */
  114. struct SquareVoice : public Oscillator
  115. {
  116. SquareVoice() {};
  117. bool canPlaySound (SynthesiserSound* sound) override { return dynamic_cast<SquareSound*> (sound) != nullptr; }
  118. double renderWaveShape (const double currentPhase) override { return (currentPhase < double_Pi ? 0.0 : 1.0); }
  119. //==============================================================================
  120. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SquareVoice)
  121. };
  122. //==============================================================================
  123. /**
  124. Sawtooth sound - applies to MIDI channel 3
  125. */
  126. struct SawSound : public SynthesiserSound
  127. {
  128. SawSound() {}
  129. bool appliesToNote (int) override { return true; }
  130. bool appliesToChannel (int midiChannel) override { return (midiChannel == 3); }
  131. //==============================================================================
  132. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SawSound)
  133. };
  134. /**
  135. Sawtooth voice that renders a sawtooth waveshape
  136. */
  137. struct SawVoice : public Oscillator
  138. {
  139. SawVoice() {}
  140. bool canPlaySound (SynthesiserSound* sound) override { return dynamic_cast<SawSound*> (sound) != nullptr; }
  141. double renderWaveShape (const double currentPhase) override { return (1.0 / double_Pi) * currentPhase - 1.0; }
  142. //==============================================================================
  143. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SawVoice)
  144. };
  145. //==============================================================================
  146. /**
  147. Triangle sound - applies to MIDI channel 4
  148. */
  149. struct TriangleSound : public SynthesiserSound
  150. {
  151. TriangleSound() {}
  152. bool appliesToNote (int) override { return true; }
  153. bool appliesToChannel (int midiChannel) override { return (midiChannel == 4); }
  154. //==============================================================================
  155. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TriangleSound)
  156. };
  157. /**
  158. Triangle voice that renders a triangle waveshape
  159. */
  160. struct TriangleVoice : public Oscillator
  161. {
  162. TriangleVoice() {}
  163. bool canPlaySound (SynthesiserSound* sound) override { return dynamic_cast<TriangleSound*> (sound) != nullptr; }
  164. double renderWaveShape (const double currentPhase) override
  165. {
  166. return (currentPhase < double_Pi ? -1.0 + (2.0 / double_Pi) * currentPhase
  167. : 3.0 - (2.0 / double_Pi) * currentPhase);
  168. }
  169. //==============================================================================
  170. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TriangleVoice)
  171. };