SoundTouch.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //////////////////////////////////////////////////////////////////////////////
  2. ///
  3. /// SoundTouch - main class for tempo/pitch/rate adjusting routines.
  4. ///
  5. /// Notes:
  6. /// - Initialize the SoundTouch object instance by setting up the sound stream
  7. /// parameters with functions 'setSampleRate' and 'setChannels', then set
  8. /// desired tempo/pitch/rate settings with the corresponding functions.
  9. ///
  10. /// - The SoundTouch class behaves like a first-in-first-out pipeline: The
  11. /// samples that are to be processed are fed into one of the pipe by calling
  12. /// function 'putSamples', while the ready processed samples can be read
  13. /// from the other end of the pipeline with function 'receiveSamples'.
  14. ///
  15. /// - The SoundTouch processing classes require certain sized 'batches' of
  16. /// samples in order to process the sound. For this reason the classes buffer
  17. /// incoming samples until there are enough of samples available for
  18. /// processing, then they carry out the processing step and consequently
  19. /// make the processed samples available for outputting.
  20. ///
  21. /// - For the above reason, the processing routines introduce a certain
  22. /// 'latency' between the input and output, so that the samples input to
  23. /// SoundTouch may not be immediately available in the output, and neither
  24. /// the amount of outputtable samples may not immediately be in direct
  25. /// relationship with the amount of previously input samples.
  26. ///
  27. /// - The tempo/pitch/rate control parameters can be altered during processing.
  28. /// Please notice though that they aren't currently protected by semaphores,
  29. /// so in multi-thread application external semaphore protection may be
  30. /// required.
  31. ///
  32. /// - This class utilizes classes 'TDStretch' for tempo change (without modifying
  33. /// pitch) and 'RateTransposer' for changing the playback rate (that is, both
  34. /// tempo and pitch in the same ratio) of the sound. The third available control
  35. /// 'pitch' (change pitch but maintain tempo) is produced by a combination of
  36. /// combining the two other controls.
  37. ///
  38. /// Author : Copyright (c) Olli Parviainen
  39. /// Author e-mail : oparviai 'at' iki.fi
  40. /// SoundTouch WWW: http://www.surina.net/soundtouch
  41. ///
  42. ////////////////////////////////////////////////////////////////////////////////
  43. //
  44. // Last changed : $Date: 2012-12-28 21:32:59 +0200 (Fri, 28 Dec 2012) $
  45. // File revision : $Revision: 4 $
  46. //
  47. // $Id: SoundTouch.h 163 2012-12-28 19:32:59Z oparviai $
  48. //
  49. ////////////////////////////////////////////////////////////////////////////////
  50. //
  51. // License :
  52. //
  53. // SoundTouch audio processing library
  54. // Copyright (c) Olli Parviainen
  55. //
  56. // This library is free software; you can redistribute it and/or
  57. // modify it under the terms of the GNU Lesser General Public
  58. // License as published by the Free Software Foundation; either
  59. // version 2.1 of the License, or (at your option) any later version.
  60. //
  61. // This library is distributed in the hope that it will be useful,
  62. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  63. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  64. // Lesser General Public License for more details.
  65. //
  66. // You should have received a copy of the GNU Lesser General Public
  67. // License along with this library; if not, write to the Free Software
  68. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  69. //
  70. ////////////////////////////////////////////////////////////////////////////////
  71. #ifndef SoundTouch_H
  72. #define SoundTouch_H
  73. #include "FIFOSamplePipe.h"
  74. #include "STTypes.h"
  75. namespace soundtouch
  76. {
  77. /// Soundtouch library version string
  78. #define SOUNDTOUCH_VERSION "1.7.1"
  79. /// SoundTouch library version id
  80. #define SOUNDTOUCH_VERSION_ID (10701)
  81. //
  82. // Available setting IDs for the 'setSetting' & 'get_setting' functions:
  83. /// Enable/disable anti-alias filter in pitch transposer (0 = disable)
  84. #define SETTING_USE_AA_FILTER 0
  85. /// Pitch transposer anti-alias filter length (8 .. 128 taps, default = 32)
  86. #define SETTING_AA_FILTER_LENGTH 1
  87. /// Enable/disable quick seeking algorithm in tempo changer routine
  88. /// (enabling quick seeking lowers CPU utilization but causes a minor sound
  89. /// quality compromising)
  90. #define SETTING_USE_QUICKSEEK 2
  91. /// Time-stretch algorithm single processing sequence length in milliseconds. This determines
  92. /// to how long sequences the original sound is chopped in the time-stretch algorithm.
  93. /// See "STTypes.h" or README for more information.
  94. #define SETTING_SEQUENCE_MS 3
  95. /// Time-stretch algorithm seeking window length in milliseconds for algorithm that finds the
  96. /// best possible overlapping location. This determines from how wide window the algorithm
  97. /// may look for an optimal joining location when mixing the sound sequences back together.
  98. /// See "STTypes.h" or README for more information.
  99. #define SETTING_SEEKWINDOW_MS 4
  100. /// Time-stretch algorithm overlap length in milliseconds. When the chopped sound sequences
  101. /// are mixed back together, to form a continuous sound stream, this parameter defines over
  102. /// how long period the two consecutive sequences are let to overlap each other.
  103. /// See "STTypes.h" or README for more information.
  104. #define SETTING_OVERLAP_MS 5
  105. /// Call "getSetting" with this ID to query nominal average processing sequence
  106. /// size in samples. This value tells approcimate value how many input samples
  107. /// SoundTouch needs to gather before it does DSP processing run for the sample batch.
  108. ///
  109. /// Notices:
  110. /// - This is read-only parameter, i.e. setSetting ignores this parameter
  111. /// - Returned value is approximate average value, exact processing batch
  112. /// size may wary from time to time
  113. /// - This parameter value is not constant but may change depending on
  114. /// tempo/pitch/rate/samplerate settings.
  115. #define SETTING_NOMINAL_INPUT_SEQUENCE 6
  116. /// Call "getSetting" with this ID to query nominal average processing output
  117. /// size in samples. This value tells approcimate value how many output samples
  118. /// SoundTouch outputs once it does DSP processing run for a batch of input samples.
  119. ///
  120. /// Notices:
  121. /// - This is read-only parameter, i.e. setSetting ignores this parameter
  122. /// - Returned value is approximate average value, exact processing batch
  123. /// size may wary from time to time
  124. /// - This parameter value is not constant but may change depending on
  125. /// tempo/pitch/rate/samplerate settings.
  126. #define SETTING_NOMINAL_OUTPUT_SEQUENCE 7
  127. class SoundTouch : public FIFOProcessor
  128. {
  129. private:
  130. /// Rate transposer class instance
  131. class RateTransposer *pRateTransposer;
  132. /// Time-stretch class instance
  133. class TDStretch *pTDStretch;
  134. /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters.
  135. float virtualRate;
  136. /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters.
  137. float virtualTempo;
  138. /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters.
  139. float virtualPitch;
  140. /// Flag: Has sample rate been set?
  141. SBOOL bSrateSet;
  142. /// Calculates effective rate & tempo valuescfrom 'virtualRate', 'virtualTempo' and
  143. /// 'virtualPitch' parameters.
  144. void calcEffectiveRateAndTempo();
  145. protected :
  146. /// Number of channels
  147. uint channels;
  148. /// Effective 'rate' value calculated from 'virtualRate', 'virtualTempo' and 'virtualPitch'
  149. float rate;
  150. /// Effective 'tempo' value calculated from 'virtualRate', 'virtualTempo' and 'virtualPitch'
  151. float tempo;
  152. public:
  153. SoundTouch();
  154. virtual ~SoundTouch();
  155. /// Get SoundTouch library version string
  156. static const char *getVersionString();
  157. /// Get SoundTouch library version Id
  158. static uint getVersionId();
  159. /// Sets new rate control value. Normal rate = 1.0, smaller values
  160. /// represent slower rate, larger faster rates.
  161. void setRate(float newRate);
  162. /// Sets new tempo control value. Normal tempo = 1.0, smaller values
  163. /// represent slower tempo, larger faster tempo.
  164. void setTempo(float newTempo);
  165. /// Sets new rate control value as a difference in percents compared
  166. /// to the original rate (-50 .. +100 %)
  167. void setRateChange(float newRate);
  168. /// Sets new tempo control value as a difference in percents compared
  169. /// to the original tempo (-50 .. +100 %)
  170. void setTempoChange(float newTempo);
  171. /// Sets new pitch control value. Original pitch = 1.0, smaller values
  172. /// represent lower pitches, larger values higher pitch.
  173. void setPitch(float newPitch);
  174. /// Sets pitch change in octaves compared to the original pitch
  175. /// (-1.00 .. +1.00)
  176. void setPitchOctaves(float newPitch);
  177. /// Sets pitch change in semi-tones compared to the original pitch
  178. /// (-12 .. +12)
  179. void setPitchSemiTones(int newPitch);
  180. void setPitchSemiTones(float newPitch);
  181. /// Sets the number of channels, 1 = mono, 2 = stereo
  182. void setChannels(uint numChannels);
  183. /// Sets sample rate.
  184. void setSampleRate(uint srate);
  185. /// Flushes the last samples from the processing pipeline to the output.
  186. /// Clears also the internal processing buffers.
  187. //
  188. /// Note: This function is meant for extracting the last samples of a sound
  189. /// stream. This function may introduce additional blank samples in the end
  190. /// of the sound stream, and thus it's not recommended to call this function
  191. /// in the middle of a sound stream.
  192. void flush();
  193. /// Adds 'numSamples' pcs of samples from the 'samples' memory position into
  194. /// the input of the object. Notice that sample rate _has_to_ be set before
  195. /// calling this function, otherwise throws a runtime_error exception.
  196. virtual void putSamples(
  197. const SAMPLETYPE *samples, ///< Pointer to sample buffer.
  198. uint numSamples ///< Number of samples in buffer. Notice
  199. ///< that in case of stereo-sound a single sample
  200. ///< contains data for both channels.
  201. );
  202. /// Clears all the samples in the object's output and internal processing
  203. /// buffers.
  204. virtual void clear();
  205. /// Changes a setting controlling the processing system behaviour. See the
  206. /// 'SETTING_...' defines for available setting ID's.
  207. ///
  208. /// \return 'TRUE' if the setting was succesfully changed
  209. SBOOL setSetting(int settingId, ///< Setting ID number. see SETTING_... defines.
  210. int value ///< New setting value.
  211. );
  212. /// Reads a setting controlling the processing system behaviour. See the
  213. /// 'SETTING_...' defines for available setting ID's.
  214. ///
  215. /// \return the setting value.
  216. int getSetting(int settingId ///< Setting ID number, see SETTING_... defines.
  217. ) const;
  218. /// Returns number of samples currently unprocessed.
  219. virtual uint numUnprocessedSamples() const;
  220. /// Other handy functions that are implemented in the ancestor classes (see
  221. /// classes 'FIFOProcessor' and 'FIFOSamplePipe')
  222. ///
  223. /// - receiveSamples() : Use this function to receive 'ready' processed samples from SoundTouch.
  224. /// - numSamples() : Get number of 'ready' samples that can be received with
  225. /// function 'receiveSamples()'
  226. /// - isEmpty() : Returns nonzero if there aren't any 'ready' samples.
  227. /// - clear() : Clears all samples from ready/processing buffers.
  228. };
  229. }
  230. #endif