annotate audioio/PhaseVocoderTimeStretcher.h @ 15:cc566264c935

* timestretcher improvements -- simplify API (it can calculate its own processing block sizes etc)
author Chris Cannam
date Wed, 13 Sep 2006 11:56:44 +0000
parents 085f34c73939
children 3715efc38f95
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@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@15 36 PhaseVocoderTimeStretcher(float ratio, size_t maxProcessInputBlockSize);
Chris@14 37 virtual ~PhaseVocoderTimeStretcher();
Chris@0 38
Chris@12 39 /**
Chris@12 40 * Process a block. The input array contains the given number of
Chris@15 41 * samples; the output must have space for lrintf(samples * m_ratio).
Chris@12 42 */
Chris@0 43 void process(float *input, float *output, size_t samples);
Chris@0 44
Chris@0 45 /**
Chris@15 46 * Get the hop size for input.
Chris@0 47 */
Chris@0 48 size_t getInputIncrement() const { return m_n1; }
Chris@0 49
Chris@0 50 /**
Chris@15 51 * Get the hop size for output.
Chris@15 52 */
Chris@15 53 size_t getOutputIncrement() const { return getInputIncrement() * getRatio(); }
Chris@15 54
Chris@15 55 /**
Chris@15 56 * Get the window size for FFT processing.
Chris@0 57 */
Chris@0 58 size_t getWindowSize() const { return m_wlen; }
Chris@0 59
Chris@0 60 /**
Chris@15 61 * Get the window type.
Chris@0 62 */
Chris@0 63 WindowType getWindowType() const { return m_window->getType(); }
Chris@0 64
Chris@15 65 /**
Chris@15 66 * Get the stretch ratio set in the constructor.
Chris@15 67 */
Chris@12 68 float getRatio() const { return m_ratio; }
Chris@15 69
Chris@15 70 /**
Chris@15 71 * Get the latency added by the time stretcher, in sample frames.
Chris@15 72 */
Chris@0 73 size_t getProcessingLatency() const;
Chris@0 74
Chris@0 75 protected:
Chris@13 76 /**
Chris@13 77 * Process a single phase vocoder frame.
Chris@13 78 *
Chris@13 79 * Take m_wlen time-domain source samples from in, perform an FFT,
Chris@13 80 * phase shift, and IFFT, and add the results to out (presumably
Chris@13 81 * overlapping parts of existing data from prior frames).
Chris@13 82 *
Chris@13 83 * Also add to the modulation output the results of windowing a
Chris@13 84 * set of 1s with the resynthesis window -- this can then be used
Chris@13 85 * to ensure the output has the correct magnitude in cases where
Chris@13 86 * the window overlap varies or otherwise results in something
Chris@13 87 * other than a flat sum.
Chris@13 88 */
Chris@13 89 void processBlock(float *in, float *out, float *modulation);
Chris@0 90
Chris@12 91 float m_ratio;
Chris@0 92 size_t m_n1;
Chris@0 93 size_t m_n2;
Chris@0 94 size_t m_wlen;
Chris@0 95 Window<float> *m_window;
Chris@0 96
Chris@0 97 fftwf_complex *m_time;
Chris@0 98 fftwf_complex *m_freq;
Chris@0 99 float *m_dbuf;
Chris@12 100 float *m_prevPhase;
Chris@12 101 float *m_prevAdjustedPhase;
Chris@0 102
Chris@0 103 fftwf_plan m_plan;
Chris@0 104 fftwf_plan m_iplan;
Chris@0 105
Chris@15 106 RingBuffer<float> *m_inbuf;
Chris@15 107 RingBuffer<float> *m_outbuf;
Chris@0 108 float *m_mashbuf;
Chris@13 109 float *m_modulationbuf;
Chris@0 110 };
Chris@0 111
Chris@0 112 #endif