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.

155 lines
6.6KB

  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_SOUNDTOUCHPROCESSOR_H__
  25. #define __DROWAUDIO_SOUNDTOUCHPROCESSOR_H__
  26. #if DROWAUDIO_USE_SOUNDTOUCH || DOXYGEN
  27. } // namespace drow
  28. #include "soundtouch/SoundTouch.h"
  29. namespace drow {
  30. //==============================================================================
  31. /** Wraps a SoundTouch object to enable pitch and tempo adjustments to an audio buffer;
  32. To use this is very simple, just create one, initialise it with the desired number
  33. of channels and sample rate then feed it with some samples and read them back out.
  34. This is not thread safe so make sure that the read and write methods are not called
  35. simultaneously by different threads.
  36. */
  37. class SoundTouchProcessor
  38. {
  39. public:
  40. //==============================================================================
  41. /** A struct use to hold all the different playback settings. */
  42. struct PlaybackSettings
  43. {
  44. PlaybackSettings()
  45. : rate (1.0f), tempo (1.0f), pitch (1.0f)
  46. {}
  47. PlaybackSettings (float rate_, float tempo_, float pitch_)
  48. : rate (rate_), tempo (tempo_), pitch (pitch_)
  49. {}
  50. float rate, tempo, pitch;
  51. };
  52. //==============================================================================
  53. /** Create a default SoundTouchProcessor.
  54. Make sure that you call initialise before any processing take place.
  55. This will apply no shifting/stretching by default, use setPlaybackSetting() to
  56. apply these effects.
  57. */
  58. SoundTouchProcessor();
  59. /** Destructor.
  60. */
  61. ~SoundTouchProcessor();
  62. /** Puts the processor into a ready state.
  63. This must be set before any processing occurs as the results are undefiend if not.
  64. It is the callers responsibility to make sure the numChannels parameter matches
  65. those supplied to the read/write methods.
  66. */
  67. void initialise (int numChannels, double sampleRate);
  68. /** Writes samples into the pipline ready to be processed.
  69. Remember to keep a 1:1 ratio of input and output samples more or less samples may
  70. be required as input compared to output (think of a time stretch). You can find
  71. this ratio using getNumSamplesRequiredRatio().
  72. */
  73. void writeSamples (float** sourceChannelData, int numChannels, int numSamples, int startSampleOffset = 0);
  74. /** Reads out processed samples.
  75. This will read out as many samples as the processor has ready. Any additional
  76. space in the buffer will be slienced. As the processor takes a certain ammount of
  77. samples to calculate an output there is a latency of around 100ms involved in the process.
  78. */
  79. void readSamples (float** destinationChannelData, int numChannels, int numSamples, int startSampleOffset = 0);
  80. /** Clears the pipeline of all samples, ready for new processing. */
  81. void clear() { soundTouch.clear(); }
  82. /** Flushes the last samples from the processing pipeline to the output.
  83. Clears also the internal processing buffers.
  84. Note: This function is meant for extracting the last samples of a sound
  85. stream. This function may introduce additional blank samples in the end
  86. of the sound stream, and thus it's not recommended to call this function
  87. in the middle of a sound stream.
  88. */
  89. void flush() { soundTouch.flush(); }
  90. /** Returns the number of samples ready. */
  91. int getNumReady() { return soundTouch.numSamples(); }
  92. /** Returns the number of samples in the pipeline but currently unprocessed. */
  93. int getNumUnprocessedSamples() { return soundTouch.numUnprocessedSamples(); }
  94. /** Sets all of the settings at once. */
  95. void setPlaybackSettings (PlaybackSettings newSettings);
  96. /** Returns all of the settings. */
  97. PlaybackSettings getPlaybackSettings() { return settings; }
  98. /** Sets a custom SoundTouch setting.
  99. See SoundTouch.h for details.
  100. */
  101. void setSoundTouchSetting (int settingId, int settingValue);
  102. /** Gets a custom SoundTouch setting.
  103. See SoundTouch.h for details.
  104. */
  105. int getSoundTouchSetting (int settingId);
  106. /** Returns the effective playback ratio i.e. the number of output samples produced per input sample. */
  107. double getEffectivePlaybackRatio() { return (double) soundTouch.getEffectiveRate() * soundTouch.getEffectiveTempo(); }
  108. private:
  109. //==============================================================================
  110. soundtouch::SoundTouch soundTouch;
  111. CriticalSection lock;
  112. HeapBlock<float> interleavedInputBuffer, interleavedOutputBuffer;
  113. int interleavedInputBufferSize, interleavedOutputBufferSize;
  114. PlaybackSettings settings;
  115. //==============================================================================
  116. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SoundTouchProcessor);
  117. };
  118. #endif
  119. #endif // __DROWAUDIO_SOUNDTOUCHPROCESSOR_H__