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.

juce_BufferingAudioSource.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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_BUFFERINGAUDIOSOURCE_H_INCLUDED
  18. #define JUCE_BUFFERINGAUDIOSOURCE_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. An AudioSource which takes another source as input, and buffers it using a thread.
  22. Create this as a wrapper around another thread, and it will read-ahead with
  23. a background thread to smooth out playback. You can either create one of these
  24. directly, or use it indirectly using an AudioTransportSource.
  25. @see PositionableAudioSource, AudioTransportSource
  26. */
  27. class JUCE_API BufferingAudioSource : public PositionableAudioSource,
  28. private TimeSliceClient
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a BufferingAudioSource.
  33. @param source the input source to read from
  34. @param backgroundThread a background thread that will be used for the
  35. background read-ahead. This object must not be deleted
  36. until after any BufferedAudioSources that are using it
  37. have been deleted!
  38. @param deleteSourceWhenDeleted if true, then the input source object will
  39. be deleted when this object is deleted
  40. @param numberOfSamplesToBuffer the size of buffer to use for reading ahead
  41. @param numberOfChannels the number of channels that will be played
  42. */
  43. BufferingAudioSource (PositionableAudioSource* source,
  44. TimeSliceThread& backgroundThread,
  45. bool deleteSourceWhenDeleted,
  46. int numberOfSamplesToBuffer,
  47. int numberOfChannels = 2);
  48. /** Destructor.
  49. The input source may be deleted depending on whether the deleteSourceWhenDeleted
  50. flag was set in the constructor.
  51. */
  52. ~BufferingAudioSource();
  53. //==============================================================================
  54. /** Implementation of the AudioSource method. */
  55. void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
  56. /** Implementation of the AudioSource method. */
  57. void releaseResources() override;
  58. /** Implementation of the AudioSource method. */
  59. void getNextAudioBlock (const AudioSourceChannelInfo&) override;
  60. //==============================================================================
  61. /** Implements the PositionableAudioSource method. */
  62. void setNextReadPosition (int64 newPosition) override;
  63. /** Implements the PositionableAudioSource method. */
  64. int64 getNextReadPosition() const override;
  65. /** Implements the PositionableAudioSource method. */
  66. int64 getTotalLength() const override { return source->getTotalLength(); }
  67. /** Implements the PositionableAudioSource method. */
  68. bool isLooping() const override { return source->isLooping(); }
  69. private:
  70. //==============================================================================
  71. OptionalScopedPointer<PositionableAudioSource> source;
  72. TimeSliceThread& backgroundThread;
  73. int numberOfSamplesToBuffer, numberOfChannels;
  74. AudioSampleBuffer buffer;
  75. CriticalSection bufferStartPosLock;
  76. int64 volatile bufferValidStart, bufferValidEnd, nextPlayPos;
  77. double volatile sampleRate;
  78. bool wasSourceLooping, isPrepared;
  79. bool readNextBufferChunk();
  80. void readBufferSection (int64 start, int length, int bufferOffset);
  81. int useTimeSlice() override;
  82. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BufferingAudioSource)
  83. };
  84. #endif // JUCE_BUFFERINGAUDIOSOURCE_H_INCLUDED