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.

134 lines
4.4KB

  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. #include "../../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_AudioFormatReaderSource.h"
  21. //==============================================================================
  22. AudioFormatReaderSource::AudioFormatReaderSource (AudioFormatReader* const reader_,
  23. const bool deleteReaderWhenThisIsDeleted)
  24. : reader (reader_),
  25. deleteReader (deleteReaderWhenThisIsDeleted),
  26. nextPlayPos (0),
  27. looping (false)
  28. {
  29. jassert (reader != 0);
  30. }
  31. AudioFormatReaderSource::~AudioFormatReaderSource()
  32. {
  33. releaseResources();
  34. if (deleteReader)
  35. delete reader;
  36. }
  37. void AudioFormatReaderSource::setNextReadPosition (int64 newPosition)
  38. {
  39. nextPlayPos = newPosition;
  40. }
  41. void AudioFormatReaderSource::setLooping (bool shouldLoop)
  42. {
  43. looping = shouldLoop;
  44. }
  45. int64 AudioFormatReaderSource::getNextReadPosition() const
  46. {
  47. return looping ? nextPlayPos % reader->lengthInSamples
  48. : nextPlayPos;
  49. }
  50. int64 AudioFormatReaderSource::getTotalLength() const
  51. {
  52. return reader->lengthInSamples;
  53. }
  54. void AudioFormatReaderSource::prepareToPlay (int /*samplesPerBlockExpected*/,
  55. double /*sampleRate*/)
  56. {
  57. }
  58. void AudioFormatReaderSource::releaseResources()
  59. {
  60. }
  61. void AudioFormatReaderSource::getNextAudioBlock (const AudioSourceChannelInfo& info)
  62. {
  63. if (info.numSamples > 0)
  64. {
  65. const int64 start = nextPlayPos;
  66. if (looping)
  67. {
  68. const int newStart = start % (int) reader->lengthInSamples;
  69. const int newEnd = (start + info.numSamples) % (int) reader->lengthInSamples;
  70. if (newEnd > newStart)
  71. {
  72. info.buffer->readFromAudioReader (reader,
  73. info.startSample,
  74. newEnd - newStart,
  75. newStart,
  76. true, true);
  77. }
  78. else
  79. {
  80. const int endSamps = (int) reader->lengthInSamples - newStart;
  81. info.buffer->readFromAudioReader (reader,
  82. info.startSample,
  83. endSamps,
  84. newStart,
  85. true, true);
  86. info.buffer->readFromAudioReader (reader,
  87. info.startSample + endSamps,
  88. newEnd,
  89. 0,
  90. true, true);
  91. }
  92. nextPlayPos = newEnd;
  93. }
  94. else
  95. {
  96. info.buffer->readFromAudioReader (reader,
  97. info.startSample,
  98. info.numSamples,
  99. start,
  100. true, true);
  101. nextPlayPos += info.numSamples;
  102. }
  103. }
  104. }
  105. END_JUCE_NAMESPACE