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.

111 lines
4.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_BUFFERINGAUDIOSOURCE_JUCEHEADER__
  19. #define __JUCE_BUFFERINGAUDIOSOURCE_JUCEHEADER__
  20. #include "juce_PositionableAudioSource.h"
  21. #include "../../threads/juce_Thread.h"
  22. #include "../dsp/juce_AudioSampleBuffer.h"
  23. #include "../../memory/juce_OptionalScopedPointer.h"
  24. //==============================================================================
  25. /**
  26. An AudioSource which takes another source as input, and buffers it using a thread.
  27. Create this as a wrapper around another thread, and it will read-ahead with
  28. a background thread to smooth out playback. You can either create one of these
  29. directly, or use it indirectly using an AudioTransportSource.
  30. @see PositionableAudioSource, AudioTransportSource
  31. */
  32. class JUCE_API BufferingAudioSource : public PositionableAudioSource
  33. {
  34. public:
  35. //==============================================================================
  36. /** Creates a BufferingAudioSource.
  37. @param source the input source to read from
  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. bool deleteSourceWhenDeleted,
  45. int numberOfSamplesToBuffer,
  46. int numberOfChannels = 2);
  47. /** Destructor.
  48. The input source may be deleted depending on whether the deleteSourceWhenDeleted
  49. flag was set in the constructor.
  50. */
  51. ~BufferingAudioSource();
  52. //==============================================================================
  53. /** Implementation of the AudioSource method. */
  54. void prepareToPlay (int samplesPerBlockExpected, double sampleRate);
  55. /** Implementation of the AudioSource method. */
  56. void releaseResources();
  57. /** Implementation of the AudioSource method. */
  58. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill);
  59. //==============================================================================
  60. /** Implements the PositionableAudioSource method. */
  61. void setNextReadPosition (int64 newPosition);
  62. /** Implements the PositionableAudioSource method. */
  63. int64 getNextReadPosition() const;
  64. /** Implements the PositionableAudioSource method. */
  65. int64 getTotalLength() const { return source->getTotalLength(); }
  66. /** Implements the PositionableAudioSource method. */
  67. bool isLooping() const { return source->isLooping(); }
  68. private:
  69. //==============================================================================
  70. OptionalScopedPointer<PositionableAudioSource> source;
  71. int numberOfSamplesToBuffer, numberOfChannels;
  72. AudioSampleBuffer buffer;
  73. CriticalSection bufferStartPosLock;
  74. int64 volatile bufferValidStart, bufferValidEnd, nextPlayPos;
  75. double volatile sampleRate;
  76. bool wasSourceLooping;
  77. friend class SharedBufferingAudioSourceThread;
  78. bool readNextBufferChunk();
  79. void readBufferSection (int64 start, int length, int bufferOffset);
  80. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BufferingAudioSource);
  81. };
  82. #endif // __JUCE_BUFFERINGAUDIOSOURCE_JUCEHEADER__