1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4     Rubber Band Library
5     An audio time-stretching and pitch-shifting library.
6     Copyright 2007-2021 Particular Programs Ltd.
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     Alternatively, if you have a valid commercial licence for the
15     Rubber Band Library obtained by agreement with the copyright
16     holders, you may redistribute and/or modify it under the terms
17     described in that licence.
18 
19     If you wish to distribute code using the Rubber Band Library
20     under terms other than those of the GNU General Public License,
21     you must obtain a valid commercial licence before doing so.
22 */
23 
24 #ifndef RUBBERBAND_STRETCHERCHANNELDATA_H
25 #define RUBBERBAND_STRETCHERCHANNELDATA_H
26 
27 #include "StretcherImpl.h"
28 
29 #include <set>
30 #include <atomic>
31 
32 namespace RubberBand
33 {
34 
35 class Resampler;
36 
37 class RubberBandStretcher::Impl::ChannelData
38 {
39 public:
40     /**
41      * Construct a ChannelData structure.
42      *
43      * The sizes passed in here are for the time-domain analysis
44      * window and FFT calculation, and most of the buffer sizes also
45      * depend on them.  In practice they are always powers of two, the
46      * window and FFT sizes are either equal or generally in a 2:1
47      * relationship either way, and except for very extreme stretches
48      * the FFT size is either 1024, 2048 or 4096.
49      *
50      * The outbuf size depends on other factors as well, including
51      * the pitch scale factor and any maximum processing block
52      * size specified by the user of the code.
53      */
54     ChannelData(size_t windowSize,
55                 size_t fftSize,
56                 size_t outbufSize);
57 
58     /**
59      * Construct a ChannelData structure that can process at different
60      * FFT sizes without requiring reallocation when the size changes.
61      * The sizes can subsequently be changed with a call to setSizes.
62      * Reallocation will only be necessary if setSizes is called with
63      * values not equal to any of those passed in to the constructor.
64      *
65      * The outbufSize should be the maximum possible outbufSize to
66      * avoid reallocation, which will happen if setOutbufSize is
67      * called subsequently.
68      */
69     ChannelData(const std::set<size_t> &sizes,
70                 size_t initialWindowSize,
71                 size_t initialFftSize,
72                 size_t outbufSize);
73     ~ChannelData();
74 
75     /**
76      * Reset buffers
77      */
78     void reset();
79 
80     /**
81      * Set the FFT, analysis window, and buffer sizes.  If this
82      * ChannelData was constructed with a set of sizes and the given
83      * window and FFT sizes here were among them, no reallocation will
84      * be required.
85      */
86     void setSizes(size_t windowSize, size_t fftSizes);
87 
88     /**
89      * Set the outbufSize for the channel data.  Reallocation will
90      * occur.
91      */
92     void setOutbufSize(size_t outbufSize);
93 
94     /**
95      * Set the resampler buffer size.  Default if not called is no
96      * buffer allocated at all.
97      */
98     void setResampleBufSize(size_t resamplebufSize);
99 
100     RingBuffer<float> *inbuf;
101     RingBuffer<float> *outbuf;
102 
103     process_t *mag;
104     process_t *phase;
105 
106     process_t *prevPhase;
107     process_t *prevError;
108     process_t *unwrappedPhase;
109 
110     float *accumulator;
111     size_t accumulatorFill;
112     float *windowAccumulator;
113     float *ms; // only used when mid-side processing
114     float *interpolator; // only used when time-domain smoothing is on
115     int interpolatorScale;
116 
117     float *fltbuf;
118     process_t *dblbuf; // owned by FFT object, only used for time domain FFT i/o
119     process_t *envelope; // for cepstral formant shift
120     bool unchanged;
121 
122     size_t prevIncrement; // only used in RT mode
123 
124     size_t chunkCount;
125     size_t inCount;
126     std::atomic<int64_t> inputSize; // set only after known (when data ended); -1 previously
127     size_t outCount;
128 
129     std::atomic<bool> draining;
130     std::atomic<bool> outputComplete;
131 
132     FFT *fft;
133     std::map<size_t, FFT *> ffts;
134 
135     Resampler *resampler;
136     float *resamplebuf;
137     size_t resamplebufSize;
138 
139 private:
140     void construct(const std::set<size_t> &sizes,
141                    size_t initialWindowSize, size_t initialFftSize,
142                    size_t outbufSize);
143 };
144 
145 }
146 
147 #endif
148