summaryrefslogtreecommitdiff
path: root/libs/rubberband/src/StretcherChannelData.h
blob: b56a6e07dc6262e1b53384129825b89b69a37fb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Rubber Band
    An audio time-stretching and pitch-shifting library.
    Copyright 2007-2008 Chris Cannam.
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#ifndef _RUBBERBAND_STRETCHERCHANNELDATA_H_
#define _RUBBERBAND_STRETCHERCHANNELDATA_H_

#include "StretcherImpl.h"

#include <set>

//#define EXPERIMENT 1

namespace RubberBand
{

class Resampler;
	
class RubberBandStretcher::Impl::ChannelData
{
public:        
    /**
     * Construct a ChannelData structure.
     *
     * The window size passed in here is the size for the FFT
     * calculation, and most of the buffer sizes also depend on
     * it.  In practice it is always a power of two and except for
     * very extreme stretches is always either 1024, 2048 or 4096.
     *
     * The outbuf size depends on other factors as well, including
     * the pitch scale factor and any maximum processing block
     * size specified by the user of the code.
     */
    ChannelData(size_t windowSize, int overSample, size_t outbufSize);

    /**
     * Construct a ChannelData structure that can process at
     * different FFT sizes without requiring reallocation when the
     * size changes.  The size can subsequently be changed with a
     * call to setWindowSize.  Reallocation will only be necessary
     * if setWindowSize is called with a value not equal to one of
     * those passed in to the constructor.
     *
     * The outbufSize should be the maximum possible outbufSize to
     * avoid reallocation, which will happen if setOutbufSize is
     * called subsequently.
     */
    ChannelData(const std::set<size_t> &windowSizes,
                int overSample, size_t initialWindowSize, size_t outbufSize);
    ~ChannelData();

    /**
     * Reset buffers
     */
    void reset();

    /**
     * Set the FFT and buffer sizes from the given processing
     * window size.  If this ChannelData was constructed with a set
     * of window sizes and the given window size here was among
     * them, no reallocation will be required.
     */
    void setWindowSize(size_t windowSize);

    /**
     * Set the outbufSize for the channel data.  Reallocation will
     * occur.
     */
    void setOutbufSize(size_t outbufSize);

    /**
     * Set the resampler buffer size.  Default if not called is no
     * buffer allocated at all.
     */
    void setResampleBufSize(size_t resamplebufSize);
    
    RingBuffer<float> *inbuf;
    RingBuffer<float> *outbuf;

    double *mag;
    double *phase;

    double *prevPhase;
    double *prevError;
    double *unwrappedPhase;


    size_t *freqPeak;

    float *accumulator;
    size_t accumulatorFill;
    float *windowAccumulator;

    float *fltbuf;
    double *dblbuf; // owned by FFT object, only used for time domain FFT i/o
    double *envelope; // for cepstral formant shift
    bool unchanged;

    size_t prevIncrement; // only used in RT mode

    size_t chunkCount;
    size_t inCount;
    long inputSize; // set only after known (when data ended); -1 previously
    size_t outCount;

    bool draining;
    bool outputComplete;

    FFT *fft;
    std::map<size_t, FFT *> ffts;

    Resampler *resampler;
    float *resamplebuf;
    size_t resamplebufSize;

    int oversample;

private:
    void construct(const std::set<size_t> &windowSizes,
                   size_t initialWindowSize, size_t outbufSize);
};        

}

#endif