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.

122 lines
4.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_AUDIOSOURCEPLAYER_H_INCLUDED
  24. #define JUCE_AUDIOSOURCEPLAYER_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. Wrapper class to continuously stream audio from an audio source to an
  28. AudioIODevice.
  29. This object acts as an AudioIODeviceCallback, so can be attached to an
  30. output device, and will stream audio from an AudioSource.
  31. */
  32. class JUCE_API AudioSourcePlayer : public AudioIODeviceCallback
  33. {
  34. public:
  35. //==============================================================================
  36. /** Creates an empty AudioSourcePlayer. */
  37. AudioSourcePlayer();
  38. /** Destructor.
  39. Make sure this object isn't still being used by an AudioIODevice before
  40. deleting it!
  41. */
  42. virtual ~AudioSourcePlayer();
  43. //==============================================================================
  44. /** Changes the current audio source to play from.
  45. If the source passed in is already being used, this method will do nothing.
  46. If the source is not null, its prepareToPlay() method will be called
  47. before it starts being used for playback.
  48. If there's another source currently playing, its releaseResources() method
  49. will be called after it has been swapped for the new one.
  50. @param newSource the new source to use - this will NOT be deleted
  51. by this object when no longer needed, so it's the
  52. caller's responsibility to manage it.
  53. */
  54. void setSource (AudioSource* newSource);
  55. /** Returns the source that's playing.
  56. May return nullptr if there's no source.
  57. */
  58. AudioSource* getCurrentSource() const noexcept { return source; }
  59. /** Sets a gain to apply to the audio data.
  60. @see getGain
  61. */
  62. void setGain (float newGain) noexcept;
  63. /** Returns the current gain.
  64. @see setGain
  65. */
  66. float getGain() const noexcept { return gain; }
  67. //==============================================================================
  68. /** Implementation of the AudioIODeviceCallback method. */
  69. void audioDeviceIOCallback (const float** inputChannelData,
  70. int totalNumInputChannels,
  71. float** outputChannelData,
  72. int totalNumOutputChannels,
  73. int numSamples) override;
  74. /** Implementation of the AudioIODeviceCallback method. */
  75. void audioDeviceAboutToStart (AudioIODevice* device) override;
  76. /** Implementation of the AudioIODeviceCallback method. */
  77. void audioDeviceStopped() override;
  78. /** An alternative method for initialising the source without an AudioIODevice. */
  79. void prepareToPlay (double sampleRate, int blockSize);
  80. private:
  81. //==============================================================================
  82. CriticalSection readLock;
  83. AudioSource* source;
  84. double sampleRate;
  85. int bufferSize;
  86. float* channels [128];
  87. float* outputChans [128];
  88. const float* inputChans [128];
  89. AudioSampleBuffer tempBuffer;
  90. float lastGain, gain;
  91. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioSourcePlayer)
  92. };
  93. #endif // JUCE_AUDIOSOURCEPLAYER_H_INCLUDED