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.

114 lines
4.7KB

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