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.

113 lines
4.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 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. //==============================================================================
  24. /**
  25. An AudioSource which takes another source as input, and buffers it using a thread.
  26. Create this as a wrapper around another thread, and it will read-ahead with
  27. a background thread to smooth out playback. You can either create one of these
  28. directly, or use it indirectly using an AudioTransportSource.
  29. @see PositionableAudioSource, AudioTransportSource
  30. */
  31. class JUCE_API BufferingAudioSource : public PositionableAudioSource
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates a BufferingAudioSource.
  36. @param source the input source to read from
  37. @param deleteSourceWhenDeleted if true, then the input source object will
  38. be deleted when this object is deleted
  39. @param numberOfSamplesToBuffer the size of buffer to use for reading ahead
  40. */
  41. BufferingAudioSource (PositionableAudioSource* source,
  42. const bool deleteSourceWhenDeleted,
  43. int numberOfSamplesToBuffer);
  44. /** Destructor.
  45. The input source may be deleted depending on whether the deleteSourceWhenDeleted
  46. flag was set in the constructor.
  47. */
  48. ~BufferingAudioSource();
  49. //==============================================================================
  50. /** Implementation of the AudioSource method. */
  51. void prepareToPlay (int samplesPerBlockExpected, double sampleRate);
  52. /** Implementation of the AudioSource method. */
  53. void releaseResources();
  54. /** Implementation of the AudioSource method. */
  55. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill);
  56. //==============================================================================
  57. /** Implements the PositionableAudioSource method. */
  58. void setNextReadPosition (int newPosition);
  59. /** Implements the PositionableAudioSource method. */
  60. int getNextReadPosition() const;
  61. /** Implements the PositionableAudioSource method. */
  62. int getTotalLength() const { return source->getTotalLength(); }
  63. /** Implements the PositionableAudioSource method. */
  64. bool isLooping() const { return source->isLooping(); }
  65. //==============================================================================
  66. juce_UseDebuggingNewOperator
  67. private:
  68. //==============================================================================
  69. PositionableAudioSource* source;
  70. bool deleteSourceWhenDeleted;
  71. int numberOfSamplesToBuffer;
  72. AudioSampleBuffer buffer;
  73. CriticalSection bufferStartPosLock;
  74. int volatile bufferValidStart, bufferValidEnd, nextPlayPos;
  75. bool wasSourceLooping;
  76. double volatile sampleRate;
  77. friend class SharedBufferingAudioSourceThread;
  78. bool readNextBufferChunk();
  79. void readBufferSection (int start, int length, int bufferOffset);
  80. BufferingAudioSource (const BufferingAudioSource&);
  81. BufferingAudioSource& operator= (const BufferingAudioSource&);
  82. };
  83. #endif // __JUCE_BUFFERINGAUDIOSOURCE_JUCEHEADER__