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.

96 lines
2.9KB

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