Chris@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: Sonic Visualiser Chris@0: An audio file viewer and annotation editor. Chris@0: Centre for Digital Music, Queen Mary, University of London. Chris@77: This file copyright 2006 Chris Cannam and QMUL. Chris@0: Chris@0: This program is free software; you can redistribute it and/or Chris@0: modify it under the terms of the GNU General Public License as Chris@0: published by the Free Software Foundation; either version 2 of the Chris@0: License, or (at your option) any later version. See the file Chris@0: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@14: #ifndef _PHASE_VOCODER_TIME_STRETCHER_H_ Chris@14: #define _PHASE_VOCODER_TIME_STRETCHER_H_ Chris@0: Chris@0: #include "base/Window.h" Chris@0: #include "base/RingBuffer.h" Chris@0: Chris@0: #include Chris@0: Chris@25: #include Chris@25: Chris@0: /** Chris@14: * A time stretcher that alters the performance speed of audio, Chris@22: * preserving pitch. Chris@0: * Chris@22: * This is based on the straightforward phase vocoder with phase Chris@22: * unwrapping (as in e.g. the DAFX book pp275-), with optional Chris@22: * percussive transient detection to avoid smearing percussive notes Chris@22: * and resynchronise phases, and adding a stream API for real-time Chris@22: * use. Principles and methods from Chris Duxbury, AES 2002 and 2004 Chris@22: * thesis; Emmanuel Ravelli, DAFX 2005; Dan Barry, ISSC 2005 on Chris@22: * percussion detection; code by Chris Cannam. Chris@0: */ Chris@0: Chris@14: class PhaseVocoderTimeStretcher Chris@0: { Chris@0: public: Chris@22: PhaseVocoderTimeStretcher(size_t sampleRate, Chris@22: size_t channels, Chris@16: float ratio, Chris@16: bool sharpen, Chris@31: size_t maxOutputBlockSize); Chris@14: virtual ~PhaseVocoderTimeStretcher(); Chris@0: Chris@12: /** Chris@16: * Return the number of samples that would need to be added via Chris@16: * putInput in order to provoke the time stretcher into doing some Chris@16: * time stretching and making more output samples available. Chris@31: * This will be an estimate, if transient sharpening is on; the Chris@31: * caller may need to do the put/get/test cycle more than once. Chris@16: */ Chris@16: size_t getRequiredInputSamples() const; Chris@16: Chris@16: /** Chris@16: * Put (and possibly process) a given number of input samples. Chris@31: * Number should usually equal the value returned from Chris@31: * getRequiredInputSamples(). Chris@16: */ Chris@16: void putInput(float **input, size_t samples); Chris@16: Chris@23: /** Chris@23: * Get the number of processed samples ready for reading. Chris@23: */ Chris@16: size_t getAvailableOutputSamples() const; Chris@16: Chris@23: /** Chris@23: * Get some processed samples. Chris@23: */ Chris@16: void getOutput(float **output, size_t samples); Chris@16: Chris@16: //!!! and reset? Chris@0: Chris@0: /** Chris@25: * Change the time stretch ratio. Chris@25: */ Chris@25: void setRatio(float ratio); Chris@25: Chris@25: /** Chris@15: * Get the hop size for input. Chris@0: */ Chris@0: size_t getInputIncrement() const { return m_n1; } Chris@0: Chris@0: /** Chris@15: * Get the hop size for output. Chris@15: */ Chris@16: size_t getOutputIncrement() const { return m_n2; } Chris@15: Chris@15: /** Chris@15: * Get the window size for FFT processing. Chris@0: */ Chris@0: size_t getWindowSize() const { return m_wlen; } Chris@0: Chris@0: /** Chris@16: * Get the stretch ratio. Chris@15: */ Chris@16: float getRatio() const { return float(m_n2) / float(m_n1); } Chris@16: Chris@16: /** Chris@16: * Return whether this time stretcher will attempt to sharpen transients. Chris@16: */ Chris@16: bool getSharpening() const { return m_sharpen; } Chris@15: Chris@15: /** Chris@26: * Return the number of channels for this time stretcher. Chris@26: */ Chris@26: size_t getChannelCount() const { return m_channels; } Chris@26: Chris@26: /** Chris@15: * Get the latency added by the time stretcher, in sample frames. Chris@23: * This will be exact if transient sharpening is off, or approximate Chris@23: * if it is on. Chris@15: */ Chris@0: size_t getProcessingLatency() const; Chris@0: Chris@0: protected: Chris@13: /** Chris@23: * Process a single phase vocoder frame from "in" into Chris@23: * m_freq[channel]. Chris@13: */ Chris@23: void analyseBlock(size_t channel, float *in); // into m_freq[channel] Chris@20: Chris@23: /** Chris@23: * Examine m_freq[0..m_channels-1] and return whether a percussive Chris@23: * transient is found. Chris@23: */ Chris@23: bool isTransient(); Chris@20: Chris@23: /** Chris@23: * Resynthesise from m_freq[channel] adding in to "out", Chris@23: * adjusting phases on the basis of a prior step size of lastStep. Chris@23: * Also add the window shape in to the modulation array (if Chris@23: * present) -- for use in ensuring the output has the correct Chris@23: * magnitude afterwards. Chris@23: */ Chris@20: void synthesiseBlock(size_t channel, float *out, float *modulation, Chris@20: size_t lastStep); Chris@0: Chris@25: void initialise(); Chris@25: void calculateParameters(); Chris@25: void cleanup(); Chris@25: Chris@32: bool shouldSharpen() { Chris@32: return m_sharpen && (m_ratio > 0.25); Chris@32: } Chris@32: Chris@22: size_t m_sampleRate; Chris@16: size_t m_channels; Chris@31: size_t m_maxOutputBlockSize; Chris@12: float m_ratio; Chris@16: bool m_sharpen; Chris@0: size_t m_n1; Chris@0: size_t m_n2; Chris@0: size_t m_wlen; Chris@20: Window *m_analysisWindow; Chris@20: Window *m_synthesisWindow; Chris@0: Chris@21: int m_totalCount; Chris@21: int m_transientCount; Chris@21: int m_n2sum; Chris@21: Chris@16: float **m_prevPhase; Chris@16: float **m_prevAdjustedPhase; Chris@16: Chris@20: float *m_prevTransientMag; Chris@21: int m_prevTransientScore; Chris@22: int m_transientThreshold; Chris@20: bool m_prevTransient; Chris@20: Chris@20: float *m_tempbuf; Chris@20: float **m_time; Chris@20: fftwf_complex **m_freq; Chris@20: fftwf_plan *m_plan; Chris@20: fftwf_plan *m_iplan; Chris@0: Chris@16: RingBuffer **m_inbuf; Chris@16: RingBuffer **m_outbuf; Chris@16: float **m_mashbuf; Chris@13: float *m_modulationbuf; Chris@25: Chris@25: QMutex *m_mutex; Chris@0: }; Chris@0: Chris@0: #endif