PageRenderTime 62ms CodeModel.GetById 33ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/external/pysoundtouch14/libsoundtouch/SoundTouch.cpp

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