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.

117 lines
4.8KB

  1. /*
  2. Copyright (C) 2017 Xenakios
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of version 3 of the GNU General Public License
  5. as published by the Free Software Foundation.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License (version 3) for more details.
  10. www.gnu.org/licenses
  11. */
  12. #pragma once
  13. #include "../JuceLibraryCode/JuceHeader.h"
  14. //==============================================================================
  15. /**
  16. An AudioSource which takes another source as input, and buffers it using a thread.
  17. Create this as a wrapper around another thread, and it will read-ahead with
  18. a background thread to smooth out playback. You can either create one of these
  19. directly, or use it indirectly using an AudioTransportSource.
  20. @see PositionableAudioSource, AudioTransportSource
  21. @tags{Audio}
  22. */
  23. class JUCE_API MyBufferingAudioSource : public PositionableAudioSource,
  24. private TimeSliceClient
  25. {
  26. public:
  27. //==============================================================================
  28. /** Creates a BufferingAudioSource.
  29. @param source the input source to read from
  30. @param backgroundThread a background thread that will be used for the
  31. background read-ahead. This object must not be deleted
  32. until after any BufferingAudioSources that are using it
  33. have been deleted!
  34. @param deleteSourceWhenDeleted if true, then the input source object will
  35. be deleted when this object is deleted
  36. @param numberOfSamplesToBuffer the size of buffer to use for reading ahead
  37. @param numberOfChannels the number of channels that will be played
  38. @param prefillBufferOnPrepareToPlay if true, then calling prepareToPlay on this object will
  39. block until the buffer has been filled
  40. */
  41. MyBufferingAudioSource(PositionableAudioSource* source,
  42. TimeSliceThread& backgroundThread,
  43. bool deleteSourceWhenDeleted,
  44. int numberOfSamplesToBuffer,
  45. int numberOfChannels = 2,
  46. bool prefillBufferOnPrepareToPlay = true);
  47. /** Destructor.
  48. The input source may be deleted depending on whether the deleteSourceWhenDeleted
  49. flag was set in the constructor.
  50. */
  51. ~MyBufferingAudioSource();
  52. //==============================================================================
  53. /** Implementation of the AudioSource method. */
  54. void prepareToPlay (int samplesPerBlockExpected, double sampleRate) override;
  55. /** Implementation of the AudioSource method. */
  56. void releaseResources() override;
  57. /** Implementation of the AudioSource method. */
  58. void getNextAudioBlock (const AudioSourceChannelInfo&) override;
  59. //==============================================================================
  60. /** Implements the PositionableAudioSource method. */
  61. void setNextReadPosition (int64 newPosition) override;
  62. /** Implements the PositionableAudioSource method. */
  63. int64 getNextReadPosition() const override;
  64. /** Implements the PositionableAudioSource method. */
  65. int64 getTotalLength() const override { return source->getTotalLength(); }
  66. /** Implements the PositionableAudioSource method. */
  67. bool isLooping() const override { return source->isLooping(); }
  68. /** A useful function to block until the next the buffer info can be filled.
  69. This is useful for offline rendering.
  70. */
  71. bool waitForNextAudioBlockReady (const AudioSourceChannelInfo& info, const uint32 timeout);
  72. double getPercentReady();
  73. int getNumberOfChannels() { return numberOfChannels; }
  74. private:
  75. //==============================================================================
  76. OptionalScopedPointer<PositionableAudioSource> source;
  77. TimeSliceThread& backgroundThread;
  78. int numberOfSamplesToBuffer, numberOfChannels;
  79. AudioBuffer<float> buffer;
  80. CriticalSection bufferStartPosLock;
  81. WaitableEvent bufferReadyEvent;
  82. std::atomic<int64> bufferValidStart { 0 }, bufferValidEnd { 0 }, nextPlayPos { 0 };
  83. double sampleRate = 0;
  84. bool wasSourceLooping = false, isPrepared = false, prefillBuffer;
  85. bool readNextBufferChunk();
  86. void readBufferSection (int64 start, int length, int bufferOffset);
  87. int useTimeSlice() override;
  88. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MyBufferingAudioSource)
  89. };