Files
librubberband/src/finer/R3StretcherImpl.h

226 lines
7.6 KiB
C
Raw Normal View History

2022-05-18 17:51:20 +01:00
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Rubber Band Library
An audio time-stretching and pitch-shifting library.
Copyright 2007-2022 Particular Programs Ltd.
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.
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.
*/
#ifndef RUBBERBAND_R3_STRETCHERIMPL_H
#define RUBBERBAND_R3_STRETCHERIMPL_H
#include "BinSegmenter.h"
2022-05-19 16:31:21 +01:00
#include "Guide.h"
#include "Peak.h"
2022-05-20 15:29:52 +01:00
#include "PhaseAdvance.h"
2022-05-18 17:51:20 +01:00
2022-05-23 17:59:40 +01:00
#include "../common/StretchCalculator.h"
2022-05-24 17:49:50 +01:00
#include "../common/Resampler.h"
#include "../common/FFT.h"
2022-05-20 15:29:52 +01:00
#include "../common/FixedVector.h"
#include "../common/Allocators.h"
2022-05-20 15:29:52 +01:00
#include "../common/Window.h"
2022-05-19 13:13:47 +01:00
2022-05-20 16:48:44 +01:00
#include <map>
#include <memory>
#include <functional>
2022-05-18 17:51:20 +01:00
namespace RubberBand
{
class R3StretcherImpl
{
public:
2022-05-20 16:48:44 +01:00
struct Parameters {
double sampleRate;
int channels;
std::function<void(const std::string &)> logger;
Parameters(double _sampleRate, int _channels,
std::function<void(const std::string &)> _log = &logCout) :
2022-05-20 16:48:44 +01:00
sampleRate(_sampleRate), channels(_channels), logger(_log) { }
};
2022-05-23 17:59:40 +01:00
R3StretcherImpl(Parameters parameters,
double initialTimeRatio,
2022-05-25 13:47:40 +01:00
double initialPitchScale);
2022-05-20 16:48:44 +01:00
~R3StretcherImpl() { }
2022-05-18 17:51:20 +01:00
void reset();
void setTimeRatio(double ratio);
void setPitchScale(double scale);
double getTimeRatio() const;
double getPitchScale() const;
2022-05-23 15:04:34 +01:00
size_t getSamplesRequired() const;
void process(const float *const *input, size_t samples, bool final);
int available() const;
size_t retrieve(float *const *output, size_t samples) const;
size_t getLatency() const;
size_t getChannelCount() const;
2022-05-18 17:51:20 +01:00
protected:
2022-05-25 11:14:19 +01:00
struct ClassificationReadaheadData {
FixedVector<double> timeDomain;
FixedVector<double> mag;
FixedVector<double> phase;
ClassificationReadaheadData(int _fftSize) :
timeDomain(_fftSize, 0.f),
mag(_fftSize/2 + 1, 0.f),
phase(_fftSize/2 + 1, 0.f)
{ }
private:
ClassificationReadaheadData(const ClassificationReadaheadData &) =delete;
ClassificationReadaheadData &operator=(const ClassificationReadaheadData &) =delete;
};
2022-05-18 17:51:20 +01:00
struct ChannelScaleData {
int fftSize;
2022-05-20 15:29:52 +01:00
int bufSize; // size of every freq-domain array here: fftSize/2 + 1
FixedVector<double> timeDomain;
FixedVector<double> real;
FixedVector<double> imag;
FixedVector<double> mag;
FixedVector<double> phase;
2022-05-25 11:14:19 +01:00
FixedVector<double> advancedPhase;
2022-05-25 11:16:06 +01:00
FixedVector<int> troughs;
FixedVector<double> prevMag;
FixedVector<double> accumulator;
2022-05-18 17:51:20 +01:00
ChannelScaleData(int _fftSize, int _longestFftSize) :
2022-05-23 15:04:34 +01:00
fftSize(_fftSize),
bufSize(fftSize/2 + 1),
timeDomain(fftSize, 0.f),
real(bufSize, 0.f),
imag(bufSize, 0.f),
2022-05-23 15:04:34 +01:00
mag(bufSize, 0.f),
phase(bufSize, 0.f),
2022-05-25 11:14:19 +01:00
advancedPhase(bufSize, 0.f),
troughs(bufSize, 0),
2022-05-23 15:04:34 +01:00
prevMag(bufSize, 0.f),
accumulator(_longestFftSize, 0.f)
2022-05-20 15:29:52 +01:00
{ }
2022-05-18 17:51:20 +01:00
private:
ChannelScaleData(const ChannelScaleData &) =delete;
ChannelScaleData &operator=(const ChannelScaleData &) =delete;
};
2022-05-19 17:28:38 +01:00
2022-05-18 17:51:20 +01:00
struct ChannelData {
std::map<int, std::shared_ptr<ChannelScaleData>> scales;
2022-05-25 11:14:19 +01:00
ClassificationReadaheadData readahead;
2022-05-19 09:16:13 +01:00
std::unique_ptr<BinSegmenter> segmenter;
BinSegmenter::Segmentation segmentation;
BinSegmenter::Segmentation prevSegmentation;
BinSegmenter::Segmentation nextSegmentation;
2022-05-19 16:31:21 +01:00
Guide::Guidance guidance;
2022-05-25 13:47:40 +01:00
FixedVector<float> mixdown;
FixedVector<float> resampled;
2022-05-23 15:04:34 +01:00
std::unique_ptr<RingBuffer<float>> inbuf;
std::unique_ptr<RingBuffer<float>> outbuf;
2022-05-20 16:48:44 +01:00
ChannelData(BinSegmenter::Parameters segmenterParameters,
BinClassifier::Parameters classifierParameters,
2022-05-25 13:47:40 +01:00
int longestFftSize,
int inRingBufferSize,
int outRingBufferSize) :
2022-05-20 16:48:44 +01:00
scales(),
2022-05-25 11:14:19 +01:00
readahead(segmenterParameters.fftSize),
2022-05-20 16:48:44 +01:00
segmenter(new BinSegmenter(segmenterParameters,
classifierParameters)),
segmentation(), prevSegmentation(), nextSegmentation(),
2022-05-25 13:47:40 +01:00
mixdown(longestFftSize, 0.f), // though it could be shorter
resampled(outRingBufferSize, 0.f),
inbuf(new RingBuffer<float>(inRingBufferSize)),
outbuf(new RingBuffer<float>(outRingBufferSize)) { }
2022-05-23 15:04:34 +01:00
};
struct ChannelAssembly {
// Vectors of bare pointers, used to package container data
// from different channels into arguments for PhaseAdvance
FixedVector<double *> mag;
FixedVector<double *> phase;
2022-05-23 15:04:34 +01:00
FixedVector<Guide::Guidance *> guidance;
FixedVector<double *> outPhase;
2022-05-25 13:47:40 +01:00
FixedVector<float *> mixdown;
FixedVector<float *> resampled;
2022-05-23 15:04:34 +01:00
ChannelAssembly(int channels) :
mag(channels, nullptr), phase(channels, nullptr),
2022-05-25 13:47:40 +01:00
guidance(channels, nullptr), outPhase(channels, nullptr),
mixdown(channels, nullptr), resampled(channels, nullptr) { }
2022-05-18 17:51:20 +01:00
};
2022-05-20 15:29:52 +01:00
2022-05-23 15:04:34 +01:00
struct ScaleData {
2022-05-25 14:43:05 +01:00
int fftSize;
2022-05-23 15:04:34 +01:00
FFT fft;
Window<double> analysisWindow;
Window<double> synthesisWindow;
2022-05-23 15:04:34 +01:00
GuidedPhaseAdvance guided;
ScaleData(GuidedPhaseAdvance::Parameters guidedParameters) :
2022-05-25 14:43:05 +01:00
fftSize(guidedParameters.fftSize),
fft(fftSize),
analysisWindow(analysisWindowShape(fftSize),
analysisWindowLength(fftSize)),
synthesisWindow(synthesisWindowShape(fftSize),
synthesisWindowLength(fftSize)),
2022-05-23 15:04:34 +01:00
guided(guidedParameters) { }
2022-05-25 14:43:05 +01:00
WindowType analysisWindowShape(int fftSize);
int analysisWindowLength(int fftSize);
WindowType synthesisWindowShape(int fftSize);
int synthesisWindowLength(int fftSize);
2022-05-23 15:04:34 +01:00
};
2022-05-20 16:48:44 +01:00
Parameters m_parameters;
2022-05-20 15:29:52 +01:00
std::atomic<double> m_timeRatio;
std::atomic<double> m_pitchScale;
2022-05-23 17:59:40 +01:00
2022-05-20 16:48:44 +01:00
std::vector<std::shared_ptr<ChannelData>> m_channelData;
2022-05-23 15:04:34 +01:00
std::map<int, std::shared_ptr<ScaleData>> m_scaleData;
2022-05-19 17:28:38 +01:00
Guide m_guide;
Guide::Configuration m_guideConfiguration;
2022-05-23 15:04:34 +01:00
ChannelAssembly m_channelAssembly;
Peak<double, std::less<double>> m_troughPicker;
2022-05-23 17:59:40 +01:00
std::unique_ptr<StretchCalculator> m_calculator;
2022-05-24 17:49:50 +01:00
std::unique_ptr<Resampler> m_resampler;
std::atomic<int> m_inhop;
int m_prevInhop;
int m_prevOuthop;
bool m_draining;
2022-05-20 16:48:44 +01:00
2022-05-23 15:04:34 +01:00
void consume();
2022-05-23 17:59:40 +01:00
void calculateHop();
void analyseChannel(int channel, int inhop, int prevInhop, int prevOuthop);
void synthesiseChannel(int channel, int outhop);
2022-05-23 17:59:40 +01:00
double getEffectiveRatio() const {
return m_timeRatio * m_pitchScale;
}
2022-05-23 15:04:34 +01:00
static void logCout(const std::string &message) {
std::cout << "RubberBandStretcher: " << message << std::endl;
2022-05-20 16:48:44 +01:00
}
2022-05-18 17:51:20 +01:00
};
}
#endif