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.

123 lines
5.1KB

  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_SOUNDTOUCHAUDIOSOURCE_H__
  25. #define __DROWAUDIO_SOUNDTOUCHAUDIOSOURCE_H__
  26. #if DROWAUDIO_USE_SOUNDTOUCH || DOXYGEN
  27. #include "dRowAudio_SoundTouchProcessor.h"
  28. //==============================================================================
  29. /** An audio source that can independently change the rate, tempo and pitch of
  30. an audio source. This uses the SoundTouch library to perform the processing.
  31. */
  32. class SoundTouchAudioSource : public PositionableAudioSource
  33. {
  34. public:
  35. //==============================================================================
  36. /** Creates a SoundTouchAudio source.
  37. The numberOfSamplesToBuffer parameter is how many samples the source will
  38. buffer internally. Due to the way SoundTouch processes samples any changes
  39. of playback settings will only come into effect some time after this number
  40. of samples has been process. For fine control try reducing this but beware
  41. the extra function calls will use more CPU.
  42. */
  43. SoundTouchAudioSource (PositionableAudioSource* source,
  44. bool deleteSourceWhenDeleted = false,
  45. int numberOfSamplesToBuffer = 2048,
  46. int numberOfChannels = 2);
  47. /** Destructor. */
  48. ~SoundTouchAudioSource();
  49. /** Sets all of the settings at once.
  50. */
  51. void setPlaybackSettings (SoundTouchProcessor::PlaybackSettings newSettings);
  52. /** Returns all of the settings.
  53. */
  54. SoundTouchProcessor::PlaybackSettings getPlaybackSettings() { return soundTouchProcessor.getPlaybackSettings(); }
  55. /** Returns the lock used when setting the buffer read positions.
  56. */
  57. inline const CriticalSection& getBufferLock() { return bufferStartPosLock; }
  58. /** Returns the SoundTouchProcessor being used.
  59. */
  60. inline SoundTouchProcessor& getSoundTouchProcessor() { return soundTouchProcessor; }
  61. //==============================================================================
  62. /** @internal. */
  63. void prepareToPlay (int samplesPerBlockExpected, double sampleRate);
  64. /** @internal. */
  65. void releaseResources();
  66. /** @internal. */
  67. void getNextAudioBlock (const AudioSourceChannelInfo& info);
  68. //==============================================================================
  69. /** Implements the PositionableAudioSource method. */
  70. void setNextReadPosition (int64 newPosition);
  71. /** Implements the PositionableAudioSource method. */
  72. int64 getNextReadPosition() const;
  73. /** Implements the PositionableAudioSource method. */
  74. int64 getTotalLength() const { return source->getTotalLength(); }
  75. /** Implements the PositionableAudioSource method. */
  76. bool isLooping() const { return source->isLooping(); }
  77. /** Implements the PositionableAudioSource method. */
  78. void setLooping (bool shouldLoop) { source->setLooping (shouldLoop); }
  79. private:
  80. //==============================================================================
  81. OptionalScopedPointer<PositionableAudioSource> source;
  82. int numberOfSamplesToBuffer, numberOfChannels;
  83. AudioSampleBuffer buffer;
  84. CriticalSection bufferStartPosLock;
  85. int64 volatile nextReadPos, effectiveNextPlayPos;
  86. double volatile sampleRate;
  87. bool isPrepared;
  88. SoundTouchProcessor soundTouchProcessor;
  89. void readNextBufferChunk();
  90. //==============================================================================
  91. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SoundTouchAudioSource);
  92. };
  93. #endif
  94. #endif // __DROWAUDIO_SOUNDTOUCHAUDIOSOURCE_H__