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.

211 lines
6.7KB

  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. AudioFilePlayer::AudioFilePlayer()
  25. : bufferingTimeSliceThread (new TimeSliceThread ("Shared Buffering Thread"), true),
  26. formatManager (new AudioFormatManager(), true)
  27. {
  28. bufferingTimeSliceThread->startThread (3);
  29. formatManager->registerBasicFormats();
  30. commonInitialise();
  31. }
  32. AudioFilePlayer::AudioFilePlayer (TimeSliceThread* threadToUse,
  33. AudioFormatManager* formatManagerToUse)
  34. : bufferingTimeSliceThread ((threadToUse == nullptr ? new TimeSliceThread ("Shared Buffering Thread")
  35. : threadToUse),
  36. threadToUse == nullptr),
  37. formatManager ((formatManagerToUse == nullptr ? new AudioFormatManager()
  38. : formatManagerToUse),
  39. formatManagerToUse == nullptr)
  40. {
  41. commonInitialise();
  42. }
  43. AudioFilePlayer::~AudioFilePlayer()
  44. {
  45. audioTransportSource.setSource (nullptr);
  46. audioTransportSource.removeChangeListener (this);
  47. }
  48. //==============================================================================
  49. void AudioFilePlayer::start()
  50. {
  51. audioTransportSource.start();
  52. listeners.call (&Listener::playerStoppedOrStarted, this);
  53. }
  54. void AudioFilePlayer::stop()
  55. {
  56. audioTransportSource.stop();
  57. listeners.call (&Listener::playerStoppedOrStarted, this);
  58. }
  59. void AudioFilePlayer::startFromZero()
  60. {
  61. if (audioFormatReaderSource == nullptr)
  62. return;
  63. audioTransportSource.setPosition (0.0);
  64. audioTransportSource.start();
  65. listeners.call (&Listener::playerStoppedOrStarted, this);
  66. }
  67. void AudioFilePlayer::pause()
  68. {
  69. if (audioTransportSource.isPlaying())
  70. audioTransportSource.stop();
  71. else
  72. audioTransportSource.start();
  73. listeners.call (&Listener::playerStoppedOrStarted, this);
  74. }
  75. //==============================================================================
  76. void AudioFilePlayer::setPosition (double newPosition, bool /*ignoreAnyLoopPoints*/)
  77. {
  78. audioTransportSource.setPosition (newPosition);
  79. }
  80. //==============================================================================
  81. void AudioFilePlayer::setAudioFormatManager (AudioFormatManager* newManager, bool deleteWhenNotNeeded)
  82. {
  83. formatManager.set (newManager, deleteWhenNotNeeded);
  84. }
  85. void AudioFilePlayer::setTimeSliceThread (TimeSliceThread* newThreadToUse, bool deleteWhenNotNeeded)
  86. {
  87. bufferingTimeSliceThread.set (newThreadToUse, deleteWhenNotNeeded);
  88. }
  89. //==============================================================================
  90. void AudioFilePlayer::prepareToPlay (int samplesPerBlockExpected, double sampleRate)
  91. {
  92. if (masterSource != nullptr)
  93. masterSource->prepareToPlay (samplesPerBlockExpected, sampleRate);
  94. }
  95. void AudioFilePlayer::releaseResources()
  96. {
  97. if (masterSource != nullptr)
  98. masterSource->releaseResources();
  99. }
  100. void AudioFilePlayer::getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill)
  101. {
  102. if (masterSource != nullptr)
  103. masterSource->getNextAudioBlock (bufferToFill);
  104. }
  105. void AudioFilePlayer::setLooping (bool shouldLoop)
  106. {
  107. if (audioFormatReaderSource != nullptr)
  108. audioFormatReaderSource->setLooping (shouldLoop);
  109. }
  110. //==============================================================================
  111. bool AudioFilePlayer::fileChanged (const File& file)
  112. {
  113. if (setSourceWithReader (formatManager->createReaderFor (file)))
  114. return true;
  115. clear();
  116. return false;
  117. }
  118. bool AudioFilePlayer::streamChanged (InputStream* inputStream)
  119. {
  120. if (setSourceWithReader (formatManager->createReaderFor (inputStream)))
  121. return true;
  122. clear();
  123. return false;
  124. }
  125. void AudioFilePlayer::changeListenerCallback (ChangeBroadcaster* source)
  126. {
  127. if (source == &audioTransportSource)
  128. listeners.call (&Listener::playerStoppedOrStarted, this);
  129. }
  130. //==============================================================================
  131. void AudioFilePlayer::addListener (AudioFilePlayer::Listener* const listener)
  132. {
  133. listeners.add (listener);
  134. }
  135. void AudioFilePlayer::removeListener (AudioFilePlayer::Listener* const listener)
  136. {
  137. listeners.remove (listener);
  138. }
  139. //==============================================================================
  140. bool AudioFilePlayer::setSourceWithReader (AudioFormatReader* reader)
  141. {
  142. bool shouldBeLooping = isLooping();
  143. audioTransportSource.setSource (nullptr);
  144. if (reader != nullptr)
  145. {
  146. // we SHOULD let the AudioFormatReaderSource delete the reader for us..
  147. audioFormatReaderSource = new AudioFormatReaderSource (reader, true);
  148. audioTransportSource.setSource (audioFormatReaderSource,
  149. 32768,
  150. bufferingTimeSliceThread);
  151. if (shouldBeLooping)
  152. audioFormatReaderSource->setLooping (true);
  153. // let our listeners know that we have loaded a new file
  154. audioTransportSource.sendChangeMessage();
  155. listeners.call (&Listener::fileChanged, this);
  156. return true;
  157. }
  158. audioTransportSource.sendChangeMessage();
  159. listeners.call (&Listener::fileChanged, this);
  160. return false;
  161. }
  162. //==============================================================================
  163. void AudioFilePlayer::commonInitialise()
  164. {
  165. audioTransportSource.addChangeListener (this);
  166. masterSource = &audioTransportSource;
  167. }