media/libsoundtouch/src/SoundTouch.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 //////////////////////////////////////////////////////////////////////////////
michael@0 2 ///
michael@0 3 /// SoundTouch - main class for tempo/pitch/rate adjusting routines.
michael@0 4 ///
michael@0 5 /// Notes:
michael@0 6 /// - Initialize the SoundTouch object instance by setting up the sound stream
michael@0 7 /// parameters with functions 'setSampleRate' and 'setChannels', then set
michael@0 8 /// desired tempo/pitch/rate settings with the corresponding functions.
michael@0 9 ///
michael@0 10 /// - The SoundTouch class behaves like a first-in-first-out pipeline: The
michael@0 11 /// samples that are to be processed are fed into one of the pipe by calling
michael@0 12 /// function 'putSamples', while the ready processed samples can be read
michael@0 13 /// from the other end of the pipeline with function 'receiveSamples'.
michael@0 14 ///
michael@0 15 /// - The SoundTouch processing classes require certain sized 'batches' of
michael@0 16 /// samples in order to process the sound. For this reason the classes buffer
michael@0 17 /// incoming samples until there are enough of samples available for
michael@0 18 /// processing, then they carry out the processing step and consequently
michael@0 19 /// make the processed samples available for outputting.
michael@0 20 ///
michael@0 21 /// - For the above reason, the processing routines introduce a certain
michael@0 22 /// 'latency' between the input and output, so that the samples input to
michael@0 23 /// SoundTouch may not be immediately available in the output, and neither
michael@0 24 /// the amount of outputtable samples may not immediately be in direct
michael@0 25 /// relationship with the amount of previously input samples.
michael@0 26 ///
michael@0 27 /// - The tempo/pitch/rate control parameters can be altered during processing.
michael@0 28 /// Please notice though that they aren't currently protected by semaphores,
michael@0 29 /// so in multi-thread application external semaphore protection may be
michael@0 30 /// required.
michael@0 31 ///
michael@0 32 /// - This class utilizes classes 'TDStretch' for tempo change (without modifying
michael@0 33 /// pitch) and 'RateTransposer' for changing the playback rate (that is, both
michael@0 34 /// tempo and pitch in the same ratio) of the sound. The third available control
michael@0 35 /// 'pitch' (change pitch but maintain tempo) is produced by a combination of
michael@0 36 /// combining the two other controls.
michael@0 37 ///
michael@0 38 /// Author : Copyright (c) Olli Parviainen
michael@0 39 /// Author e-mail : oparviai 'at' iki.fi
michael@0 40 /// SoundTouch WWW: http://www.surina.net/soundtouch
michael@0 41 ///
michael@0 42 ////////////////////////////////////////////////////////////////////////////////
michael@0 43 //
michael@0 44 // Last changed : $Date: 2014-04-06 10:57:21 -0500 (Sun, 06 Apr 2014) $
michael@0 45 // File revision : $Revision: 4 $
michael@0 46 //
michael@0 47 // $Id: SoundTouch.cpp 195 2014-04-06 15:57:21Z oparviai $
michael@0 48 //
michael@0 49 ////////////////////////////////////////////////////////////////////////////////
michael@0 50 //
michael@0 51 // License :
michael@0 52 //
michael@0 53 // SoundTouch audio processing library
michael@0 54 // Copyright (c) Olli Parviainen
michael@0 55 //
michael@0 56 // This library is free software; you can redistribute it and/or
michael@0 57 // modify it under the terms of the GNU Lesser General Public
michael@0 58 // License as published by the Free Software Foundation; either
michael@0 59 // version 2.1 of the License, or (at your option) any later version.
michael@0 60 //
michael@0 61 // This library is distributed in the hope that it will be useful,
michael@0 62 // but WITHOUT ANY WARRANTY; without even the implied warranty of
michael@0 63 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
michael@0 64 // Lesser General Public License for more details.
michael@0 65 //
michael@0 66 // You should have received a copy of the GNU Lesser General Public
michael@0 67 // License along with this library; if not, write to the Free Software
michael@0 68 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
michael@0 69 //
michael@0 70 ////////////////////////////////////////////////////////////////////////////////
michael@0 71
michael@0 72 #include <assert.h>
michael@0 73 #include <stdlib.h>
michael@0 74 #include <memory.h>
michael@0 75 #include <math.h>
michael@0 76 #include <stdio.h>
michael@0 77
michael@0 78 #include "SoundTouch.h"
michael@0 79 #include "TDStretch.h"
michael@0 80 #include "RateTransposer.h"
michael@0 81 #include "cpu_detect.h"
michael@0 82
michael@0 83 #ifdef _MSC_VER
michael@0 84 #include <malloc.h>
michael@0 85 #define alloca _alloca
michael@0 86 #endif
michael@0 87
michael@0 88 using namespace soundtouch;
michael@0 89
michael@0 90 /// test if two floating point numbers are equal
michael@0 91 #define TEST_FLOAT_EQUAL(a, b) (fabs(a - b) < 1e-10)
michael@0 92
michael@0 93
michael@0 94 /// Print library version string for autoconf
michael@0 95 extern "C" void soundtouch_ac_test()
michael@0 96 {
michael@0 97 printf("SoundTouch Version: %s\n",SOUNDTOUCH_VERSION);
michael@0 98 }
michael@0 99
michael@0 100
michael@0 101 SoundTouch::SoundTouch()
michael@0 102 {
michael@0 103 // Initialize rate transposer and tempo changer instances
michael@0 104
michael@0 105 pRateTransposer = new RateTransposer();
michael@0 106 pTDStretch = TDStretch::newInstance();
michael@0 107
michael@0 108 setOutPipe(pTDStretch);
michael@0 109
michael@0 110 rate = tempo = 0;
michael@0 111
michael@0 112 virtualPitch =
michael@0 113 virtualRate =
michael@0 114 virtualTempo = 1.0;
michael@0 115
michael@0 116 calcEffectiveRateAndTempo();
michael@0 117
michael@0 118 channels = 0;
michael@0 119 bSrateSet = false;
michael@0 120 }
michael@0 121
michael@0 122
michael@0 123
michael@0 124 SoundTouch::~SoundTouch()
michael@0 125 {
michael@0 126 delete pRateTransposer;
michael@0 127 delete pTDStretch;
michael@0 128 }
michael@0 129
michael@0 130
michael@0 131
michael@0 132 /// Get SoundTouch library version string
michael@0 133 const char *SoundTouch::getVersionString()
michael@0 134 {
michael@0 135 static const char *_version = SOUNDTOUCH_VERSION;
michael@0 136
michael@0 137 return _version;
michael@0 138 }
michael@0 139
michael@0 140
michael@0 141 /// Get SoundTouch library version Id
michael@0 142 uint SoundTouch::getVersionId()
michael@0 143 {
michael@0 144 return SOUNDTOUCH_VERSION_ID;
michael@0 145 }
michael@0 146
michael@0 147
michael@0 148 // Sets the number of channels, 1 = mono, 2 = stereo
michael@0 149 void SoundTouch::setChannels(uint numChannels)
michael@0 150 {
michael@0 151 /*if (numChannels != 1 && numChannels != 2)
michael@0 152 {
michael@0 153 //ST_THROW_RT_ERROR("Illegal number of channels");
michael@0 154 return;
michael@0 155 }*/
michael@0 156 channels = numChannels;
michael@0 157 pRateTransposer->setChannels((int)numChannels);
michael@0 158 pTDStretch->setChannels((int)numChannels);
michael@0 159 }
michael@0 160
michael@0 161
michael@0 162
michael@0 163 // Sets new rate control value. Normal rate = 1.0, smaller values
michael@0 164 // represent slower rate, larger faster rates.
michael@0 165 void SoundTouch::setRate(float newRate)
michael@0 166 {
michael@0 167 virtualRate = newRate;
michael@0 168 calcEffectiveRateAndTempo();
michael@0 169 }
michael@0 170
michael@0 171
michael@0 172
michael@0 173 // Sets new rate control value as a difference in percents compared
michael@0 174 // to the original rate (-50 .. +100 %)
michael@0 175 void SoundTouch::setRateChange(float newRate)
michael@0 176 {
michael@0 177 virtualRate = 1.0f + 0.01f * newRate;
michael@0 178 calcEffectiveRateAndTempo();
michael@0 179 }
michael@0 180
michael@0 181
michael@0 182
michael@0 183 // Sets new tempo control value. Normal tempo = 1.0, smaller values
michael@0 184 // represent slower tempo, larger faster tempo.
michael@0 185 void SoundTouch::setTempo(float newTempo)
michael@0 186 {
michael@0 187 virtualTempo = newTempo;
michael@0 188 calcEffectiveRateAndTempo();
michael@0 189 }
michael@0 190
michael@0 191
michael@0 192
michael@0 193 // Sets new tempo control value as a difference in percents compared
michael@0 194 // to the original tempo (-50 .. +100 %)
michael@0 195 void SoundTouch::setTempoChange(float newTempo)
michael@0 196 {
michael@0 197 virtualTempo = 1.0f + 0.01f * newTempo;
michael@0 198 calcEffectiveRateAndTempo();
michael@0 199 }
michael@0 200
michael@0 201
michael@0 202
michael@0 203 // Sets new pitch control value. Original pitch = 1.0, smaller values
michael@0 204 // represent lower pitches, larger values higher pitch.
michael@0 205 void SoundTouch::setPitch(float newPitch)
michael@0 206 {
michael@0 207 virtualPitch = newPitch;
michael@0 208 calcEffectiveRateAndTempo();
michael@0 209 }
michael@0 210
michael@0 211
michael@0 212
michael@0 213 // Sets pitch change in octaves compared to the original pitch
michael@0 214 // (-1.00 .. +1.00)
michael@0 215 void SoundTouch::setPitchOctaves(float newPitch)
michael@0 216 {
michael@0 217 virtualPitch = (float)exp(0.69314718056f * newPitch);
michael@0 218 calcEffectiveRateAndTempo();
michael@0 219 }
michael@0 220
michael@0 221
michael@0 222
michael@0 223 // Sets pitch change in semi-tones compared to the original pitch
michael@0 224 // (-12 .. +12)
michael@0 225 void SoundTouch::setPitchSemiTones(int newPitch)
michael@0 226 {
michael@0 227 setPitchOctaves((float)newPitch / 12.0f);
michael@0 228 }
michael@0 229
michael@0 230
michael@0 231
michael@0 232 void SoundTouch::setPitchSemiTones(float newPitch)
michael@0 233 {
michael@0 234 setPitchOctaves(newPitch / 12.0f);
michael@0 235 }
michael@0 236
michael@0 237
michael@0 238 // Calculates 'effective' rate and tempo values from the
michael@0 239 // nominal control values.
michael@0 240 void SoundTouch::calcEffectiveRateAndTempo()
michael@0 241 {
michael@0 242 float oldTempo = tempo;
michael@0 243 float oldRate = rate;
michael@0 244
michael@0 245 tempo = virtualTempo / virtualPitch;
michael@0 246 rate = virtualPitch * virtualRate;
michael@0 247
michael@0 248 if (!TEST_FLOAT_EQUAL(rate,oldRate)) pRateTransposer->setRate(rate);
michael@0 249 if (!TEST_FLOAT_EQUAL(tempo, oldTempo)) pTDStretch->setTempo(tempo);
michael@0 250
michael@0 251 #ifndef SOUNDTOUCH_PREVENT_CLICK_AT_RATE_CROSSOVER
michael@0 252 if (rate <= 1.0f)
michael@0 253 {
michael@0 254 if (output != pTDStretch)
michael@0 255 {
michael@0 256 FIFOSamplePipe *tempoOut;
michael@0 257
michael@0 258 assert(output == pRateTransposer);
michael@0 259 // move samples in the current output buffer to the output of pTDStretch
michael@0 260 tempoOut = pTDStretch->getOutput();
michael@0 261 tempoOut->moveSamples(*output);
michael@0 262 // move samples in pitch transposer's store buffer to tempo changer's input
michael@0 263 // deprecated : pTDStretch->moveSamples(*pRateTransposer->getStore());
michael@0 264
michael@0 265 output = pTDStretch;
michael@0 266 }
michael@0 267 }
michael@0 268 else
michael@0 269 #endif
michael@0 270 {
michael@0 271 if (output != pRateTransposer)
michael@0 272 {
michael@0 273 FIFOSamplePipe *transOut;
michael@0 274
michael@0 275 assert(output == pTDStretch);
michael@0 276 // move samples in the current output buffer to the output of pRateTransposer
michael@0 277 transOut = pRateTransposer->getOutput();
michael@0 278 transOut->moveSamples(*output);
michael@0 279 // move samples in tempo changer's input to pitch transposer's input
michael@0 280 pRateTransposer->moveSamples(*pTDStretch->getInput());
michael@0 281
michael@0 282 output = pRateTransposer;
michael@0 283 }
michael@0 284 }
michael@0 285 }
michael@0 286
michael@0 287
michael@0 288 // Sets sample rate.
michael@0 289 void SoundTouch::setSampleRate(uint srate)
michael@0 290 {
michael@0 291 bSrateSet = true;
michael@0 292 // set sample rate, leave other tempo changer parameters as they are.
michael@0 293 pTDStretch->setParameters((int)srate);
michael@0 294 }
michael@0 295
michael@0 296
michael@0 297 // Adds 'numSamples' pcs of samples from the 'samples' memory position into
michael@0 298 // the input of the object.
michael@0 299 void SoundTouch::putSamples(const SAMPLETYPE *samples, uint nSamples)
michael@0 300 {
michael@0 301 if (bSrateSet == false)
michael@0 302 {
michael@0 303 ST_THROW_RT_ERROR("SoundTouch : Sample rate not defined");
michael@0 304 }
michael@0 305 else if (channels == 0)
michael@0 306 {
michael@0 307 ST_THROW_RT_ERROR("SoundTouch : Number of channels not defined");
michael@0 308 }
michael@0 309
michael@0 310 // Transpose the rate of the new samples if necessary
michael@0 311 /* Bypass the nominal setting - can introduce a click in sound when tempo/pitch control crosses the nominal value...
michael@0 312 if (rate == 1.0f)
michael@0 313 {
michael@0 314 // The rate value is same as the original, simply evaluate the tempo changer.
michael@0 315 assert(output == pTDStretch);
michael@0 316 if (pRateTransposer->isEmpty() == 0)
michael@0 317 {
michael@0 318 // yet flush the last samples in the pitch transposer buffer
michael@0 319 // (may happen if 'rate' changes from a non-zero value to zero)
michael@0 320 pTDStretch->moveSamples(*pRateTransposer);
michael@0 321 }
michael@0 322 pTDStretch->putSamples(samples, nSamples);
michael@0 323 }
michael@0 324 */
michael@0 325 #ifndef SOUNDTOUCH_PREVENT_CLICK_AT_RATE_CROSSOVER
michael@0 326 else if (rate <= 1.0f)
michael@0 327 {
michael@0 328 // transpose the rate down, output the transposed sound to tempo changer buffer
michael@0 329 assert(output == pTDStretch);
michael@0 330 pRateTransposer->putSamples(samples, nSamples);
michael@0 331 pTDStretch->moveSamples(*pRateTransposer);
michael@0 332 }
michael@0 333 else
michael@0 334 #endif
michael@0 335 {
michael@0 336 // evaluate the tempo changer, then transpose the rate up,
michael@0 337 assert(output == pRateTransposer);
michael@0 338 pTDStretch->putSamples(samples, nSamples);
michael@0 339 pRateTransposer->moveSamples(*pTDStretch);
michael@0 340 }
michael@0 341 }
michael@0 342
michael@0 343
michael@0 344 // Flushes the last samples from the processing pipeline to the output.
michael@0 345 // Clears also the internal processing buffers.
michael@0 346 //
michael@0 347 // Note: This function is meant for extracting the last samples of a sound
michael@0 348 // stream. This function may introduce additional blank samples in the end
michael@0 349 // of the sound stream, and thus it's not recommended to call this function
michael@0 350 // in the middle of a sound stream.
michael@0 351 void SoundTouch::flush()
michael@0 352 {
michael@0 353 int i;
michael@0 354 int nUnprocessed;
michael@0 355 int nOut;
michael@0 356 SAMPLETYPE *buff=(SAMPLETYPE*)alloca(64*channels*sizeof(SAMPLETYPE));
michael@0 357
michael@0 358 // check how many samples still await processing, and scale
michael@0 359 // that by tempo & rate to get expected output sample count
michael@0 360 nUnprocessed = numUnprocessedSamples();
michael@0 361 nUnprocessed = (int)((double)nUnprocessed / (tempo * rate) + 0.5);
michael@0 362
michael@0 363 nOut = numSamples(); // ready samples currently in buffer ...
michael@0 364 nOut += nUnprocessed; // ... and how many we expect there to be in the end
michael@0 365
michael@0 366 memset(buff, 0, 64 * channels * sizeof(SAMPLETYPE));
michael@0 367 // "Push" the last active samples out from the processing pipeline by
michael@0 368 // feeding blank samples into the processing pipeline until new,
michael@0 369 // processed samples appear in the output (not however, more than
michael@0 370 // 8ksamples in any case)
michael@0 371 for (i = 0; i < 128; i ++)
michael@0 372 {
michael@0 373 putSamples(buff, 64);
michael@0 374 if ((int)numSamples() >= nOut)
michael@0 375 {
michael@0 376 // Enough new samples have appeared into the output!
michael@0 377 // As samples come from processing with bigger chunks, now truncate it
michael@0 378 // back to maximum "nOut" samples to improve duration accuracy
michael@0 379 adjustAmountOfSamples(nOut);
michael@0 380
michael@0 381 // finish
michael@0 382 break;
michael@0 383 }
michael@0 384 }
michael@0 385
michael@0 386 // Clear working buffers
michael@0 387 pRateTransposer->clear();
michael@0 388 pTDStretch->clearInput();
michael@0 389 // yet leave the 'tempoChanger' output intouched as that's where the
michael@0 390 // flushed samples are!
michael@0 391 }
michael@0 392
michael@0 393
michael@0 394 // Changes a setting controlling the processing system behaviour. See the
michael@0 395 // 'SETTING_...' defines for available setting ID's.
michael@0 396 bool SoundTouch::setSetting(int settingId, int value)
michael@0 397 {
michael@0 398 int sampleRate, sequenceMs, seekWindowMs, overlapMs;
michael@0 399
michael@0 400 // read current tdstretch routine parameters
michael@0 401 pTDStretch->getParameters(&sampleRate, &sequenceMs, &seekWindowMs, &overlapMs);
michael@0 402
michael@0 403 switch (settingId)
michael@0 404 {
michael@0 405 case SETTING_USE_AA_FILTER :
michael@0 406 // enables / disabless anti-alias filter
michael@0 407 pRateTransposer->enableAAFilter((value != 0) ? true : false);
michael@0 408 return true;
michael@0 409
michael@0 410 case SETTING_AA_FILTER_LENGTH :
michael@0 411 // sets anti-alias filter length
michael@0 412 pRateTransposer->getAAFilter()->setLength(value);
michael@0 413 return true;
michael@0 414
michael@0 415 case SETTING_USE_QUICKSEEK :
michael@0 416 // enables / disables tempo routine quick seeking algorithm
michael@0 417 pTDStretch->enableQuickSeek((value != 0) ? true : false);
michael@0 418 return true;
michael@0 419
michael@0 420 case SETTING_SEQUENCE_MS:
michael@0 421 // change time-stretch sequence duration parameter
michael@0 422 pTDStretch->setParameters(sampleRate, value, seekWindowMs, overlapMs);
michael@0 423 return true;
michael@0 424
michael@0 425 case SETTING_SEEKWINDOW_MS:
michael@0 426 // change time-stretch seek window length parameter
michael@0 427 pTDStretch->setParameters(sampleRate, sequenceMs, value, overlapMs);
michael@0 428 return true;
michael@0 429
michael@0 430 case SETTING_OVERLAP_MS:
michael@0 431 // change time-stretch overlap length parameter
michael@0 432 pTDStretch->setParameters(sampleRate, sequenceMs, seekWindowMs, value);
michael@0 433 return true;
michael@0 434
michael@0 435 default :
michael@0 436 return false;
michael@0 437 }
michael@0 438 }
michael@0 439
michael@0 440
michael@0 441 // Reads a setting controlling the processing system behaviour. See the
michael@0 442 // 'SETTING_...' defines for available setting ID's.
michael@0 443 //
michael@0 444 // Returns the setting value.
michael@0 445 int SoundTouch::getSetting(int settingId) const
michael@0 446 {
michael@0 447 int temp;
michael@0 448
michael@0 449 switch (settingId)
michael@0 450 {
michael@0 451 case SETTING_USE_AA_FILTER :
michael@0 452 return (uint)pRateTransposer->isAAFilterEnabled();
michael@0 453
michael@0 454 case SETTING_AA_FILTER_LENGTH :
michael@0 455 return pRateTransposer->getAAFilter()->getLength();
michael@0 456
michael@0 457 case SETTING_USE_QUICKSEEK :
michael@0 458 return (uint) pTDStretch->isQuickSeekEnabled();
michael@0 459
michael@0 460 case SETTING_SEQUENCE_MS:
michael@0 461 pTDStretch->getParameters(NULL, &temp, NULL, NULL);
michael@0 462 return temp;
michael@0 463
michael@0 464 case SETTING_SEEKWINDOW_MS:
michael@0 465 pTDStretch->getParameters(NULL, NULL, &temp, NULL);
michael@0 466 return temp;
michael@0 467
michael@0 468 case SETTING_OVERLAP_MS:
michael@0 469 pTDStretch->getParameters(NULL, NULL, NULL, &temp);
michael@0 470 return temp;
michael@0 471
michael@0 472 case SETTING_NOMINAL_INPUT_SEQUENCE :
michael@0 473 return pTDStretch->getInputSampleReq();
michael@0 474
michael@0 475 case SETTING_NOMINAL_OUTPUT_SEQUENCE :
michael@0 476 return pTDStretch->getOutputBatchSize();
michael@0 477
michael@0 478 default :
michael@0 479 return 0;
michael@0 480 }
michael@0 481 }
michael@0 482
michael@0 483
michael@0 484 // Clears all the samples in the object's output and internal processing
michael@0 485 // buffers.
michael@0 486 void SoundTouch::clear()
michael@0 487 {
michael@0 488 pRateTransposer->clear();
michael@0 489 pTDStretch->clear();
michael@0 490 }
michael@0 491
michael@0 492
michael@0 493
michael@0 494 /// Returns number of samples currently unprocessed.
michael@0 495 uint SoundTouch::numUnprocessedSamples() const
michael@0 496 {
michael@0 497 FIFOSamplePipe * psp;
michael@0 498 if (pTDStretch)
michael@0 499 {
michael@0 500 psp = pTDStretch->getInput();
michael@0 501 if (psp)
michael@0 502 {
michael@0 503 return psp->numSamples();
michael@0 504 }
michael@0 505 }
michael@0 506 return 0;
michael@0 507 }

mercurial