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.

137 lines
5.8KB

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