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.

193 lines
6.3KB

  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. //==============================================================================
  25. LoopingAudioSource::LoopingAudioSource (PositionableAudioSource* const inputSource,
  26. const bool deleteInputWhenDeleted)
  27. : input (inputSource, deleteInputWhenDeleted),
  28. isLoopingBetweenTimes (false),
  29. loopStartSample (0),
  30. loopEndSample (0),
  31. tempBuffer (2, 512)
  32. {
  33. jassert (inputSource != nullptr);
  34. tempInfo.numSamples = tempBuffer.getNumSamples();
  35. tempInfo.startSample = 0;
  36. tempInfo.buffer = &tempBuffer;
  37. }
  38. LoopingAudioSource::~LoopingAudioSource()
  39. {
  40. }
  41. //==============================================================================
  42. void LoopingAudioSource::setLoopTimes (double startTime, double endTime)
  43. {
  44. jassert (endTime > startTime); // end time has to be after start!
  45. {
  46. const ScopedLock sl (loopPosLock);
  47. loopStartTime = startTime;
  48. loopEndTime = endTime;
  49. loopStartSample = (int64) (startTime * currentSampleRate);
  50. loopEndSample = (int64) (endTime * currentSampleRate);
  51. }
  52. // need to update read position based on new limits
  53. setNextReadPosition (getNextReadPosition());
  54. }
  55. void LoopingAudioSource::getLoopTimes (double& startTime, double& endTime)
  56. {
  57. startTime = loopStartTime;
  58. endTime =loopEndTime;
  59. }
  60. void LoopingAudioSource::setLoopBetweenTimes (bool shouldLoop)
  61. {
  62. isLoopingBetweenTimes = shouldLoop;
  63. }
  64. bool LoopingAudioSource::getLoopBetweenTimes()
  65. {
  66. return isLoopingBetweenTimes;
  67. }
  68. //==============================================================================
  69. void LoopingAudioSource::prepareToPlay (int samplesPerBlockExpected,
  70. double sampleRate)
  71. {
  72. currentSampleRate = sampleRate;
  73. input->prepareToPlay (samplesPerBlockExpected, sampleRate);
  74. if (tempBuffer.getNumSamples() < samplesPerBlockExpected)
  75. {
  76. tempBuffer.setSize (2, samplesPerBlockExpected);
  77. tempInfo.numSamples = tempBuffer.getNumSamples();
  78. }
  79. }
  80. void LoopingAudioSource::releaseResources()
  81. {
  82. input->releaseResources();
  83. }
  84. void LoopingAudioSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  85. {
  86. if (info.numSamples > 0)
  87. {
  88. if (isLoopingBetweenTimes)
  89. {
  90. const ScopedLock sl (loopPosLock);
  91. const int64 newStart = getNextReadPosition();
  92. int64 newEnd = loopStartSample + ((newStart + info.numSamples) % loopEndSample);
  93. if (newStart > loopEndSample)
  94. newEnd = newStart + info.numSamples;
  95. if (newEnd > newStart)
  96. {
  97. input->getNextAudioBlock (info);
  98. }
  99. else
  100. {
  101. const int64 numEndSamps = loopEndSample - newStart;
  102. const int64 numStartSamps = newEnd - loopStartSample;
  103. tempInfo.startSample = 0;
  104. tempInfo.numSamples = (int) numEndSamps;
  105. input->getNextAudioBlock (tempInfo);
  106. tempInfo.startSample = (int) numEndSamps;
  107. tempInfo.numSamples = (int) numStartSamps;
  108. input->setNextReadPosition (loopStartSample);
  109. input->getNextAudioBlock (tempInfo);
  110. for (int i = 0; i < info.buffer->getNumChannels(); ++i)
  111. info.buffer->copyFrom (i, info.startSample,
  112. tempBuffer,
  113. i, 0, info.numSamples);
  114. }
  115. }
  116. else
  117. {
  118. input->getNextAudioBlock (info);
  119. }
  120. }
  121. }
  122. //==============================================================================
  123. void LoopingAudioSource::setNextReadPosition (int64 newPosition)
  124. {
  125. const ScopedLock sl (loopPosLock);
  126. if (isLoopingBetweenTimes
  127. && getNextReadPosition() > loopStartSample
  128. && getNextReadPosition() < loopEndSample)
  129. {
  130. const int64 numLoopSamples = loopEndSample - loopStartSample;
  131. if (newPosition > loopEndSample)
  132. newPosition = loopStartSample + ((newPosition - loopEndSample) % numLoopSamples);
  133. else if (newPosition < loopStartSample)
  134. newPosition = loopEndSample - ((loopStartSample - newPosition) % numLoopSamples);
  135. }
  136. input->setNextReadPosition (newPosition);
  137. }
  138. void LoopingAudioSource::setNextReadPositionIgnoringLoop (int64 newPosition)
  139. {
  140. input->setNextReadPosition (newPosition);
  141. }
  142. int64 LoopingAudioSource::getNextReadPosition() const
  143. {
  144. return input->getNextReadPosition();
  145. }
  146. int64 LoopingAudioSource::getTotalLength() const
  147. {
  148. return input->getTotalLength();
  149. }
  150. bool LoopingAudioSource::isLooping() const
  151. {
  152. return input->isLooping();
  153. }