/external/pysoundtouch14/libsoundtouch/SoundTouch.h

http://echo-nest-remix.googlecode.com/ · C Header · 252 lines · 58 code · 44 blank · 150 comment · 0 complexity · a416f9ef96abd9d067492775ecd349c0 MD5 · raw file

  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: 2008-12-25 19:03:48 +0200 (Thu, 25 Dec 2008) $
  45. // File revision : $Revision: 4 $
  46. //
  47. // $Id: SoundTouch.h 40 2008-12-25 17:03:48Z 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.4.0"
  79. /// SoundTouch library version id
  80. #define SOUNDTOUCH_VERSION_ID (10400)
  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. class SoundTouch : public FIFOProcessor
  106. {
  107. private:
  108. /// Rate transposer class instance
  109. class RateTransposer *pRateTransposer;
  110. /// Time-stretch class instance
  111. class TDStretch *pTDStretch;
  112. /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters.
  113. float virtualRate;
  114. /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters.
  115. float virtualTempo;
  116. /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters.
  117. float virtualPitch;
  118. /// Flag: Has sample rate been set?
  119. BOOL bSrateSet;
  120. /// Calculates effective rate & tempo valuescfrom 'virtualRate', 'virtualTempo' and
  121. /// 'virtualPitch' parameters.
  122. void calcEffectiveRateAndTempo();
  123. protected :
  124. /// Number of channels
  125. uint channels;
  126. /// Effective 'rate' value calculated from 'virtualRate', 'virtualTempo' and 'virtualPitch'
  127. float rate;
  128. /// Effective 'tempo' value calculated from 'virtualRate', 'virtualTempo' and 'virtualPitch'
  129. float tempo;
  130. public:
  131. SoundTouch();
  132. virtual ~SoundTouch();
  133. /// Get SoundTouch library version string
  134. static const char *getVersionString();
  135. /// Get SoundTouch library version Id
  136. static uint getVersionId();
  137. /// Sets new rate control value. Normal rate = 1.0, smaller values
  138. /// represent slower rate, larger faster rates.
  139. void setRate(float newRate);
  140. /// Sets new tempo control value. Normal tempo = 1.0, smaller values
  141. /// represent slower tempo, larger faster tempo.
  142. void setTempo(float newTempo);
  143. /// Sets new rate control value as a difference in percents compared
  144. /// to the original rate (-50 .. +100 %)
  145. void setRateChange(float newRate);
  146. /// Sets new tempo control value as a difference in percents compared
  147. /// to the original tempo (-50 .. +100 %)
  148. void setTempoChange(float newTempo);
  149. /// Sets new pitch control value. Original pitch = 1.0, smaller values
  150. /// represent lower pitches, larger values higher pitch.
  151. void setPitch(float newPitch);
  152. /// Sets pitch change in octaves compared to the original pitch
  153. /// (-1.00 .. +1.00)
  154. void setPitchOctaves(float newPitch);
  155. /// Sets pitch change in semi-tones compared to the original pitch
  156. /// (-12 .. +12)
  157. void setPitchSemiTones(int newPitch);
  158. void setPitchSemiTones(float newPitch);
  159. /// Sets the number of channels, 1 = mono, 2 = stereo
  160. void setChannels(uint numChannels);
  161. /// Sets sample rate.
  162. void setSampleRate(uint srate);
  163. /// Flushes the last samples from the processing pipeline to the output.
  164. /// Clears also the internal processing buffers.
  165. //
  166. /// Note: This function is meant for extracting the last samples of a sound
  167. /// stream. This function may introduce additional blank samples in the end
  168. /// of the sound stream, and thus it's not recommended to call this function
  169. /// in the middle of a sound stream.
  170. void flush();
  171. /// Adds 'numSamples' pcs of samples from the 'samples' memory position into
  172. /// the input of the object. Notice that sample rate _has_to_ be set before
  173. /// calling this function, otherwise throws a runtime_error exception.
  174. virtual void putSamples(
  175. const SAMPLETYPE *samples, ///< Pointer to sample buffer.
  176. uint numSamples ///< Number of samples in buffer. Notice
  177. ///< that in case of stereo-sound a single sample
  178. ///< contains data for both channels.
  179. );
  180. /// Clears all the samples in the object's output and internal processing
  181. /// buffers.
  182. virtual void clear();
  183. /// Changes a setting controlling the processing system behaviour. See the
  184. /// 'SETTING_...' defines for available setting ID's.
  185. ///
  186. /// \return 'TRUE' if the setting was succesfully changed
  187. BOOL setSetting(uint settingId, ///< Setting ID number. see SETTING_... defines.
  188. uint value ///< New setting value.
  189. );
  190. /// Reads a setting controlling the processing system behaviour. See the
  191. /// 'SETTING_...' defines for available setting ID's.
  192. ///
  193. /// \return the setting value.
  194. int getSetting(int settingId ///< Setting ID number, see SETTING_... defines.
  195. ) const;
  196. /// Returns number of samples currently unprocessed.
  197. virtual uint numUnprocessedSamples() const;
  198. /// Other handy functions that are implemented in the ancestor classes (see
  199. /// classes 'FIFOProcessor' and 'FIFOSamplePipe')
  200. ///
  201. /// - receiveSamples() : Use this function to receive 'ready' processed samples from SoundTouch.
  202. /// - numSamples() : Get number of 'ready' samples that can be received with
  203. /// function 'receiveSamples()'
  204. /// - isEmpty() : Returns nonzero if there aren't any 'ready' samples.
  205. /// - clear() : Clears all samples from ready/processing buffers.
  206. };
  207. }
  208. #endif