2007-11-06 21:41:16 +00:00
|
|
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
|
|
|
|
|
|
|
|
|
/*
|
2012-09-09 16:57:42 +01:00
|
|
|
Rubber Band Library
|
2007-11-06 21:41:16 +00:00
|
|
|
An audio time-stretching and pitch-shifting library.
|
2022-01-04 17:50:15 +00:00
|
|
|
Copyright 2007-2022 Particular Programs Ltd.
|
2012-09-09 16:57:42 +01:00
|
|
|
|
2007-11-06 21:41:16 +00:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
|
License, or (at your option) any later version. See the file
|
|
|
|
|
COPYING included with this distribution for more information.
|
2012-09-09 16:57:42 +01:00
|
|
|
|
|
|
|
|
Alternatively, if you have a valid commercial licence for the
|
|
|
|
|
Rubber Band Library obtained by agreement with the copyright
|
|
|
|
|
holders, you may redistribute and/or modify it under the terms
|
|
|
|
|
described in that licence.
|
|
|
|
|
|
|
|
|
|
If you wish to distribute code using the Rubber Band Library
|
|
|
|
|
under terms other than those of the GNU General Public License,
|
|
|
|
|
you must obtain a valid commercial licence before doing so.
|
2007-11-06 21:41:16 +00:00
|
|
|
*/
|
|
|
|
|
|
2021-02-10 11:07:33 +00:00
|
|
|
#ifndef RUBBERBAND_STRETCH_CALCULATOR_H
|
|
|
|
|
#define RUBBERBAND_STRETCH_CALCULATOR_H
|
2007-11-06 21:41:16 +00:00
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
2010-03-24 09:44:51 +00:00
|
|
|
#include <map>
|
2021-10-26 16:45:07 +01:00
|
|
|
#include <cstdint>
|
2007-11-06 21:41:16 +00:00
|
|
|
|
|
|
|
|
namespace RubberBand
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
class StretchCalculator
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
StretchCalculator(size_t sampleRate, size_t inputIncrement, bool useHardPeaks);
|
|
|
|
|
virtual ~StretchCalculator();
|
|
|
|
|
|
2010-03-24 09:44:51 +00:00
|
|
|
/**
|
|
|
|
|
* Provide a set of mappings from "before" to "after" sample
|
|
|
|
|
* numbers so as to enforce a particular stretch profile. This
|
|
|
|
|
* must be called before calculate(). The argument is a map from
|
|
|
|
|
* audio sample frame number in the source material to the
|
|
|
|
|
* corresponding sample frame number in the stretched output.
|
|
|
|
|
*/
|
|
|
|
|
void setKeyFrameMap(const std::map<size_t, size_t> &mapping);
|
|
|
|
|
|
2007-11-06 21:41:16 +00:00
|
|
|
/**
|
|
|
|
|
* Calculate phase increments for a region of audio, given the
|
|
|
|
|
* overall target stretch ratio, input duration in audio samples,
|
2022-05-18 14:12:57 +01:00
|
|
|
* and the audio curves to use for identifying phase lock points.
|
2007-11-06 21:41:16 +00:00
|
|
|
*/
|
2010-03-24 09:44:51 +00:00
|
|
|
std::vector<int> calculate(double ratio, size_t inputDuration,
|
2022-05-18 14:12:57 +01:00
|
|
|
const std::vector<float> &lockAudioCurve);
|
2007-11-06 21:41:16 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculate the phase increment for a single audio block, given
|
|
|
|
|
* the overall target stretch ratio and the block's value on the
|
|
|
|
|
* phase-lock audio curve. State is retained between calls in the
|
|
|
|
|
* StretchCalculator object; call reset() to reset it. This uses
|
|
|
|
|
* a less sophisticated method than the offline calculate().
|
2008-05-22 16:54:27 +00:00
|
|
|
*
|
|
|
|
|
* If increment is non-zero, use it for the input increment for
|
|
|
|
|
* this block in preference to m_increment.
|
2007-11-06 21:41:16 +00:00
|
|
|
*/
|
2021-05-13 18:04:43 +01:00
|
|
|
int calculateSingle(double timeRatio,
|
|
|
|
|
double effectivePitchRatio,
|
|
|
|
|
float curveValue,
|
|
|
|
|
size_t increment,
|
|
|
|
|
size_t analysisWindowSize,
|
|
|
|
|
size_t synthesisWindowSize);
|
2007-11-06 21:41:16 +00:00
|
|
|
|
2007-11-21 19:57:49 +00:00
|
|
|
void setUseHardPeaks(bool use) { m_useHardPeaks = use; }
|
|
|
|
|
|
2007-11-06 21:41:16 +00:00
|
|
|
void reset();
|
|
|
|
|
|
|
|
|
|
void setDebugLevel(int level) { m_debugLevel = level; }
|
|
|
|
|
|
|
|
|
|
struct Peak {
|
2007-11-18 21:38:18 +00:00
|
|
|
size_t chunk;
|
2007-11-06 21:41:16 +00:00
|
|
|
bool hard;
|
|
|
|
|
};
|
2010-03-24 09:44:51 +00:00
|
|
|
std::vector<Peak> getLastCalculatedPeaks() const { return m_peaks; }
|
2007-11-07 22:34:30 +00:00
|
|
|
|
|
|
|
|
std::vector<float> smoothDF(const std::vector<float> &df);
|
|
|
|
|
|
|
|
|
|
protected:
|
2007-11-06 21:41:16 +00:00
|
|
|
std::vector<Peak> findPeaks(const std::vector<float> &audioCurve);
|
|
|
|
|
|
2010-03-24 09:44:51 +00:00
|
|
|
void mapPeaks(std::vector<Peak> &peaks, std::vector<size_t> &targets,
|
|
|
|
|
size_t outputDuration, size_t totalCount);
|
|
|
|
|
|
2007-11-06 21:41:16 +00:00
|
|
|
size_t m_sampleRate;
|
|
|
|
|
size_t m_increment;
|
|
|
|
|
float m_prevDf;
|
2021-05-13 18:04:43 +01:00
|
|
|
double m_prevRatio;
|
|
|
|
|
double m_prevTimeRatio;
|
2022-05-24 12:00:54 +01:00
|
|
|
bool m_justReset;
|
2007-11-20 20:17:13 +00:00
|
|
|
int m_transientAmnesty; // only in RT mode; handled differently offline
|
2007-11-06 21:41:16 +00:00
|
|
|
int m_debugLevel;
|
|
|
|
|
bool m_useHardPeaks;
|
2021-05-13 18:04:43 +01:00
|
|
|
int64_t m_inFrameCounter;
|
|
|
|
|
std::pair<int64_t, int64_t> m_frameCheckpoint;
|
|
|
|
|
int64_t expectedOutFrame(int64_t inFrame, double timeRatio);
|
|
|
|
|
double m_outFrameCounter;
|
2010-03-24 09:44:51 +00:00
|
|
|
|
|
|
|
|
std::map<size_t, size_t> m_keyFrameMap;
|
|
|
|
|
std::vector<Peak> m_peaks;
|
2007-11-06 21:41:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|