comparison audioio/PhaseVocoderTimeStretcher.h @ 43:3c5756fb6a68

* Move some things around to facilitate plundering libraries for other applications without needing to duplicate so much code. sv/osc -> data/osc sv/audioio -> audioio sv/transform -> plugin/transform sv/document -> document (will rename to framework in next commit)
author Chris Cannam
date Wed, 24 Oct 2007 16:34:31 +0000
parents
children 0ffab5d7e3e1
comparison
equal deleted inserted replaced
42:0619006a1ee3 43:3c5756fb6a68
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam and QMUL.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #ifndef _PHASE_VOCODER_TIME_STRETCHER_H_
17 #define _PHASE_VOCODER_TIME_STRETCHER_H_
18
19 #include "base/Window.h"
20 #include "base/RingBuffer.h"
21
22 #include "data/fft/FFTapi.h"
23
24 #include <QMutex>
25
26 /**
27 * A time stretcher that alters the performance speed of audio,
28 * preserving pitch.
29 *
30 * This is based on the straightforward phase vocoder with phase
31 * unwrapping (as in e.g. the DAFX book pp275-), with optional
32 * percussive transient detection to avoid smearing percussive notes
33 * and resynchronise phases, and adding a stream API for real-time
34 * use. Principles and methods from Chris Duxbury, AES 2002 and 2004
35 * thesis; Emmanuel Ravelli, DAFX 2005; Dan Barry, ISSC 2005 on
36 * percussion detection; code by Chris Cannam.
37 */
38
39 class PhaseVocoderTimeStretcher
40 {
41 public:
42 PhaseVocoderTimeStretcher(size_t sampleRate,
43 size_t channels,
44 float ratio,
45 bool sharpen,
46 size_t maxOutputBlockSize);
47 virtual ~PhaseVocoderTimeStretcher();
48
49 /**
50 * Return the number of samples that would need to be added via
51 * putInput in order to provoke the time stretcher into doing some
52 * time stretching and making more output samples available.
53 * This will be an estimate, if transient sharpening is on; the
54 * caller may need to do the put/get/test cycle more than once.
55 */
56 size_t getRequiredInputSamples() const;
57
58 /**
59 * Put (and possibly process) a given number of input samples.
60 * Number should usually equal the value returned from
61 * getRequiredInputSamples().
62 */
63 void putInput(float **input, size_t samples);
64
65 /**
66 * Get the number of processed samples ready for reading.
67 */
68 size_t getAvailableOutputSamples() const;
69
70 /**
71 * Get some processed samples.
72 */
73 void getOutput(float **output, size_t samples);
74
75 //!!! and reset?
76
77 /**
78 * Change the time stretch ratio.
79 */
80 void setRatio(float ratio);
81
82 /**
83 * Get the hop size for input.
84 */
85 size_t getInputIncrement() const { return m_n1; }
86
87 /**
88 * Get the hop size for output.
89 */
90 size_t getOutputIncrement() const { return m_n2; }
91
92 /**
93 * Get the window size for FFT processing.
94 */
95 size_t getWindowSize() const { return m_wlen; }
96
97 /**
98 * Get the stretch ratio.
99 */
100 float getRatio() const { return float(m_n2) / float(m_n1); }
101
102 /**
103 * Return whether this time stretcher will attempt to sharpen transients.
104 */
105 bool getSharpening() const { return m_sharpen; }
106
107 /**
108 * Return the number of channels for this time stretcher.
109 */
110 size_t getChannelCount() const { return m_channels; }
111
112 /**
113 * Get the latency added by the time stretcher, in sample frames.
114 * This will be exact if transient sharpening is off, or approximate
115 * if it is on.
116 */
117 size_t getProcessingLatency() const;
118
119 protected:
120 /**
121 * Process a single phase vocoder frame from "in" into
122 * m_freq[channel].
123 */
124 void analyseBlock(size_t channel, float *in); // into m_freq[channel]
125
126 /**
127 * Examine m_freq[0..m_channels-1] and return whether a percussive
128 * transient is found.
129 */
130 bool isTransient();
131
132 /**
133 * Resynthesise from m_freq[channel] adding in to "out",
134 * adjusting phases on the basis of a prior step size of lastStep.
135 * Also add the window shape in to the modulation array (if
136 * present) -- for use in ensuring the output has the correct
137 * magnitude afterwards.
138 */
139 void synthesiseBlock(size_t channel, float *out, float *modulation,
140 size_t lastStep);
141
142 void initialise();
143 void calculateParameters();
144 void cleanup();
145
146 bool shouldSharpen() {
147 return m_sharpen && (m_ratio > 0.25);
148 }
149
150 size_t m_sampleRate;
151 size_t m_channels;
152 size_t m_maxOutputBlockSize;
153 float m_ratio;
154 bool m_sharpen;
155 size_t m_n1;
156 size_t m_n2;
157 size_t m_wlen;
158 Window<float> *m_analysisWindow;
159 Window<float> *m_synthesisWindow;
160
161 int m_totalCount;
162 int m_transientCount;
163 int m_n2sum;
164
165 float **m_prevPhase;
166 float **m_prevAdjustedPhase;
167
168 float *m_prevTransientMag;
169 int m_prevTransientScore;
170 int m_transientThreshold;
171 bool m_prevTransient;
172
173 float *m_tempbuf;
174 float **m_time;
175 fftf_complex **m_freq;
176 fftf_plan *m_plan;
177 fftf_plan *m_iplan;
178
179 RingBuffer<float> **m_inbuf;
180 RingBuffer<float> **m_outbuf;
181 float **m_mashbuf;
182 float *m_modulationbuf;
183
184 QMutex *m_mutex;
185 };
186
187 #endif