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.

129 lines
5.5KB

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