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.

242 lines
10.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the dRowAudio JUCE module
  4. Copyright 2004-13 by dRowAudio.
  5. ------------------------------------------------------------------------------
  6. dRowAudio is provided under the terms of The MIT License (MIT):
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. ==============================================================================
  23. */
  24. #ifndef __DROWAUDIO_AUDIOFILEPLAYER_H__
  25. #define __DROWAUDIO_AUDIOFILEPLAYER_H__
  26. #include "../streams/dRowAudio_StreamAndFileHandler.h"
  27. //==============================================================================
  28. /**
  29. This class can be used to load and play an audio file from disk.
  30. This combines the functionality of an AudioTransportSource,
  31. AudioFormatReader and AudioFormatReaderSource.
  32. @see AudioTransportSource
  33. @see AudioFormatReader
  34. @see AudioFormatReaderSource
  35. @see AudioFilePlayerExt
  36. */
  37. class AudioFilePlayer : public StreamAndFileHandler,
  38. public PositionableAudioSource,
  39. public ChangeListener
  40. {
  41. public:
  42. //==============================================================================
  43. /** Creates an empty AudioFilePlayer.
  44. This is a quick way to create an AudioFilePlayer as it will use its own
  45. AudioFormatManager and TimeSliceThread.
  46. */
  47. AudioFilePlayer();
  48. /** Creates an empty AudioFilePlayer that will use a supplied background thread
  49. and format manager.
  50. If either of these parameters is nullptr the AudioFilePlayer will create its
  51. own. This constructor is useful if you have lots of players and don't want
  52. loads of background thread running etc. If you supply your own thread
  53. remember to start it!
  54. */
  55. explicit AudioFilePlayer (TimeSliceThread* threadToUse,
  56. AudioFormatManager* formatManagerToUse);
  57. /** Destructor.
  58. If you subclass from this, make sure to call
  59. audioTransportSource->setSource (nullptr) in your destructor so you don't
  60. mess up the audio chain dependancies and crash
  61. */
  62. virtual ~AudioFilePlayer();
  63. //==============================================================================
  64. /** Starts playing (if a source has been selected). */
  65. void start();
  66. /** Stops playing. */
  67. void stop();
  68. /** Play the audio file from the start. */
  69. void startFromZero();
  70. /** Pauses or plays the audio file. */
  71. void pause();
  72. /** Returns true if it's currently playing. */
  73. bool isPlaying() const noexcept { return audioTransportSource.isPlaying(); }
  74. //==============================================================================
  75. /** Changes the current playback position in the source stream.
  76. */
  77. virtual void setPosition (double newPosition, bool ignoreAnyLoopBounds = false);
  78. /** Returns the position that the next data block will be read from in seconds.
  79. */
  80. double getCurrentPosition() const { return audioTransportSource.getCurrentPosition();}
  81. /** Returns the stream's length in seconds.
  82. */
  83. double getLengthInSeconds() const { return audioTransportSource.getLengthInSeconds();}
  84. /** Returns true if the player has stopped because its input stream ran out of data.
  85. */
  86. bool hasStreamFinished() const noexcept { return audioTransportSource.hasStreamFinished(); }
  87. //==============================================================================
  88. /** Returns the AudioFormatReaderSource currently being used.
  89. */
  90. inline AudioFormatReaderSource* getAudioFormatReaderSource() { return audioFormatReaderSource; }
  91. /** Returns the AudioTransportSource being used.
  92. */
  93. inline AudioTransportSource* getAudioTransportSource() { return &audioTransportSource; }
  94. /** Sets the AudioFormatManager to use.
  95. */
  96. void setAudioFormatManager (AudioFormatManager* newManager, bool deleteWhenNotNeeded);
  97. /** Returns the AudioFormatManager being used.
  98. */
  99. inline AudioFormatManager* getAudioFormatManager() { return formatManager; }
  100. /** Sets the TimeSliceThread to use.
  101. */
  102. void setTimeSliceThread (TimeSliceThread* newThreadToUse, bool deleteWhenNotNeeded);
  103. /** Returns the background TimeSliceThread being used.
  104. */
  105. inline TimeSliceThread* getTimeSliceThread() { return bufferingTimeSliceThread; }
  106. //==============================================================================
  107. /** A class for receiving callbacks from a AudioFilePlayer.
  108. To be told when a player's file changes, you can register an AudioFilePlayer::Listener
  109. object using AudioFilePlayer::addListener().
  110. @see AudioFilePlayer::addListener, AudioFilePlayer::removeListener
  111. */
  112. class Listener
  113. {
  114. public:
  115. //==============================================================================
  116. /** Destructor. */
  117. virtual ~Listener() {}
  118. //==============================================================================
  119. /** Called when the player's file is changed.
  120. You can find out the new file using AudioFilePlayer::getFile().
  121. */
  122. virtual void fileChanged (AudioFilePlayer* player) = 0;
  123. /** Called when the the player is stopped or started.
  124. You can find out if it is currently stopped with isPlaying().
  125. */
  126. virtual void playerStoppedOrStarted (AudioFilePlayer* /*player*/) {}
  127. /** To avoid having to create a new listener interface for each subclass of AudioFilePlayer
  128. you can call this and send a SettingCode to your listeners to identify what sort of change occured
  129. e.g. playback rate, filter gain etc.
  130. */
  131. virtual void audioFilePlayerSettingChanged (AudioFilePlayer* /*player*/, int /*settingCode*/) {}
  132. };
  133. /** Adds a listener to be called when this slider's value changes. */
  134. void addListener (Listener* listener);
  135. /** Removes a previously-registered listener. */
  136. void removeListener (Listener* listener);
  137. //==============================================================================
  138. /** Implementation of the AudioSource method. */
  139. void prepareToPlay (int samplesPerBlockExpected, double sampleRate);
  140. /** Implementation of the AudioSource method. */
  141. void releaseResources();
  142. /** Implementation of the AudioSource method. */
  143. void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill);
  144. //==============================================================================
  145. /** Sets the next read position in samples. */
  146. void setNextReadPosition (int64 newPosition) { audioTransportSource.setNextReadPosition (newPosition); }
  147. /** Returns the position from which the next block will be returned. */
  148. int64 getNextReadPosition() const { return audioTransportSource.getNextReadPosition(); }
  149. /** Returns the total length of the stream (in samples). */
  150. int64 getTotalLength() const { return audioTransportSource.getTotalLength(); }
  151. /** Returns true if this source is actually playing in a loop. */
  152. bool isLooping() const { return audioTransportSource.isLooping(); }
  153. /** Tells the source whether you'd like it to play in a loop. */
  154. virtual void setLooping (bool shouldLoop);
  155. //==============================================================================
  156. /** @internal. */
  157. bool fileChanged (const File& file) override;
  158. /** @internal. */
  159. bool streamChanged (InputStream* inputStream) override;
  160. /** @internal. */
  161. void changeListenerCallback (ChangeBroadcaster* source) override;
  162. protected:
  163. //==============================================================================
  164. OptionalScopedPointer<TimeSliceThread> bufferingTimeSliceThread;
  165. OptionalScopedPointer<AudioFormatManager> formatManager;
  166. AudioSource* masterSource;
  167. ScopedPointer<AudioFormatReaderSource> audioFormatReaderSource;
  168. AudioTransportSource audioTransportSource;
  169. ListenerList <Listener> listeners;
  170. //==============================================================================
  171. /** Sets up the audio chain when a new source is chosen.
  172. By default this will create a new AudioFormatReader source and attach it to the
  173. AudioTransportSource. If you want to add your own sources overide this method.
  174. If you do change this make sure you set the masterSource member to the top level
  175. of your audio source chain.
  176. */
  177. virtual bool setSourceWithReader (AudioFormatReader* reader);
  178. private:
  179. //==============================================================================
  180. void commonInitialise();
  181. //==============================================================================
  182. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AudioFilePlayer);
  183. };
  184. #endif // __DROWAUDIO_AUDIOFILEPLAYER_H__