Revert to fader-is-for-route on double-clicking a mixer strip's fader. Fixes #3685.
[ardour.git] / libs / rubberband / src / StretchCalculator.h
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2
3 /*
4     Rubber Band
5     An audio time-stretching and pitch-shifting library.
6     Copyright 2007-2008 Chris Cannam.
7     
8     This program is free software; you can redistribute it and/or
9     modify it under the terms of the GNU General Public License as
10     published by the Free Software Foundation; either version 2 of the
11     License, or (at your option) any later version.  See the file
12     COPYING included with this distribution for more information.
13 */
14
15 #ifndef _RUBBERBAND_STRETCH_CALCULATOR_H_
16 #define _RUBBERBAND_STRETCH_CALCULATOR_H_
17
18 #include <sys/types.h>
19
20 #include <vector>
21
22 namespace RubberBand
23 {
24
25 class StretchCalculator
26 {
27 public:
28     StretchCalculator(size_t sampleRate, size_t inputIncrement, bool useHardPeaks);
29     virtual ~StretchCalculator();
30
31     /**
32      * Calculate phase increments for a region of audio, given the
33      * overall target stretch ratio, input duration in audio samples,
34      * and the audio curves to use for identifying phase lock points
35      * (lockAudioCurve) and for allocating stretches to relatively
36      * less prominent points (stretchAudioCurve).
37      */
38     virtual std::vector<int> calculate(double ratio, size_t inputDuration,
39                                        const std::vector<float> &lockAudioCurve,
40                                        const std::vector<float> &stretchAudioCurve);
41
42     /**
43      * Calculate the phase increment for a single audio block, given
44      * the overall target stretch ratio and the block's value on the
45      * phase-lock audio curve.  State is retained between calls in the
46      * StretchCalculator object; call reset() to reset it.  This uses
47      * a less sophisticated method than the offline calculate().
48      *
49      * If increment is non-zero, use it for the input increment for
50      * this block in preference to m_increment.
51      */
52     virtual int calculateSingle(double ratio, float curveValue,
53                                 size_t increment = 0);
54
55     void setUseHardPeaks(bool use) { m_useHardPeaks = use; }
56
57     void reset();
58   
59     void setDebugLevel(int level) { m_debugLevel = level; }
60
61     struct Peak {
62         size_t chunk;
63         bool hard;
64     };
65     std::vector<Peak> getLastCalculatedPeaks() const { return m_lastPeaks; }
66
67     std::vector<float> smoothDF(const std::vector<float> &df);
68
69 protected:
70     std::vector<Peak> findPeaks(const std::vector<float> &audioCurve);
71
72     std::vector<int> distributeRegion(const std::vector<float> &regionCurve,
73                                       size_t outputDuration, float ratio,
74                                       bool phaseReset);
75
76     void calculateDisplacements(const std::vector<float> &df,
77                                 float &maxDf,
78                                 double &totalDisplacement,
79                                 double &maxDisplacement,
80                                 float adj) const;
81
82     size_t m_sampleRate;
83     size_t m_blockSize;
84     size_t m_increment;
85     float m_prevDf;
86     double m_divergence;
87     float m_recovery;
88     float m_prevRatio;
89     int m_transientAmnesty; // only in RT mode; handled differently offline
90     int m_debugLevel;
91     bool m_useHardPeaks;
92     
93     std::vector<Peak> m_lastPeaks;
94 };
95
96 }
97
98 #endif