SoundTouch.cpp 16 KB

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