Use PBD::ffs for portability
[ardour.git] / libs / rubberband / src / StretcherChannelData.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_STRETCHERCHANNELDATA_H_
16 #define _RUBBERBAND_STRETCHERCHANNELDATA_H_
17
18 #include "StretcherImpl.h"
19
20 #include <set>
21
22 //#define EXPERIMENT 1
23
24 namespace RubberBand
25 {
26
27 class Resampler;
28         
29 class RubberBandStretcher::Impl::ChannelData
30 {
31 public:        
32     /**
33      * Construct a ChannelData structure.
34      *
35      * The window size passed in here is the size for the FFT
36      * calculation, and most of the buffer sizes also depend on
37      * it.  In practice it is always a power of two and except for
38      * very extreme stretches is always either 1024, 2048 or 4096.
39      *
40      * The outbuf size depends on other factors as well, including
41      * the pitch scale factor and any maximum processing block
42      * size specified by the user of the code.
43      */
44     ChannelData(size_t windowSize, int overSample, size_t outbufSize);
45
46     /**
47      * Construct a ChannelData structure that can process at
48      * different FFT sizes without requiring reallocation when the
49      * size changes.  The size can subsequently be changed with a
50      * call to setWindowSize.  Reallocation will only be necessary
51      * if setWindowSize is called with a value not equal to one of
52      * those passed in to the constructor.
53      *
54      * The outbufSize should be the maximum possible outbufSize to
55      * avoid reallocation, which will happen if setOutbufSize is
56      * called subsequently.
57      */
58     ChannelData(const std::set<size_t> &windowSizes,
59                 int overSample, size_t initialWindowSize, size_t outbufSize);
60     ~ChannelData();
61
62     /**
63      * Reset buffers
64      */
65     void reset();
66
67     /**
68      * Set the FFT and buffer sizes from the given processing
69      * window size.  If this ChannelData was constructed with a set
70      * of window sizes and the given window size here was among
71      * them, no reallocation will be required.
72      */
73     void setWindowSize(size_t windowSize);
74
75     /**
76      * Set the outbufSize for the channel data.  Reallocation will
77      * occur.
78      */
79     void setOutbufSize(size_t outbufSize);
80
81     /**
82      * Set the resampler buffer size.  Default if not called is no
83      * buffer allocated at all.
84      */
85     void setResampleBufSize(size_t resamplebufSize);
86     
87     RingBuffer<float> *inbuf;
88     RingBuffer<float> *outbuf;
89
90     double *mag;
91     double *phase;
92
93     double *prevPhase;
94     double *prevError;
95     double *unwrappedPhase;
96
97
98     size_t *freqPeak;
99
100     float *accumulator;
101     size_t accumulatorFill;
102     float *windowAccumulator;
103
104     float *fltbuf;
105     double *dblbuf; // owned by FFT object, only used for time domain FFT i/o
106     double *envelope; // for cepstral formant shift
107     bool unchanged;
108
109     size_t prevIncrement; // only used in RT mode
110
111     size_t chunkCount;
112     size_t inCount;
113     long inputSize; // set only after known (when data ended); -1 previously
114     size_t outCount;
115
116     bool draining;
117     bool outputComplete;
118
119     FFT *fft;
120     std::map<size_t, FFT *> ffts;
121
122     Resampler *resampler;
123     float *resamplebuf;
124     size_t resamplebufSize;
125
126     int oversample;
127
128 private:
129     void construct(const std::set<size_t> &windowSizes,
130                    size_t initialWindowSize, size_t outbufSize);
131 };        
132
133 }
134
135 #endif