annotate audioio/PhaseVocoderTimeStretcher.h @ 23:a2ad974b0c8c

...
author Chris Cannam
date Fri, 15 Sep 2006 13:46:45 +0000
parents 80126455d169
children e74f508db18c
rev   line source
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@22 26 * preserving pitch.
Chris@0 27 *
Chris@22 28 * This is based on the straightforward phase vocoder with phase
Chris@22 29 * unwrapping (as in e.g. the DAFX book pp275-), with optional
Chris@22 30 * percussive transient detection to avoid smearing percussive notes
Chris@22 31 * and resynchronise phases, and adding a stream API for real-time
Chris@22 32 * use. Principles and methods from Chris Duxbury, AES 2002 and 2004
Chris@22 33 * thesis; Emmanuel Ravelli, DAFX 2005; Dan Barry, ISSC 2005 on
Chris@22 34 * percussion detection; code by Chris Cannam.
Chris@0 35 */
Chris@0 36
Chris@14 37 class PhaseVocoderTimeStretcher
Chris@0 38 {
Chris@0 39 public:
Chris@22 40 PhaseVocoderTimeStretcher(size_t sampleRate,
Chris@22 41 size_t channels,
Chris@16 42 float ratio,
Chris@16 43 bool sharpen,
Chris@16 44 size_t maxProcessInputBlockSize);
Chris@14 45 virtual ~PhaseVocoderTimeStretcher();
Chris@0 46
Chris@12 47 /**
Chris@12 48 * Process a block. The input array contains the given number of
Chris@16 49 * samples (on each channel); the output must have space for
Chris@16 50 * lrintf(samples * m_ratio).
Chris@16 51 *
Chris@23 52 * This function isn't really recommended, and I may yet remove it.
Chris@23 53 * It should work correctly for some ratios, e.g. small powers of
Chris@23 54 * two, if transient sharpening is off. For other ratios it may
Chris@23 55 * drop samples -- use putInput in a loop followed by getOutput
Chris@23 56 * (when getAvailableOutputSamples reports enough) instead.
Chris@16 57 *
Chris@16 58 * Do not mix process calls with putInput/getOutput calls.
Chris@12 59 */
Chris@16 60 void process(float **input, float **output, size_t samples);
Chris@16 61
Chris@16 62 /**
Chris@16 63 * Return the number of samples that would need to be added via
Chris@16 64 * putInput in order to provoke the time stretcher into doing some
Chris@16 65 * time stretching and making more output samples available.
Chris@23 66 * This will be an estimate, if transient sharpening is on.
Chris@16 67 */
Chris@16 68 size_t getRequiredInputSamples() const;
Chris@16 69
Chris@16 70 /**
Chris@16 71 * Put (and possibly process) a given number of input samples.
Chris@16 72 * Number must not exceed the maxProcessInputBlockSize passed to
Chris@16 73 * constructor.
Chris@16 74 */
Chris@16 75 void putInput(float **input, size_t samples);
Chris@16 76
Chris@23 77 /**
Chris@23 78 * Get the number of processed samples ready for reading.
Chris@23 79 */
Chris@16 80 size_t getAvailableOutputSamples() const;
Chris@16 81
Chris@23 82 /**
Chris@23 83 * Get some processed samples.
Chris@23 84 */
Chris@16 85 void getOutput(float **output, size_t samples);
Chris@16 86
Chris@16 87 //!!! and reset?
Chris@0 88
Chris@0 89 /**
Chris@15 90 * Get the hop size for input.
Chris@0 91 */
Chris@0 92 size_t getInputIncrement() const { return m_n1; }
Chris@0 93
Chris@0 94 /**
Chris@15 95 * Get the hop size for output.
Chris@15 96 */
Chris@16 97 size_t getOutputIncrement() const { return m_n2; }
Chris@15 98
Chris@15 99 /**
Chris@15 100 * Get the window size for FFT processing.
Chris@0 101 */
Chris@0 102 size_t getWindowSize() const { return m_wlen; }
Chris@0 103
Chris@0 104 /**
Chris@16 105 * Get the stretch ratio.
Chris@15 106 */
Chris@16 107 float getRatio() const { return float(m_n2) / float(m_n1); }
Chris@16 108
Chris@16 109 /**
Chris@16 110 * Return whether this time stretcher will attempt to sharpen transients.
Chris@16 111 */
Chris@16 112 bool getSharpening() const { return m_sharpen; }
Chris@15 113
Chris@15 114 /**
Chris@15 115 * Get the latency added by the time stretcher, in sample frames.
Chris@23 116 * This will be exact if transient sharpening is off, or approximate
Chris@23 117 * if it is on.
Chris@15 118 */
Chris@0 119 size_t getProcessingLatency() const;
Chris@0 120
Chris@0 121 protected:
Chris@13 122 /**
Chris@23 123 * Process a single phase vocoder frame from "in" into
Chris@23 124 * m_freq[channel].
Chris@13 125 */
Chris@23 126 void analyseBlock(size_t channel, float *in); // into m_freq[channel]
Chris@20 127
Chris@23 128 /**
Chris@23 129 * Examine m_freq[0..m_channels-1] and return whether a percussive
Chris@23 130 * transient is found.
Chris@23 131 */
Chris@23 132 bool isTransient();
Chris@20 133
Chris@23 134 /**
Chris@23 135 * Resynthesise from m_freq[channel] adding in to "out",
Chris@23 136 * adjusting phases on the basis of a prior step size of lastStep.
Chris@23 137 * Also add the window shape in to the modulation array (if
Chris@23 138 * present) -- for use in ensuring the output has the correct
Chris@23 139 * magnitude afterwards.
Chris@23 140 */
Chris@20 141 void synthesiseBlock(size_t channel, float *out, float *modulation,
Chris@20 142 size_t lastStep);
Chris@0 143
Chris@22 144 size_t m_sampleRate;
Chris@16 145 size_t m_channels;
Chris@12 146 float m_ratio;
Chris@16 147 bool m_sharpen;
Chris@0 148 size_t m_n1;
Chris@0 149 size_t m_n2;
Chris@0 150 size_t m_wlen;
Chris@20 151 Window<float> *m_analysisWindow;
Chris@20 152 Window<float> *m_synthesisWindow;
Chris@0 153
Chris@21 154 int m_totalCount;
Chris@21 155 int m_transientCount;
Chris@21 156 int m_n2sum;
Chris@21 157
Chris@16 158 float **m_prevPhase;
Chris@16 159 float **m_prevAdjustedPhase;
Chris@16 160
Chris@20 161 float *m_prevTransientMag;
Chris@21 162 int m_prevTransientScore;
Chris@22 163 int m_transientThreshold;
Chris@20 164 bool m_prevTransient;
Chris@20 165
Chris@20 166 float *m_tempbuf;
Chris@20 167 float **m_time;
Chris@20 168 fftwf_complex **m_freq;
Chris@20 169 fftwf_plan *m_plan;
Chris@20 170 fftwf_plan *m_iplan;
Chris@0 171
Chris@16 172 RingBuffer<float> **m_inbuf;
Chris@16 173 RingBuffer<float> **m_outbuf;
Chris@16 174 float **m_mashbuf;
Chris@13 175 float *m_modulationbuf;
Chris@0 176 };
Chris@0 177
Chris@0 178 #endif