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.

116 lines
5.0KB

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