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.

136 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. A base class for writing audio apps that stream from the audio i/o devices.
  23. Conveniently combines a Component with an AudioSource to provide a starting
  24. point for your audio applications.
  25. A subclass can inherit from this and implement just a few methods such as
  26. getNextAudioBlock(). The base class provides a basic AudioDeviceManager object
  27. and runs audio through the default output device.
  28. An application should only create one global instance of this object and multiple
  29. classes should not inherit from this.
  30. This class should not be inherited when creating a plug-in as the host will
  31. handle audio streams from hardware devices.
  32. @tags{Audio}
  33. */
  34. class JUCE_API AudioAppComponent : public Component,
  35. public AudioSource
  36. {
  37. public:
  38. AudioAppComponent();
  39. AudioAppComponent (AudioDeviceManager&);
  40. ~AudioAppComponent() override;
  41. /** A subclass should call this from their constructor, to set up the audio. */
  42. void setAudioChannels (int numInputChannels, int numOutputChannels, const XmlElement* const storedSettings = nullptr);
  43. /** Tells the source to prepare for playing.
  44. An AudioSource has two states: prepared and unprepared.
  45. The prepareToPlay() method is guaranteed to be called at least once on an 'unprepared'
  46. source to put it into a 'prepared' state before any calls will be made to getNextAudioBlock().
  47. This callback allows the source to initialise any resources it might need when playing.
  48. Once playback has finished, the releaseResources() method is called to put the stream
  49. back into an 'unprepared' state.
  50. Note that this method could be called more than once in succession without
  51. a matching call to releaseResources(), so make sure your code is robust and
  52. can handle that kind of situation.
  53. @param samplesPerBlockExpected the number of samples that the source
  54. will be expected to supply each time its
  55. getNextAudioBlock() method is called. This
  56. number may vary slightly, because it will be dependent
  57. on audio hardware callbacks, and these aren't
  58. guaranteed to always use a constant block size, so
  59. the source should be able to cope with small variations.
  60. @param sampleRate the sample rate that the output will be used at - this
  61. is needed by sources such as tone generators.
  62. @see releaseResources, getNextAudioBlock
  63. */
  64. void prepareToPlay (int samplesPerBlockExpected,
  65. double sampleRate) override = 0;
  66. /** Allows the source to release anything it no longer needs after playback has stopped.
  67. This will be called when the source is no longer going to have its getNextAudioBlock()
  68. method called, so it should release any spare memory, etc. that it might have
  69. allocated during the prepareToPlay() call.
  70. Note that there's no guarantee that prepareToPlay() will actually have been called before
  71. releaseResources(), and it may be called more than once in succession, so make sure your
  72. code is robust and doesn't make any assumptions about when it will be called.
  73. @see prepareToPlay, getNextAudioBlock
  74. */
  75. void releaseResources() override = 0;
  76. /** Called repeatedly to fetch subsequent blocks of audio data.
  77. After calling the prepareToPlay() method, this callback will be made each
  78. time the audio playback hardware (or whatever other destination the audio
  79. data is going to) needs another block of data.
  80. It will generally be called on a high-priority system thread, or possibly even
  81. an interrupt, so be careful not to do too much work here, as that will cause
  82. audio glitches!
  83. @see AudioSourceChannelInfo, prepareToPlay, releaseResources
  84. */
  85. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override = 0;
  86. /** Shuts down the audio device and clears the audio source.
  87. This method should be called in the destructor of the derived class
  88. otherwise an assertion will be triggered.
  89. */
  90. void shutdownAudio();
  91. AudioDeviceManager& deviceManager;
  92. private:
  93. //==============================================================================
  94. AudioDeviceManager defaultDeviceManager;
  95. AudioSourcePlayer audioSourcePlayer;
  96. bool usingCustomDeviceManager;
  97. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioAppComponent)
  98. };
  99. } // namespace juce