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.

115 lines
4.2KB

  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. @tags{Audio}
  26. */
  27. class JUCE_API AudioSourcePlayer : public AudioIODeviceCallback
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates an empty AudioSourcePlayer. */
  32. AudioSourcePlayer();
  33. /** Destructor.
  34. Make sure this object isn't still being used by an AudioIODevice before
  35. deleting it!
  36. */
  37. ~AudioSourcePlayer() override;
  38. //==============================================================================
  39. /** Changes the current audio source to play from.
  40. If the source passed in is already being used, this method will do nothing.
  41. If the source is not null, its prepareToPlay() method will be called
  42. before it starts being used for playback.
  43. If there's another source currently playing, its releaseResources() method
  44. will be called after it has been swapped for the new one.
  45. @param newSource the new source to use - this will NOT be deleted
  46. by this object when no longer needed, so it's the
  47. caller's responsibility to manage it.
  48. */
  49. void setSource (AudioSource* newSource);
  50. /** Returns the source that's playing.
  51. May return nullptr if there's no source.
  52. */
  53. AudioSource* getCurrentSource() const noexcept { return source; }
  54. /** Sets a gain to apply to the audio data.
  55. @see getGain
  56. */
  57. void setGain (float newGain) noexcept;
  58. /** Returns the current gain.
  59. @see setGain
  60. */
  61. float getGain() const noexcept { return gain; }
  62. //==============================================================================
  63. /** Implementation of the AudioIODeviceCallback method. */
  64. void audioDeviceIOCallback (const float** inputChannelData,
  65. int totalNumInputChannels,
  66. float** outputChannelData,
  67. int totalNumOutputChannels,
  68. int numSamples) override;
  69. /** Implementation of the AudioIODeviceCallback method. */
  70. void audioDeviceAboutToStart (AudioIODevice* device) override;
  71. /** Implementation of the AudioIODeviceCallback method. */
  72. void audioDeviceStopped() override;
  73. /** An alternative method for initialising the source without an AudioIODevice. */
  74. void prepareToPlay (double sampleRate, int blockSize);
  75. private:
  76. //==============================================================================
  77. CriticalSection readLock;
  78. AudioSource* source = nullptr;
  79. double sampleRate = 0;
  80. int bufferSize = 0;
  81. float* channels[128];
  82. float* outputChans[128];
  83. const float* inputChans[128];
  84. AudioBuffer<float> tempBuffer;
  85. float lastGain = 1.0f;
  86. std::atomic<float> gain { 1.0f };
  87. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioSourcePlayer)
  88. };
  89. } // namespace juce