various fixes to MidiRegionView selection handling, key handling, drawing of ghost...
[ardour2.git] / libs / rubberband / src / StretchCalculator.h
blobe79c8e3c1e16324324d1b1922e4a4a96a14adc3b
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
3 /*
4 Rubber Band
5 An audio time-stretching and pitch-shifting library.
6 Copyright 2007-2008 Chris Cannam.
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.
15 #ifndef _RUBBERBAND_STRETCH_CALCULATOR_H_
16 #define _RUBBERBAND_STRETCH_CALCULATOR_H_
18 #include <sys/types.h>
20 #include <vector>
22 namespace RubberBand
25 class StretchCalculator
27 public:
28 StretchCalculator(size_t sampleRate, size_t inputIncrement, bool useHardPeaks);
29 virtual ~StretchCalculator();
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).
38 virtual std::vector<int> calculate(double ratio, size_t inputDuration,
39 const std::vector<float> &lockAudioCurve,
40 const std::vector<float> &stretchAudioCurve);
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().
49 * If increment is non-zero, use it for the input increment for
50 * this block in preference to m_increment.
52 virtual int calculateSingle(double ratio, float curveValue,
53 size_t increment = 0);
55 void setUseHardPeaks(bool use) { m_useHardPeaks = use; }
57 void reset();
59 void setDebugLevel(int level) { m_debugLevel = level; }
61 struct Peak {
62 size_t chunk;
63 bool hard;
65 std::vector<Peak> getLastCalculatedPeaks() const { return m_lastPeaks; }
67 std::vector<float> smoothDF(const std::vector<float> &df);
69 protected:
70 std::vector<Peak> findPeaks(const std::vector<float> &audioCurve);
72 std::vector<int> distributeRegion(const std::vector<float> &regionCurve,
73 size_t outputDuration, float ratio,
74 bool phaseReset);
76 void calculateDisplacements(const std::vector<float> &df,
77 float &maxDf,
78 double &totalDisplacement,
79 double &maxDisplacement,
80 float adj) const;
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;
93 std::vector<Peak> m_lastPeaks;
98 #endif