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.

118 lines
3.7KB

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