1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
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_STRETCHERIMPL_H_
16 #define _RUBBERBAND_STRETCHERIMPL_H_
18 #include "RubberBandStretcher.h"
22 #include "RingBuffer.h"
32 class StretchCalculator;
34 class RubberBandStretcher::Impl
37 Impl(size_t sampleRate, size_t channels, Options options,
38 double initialTimeRatio, double initialPitchScale);
42 void setTimeRatio(double ratio);
43 void setPitchScale(double scale);
45 double getTimeRatio() const;
46 double getPitchScale() const;
48 size_t getLatency() const;
50 void setTransientsOption(Options);
51 void setPhaseOption(Options);
52 void setFormantOption(Options);
53 void setPitchOption(Options);
55 void setExpectedInputDuration(size_t samples);
56 void setMaxProcessSize(size_t samples);
58 size_t getSamplesRequired() const;
60 void study(const float *const *input, size_t samples, bool final);
61 void process(const float *const *input, size_t samples, bool final);
63 int available() const;
64 size_t retrieve(float *const *output, size_t samples) const;
66 float getFrequencyCutoff(int n) const;
67 void setFrequencyCutoff(int n, float f);
69 size_t getInputIncrement() const {
73 std::vector<int> getOutputIncrements() const;
74 std::vector<float> getPhaseResetCurve() const;
75 std::vector<int> getExactTimePoints() const;
77 size_t getChannelCount() const {
81 void calculateStretch();
83 void setDebugLevel(int level);
84 static void setDefaultDebugLevel(int level) { m_defaultDebugLevel = level; }
90 size_t consumeChannel(size_t channel, const float *input,
91 size_t samples, bool final);
92 void processChunks(size_t channel, bool &any, bool &last);
93 bool processOneChunk(); // across all channels, for real time use
94 bool processChunkForChannel(size_t channel, size_t phaseIncrement,
95 size_t shiftIncrement, bool phaseReset);
96 bool testInbufReadSpace(size_t channel);
97 void calculateIncrements(size_t &phaseIncrement,
98 size_t &shiftIncrement, bool &phaseReset);
99 bool getIncrements(size_t channel, size_t &phaseIncrement,
100 size_t &shiftIncrement, bool &phaseReset);
101 void analyseChunk(size_t channel);
102 void modifyChunk(size_t channel, size_t outputIncrement, bool phaseReset);
103 void formantShiftChunk(size_t channel);
104 void synthesiseChunk(size_t channel);
105 void writeChunk(size_t channel, size_t shiftIncrement, bool last);
107 void calculateSizes();
111 double getEffectiveRatio() const;
113 size_t roundUp(size_t value); // to next power of two
115 bool resampleBeforeStretching() const;
124 size_t m_maxProcessSize;
125 size_t m_expectedInputDuration;
141 std::map<size_t, Window<float> *> m_windows;
142 Window<float> *m_window;
145 Condition m_spaceAvailable;
147 class ProcessThread : public Thread
150 ProcessThread(Impl *s, size_t c);
152 void signalDataAvailable();
157 Condition m_dataAvailable;
161 mutable Mutex m_threadSetMutex;
162 typedef std::set<ProcessThread *> ThreadSet;
163 ThreadSet m_threadSet;
166 size_t m_inputDuration;
167 std::vector<float> m_phaseResetDf;
168 std::vector<float> m_stretchDf;
169 std::vector<bool> m_silence;
173 std::vector<ChannelData *> m_channelData;
175 std::vector<int> m_outputIncrements;
177 mutable RingBuffer<int> m_lastProcessOutputIncrements;
178 mutable RingBuffer<float> m_lastProcessPhaseResetDf;
180 AudioCurve *m_phaseResetAudioCurve;
181 AudioCurve *m_stretchAudioCurve;
182 AudioCurve *m_silentAudioCurve;
183 StretchCalculator *m_stretchCalculator;
189 size_t m_baseWindowSize;
190 float m_rateMultiple;
192 void writeOutput(RingBuffer<float> &to, float *from,
193 size_t qty, size_t &outCount, size_t theoreticalOut);
195 static int m_defaultDebugLevel;
196 static const size_t m_defaultIncrement;
197 static const size_t m_defaultWindowSize;