Audio plugin host https://kx.studio/carla
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.

116 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_AUDIOSOURCEPLAYER_H_INCLUDED
  18. #define JUCE_AUDIOSOURCEPLAYER_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Wrapper class to continuously stream audio from an audio source to an
  22. AudioIODevice.
  23. This object acts as an AudioIODeviceCallback, so can be attached to an
  24. output device, and will stream audio from an AudioSource.
  25. */
  26. class JUCE_API AudioSourcePlayer : public AudioIODeviceCallback
  27. {
  28. public:
  29. //==============================================================================
  30. /** Creates an empty AudioSourcePlayer. */
  31. AudioSourcePlayer();
  32. /** Destructor.
  33. Make sure this object isn't still being used by an AudioIODevice before
  34. deleting it!
  35. */
  36. virtual ~AudioSourcePlayer();
  37. //==============================================================================
  38. /** Changes the current audio source to play from.
  39. If the source passed in is already being used, this method will do nothing.
  40. If the source is not null, its prepareToPlay() method will be called
  41. before it starts being used for playback.
  42. If there's another source currently playing, its releaseResources() method
  43. will be called after it has been swapped for the new one.
  44. @param newSource the new source to use - this will NOT be deleted
  45. by this object when no longer needed, so it's the
  46. caller's responsibility to manage it.
  47. */
  48. void setSource (AudioSource* newSource);
  49. /** Returns the source that's playing.
  50. May return nullptr if there's no source.
  51. */
  52. AudioSource* getCurrentSource() const noexcept { return source; }
  53. /** Sets a gain to apply to the audio data.
  54. @see getGain
  55. */
  56. void setGain (float newGain) noexcept;
  57. /** Returns the current gain.
  58. @see setGain
  59. */
  60. float getGain() const noexcept { return gain; }
  61. //==============================================================================
  62. /** Implementation of the AudioIODeviceCallback method. */
  63. void audioDeviceIOCallback (const float** inputChannelData,
  64. int totalNumInputChannels,
  65. float** outputChannelData,
  66. int totalNumOutputChannels,
  67. int numSamples) override;
  68. /** Implementation of the AudioIODeviceCallback method. */
  69. void audioDeviceAboutToStart (AudioIODevice* device) override;
  70. /** Implementation of the AudioIODeviceCallback method. */
  71. void audioDeviceStopped() override;
  72. /** An alternative method for initialising the source without an AudioIODevice. */
  73. void prepareToPlay (double sampleRate, int blockSize);
  74. private:
  75. //==============================================================================
  76. CriticalSection readLock;
  77. AudioSource* source;
  78. double sampleRate;
  79. int bufferSize;
  80. float* channels [128];
  81. float* outputChans [128];
  82. const float* inputChans [128];
  83. AudioSampleBuffer tempBuffer;
  84. float lastGain, gain;
  85. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioSourcePlayer)
  86. };
  87. #endif // JUCE_AUDIOSOURCEPLAYER_H_INCLUDED