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@0
|
16 #ifndef _INTEGER_TIME_STRETCHER_H_
|
Chris@0
|
17 #define _INTEGER_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@0
|
25 * A time stretcher that slows down audio by an integer multiple of
|
Chris@0
|
26 * its original duration, preserving pitch. This uses the simple
|
Chris@0
|
27 * phase vocoder technique from DAFX pp275-276, adding a block-based
|
Chris@0
|
28 * stream oriented API.
|
Chris@0
|
29 *
|
Chris@0
|
30 * Causes significant transient smearing, but sounds good for steady
|
Chris@0
|
31 * notes and is generally predictable.
|
Chris@0
|
32 */
|
Chris@0
|
33
|
Chris@0
|
34 class IntegerTimeStretcher
|
Chris@0
|
35 {
|
Chris@0
|
36 public:
|
Chris@12
|
37 IntegerTimeStretcher(float ratio,
|
Chris@0
|
38 size_t maxProcessInputBlockSize,
|
Chris@0
|
39 size_t inputIncrement = 64,
|
Chris@0
|
40 size_t windowSize = 2048,
|
Chris@0
|
41 WindowType windowType = HanningWindow);
|
Chris@0
|
42 virtual ~IntegerTimeStretcher();
|
Chris@0
|
43
|
Chris@12
|
44 /**
|
Chris@12
|
45 * Process a block. The input array contains the given number of
|
Chris@12
|
46 * samples; the output has enough space for samples * m_ratio.
|
Chris@12
|
47 */
|
Chris@0
|
48 void process(float *input, float *output, size_t samples);
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * Get the hop size for input. Smaller values may produce better
|
Chris@0
|
52 * results, at a cost in processing time. Larger values are
|
Chris@0
|
53 * faster but increase the likelihood of echo-like effects. The
|
Chris@0
|
54 * default is 64, which is usually pretty good, though heavy on
|
Chris@0
|
55 * processor power.
|
Chris@0
|
56 */
|
Chris@0
|
57 size_t getInputIncrement() const { return m_n1; }
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * Get the window size for FFT processing. Must be larger than
|
Chris@0
|
61 * the input and output increments. The default is 2048.
|
Chris@0
|
62 */
|
Chris@0
|
63 size_t getWindowSize() const { return m_wlen; }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * Get the window type. The default is a Hanning window.
|
Chris@0
|
67 */
|
Chris@0
|
68 WindowType getWindowType() const { return m_window->getType(); }
|
Chris@0
|
69
|
Chris@12
|
70 float getRatio() const { return m_ratio; }
|
Chris@0
|
71 size_t getOutputIncrement() const { return getInputIncrement() * getRatio(); }
|
Chris@0
|
72 size_t getProcessingLatency() const;
|
Chris@0
|
73
|
Chris@0
|
74 protected:
|
Chris@13
|
75 /**
|
Chris@13
|
76 * Process a single phase vocoder frame.
|
Chris@13
|
77 *
|
Chris@13
|
78 * Take m_wlen time-domain source samples from in, perform an FFT,
|
Chris@13
|
79 * phase shift, and IFFT, and add the results to out (presumably
|
Chris@13
|
80 * overlapping parts of existing data from prior frames).
|
Chris@13
|
81 *
|
Chris@13
|
82 * Also add to the modulation output the results of windowing a
|
Chris@13
|
83 * set of 1s with the resynthesis window -- this can then be used
|
Chris@13
|
84 * to ensure the output has the correct magnitude in cases where
|
Chris@13
|
85 * the window overlap varies or otherwise results in something
|
Chris@13
|
86 * other than a flat sum.
|
Chris@13
|
87 */
|
Chris@13
|
88 void processBlock(float *in, float *out, float *modulation);
|
Chris@0
|
89
|
Chris@12
|
90 float m_ratio;
|
Chris@0
|
91 size_t m_n1;
|
Chris@0
|
92 size_t m_n2;
|
Chris@0
|
93 size_t m_wlen;
|
Chris@0
|
94 Window<float> *m_window;
|
Chris@0
|
95
|
Chris@0
|
96 fftwf_complex *m_time;
|
Chris@0
|
97 fftwf_complex *m_freq;
|
Chris@0
|
98 float *m_dbuf;
|
Chris@12
|
99 float *m_prevPhase;
|
Chris@12
|
100 float *m_prevAdjustedPhase;
|
Chris@0
|
101
|
Chris@0
|
102 fftwf_plan m_plan;
|
Chris@0
|
103 fftwf_plan m_iplan;
|
Chris@0
|
104
|
Chris@0
|
105 RingBuffer<float> m_inbuf;
|
Chris@0
|
106 RingBuffer<float> m_outbuf;
|
Chris@0
|
107 float *m_mashbuf;
|
Chris@13
|
108 float *m_modulationbuf;
|
Chris@0
|
109 };
|
Chris@0
|
110
|
Chris@0
|
111 #endif
|