SoundTouch.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. // License :
  45. //
  46. // SoundTouch audio processing library
  47. // Copyright (c) Olli Parviainen
  48. //
  49. // This library is free software; you can redistribute it and/or
  50. // modify it under the terms of the GNU Lesser General Public
  51. // License as published by the Free Software Foundation; either
  52. // version 2.1 of the License, or (at your option) any later version.
  53. //
  54. // This library is distributed in the hope that it will be useful,
  55. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  56. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  57. // Lesser General Public License for more details.
  58. //
  59. // You should have received a copy of the GNU Lesser General Public
  60. // License along with this library; if not, write to the Free Software
  61. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  62. //
  63. ////////////////////////////////////////////////////////////////////////////////
  64. #ifndef SoundTouch_H
  65. #define SoundTouch_H
  66. #include "FIFOSamplePipe.h"
  67. #include "STTypes.h"
  68. namespace soundtouch
  69. {
  70. /// Soundtouch library version string
  71. #define SOUNDTOUCH_VERSION "2.3.2"
  72. /// SoundTouch library version id
  73. #define SOUNDTOUCH_VERSION_ID (20302)
  74. //
  75. // Available setting IDs for the 'setSetting' & 'get_setting' functions:
  76. /// Enable/disable anti-alias filter in pitch transposer (0 = disable)
  77. #define SETTING_USE_AA_FILTER 0
  78. /// Pitch transposer anti-alias filter length (8 .. 128 taps, default = 32)
  79. #define SETTING_AA_FILTER_LENGTH 1
  80. /// Enable/disable quick seeking algorithm in tempo changer routine
  81. /// (enabling quick seeking lowers CPU utilization but causes a minor sound
  82. /// quality compromising)
  83. #define SETTING_USE_QUICKSEEK 2
  84. /// Time-stretch algorithm single processing sequence length in milliseconds. This determines
  85. /// to how long sequences the original sound is chopped in the time-stretch algorithm.
  86. /// See "STTypes.h" or README for more information.
  87. #define SETTING_SEQUENCE_MS 3
  88. /// Time-stretch algorithm seeking window length in milliseconds for algorithm that finds the
  89. /// best possible overlapping location. This determines from how wide window the algorithm
  90. /// may look for an optimal joining location when mixing the sound sequences back together.
  91. /// See "STTypes.h" or README for more information.
  92. #define SETTING_SEEKWINDOW_MS 4
  93. /// Time-stretch algorithm overlap length in milliseconds. When the chopped sound sequences
  94. /// are mixed back together, to form a continuous sound stream, this parameter defines over
  95. /// how long period the two consecutive sequences are let to overlap each other.
  96. /// See "STTypes.h" or README for more information.
  97. #define SETTING_OVERLAP_MS 5
  98. /// Call "getSetting" with this ID to query processing sequence size in samples.
  99. /// This value gives approximate value of how many input samples you'll need to
  100. /// feed into SoundTouch after initial buffering to get out a new batch of
  101. /// output samples.
  102. ///
  103. /// This value does not include initial buffering at beginning of a new processing
  104. /// stream, use SETTING_INITIAL_LATENCY to get the initial buffering size.
  105. ///
  106. /// Notices:
  107. /// - This is read-only parameter, i.e. setSetting ignores this parameter
  108. /// - This parameter value is not constant but change depending on
  109. /// tempo/pitch/rate/samplerate settings.
  110. #define SETTING_NOMINAL_INPUT_SEQUENCE 6
  111. /// Call "getSetting" with this ID to query nominal average processing output
  112. /// size in samples. This value tells approcimate value how many output samples
  113. /// SoundTouch outputs once it does DSP processing run for a batch of input samples.
  114. ///
  115. /// Notices:
  116. /// - This is read-only parameter, i.e. setSetting ignores this parameter
  117. /// - This parameter value is not constant but change depending on
  118. /// tempo/pitch/rate/samplerate settings.
  119. #define SETTING_NOMINAL_OUTPUT_SEQUENCE 7
  120. /// Call "getSetting" with this ID to query initial processing latency, i.e.
  121. /// approx. how many samples you'll need to enter to SoundTouch pipeline before
  122. /// you can expect to get first batch of ready output samples out.
  123. ///
  124. /// After the first output batch, you can then expect to get approx.
  125. /// SETTING_NOMINAL_OUTPUT_SEQUENCE ready samples out for every
  126. /// SETTING_NOMINAL_INPUT_SEQUENCE samples that you enter into SoundTouch.
  127. ///
  128. /// Example:
  129. /// processing with parameter -tempo=5
  130. /// => initial latency = 5509 samples
  131. /// input sequence = 4167 samples
  132. /// output sequence = 3969 samples
  133. ///
  134. /// Accordingly, you can expect to feed in approx. 5509 samples at beginning of
  135. /// the stream, and then you'll get out the first 3969 samples. After that, for
  136. /// every approx. 4167 samples that you'll put in, you'll receive again approx.
  137. /// 3969 samples out.
  138. ///
  139. /// This also means that average latency during stream processing is
  140. /// INITIAL_LATENCY-OUTPUT_SEQUENCE/2, in the above example case 5509-3969/2
  141. /// = 3524 samples
  142. ///
  143. /// Notices:
  144. /// - This is read-only parameter, i.e. setSetting ignores this parameter
  145. /// - This parameter value is not constant but change depending on
  146. /// tempo/pitch/rate/samplerate settings.
  147. #define SETTING_INITIAL_LATENCY 8
  148. class SoundTouch : public FIFOProcessor
  149. {
  150. private:
  151. /// Rate transposer class instance
  152. class RateTransposer *pRateTransposer;
  153. /// Time-stretch class instance
  154. class TDStretch *pTDStretch;
  155. /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters.
  156. double virtualRate;
  157. /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters.
  158. double virtualTempo;
  159. /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters.
  160. double virtualPitch;
  161. /// Flag: Has sample rate been set?
  162. bool bSrateSet;
  163. /// Accumulator for how many samples in total will be expected as output vs. samples put in,
  164. /// considering current processing settings.
  165. double samplesExpectedOut;
  166. /// Accumulator for how many samples in total have been read out from the processing so far
  167. long samplesOutput;
  168. /// Calculates effective rate & tempo valuescfrom 'virtualRate', 'virtualTempo' and
  169. /// 'virtualPitch' parameters.
  170. void calcEffectiveRateAndTempo();
  171. protected :
  172. /// Number of channels
  173. uint channels;
  174. /// Effective 'rate' value calculated from 'virtualRate', 'virtualTempo' and 'virtualPitch'
  175. double rate;
  176. /// Effective 'tempo' value calculated from 'virtualRate', 'virtualTempo' and 'virtualPitch'
  177. double tempo;
  178. public:
  179. SoundTouch();
  180. virtual ~SoundTouch() override;
  181. /// Get SoundTouch library version string
  182. static const char *getVersionString();
  183. /// Get SoundTouch library version Id
  184. static uint getVersionId();
  185. /// Sets new rate control value. Normal rate = 1.0, smaller values
  186. /// represent slower rate, larger faster rates.
  187. void setRate(double newRate);
  188. /// Sets new tempo control value. Normal tempo = 1.0, smaller values
  189. /// represent slower tempo, larger faster tempo.
  190. void setTempo(double newTempo);
  191. /// Sets new rate control value as a difference in percents compared
  192. /// to the original rate (-50 .. +100 %)
  193. void setRateChange(double newRate);
  194. /// Sets new tempo control value as a difference in percents compared
  195. /// to the original tempo (-50 .. +100 %)
  196. void setTempoChange(double newTempo);
  197. /// Sets new pitch control value. Original pitch = 1.0, smaller values
  198. /// represent lower pitches, larger values higher pitch.
  199. void setPitch(double newPitch);
  200. /// Sets pitch change in octaves compared to the original pitch
  201. /// (-1.00 .. +1.00)
  202. void setPitchOctaves(double newPitch);
  203. /// Sets pitch change in semi-tones compared to the original pitch
  204. /// (-12 .. +12)
  205. void setPitchSemiTones(int newPitch);
  206. void setPitchSemiTones(double newPitch);
  207. /// Sets the number of channels, 1 = mono, 2 = stereo
  208. void setChannels(uint numChannels);
  209. /// Sets sample rate.
  210. void setSampleRate(uint srate);
  211. /// Get ratio between input and output audio durations, useful for calculating
  212. /// processed output duration: if you'll process a stream of N samples, then
  213. /// you can expect to get out N * getInputOutputSampleRatio() samples.
  214. ///
  215. /// This ratio will give accurate target duration ratio for a full audio track,
  216. /// given that the the whole track is processed with same processing parameters.
  217. ///
  218. /// If this ratio is applied to calculate intermediate offsets inside a processing
  219. /// stream, then this ratio is approximate and can deviate +- some tens of milliseconds
  220. /// from ideal offset, yet by end of the audio stream the duration ratio will become
  221. /// exact.
  222. ///
  223. /// Example: if processing with parameters "-tempo=15 -pitch=-3", the function
  224. /// will return value 0.8695652... Now, if processing an audio stream whose duration
  225. /// is exactly one million audio samples, then you can expect the processed
  226. /// output duration be 0.869565 * 1000000 = 869565 samples.
  227. double getInputOutputSampleRatio();
  228. /// Flushes the last samples from the processing pipeline to the output.
  229. /// Clears also the internal processing buffers.
  230. //
  231. /// Note: This function is meant for extracting the last samples of a sound
  232. /// stream. This function may introduce additional blank samples in the end
  233. /// of the sound stream, and thus it's not recommended to call this function
  234. /// in the middle of a sound stream.
  235. void flush();
  236. /// Adds 'numSamples' pcs of samples from the 'samples' memory position into
  237. /// the input of the object. Notice that sample rate _has_to_ be set before
  238. /// calling this function, otherwise throws a runtime_error exception.
  239. virtual void putSamples(
  240. const SAMPLETYPE *samples, ///< Pointer to sample buffer.
  241. uint numSamples ///< Number of samples in buffer. Notice
  242. ///< that in case of stereo-sound a single sample
  243. ///< contains data for both channels.
  244. ) override;
  245. /// Output samples from beginning of the sample buffer. Copies requested samples to
  246. /// output buffer and removes them from the sample buffer. If there are less than
  247. /// 'numsample' samples in the buffer, returns all that available.
  248. ///
  249. /// \return Number of samples returned.
  250. virtual uint receiveSamples(SAMPLETYPE *output, ///< Buffer where to copy output samples.
  251. uint maxSamples ///< How many samples to receive at max.
  252. ) override;
  253. /// Adjusts book-keeping so that given number of samples are removed from beginning of the
  254. /// sample buffer without copying them anywhere.
  255. ///
  256. /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly
  257. /// with 'ptrBegin' function.
  258. virtual uint receiveSamples(uint maxSamples ///< Remove this many samples from the beginning of pipe.
  259. ) override;
  260. /// Clears all the samples in the object's output and internal processing
  261. /// buffers.
  262. virtual void clear() override;
  263. /// Changes a setting controlling the processing system behaviour. See the
  264. /// 'SETTING_...' defines for available setting ID's.
  265. ///
  266. /// \return 'true' if the setting was successfully changed
  267. bool setSetting(int settingId, ///< Setting ID number. see SETTING_... defines.
  268. int value ///< New setting value.
  269. );
  270. /// Reads a setting controlling the processing system behaviour. See the
  271. /// 'SETTING_...' defines for available setting ID's.
  272. ///
  273. /// \return the setting value.
  274. int getSetting(int settingId ///< Setting ID number, see SETTING_... defines.
  275. ) const;
  276. /// Returns number of samples currently unprocessed.
  277. virtual uint numUnprocessedSamples() const;
  278. /// Return number of channels
  279. uint numChannels() const
  280. {
  281. return channels;
  282. }
  283. /// Other handy functions that are implemented in the ancestor classes (see
  284. /// classes 'FIFOProcessor' and 'FIFOSamplePipe')
  285. ///
  286. /// - receiveSamples() : Use this function to receive 'ready' processed samples from SoundTouch.
  287. /// - numSamples() : Get number of 'ready' samples that can be received with
  288. /// function 'receiveSamples()'
  289. /// - isEmpty() : Returns nonzero if there aren't any 'ready' samples.
  290. /// - clear() : Clears all samples from ready/processing buffers.
  291. };
  292. }
  293. #endif