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.

92 lines
2.8KB

  1. #pragma once
  2. #include "Oscillators.h"
  3. /**
  4. Class to handle the Audio functionality
  5. */
  6. class Audio : public AudioIODeviceCallback
  7. {
  8. public:
  9. Audio()
  10. {
  11. // Set up the audio device manager
  12. audioDeviceManager.initialiseWithDefaultDevices (0, 2);
  13. audioDeviceManager.addAudioCallback (this);
  14. // Set up the synthesiser and add each of the waveshapes
  15. synthesiser.clearVoices();
  16. synthesiser.clearSounds();
  17. synthesiser.addVoice (new SineVoice());
  18. synthesiser.addVoice (new SquareVoice());
  19. synthesiser.addVoice (new SawVoice());
  20. synthesiser.addVoice (new TriangleVoice());
  21. synthesiser.addSound (new SineSound());
  22. synthesiser.addSound (new SquareSound());
  23. synthesiser.addSound (new SawSound());
  24. synthesiser.addSound (new TriangleSound());
  25. }
  26. ~Audio()
  27. {
  28. audioDeviceManager.removeAudioCallback (this);
  29. }
  30. /** Audio callback */
  31. void audioDeviceIOCallback (const float **/*inputChannelData*/, int /*numInputChannels*/,
  32. float **outputChannelData, int numOutputChannels, int numSamples) override
  33. {
  34. AudioSampleBuffer sampleBuffer = AudioSampleBuffer (outputChannelData, numOutputChannels, numSamples);
  35. sampleBuffer.clear();
  36. synthesiser.renderNextBlock (sampleBuffer, MidiBuffer(), 0, numSamples);
  37. }
  38. void audioDeviceAboutToStart (AudioIODevice* device) override
  39. {
  40. synthesiser.setCurrentPlaybackSampleRate (device->getCurrentSampleRate());
  41. }
  42. void audioDeviceStopped() override {}
  43. /** Called to turn a synthesiser note on */
  44. void noteOn (int channel, int noteNum, float velocity)
  45. {
  46. synthesiser.noteOn (channel, noteNum, velocity);
  47. }
  48. /** Called to turn a synthesiser note off */
  49. void noteOff (int channel, int noteNum, float velocity)
  50. {
  51. synthesiser.noteOff (channel, noteNum, velocity, false);
  52. }
  53. /** Called to turn all synthesiser notes off */
  54. void allNotesOff()
  55. {
  56. for (int i = 1; i < 5; ++i)
  57. synthesiser.allNotesOff (i, false);
  58. }
  59. /** Send pressure change message to synthesiser */
  60. void pressureChange (int channel, float newPressure)
  61. {
  62. synthesiser.handleChannelPressure (channel, static_cast<int> (newPressure * 127));
  63. }
  64. /** Send pitch change message to synthesiser */
  65. void pitchChange (int channel, float pitchChange)
  66. {
  67. synthesiser.handlePitchWheel (channel, static_cast<int> (pitchChange * 127));
  68. }
  69. private:
  70. AudioDeviceManager audioDeviceManager;
  71. Synthesiser synthesiser;
  72. //==============================================================================
  73. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Audio)
  74. };