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.

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