Chris@0
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 Sonic Visualiser
|
Chris@0
|
5 An audio file viewer and annotation editor.
|
Chris@0
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@0
|
7 This file copyright 2006 Chris Cannam.
|
Chris@0
|
8
|
Chris@0
|
9 This program is free software; you can redistribute it and/or
|
Chris@0
|
10 modify it under the terms of the GNU General Public License as
|
Chris@0
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@0
|
12 License, or (at your option) any later version. See the file
|
Chris@0
|
13 COPYING included with this distribution for more information.
|
Chris@0
|
14 */
|
Chris@0
|
15
|
Chris@14
|
16 #ifndef _PHASE_VOCODER_TIME_STRETCHER_H_
|
Chris@14
|
17 #define _PHASE_VOCODER_TIME_STRETCHER_H_
|
Chris@0
|
18
|
Chris@0
|
19 #include "base/Window.h"
|
Chris@0
|
20 #include "base/RingBuffer.h"
|
Chris@0
|
21
|
Chris@0
|
22 #include <fftw3.h>
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@14
|
25 * A time stretcher that alters the performance speed of audio,
|
Chris@14
|
26 * preserving pitch. This uses the simple phase vocoder technique
|
Chris@14
|
27 * from DAFX pp275-276, adding a block-based stream oriented API.
|
Chris@0
|
28 *
|
Chris@0
|
29 * Causes significant transient smearing, but sounds good for steady
|
Chris@0
|
30 * notes and is generally predictable.
|
Chris@0
|
31 */
|
Chris@0
|
32
|
Chris@14
|
33 class PhaseVocoderTimeStretcher
|
Chris@0
|
34 {
|
Chris@0
|
35 public:
|
Chris@14
|
36 PhaseVocoderTimeStretcher(float ratio,
|
Chris@0
|
37 size_t maxProcessInputBlockSize,
|
Chris@0
|
38 size_t inputIncrement = 64,
|
Chris@0
|
39 size_t windowSize = 2048,
|
Chris@0
|
40 WindowType windowType = HanningWindow);
|
Chris@14
|
41 virtual ~PhaseVocoderTimeStretcher();
|
Chris@0
|
42
|
Chris@12
|
43 /**
|
Chris@12
|
44 * Process a block. The input array contains the given number of
|
Chris@12
|
45 * samples; the output has enough space for samples * m_ratio.
|
Chris@12
|
46 */
|
Chris@0
|
47 void process(float *input, float *output, size_t samples);
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * Get the hop size for input. Smaller values may produce better
|
Chris@0
|
51 * results, at a cost in processing time. Larger values are
|
Chris@0
|
52 * faster but increase the likelihood of echo-like effects. The
|
Chris@0
|
53 * default is 64, which is usually pretty good, though heavy on
|
Chris@0
|
54 * processor power.
|
Chris@0
|
55 */
|
Chris@0
|
56 size_t getInputIncrement() const { return m_n1; }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * Get the window size for FFT processing. Must be larger than
|
Chris@0
|
60 * the input and output increments. The default is 2048.
|
Chris@0
|
61 */
|
Chris@0
|
62 size_t getWindowSize() const { return m_wlen; }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * Get the window type. The default is a Hanning window.
|
Chris@0
|
66 */
|
Chris@0
|
67 WindowType getWindowType() const { return m_window->getType(); }
|
Chris@0
|
68
|
Chris@12
|
69 float getRatio() const { return m_ratio; }
|
Chris@0
|
70 size_t getOutputIncrement() const { return getInputIncrement() * getRatio(); }
|
Chris@0
|
71 size_t getProcessingLatency() const;
|
Chris@0
|
72
|
Chris@0
|
73 protected:
|
Chris@13
|
74 /**
|
Chris@13
|
75 * Process a single phase vocoder frame.
|
Chris@13
|
76 *
|
Chris@13
|
77 * Take m_wlen time-domain source samples from in, perform an FFT,
|
Chris@13
|
78 * phase shift, and IFFT, and add the results to out (presumably
|
Chris@13
|
79 * overlapping parts of existing data from prior frames).
|
Chris@13
|
80 *
|
Chris@13
|
81 * Also add to the modulation output the results of windowing a
|
Chris@13
|
82 * set of 1s with the resynthesis window -- this can then be used
|
Chris@13
|
83 * to ensure the output has the correct magnitude in cases where
|
Chris@13
|
84 * the window overlap varies or otherwise results in something
|
Chris@13
|
85 * other than a flat sum.
|
Chris@13
|
86 */
|
Chris@13
|
87 void processBlock(float *in, float *out, float *modulation);
|
Chris@0
|
88
|
Chris@12
|
89 float m_ratio;
|
Chris@0
|
90 size_t m_n1;
|
Chris@0
|
91 size_t m_n2;
|
Chris@0
|
92 size_t m_wlen;
|
Chris@0
|
93 Window<float> *m_window;
|
Chris@0
|
94
|
Chris@0
|
95 fftwf_complex *m_time;
|
Chris@0
|
96 fftwf_complex *m_freq;
|
Chris@0
|
97 float *m_dbuf;
|
Chris@12
|
98 float *m_prevPhase;
|
Chris@12
|
99 float *m_prevAdjustedPhase;
|
Chris@0
|
100
|
Chris@0
|
101 fftwf_plan m_plan;
|
Chris@0
|
102 fftwf_plan m_iplan;
|
Chris@0
|
103
|
Chris@0
|
104 RingBuffer<float> m_inbuf;
|
Chris@0
|
105 RingBuffer<float> m_outbuf;
|
Chris@0
|
106 float *m_mashbuf;
|
Chris@13
|
107 float *m_modulationbuf;
|
Chris@0
|
108 };
|
Chris@0
|
109
|
Chris@0
|
110 #endif
|