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.

112 lines
4.1KB

  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. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  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. } // namespace juce