SoundTouch.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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: 2013-06-12 15:24:44 +0000 (Wed, 12 Jun 2013) $
  45. // File revision : $Revision: 4 $
  46. //
  47. // $Id: SoundTouch.cpp 171 2013-06-12 15:24:44Z 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. #include <assert.h>
  72. #include <stdlib.h>
  73. #include <memory.h>
  74. #include <math.h>
  75. #include <stdio.h>
  76. #include "SoundTouch.h"
  77. #include "TDStretch.h"
  78. #include "RateTransposer.h"
  79. #include "cpu_detect.h"
  80. using namespace soundtouch;
  81. /// test if two floating point numbers are equal
  82. #define TEST_FLOAT_EQUAL(a, b) (fabs(a - b) < 1e-10)
  83. /// Print library version string for autoconf
  84. extern "C" void soundtouch_ac_test()
  85. {
  86. printf("SoundTouch Version: %s\n",SOUNDTOUCH_VERSION);
  87. }
  88. SoundTouch::SoundTouch()
  89. {
  90. // Initialize rate transposer and tempo changer instances
  91. pRateTransposer = RateTransposer::newInstance();
  92. pTDStretch = TDStretch::newInstance();
  93. setOutPipe(pTDStretch);
  94. rate = tempo = 0;
  95. virtualPitch =
  96. virtualRate =
  97. virtualTempo = 1.0;
  98. calcEffectiveRateAndTempo();
  99. channels = 0;
  100. bSrateSet = FALSE;
  101. }
  102. SoundTouch::~SoundTouch()
  103. {
  104. delete pRateTransposer;
  105. delete pTDStretch;
  106. }
  107. /// Get SoundTouch library version string
  108. const char *SoundTouch::getVersionString()
  109. {
  110. static const char *_version = SOUNDTOUCH_VERSION;
  111. return _version;
  112. }
  113. /// Get SoundTouch library version Id
  114. uint SoundTouch::getVersionId()
  115. {
  116. return SOUNDTOUCH_VERSION_ID;
  117. }
  118. // Sets the number of channels, 1 = mono, 2 = stereo
  119. void SoundTouch::setChannels(uint numChannels)
  120. {
  121. /*if (numChannels != 1 && numChannels != 2)
  122. {
  123. //ST_THROW_RT_ERROR("Illegal number of channels");
  124. return;
  125. }*/
  126. channels = numChannels;
  127. pRateTransposer->setChannels((int)numChannels);
  128. pTDStretch->setChannels((int)numChannels);
  129. }
  130. // Sets new rate control value. Normal rate = 1.0, smaller values
  131. // represent slower rate, larger faster rates.
  132. void SoundTouch::setRate(float newRate)
  133. {
  134. virtualRate = newRate;
  135. calcEffectiveRateAndTempo();
  136. }
  137. // Sets new rate control value as a difference in percents compared
  138. // to the original rate (-50 .. +100 %)
  139. void SoundTouch::setRateChange(float newRate)
  140. {
  141. virtualRate = 1.0f + 0.01f * newRate;
  142. calcEffectiveRateAndTempo();
  143. }
  144. // Sets new tempo control value. Normal tempo = 1.0, smaller values
  145. // represent slower tempo, larger faster tempo.
  146. void SoundTouch::setTempo(float newTempo)
  147. {
  148. virtualTempo = newTempo;
  149. calcEffectiveRateAndTempo();
  150. }
  151. // Sets new tempo control value as a difference in percents compared
  152. // to the original tempo (-50 .. +100 %)
  153. void SoundTouch::setTempoChange(float newTempo)
  154. {
  155. virtualTempo = 1.0f + 0.01f * newTempo;
  156. calcEffectiveRateAndTempo();
  157. }
  158. // Sets new pitch control value. Original pitch = 1.0, smaller values
  159. // represent lower pitches, larger values higher pitch.
  160. void SoundTouch::setPitch(float newPitch)
  161. {
  162. virtualPitch = newPitch;
  163. calcEffectiveRateAndTempo();
  164. }
  165. // Sets pitch change in octaves compared to the original pitch
  166. // (-1.00 .. +1.00)
  167. void SoundTouch::setPitchOctaves(float newPitch)
  168. {
  169. virtualPitch = (float)exp(0.69314718056f * newPitch);
  170. calcEffectiveRateAndTempo();
  171. }
  172. // Sets pitch change in semi-tones compared to the original pitch
  173. // (-12 .. +12)
  174. void SoundTouch::setPitchSemiTones(int newPitch)
  175. {
  176. setPitchOctaves((float)newPitch / 12.0f);
  177. }
  178. void SoundTouch::setPitchSemiTones(float newPitch)
  179. {
  180. setPitchOctaves(newPitch / 12.0f);
  181. }
  182. // Calculates 'effective' rate and tempo values from the
  183. // nominal control values.
  184. void SoundTouch::calcEffectiveRateAndTempo()
  185. {
  186. float oldTempo = tempo;
  187. float oldRate = rate;
  188. tempo = virtualTempo / virtualPitch;
  189. rate = virtualPitch * virtualRate;
  190. if (!TEST_FLOAT_EQUAL(rate,oldRate)) pRateTransposer->setRate(rate);
  191. if (!TEST_FLOAT_EQUAL(tempo, oldTempo)) pTDStretch->setTempo(tempo);
  192. #ifndef SOUNDTOUCH_PREVENT_CLICK_AT_RATE_CROSSOVER
  193. if (rate <= 1.0f)
  194. {
  195. if (output != pTDStretch)
  196. {
  197. FIFOSamplePipe *tempoOut;
  198. assert(output == pRateTransposer);
  199. // move samples in the current output buffer to the output of pTDStretch
  200. tempoOut = pTDStretch->getOutput();
  201. tempoOut->moveSamples(*output);
  202. // move samples in pitch transposer's store buffer to tempo changer's input
  203. pTDStretch->moveSamples(*pRateTransposer->getStore());
  204. output = pTDStretch;
  205. }
  206. }
  207. else
  208. #endif
  209. {
  210. if (output != pRateTransposer)
  211. {
  212. FIFOSamplePipe *transOut;
  213. assert(output == pTDStretch);
  214. // move samples in the current output buffer to the output of pRateTransposer
  215. transOut = pRateTransposer->getOutput();
  216. transOut->moveSamples(*output);
  217. // move samples in tempo changer's input to pitch transposer's input
  218. pRateTransposer->moveSamples(*pTDStretch->getInput());
  219. output = pRateTransposer;
  220. }
  221. }
  222. }
  223. // Sets sample rate.
  224. void SoundTouch::setSampleRate(uint srate)
  225. {
  226. bSrateSet = TRUE;
  227. // set sample rate, leave other tempo changer parameters as they are.
  228. pTDStretch->setParameters((int)srate);
  229. }
  230. // Adds 'numSamples' pcs of samples from the 'samples' memory position into
  231. // the input of the object.
  232. void SoundTouch::putSamples(const SAMPLETYPE *samples, uint nSamples)
  233. {
  234. if (bSrateSet == FALSE)
  235. {
  236. ST_THROW_RT_ERROR("SoundTouch : Sample rate not defined");
  237. }
  238. else if (channels == 0)
  239. {
  240. ST_THROW_RT_ERROR("SoundTouch : Number of channels not defined");
  241. }
  242. // Transpose the rate of the new samples if necessary
  243. /* Bypass the nominal setting - can introduce a click in sound when tempo/pitch control crosses the nominal value...
  244. if (rate == 1.0f)
  245. {
  246. // The rate value is same as the original, simply evaluate the tempo changer.
  247. assert(output == pTDStretch);
  248. if (pRateTransposer->isEmpty() == 0)
  249. {
  250. // yet flush the last samples in the pitch transposer buffer
  251. // (may happen if 'rate' changes from a non-zero value to zero)
  252. pTDStretch->moveSamples(*pRateTransposer);
  253. }
  254. pTDStretch->putSamples(samples, nSamples);
  255. }
  256. */
  257. #ifndef SOUNDTOUCH_PREVENT_CLICK_AT_RATE_CROSSOVER
  258. else if (rate <= 1.0f)
  259. {
  260. // transpose the rate down, output the transposed sound to tempo changer buffer
  261. assert(output == pTDStretch);
  262. pRateTransposer->putSamples(samples, nSamples);
  263. pTDStretch->moveSamples(*pRateTransposer);
  264. }
  265. else
  266. #endif
  267. {
  268. // evaluate the tempo changer, then transpose the rate up,
  269. assert(output == pRateTransposer);
  270. pTDStretch->putSamples(samples, nSamples);
  271. pRateTransposer->moveSamples(*pTDStretch);
  272. }
  273. }
  274. // Flushes the last samples from the processing pipeline to the output.
  275. // Clears also the internal processing buffers.
  276. //
  277. // Note: This function is meant for extracting the last samples of a sound
  278. // stream. This function may introduce additional blank samples in the end
  279. // of the sound stream, and thus it's not recommended to call this function
  280. // in the middle of a sound stream.
  281. void SoundTouch::flush()
  282. {
  283. int i;
  284. int nUnprocessed;
  285. int nOut;
  286. SAMPLETYPE *buff=(SAMPLETYPE*)malloc(64*channels*sizeof(SAMPLETYPE));
  287. // check how many samples still await processing, and scale
  288. // that by tempo & rate to get expected output sample count
  289. nUnprocessed = numUnprocessedSamples();
  290. nUnprocessed = (int)((double)nUnprocessed / (tempo * rate) + 0.5);
  291. nOut = numSamples(); // ready samples currently in buffer ...
  292. nOut += nUnprocessed; // ... and how many we expect there to be in the end
  293. memset(buff, 0, 64 * channels * sizeof(SAMPLETYPE));
  294. // "Push" the last active samples out from the processing pipeline by
  295. // feeding blank samples into the processing pipeline until new,
  296. // processed samples appear in the output (not however, more than
  297. // 8ksamples in any case)
  298. for (i = 0; i < 128; i ++)
  299. {
  300. putSamples(buff, 64);
  301. if ((int)numSamples() >= nOut)
  302. {
  303. // Enough new samples have appeared into the output!
  304. // As samples come from processing with bigger chunks, now truncate it
  305. // back to maximum "nOut" samples to improve duration accuracy
  306. adjustAmountOfSamples(nOut);
  307. // finish
  308. break;
  309. }
  310. }
  311. // Clear working buffers
  312. pRateTransposer->clear();
  313. pTDStretch->clearInput();
  314. // yet leave the 'tempoChanger' output intouched as that's where the
  315. // flushed samples are!
  316. free(buff);
  317. }
  318. // Changes a setting controlling the processing system behaviour. See the
  319. // 'SETTING_...' defines for available setting ID's.
  320. BOOL SoundTouch::setSetting(int settingId, int value)
  321. {
  322. int sampleRate, sequenceMs, seekWindowMs, overlapMs;
  323. // read current tdstretch routine parameters
  324. pTDStretch->getParameters(&sampleRate, &sequenceMs, &seekWindowMs, &overlapMs);
  325. switch (settingId)
  326. {
  327. case SETTING_USE_AA_FILTER :
  328. // enables / disabless anti-alias filter
  329. pRateTransposer->enableAAFilter((value != 0) ? TRUE : FALSE);
  330. return TRUE;
  331. case SETTING_AA_FILTER_LENGTH :
  332. // sets anti-alias filter length
  333. pRateTransposer->getAAFilter()->setLength(value);
  334. return TRUE;
  335. case SETTING_USE_QUICKSEEK :
  336. // enables / disables tempo routine quick seeking algorithm
  337. pTDStretch->enableQuickSeek((value != 0) ? TRUE : FALSE);
  338. return TRUE;
  339. case SETTING_SEQUENCE_MS:
  340. // change time-stretch sequence duration parameter
  341. pTDStretch->setParameters(sampleRate, value, seekWindowMs, overlapMs);
  342. return TRUE;
  343. case SETTING_SEEKWINDOW_MS:
  344. // change time-stretch seek window length parameter
  345. pTDStretch->setParameters(sampleRate, sequenceMs, value, overlapMs);
  346. return TRUE;
  347. case SETTING_OVERLAP_MS:
  348. // change time-stretch overlap length parameter
  349. pTDStretch->setParameters(sampleRate, sequenceMs, seekWindowMs, value);
  350. return TRUE;
  351. default :
  352. return FALSE;
  353. }
  354. }
  355. // Reads a setting controlling the processing system behaviour. See the
  356. // 'SETTING_...' defines for available setting ID's.
  357. //
  358. // Returns the setting value.
  359. int SoundTouch::getSetting(int settingId) const
  360. {
  361. int temp;
  362. switch (settingId)
  363. {
  364. case SETTING_USE_AA_FILTER :
  365. return (uint)pRateTransposer->isAAFilterEnabled();
  366. case SETTING_AA_FILTER_LENGTH :
  367. return pRateTransposer->getAAFilter()->getLength();
  368. case SETTING_USE_QUICKSEEK :
  369. return (uint) pTDStretch->isQuickSeekEnabled();
  370. case SETTING_SEQUENCE_MS:
  371. pTDStretch->getParameters(NULL, &temp, NULL, NULL);
  372. return temp;
  373. case SETTING_SEEKWINDOW_MS:
  374. pTDStretch->getParameters(NULL, NULL, &temp, NULL);
  375. return temp;
  376. case SETTING_OVERLAP_MS:
  377. pTDStretch->getParameters(NULL, NULL, NULL, &temp);
  378. return temp;
  379. case SETTING_NOMINAL_INPUT_SEQUENCE :
  380. return pTDStretch->getInputSampleReq();
  381. case SETTING_NOMINAL_OUTPUT_SEQUENCE :
  382. return pTDStretch->getOutputBatchSize();
  383. default :
  384. return 0;
  385. }
  386. }
  387. // Clears all the samples in the object's output and internal processing
  388. // buffers.
  389. void SoundTouch::clear()
  390. {
  391. pRateTransposer->clear();
  392. pTDStretch->clear();
  393. }
  394. /// Returns number of samples currently unprocessed.
  395. uint SoundTouch::numUnprocessedSamples() const
  396. {
  397. FIFOSamplePipe * psp;
  398. if (pTDStretch)
  399. {
  400. psp = pTDStretch->getInput();
  401. if (psp)
  402. {
  403. return psp->numSamples();
  404. }
  405. }
  406. return 0;
  407. }