2022-05-23 15:04:34 +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 .
2024-03-07 15:19:37 +00:00
Copyright 2007 - 2024 Particular Programs Ltd .
2022-05-23 15:04:34 +01: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 .
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 .
*/
2022-06-21 10:25:08 +01:00
# include "R3Stretcher.h"
2022-05-23 15:04:34 +01:00
2022-05-24 17:35:23 +01:00
# include "../common/VectorOpsComplex.h"
2022-07-14 10:02:39 +01:00
# include "../common/Profiler.h"
2022-05-24 16:54:05 +01:00
2022-05-23 15:04:34 +01:00
# include <array>
namespace RubberBand {
2022-06-21 10:25:08 +01:00
R3Stretcher : : R3Stretcher ( Parameters parameters ,
double initialTimeRatio ,
2022-06-21 16:06:16 +01:00
double initialPitchScale ,
Log log ) :
m_log ( log ) ,
2022-09-26 16:02:13 +01:00
m_parameters ( validateSampleRate ( parameters ) ) ,
m_limits ( parameters . options , m_parameters . sampleRate ) ,
2022-05-25 13:47:40 +01:00
m_timeRatio ( initialTimeRatio ) ,
m_pitchScale ( initialPitchScale ) ,
2022-06-13 10:39:13 +01:00
m_formantScale ( 0.0 ) ,
2022-07-14 11:55:21 +01:00
m_guide ( Guide : : Parameters
( m_parameters . sampleRate ,
m_parameters . options & RubberBandStretcher : : OptionWindowShort ) ,
m_log ) ,
2022-05-25 13:47:40 +01:00
m_guideConfiguration ( m_guide . getConfiguration ( ) ) ,
m_channelAssembly ( m_parameters . channels ) ,
2022-08-05 14:58:12 +01:00
m_useReadahead ( true ) ,
2022-05-25 13:47:40 +01:00
m_inhop ( 1 ) ,
2022-06-17 15:01:26 +01:00
m_prevInhop ( 1 ) ,
2022-05-25 13:47:40 +01:00
m_prevOuthop ( 1 ) ,
2022-06-17 15:01:26 +01:00
m_unityCount ( 0 ) ,
2022-06-10 20:26:37 +01:00
m_startSkip ( 0 ) ,
2022-06-13 16:06:21 +01:00
m_studyInputDuration ( 0 ) ,
2022-07-07 10:54:23 +01:00
m_suppliedInputDuration ( 0 ) ,
2022-06-13 16:06:21 +01:00
m_totalTargetDuration ( 0 ) ,
2022-07-07 09:43:17 +01:00
m_consumedInputDuration ( 0 ) ,
2022-06-17 17:52:28 +01:00
m_lastKeyFrameSurpassed ( 0 ) ,
2022-06-13 16:06:21 +01:00
m_totalOutputDuration ( 0 ) ,
m_mode ( ProcessMode : : JustCreated )
2022-05-25 13:47:40 +01:00
{
2022-07-14 10:02:39 +01:00
Profiler profiler ( " R3Stretcher::R3Stretcher " ) ;
2023-03-23 10:25:18 +00:00
initialise ( ) ;
}
void
R3Stretcher : : initialise ( )
{
2022-06-22 13:42:58 +01:00
m_log . log ( 1 , " R3Stretcher::R3Stretcher: rate, options " ,
m_parameters . sampleRate , m_parameters . options ) ;
m_log . log ( 1 , " R3Stretcher::R3Stretcher: initial time ratio and pitch scale " ,
m_timeRatio , m_pitchScale ) ;
2022-07-14 11:55:21 +01:00
if ( isRealTime ( ) ) {
m_log . log ( 1 , " R3Stretcher::R3Stretcher: real-time mode " ) ;
} else {
m_log . log ( 1 , " R3Stretcher::R3Stretcher: offline mode " ) ;
}
2022-07-14 13:44:22 +01:00
if ( isSingleWindowed ( ) ) {
2022-07-14 11:55:21 +01:00
m_log . log ( 1 , " R3Stretcher::R3Stretcher: intermediate shorter-window mode requested " ) ;
}
2022-06-07 11:05:50 +01:00
double maxClassifierFrequency = 16000.0 ;
if ( maxClassifierFrequency > m_parameters . sampleRate / 2 ) {
maxClassifierFrequency = m_parameters . sampleRate / 2 ;
}
int classificationBins =
int ( floor ( m_guideConfiguration . classificationFftSize *
maxClassifierFrequency / m_parameters . sampleRate ) ) ;
2022-05-25 13:47:40 +01:00
BinSegmenter : : Parameters segmenterParameters
( m_guideConfiguration . classificationFftSize ,
2022-06-14 13:59:17 +01:00
classificationBins , m_parameters . sampleRate , 18 ) ;
2022-06-07 11:05:50 +01:00
2022-05-25 13:47:40 +01:00
BinClassifier : : Parameters classifierParameters
2022-06-14 13:59:17 +01:00
( classificationBins , 9 , 1 , 10 , 2.0 , 2.0 ) ;
2022-05-25 13:47:40 +01:00
2022-08-18 15:24:24 +01:00
if ( isSingleWindowed ( ) ) {
classifierParameters . horizontalFilterLength = 7 ;
}
2022-09-30 10:36:29 +01:00
int inRingBufferSize = getWindowSourceSize ( ) * 16 ;
2022-08-04 10:31:36 +01:00
int outRingBufferSize = getWindowSourceSize ( ) * 16 ;
2022-05-25 13:47:40 +01:00
2023-06-01 10:51:20 +01:00
int hopBufferSize =
2 * std : : max ( m_limits . maxInhop , m_limits . maxPreferredOuthop ) ;
2023-03-23 10:25:18 +00:00
m_channelData . clear ( ) ;
2022-05-25 13:47:40 +01:00
for ( int c = 0 ; c < m_parameters . channels ; + + c ) {
m_channelData . push_back ( std : : make_shared < ChannelData >
( segmenterParameters ,
classifierParameters ,
2022-08-04 10:31:36 +01:00
getWindowSourceSize ( ) ,
2022-05-25 13:47:40 +01:00
inRingBufferSize ,
2023-06-01 10:51:20 +01:00
outRingBufferSize ,
hopBufferSize ) ) ;
2022-08-03 14:16:17 +01:00
for ( int b = 0 ; b < m_guideConfiguration . fftBandLimitCount ; + + b ) {
const auto & band = m_guideConfiguration . fftBandLimits [ b ] ;
2022-05-25 13:47:40 +01:00
int fftSize = band . fftSize ;
m_channelData [ c ] - > scales [ fftSize ] =
std : : make_shared < ChannelScaleData >
2022-08-04 10:02:09 +01:00
( fftSize , m_guideConfiguration . longestFftSize ) ;
2022-05-25 13:47:40 +01:00
}
}
2023-03-23 10:25:18 +00:00
m_scaleData . clear ( ) ;
2022-06-21 16:06:16 +01:00
2022-08-03 14:16:17 +01:00
for ( int b = 0 ; b < m_guideConfiguration . fftBandLimitCount ; + + b ) {
const auto & band = m_guideConfiguration . fftBandLimits [ b ] ;
2022-05-25 13:47:40 +01:00
int fftSize = band . fftSize ;
GuidedPhaseAdvance : : Parameters guidedParameters
2022-07-14 11:55:21 +01:00
( fftSize , m_parameters . sampleRate , m_parameters . channels ,
2022-07-14 13:44:22 +01:00
isSingleWindowed ( ) ) ;
2022-06-21 17:03:24 +01:00
m_scaleData [ fftSize ] = std : : make_shared < ScaleData >
( guidedParameters , m_log ) ;
2022-05-25 13:47:40 +01:00
}
m_calculator = std : : unique_ptr < StretchCalculator >
( new StretchCalculator ( int ( round ( m_parameters . sampleRate ) ) , //!!! which is a double...
2022-06-21 16:06:16 +01:00
1 , false , // no fixed inputIncrement
m_log ) ) ;
2022-05-25 13:47:40 +01:00
2022-06-13 16:06:21 +01:00
if ( isRealTime ( ) ) {
2022-06-24 10:51:40 +01:00
createResampler ( ) ;
// In offline mode we don't create the resampler yet - we
// don't want to have one at all if the pitch ratio is 1.0,
// but that could change before the first process call, so we
// create the resampler if needed then
2022-06-07 09:06:52 +01:00
}
2022-05-27 14:58:42 +01:00
2022-05-25 13:47:40 +01:00
calculateHop ( ) ;
2022-05-25 13:51:23 +01:00
2022-05-25 14:10:41 +01:00
if ( ! m_inhop . is_lock_free ( ) ) {
2022-09-26 16:02:13 +01:00
m_log . log ( 0 , " R3Stretcher: WARNING: std::atomic<int> is not lock-free " ) ;
2022-05-25 14:10:41 +01:00
}
if ( ! m_timeRatio . is_lock_free ( ) ) {
2022-09-26 16:02:13 +01:00
m_log . log ( 0 , " R3Stretcher: WARNING: std::atomic<double> is not lock-free " ) ;
2022-05-25 14:10:41 +01:00
}
2022-05-25 13:47:40 +01:00
}
2022-05-26 15:08:07 +01:00
WindowType
2022-07-14 11:55:21 +01:00
R3Stretcher : : ScaleData : : analysisWindowShape ( )
2022-05-26 15:08:07 +01:00
{
2022-07-14 13:44:22 +01:00
if ( singleWindowMode ) {
return HannWindow ;
2022-07-14 11:55:21 +01:00
} else {
2022-09-26 16:02:13 +01:00
if ( fftSize < 1024 | | fftSize > 2048 ) return HannWindow ;
2022-07-14 11:55:21 +01:00
else return NiemitaloForwardWindow ;
}
2022-05-26 15:08:07 +01:00
}
int
2022-07-14 11:55:21 +01:00
R3Stretcher : : ScaleData : : analysisWindowLength ( )
2022-05-26 15:08:07 +01:00
{
return fftSize ;
}
WindowType
2022-07-14 11:55:21 +01:00
R3Stretcher : : ScaleData : : synthesisWindowShape ( )
2022-05-26 15:08:07 +01:00
{
2022-07-14 13:44:22 +01:00
if ( singleWindowMode ) {
return HannWindow ;
2022-07-14 11:55:21 +01:00
} else {
2022-09-26 16:02:13 +01:00
if ( fftSize < 1024 | | fftSize > 2048 ) return HannWindow ;
2022-07-14 11:55:21 +01:00
else return NiemitaloReverseWindow ;
}
2022-05-26 15:08:07 +01:00
}
int
2022-07-14 11:55:21 +01:00
R3Stretcher : : ScaleData : : synthesisWindowLength ( )
2022-05-26 15:08:07 +01:00
{
2022-07-14 13:44:22 +01:00
if ( singleWindowMode ) {
2022-07-14 11:55:21 +01:00
return fftSize ;
} else {
if ( fftSize > 2048 ) return fftSize / 2 ;
else return fftSize ;
}
2022-05-26 15:08:07 +01:00
}
2022-05-23 15:04:34 +01:00
void
2022-06-21 10:25:08 +01:00
R3Stretcher : : setTimeRatio ( double ratio )
2022-05-23 15:04:34 +01:00
{
2022-06-13 16:06:21 +01:00
if ( ! isRealTime ( ) ) {
if ( m_mode = = ProcessMode : : Studying | |
m_mode = = ProcessMode : : Processing ) {
2022-06-22 09:10:02 +01:00
m_log . log ( 0 , " R3Stretcher::setTimeRatio: Cannot set time ratio while studying or processing in non-RT mode " ) ;
2022-06-13 16:06:21 +01:00
return ;
}
}
if ( ratio = = m_timeRatio ) return ;
2022-05-23 15:04:34 +01:00
m_timeRatio = ratio ;
2022-05-23 17:59:40 +01:00
calculateHop ( ) ;
2022-05-23 15:04:34 +01:00
}
void
2022-06-21 10:25:08 +01:00
R3Stretcher : : setPitchScale ( double scale )
2022-05-23 15:04:34 +01:00
{
2022-06-13 16:06:21 +01:00
if ( ! isRealTime ( ) ) {
if ( m_mode = = ProcessMode : : Studying | |
m_mode = = ProcessMode : : Processing ) {
2023-03-08 14:44:52 +00:00
m_log . log ( 0 , " R3Stretcher::setPitchScale: Cannot set pitch scale while studying or processing in non-RT mode " ) ;
2022-06-13 16:06:21 +01:00
return ;
}
}
if ( scale = = m_pitchScale ) return ;
2022-05-23 15:04:34 +01:00
m_pitchScale = scale ;
2022-05-23 17:59:40 +01:00
calculateHop ( ) ;
}
2022-06-13 10:39:13 +01:00
void
2022-06-21 10:25:08 +01:00
R3Stretcher : : setFormantScale ( double scale )
2022-06-13 10:39:13 +01:00
{
2022-06-13 16:06:21 +01:00
if ( ! isRealTime ( ) ) {
if ( m_mode = = ProcessMode : : Studying | |
m_mode = = ProcessMode : : Processing ) {
2023-03-08 14:44:52 +00:00
m_log . log ( 0 , " R3Stretcher::setFormantScale: Cannot set formant scale while studying or processing in non-RT mode " ) ;
2022-06-13 16:06:21 +01:00
return ;
}
}
2022-06-13 10:39:13 +01:00
m_formantScale = scale ;
}
2022-05-27 14:58:42 +01:00
void
2022-06-21 10:25:08 +01:00
R3Stretcher : : setFormantOption ( RubberBandStretcher : : Options options )
2022-05-27 14:58:42 +01:00
{
int mask = ( RubberBandStretcher : : OptionFormantShifted |
RubberBandStretcher : : OptionFormantPreserved ) ;
m_parameters . options & = ~ mask ;
options & = mask ;
m_parameters . options | = options ;
}
2022-07-06 10:22:50 +01:00
void
R3Stretcher : : setPitchOption ( RubberBandStretcher : : Options )
{
m_log . log ( 0 , " R3Stretcher::setPitchOption: Option change after construction is not supported in R3 engine " ) ;
}
2022-05-27 14:58:42 +01:00
void
2022-06-21 10:25:08 +01:00
R3Stretcher : : setKeyFrameMap ( const std : : map < size_t , size_t > & mapping )
2022-05-27 14:58:42 +01:00
{
2022-06-13 16:06:21 +01:00
if ( isRealTime ( ) ) {
2022-06-22 09:10:02 +01:00
m_log . log ( 0 , " R3Stretcher::setKeyFrameMap: Cannot specify key frame map in RT mode " ) ;
2022-06-13 16:06:21 +01:00
return ;
}
if ( m_mode = = ProcessMode : : Processing | | m_mode = = ProcessMode : : Finished ) {
2022-06-22 09:10:02 +01:00
m_log . log ( 0 , " R3Stretcher::setKeyFrameMap: Cannot specify key frame map after process() has begun " ) ;
2022-06-13 16:06:21 +01:00
return ;
}
m_keyFrameMap = mapping ;
2022-05-27 14:58:42 +01:00
}
2022-06-24 10:51:40 +01:00
void
R3Stretcher : : createResampler ( )
{
2022-07-14 10:02:39 +01:00
Profiler profiler ( " R3Stretcher::createResampler " ) ;
2022-06-24 10:51:40 +01:00
Resampler : : Parameters resamplerParameters ;
2022-09-29 11:19:21 +01:00
resamplerParameters . quality = Resampler : : FastestTolerable ;
2022-06-24 10:51:40 +01:00
resamplerParameters . initialSampleRate = m_parameters . sampleRate ;
2022-08-04 10:02:09 +01:00
resamplerParameters . maxBufferSize = m_guideConfiguration . longestFftSize ;
2022-06-24 10:51:40 +01:00
if ( isRealTime ( ) ) {
2022-08-02 16:22:24 +01:00
// If we knew the caller would never change ratio, we could
// supply RatioMostlyFixed - but it can have such overhead
2022-08-04 12:08:15 +01:00
// when the ratio *does* change (and it's not RT-safe overhead
// either) that a single call would kill RT use
2022-08-02 16:22:24 +01:00
resamplerParameters . dynamism = Resampler : : RatioOftenChanging ;
resamplerParameters . ratioChange = Resampler : : SmoothRatioChange ;
2022-06-24 10:51:40 +01:00
} else {
resamplerParameters . dynamism = Resampler : : RatioMostlyFixed ;
resamplerParameters . ratioChange = Resampler : : SuddenRatioChange ;
}
2024-02-23 13:21:28 +00:00
int debug = m_log . getDebugLevel ( ) ;
if ( debug > 0 ) - - debug ;
resamplerParameters . debugLevel = debug ;
2022-06-24 10:51:40 +01:00
m_resampler = std : : unique_ptr < Resampler >
( new Resampler ( resamplerParameters , m_parameters . channels ) ) ;
2022-09-02 14:49:34 +01:00
bool before , after ;
areWeResampling ( & before , & after ) ;
if ( before ) {
if ( after ) {
2022-09-26 16:02:13 +01:00
m_log . log ( 0 , " R3Stretcher: WARNING: we think we are resampling both before and after! " ) ;
2022-09-02 14:49:34 +01:00
} else {
m_log . log ( 1 , " createResampler: resampling before " ) ;
}
} else {
if ( after ) {
2022-09-02 15:08:29 +01:00
m_log . log ( 1 , " createResampler: resampling after " ) ;
2022-09-02 14:49:34 +01:00
}
}
2022-06-24 10:51:40 +01:00
}
2022-05-23 17:59:40 +01:00
void
2022-06-21 10:25:08 +01:00
R3Stretcher : : calculateHop ( )
2022-05-23 17:59:40 +01:00
{
2022-09-30 10:36:29 +01:00
if ( m_pitchScale < = 0.0 ) {
// This special case is likelier than one might hope, because
// of naive initialisations in programs that set it from a
// variable
m_log . log ( 0 , " WARNING: Pitch scale must be greater than zero! Resetting it to default, no pitch shift will happen " , m_pitchScale ) ;
m_pitchScale = 1.0 ;
}
if ( m_timeRatio < = 0.0 ) {
// Likewise
m_log . log ( 0 , " WARNING: Time ratio must be greater than zero! Resetting it to default, no time stretch will happen " , m_timeRatio ) ;
m_timeRatio = 1.0 ;
}
if ( m_pitchScale ! = m_pitchScale | | m_timeRatio ! = m_timeRatio | |
m_pitchScale = = m_pitchScale / 2.0 | | m_timeRatio = = m_timeRatio / 2.0 ) {
m_log . log ( 0 , " WARNING: NaN or Inf presented for time ratio or pitch scale! Resetting it to default, no time stretch will happen " , m_timeRatio , m_pitchScale ) ;
m_timeRatio = 1.0 ;
m_pitchScale = 1.0 ;
}
2022-05-23 17:59:40 +01:00
double ratio = getEffectiveRatio ( ) ;
2022-06-14 15:01:44 +01:00
// In R2 we generally targeted a certain inhop, and calculated
// outhop from that. Here we are the other way around. We aim for
// outhop = 256 at ratios around 1, reducing down to 128 for
// ratios far below 1 and up to 512 for ratios far above. As soon
// as outhop exceeds 256 we have to drop the 1024-bin FFT, as the
2022-06-15 09:40:09 +01:00
// overlap will be inadequate for it (that's among the jobs of the
// Guide class) so we don't want to go above 256 until at least
// factor 1.5. Also we can't go above 512 without changing the
// window shape or dropping the 2048-bin FFT, and we can't do
// either of those dynamically.
double proposedOuthop = 256.0 ;
if ( ratio > 1.5 ) {
proposedOuthop = pow ( 2.0 , 8.0 + 2.0 * log10 ( ratio - 0.5 ) ) ;
} else if ( ratio < 1.0 ) {
proposedOuthop = pow ( 2.0 , 8.0 + 2.0 * log10 ( ratio ) ) ;
2022-08-05 14:58:12 +01:00
}
2022-06-14 15:01:44 +01:00
2022-07-14 13:44:22 +01:00
if ( isSingleWindowed ( ) ) {
// the single (shorter) window mode actually uses a longer
// synthesis window for the 2048-bin FFT and drops the
// 1024-bin one, so it can survive longer hops, which is good
// because reduced CPU consumption is the whole motivation
proposedOuthop * = 2.0 ;
2022-08-05 14:58:12 +01:00
}
if ( proposedOuthop > m_limits . maxPreferredOuthop ) {
proposedOuthop = m_limits . maxPreferredOuthop ;
}
if ( proposedOuthop < m_limits . minPreferredOuthop ) {
proposedOuthop = m_limits . minPreferredOuthop ;
2022-07-14 11:55:21 +01:00
}
2022-06-22 09:10:02 +01:00
m_log . log ( 1 , " calculateHop: ratio and proposed outhop " , ratio , proposedOuthop ) ;
2022-06-14 15:01:44 +01:00
double inhop = proposedOuthop / ratio ;
2022-08-05 14:58:12 +01:00
if ( inhop < m_limits . minInhop ) {
2022-09-26 16:02:13 +01:00
m_log . log ( 0 , " R3Stretcher: WARNING: Ratio yields ideal inhop < minimum, results may be suspect " , inhop , m_limits . minInhop ) ;
2022-08-05 14:58:12 +01:00
inhop = m_limits . minInhop ;
2022-06-14 15:01:44 +01:00
}
2022-08-05 14:58:12 +01:00
if ( inhop > m_limits . maxInhop ) {
// Log level 1, this is not as big a deal as < minInhop above
2022-09-26 16:02:13 +01:00
m_log . log ( 1 , " R3Stretcher: WARNING: Ratio yields ideal inhop > maximum, results may be suspect " , inhop , m_limits . maxInhop ) ;
2022-08-05 14:58:12 +01:00
inhop = m_limits . maxInhop ;
2022-05-23 17:59:40 +01:00
}
2022-05-25 14:10:41 +01:00
2022-06-15 10:00:49 +01:00
m_inhop = int ( floor ( inhop ) ) ;
2022-06-22 09:10:02 +01:00
m_log . log ( 1 , " calculateHop: inhop and mean outhop " , m_inhop , m_inhop * ratio ) ;
2022-08-05 14:58:12 +01:00
if ( m_inhop < m_limits . maxInhopWithReadahead ) {
2023-03-23 17:26:28 +00:00
m_log . log ( 1 , " calculateHop: using readahead; maxInhopWithReadahead " , m_limits . maxInhopWithReadahead ) ;
2022-08-05 14:58:12 +01:00
m_useReadahead = true ;
} else {
2023-03-23 17:26:28 +00:00
m_log . log ( 1 , " calculateHop: not using readahead; maxInhopWithReadahead " , m_limits . maxInhopWithReadahead ) ;
2022-08-05 14:58:12 +01:00
m_useReadahead = false ;
}
2023-03-23 10:25:18 +00:00
if ( m_mode = = ProcessMode : : JustCreated ) {
m_prevInhop = m_inhop ;
m_prevOuthop = int ( round ( m_inhop * getEffectiveRatio ( ) ) ) ;
}
2022-05-23 15:04:34 +01:00
}
2022-06-13 16:06:21 +01:00
void
2022-06-21 10:25:08 +01:00
R3Stretcher : : updateRatioFromMap ( )
2022-06-13 16:06:21 +01:00
{
if ( m_keyFrameMap . empty ( ) ) return ;
2022-06-17 17:52:28 +01:00
2022-07-07 09:43:17 +01:00
if ( m_consumedInputDuration = = 0 ) {
2022-06-17 17:52:28 +01:00
m_timeRatio = double ( m_keyFrameMap . begin ( ) - > second ) /
double ( m_keyFrameMap . begin ( ) - > first ) ;
2022-06-21 17:03:24 +01:00
2022-06-22 09:10:02 +01:00
m_log . log ( 1 , " initial key-frame map entry " ,
2022-06-21 17:03:24 +01:00
double ( m_keyFrameMap . begin ( ) - > first ) ,
double ( m_keyFrameMap . begin ( ) - > second ) ) ;
2022-06-22 09:10:02 +01:00
m_log . log ( 1 , " giving initial ratio " , m_timeRatio ) ;
2022-06-21 17:03:24 +01:00
2022-06-17 17:52:28 +01:00
calculateHop ( ) ;
m_lastKeyFrameSurpassed = 0 ;
return ;
}
auto i0 = m_keyFrameMap . upper_bound ( m_lastKeyFrameSurpassed ) ;
if ( i0 = = m_keyFrameMap . end ( ) ) {
return ;
}
2022-07-07 09:43:17 +01:00
if ( m_consumedInputDuration > = i0 - > first ) {
2022-06-17 17:52:28 +01:00
2022-06-29 14:44:21 +01:00
m_log . log ( 1 , " input duration surpasses pending key frame " ,
2022-07-07 09:43:17 +01:00
double ( m_consumedInputDuration ) , double ( i0 - > first ) ) ;
2022-06-17 17:52:28 +01:00
2022-07-07 09:43:17 +01:00
auto i1 = m_keyFrameMap . upper_bound ( m_consumedInputDuration ) ;
2022-06-17 17:52:28 +01:00
size_t keyFrameAtInput , keyFrameAtOutput ;
if ( i1 ! = m_keyFrameMap . end ( ) ) {
keyFrameAtInput = i1 - > first ;
keyFrameAtOutput = i1 - > second ;
} else {
keyFrameAtInput = m_studyInputDuration ;
keyFrameAtOutput = m_totalTargetDuration ;
}
2022-07-07 09:43:17 +01:00
m_log . log ( 1 , " current input and output " ,
double ( m_consumedInputDuration ) , double ( m_totalOutputDuration ) ) ;
2022-06-29 14:44:21 +01:00
m_log . log ( 1 , " next key frame input and output " ,
2022-06-21 17:03:24 +01:00
double ( keyFrameAtInput ) , double ( keyFrameAtOutput ) ) ;
2022-07-07 09:43:17 +01:00
double ratio ;
if ( keyFrameAtInput > i0 - > first ) {
size_t toKeyFrameAtInput , toKeyFrameAtOutput ;
toKeyFrameAtInput = keyFrameAtInput - i0 - > first ;
if ( keyFrameAtOutput > i0 - > second ) {
toKeyFrameAtOutput = keyFrameAtOutput - i0 - > second ;
} else {
m_log . log ( 1 , " previous target key frame overruns next key frame (or total output duration) " , i0 - > second , keyFrameAtOutput ) ;
toKeyFrameAtOutput = 1 ;
}
m_log . log ( 1 , " diff to next key frame input and output " ,
double ( toKeyFrameAtInput ) , double ( toKeyFrameAtOutput ) ) ;
ratio = double ( toKeyFrameAtOutput ) / double ( toKeyFrameAtInput ) ;
} else {
m_log . log ( 1 , " source key frame overruns following key frame or total input duration " , i0 - > first , keyFrameAtInput ) ;
ratio = 1.0 ;
}
2022-06-29 14:44:21 +01:00
m_log . log ( 1 , " new ratio " , ratio ) ;
2022-06-17 17:52:28 +01:00
m_timeRatio = ratio ;
calculateHop ( ) ;
m_lastKeyFrameSurpassed = i0 - > first ;
}
2022-06-13 16:06:21 +01:00
}
2022-05-23 15:04:34 +01:00
double
2022-06-21 10:25:08 +01:00
R3Stretcher : : getTimeRatio ( ) const
2022-05-23 15:04:34 +01:00
{
return m_timeRatio ;
}
double
2022-06-21 10:25:08 +01:00
R3Stretcher : : getPitchScale ( ) const
2022-05-23 15:04:34 +01:00
{
return m_pitchScale ;
}
2022-06-13 10:39:13 +01:00
double
2022-06-21 10:25:08 +01:00
R3Stretcher : : getFormantScale ( ) const
2022-06-13 10:39:13 +01:00
{
return m_formantScale ;
}
2022-05-23 15:04:34 +01:00
size_t
2022-07-05 17:53:36 +01:00
R3Stretcher : : getPreferredStartPad ( ) const
2022-05-23 15:04:34 +01:00
{
2022-06-13 16:06:21 +01:00
if ( ! isRealTime ( ) ) {
2022-06-10 18:15:58 +01:00
return 0 ;
} else {
2023-02-20 15:26:12 +00:00
bool resamplingBefore = false ;
areWeResampling ( & resamplingBefore , nullptr ) ;
size_t pad = getWindowSourceSize ( ) / 2 ;
if ( resamplingBefore ) {
return size_t ( ceil ( pad * m_pitchScale ) ) ;
} else {
return pad ;
}
2022-07-05 17:53:36 +01:00
}
}
size_t
R3Stretcher : : getStartDelay ( ) const
{
if ( ! isRealTime ( ) ) {
return 0 ;
} else {
2023-02-20 15:26:12 +00:00
bool resamplingBefore = false ;
areWeResampling ( & resamplingBefore , nullptr ) ;
size_t delay = getWindowSourceSize ( ) / 2 ;
if ( resamplingBefore ) {
return delay ;
} else {
return size_t ( ceil ( delay / m_pitchScale ) ) ;
}
2022-06-10 18:15:58 +01:00
}
2022-05-23 15:04:34 +01:00
}
size_t
2022-06-21 10:25:08 +01:00
R3Stretcher : : getChannelCount ( ) const
2022-05-23 15:04:34 +01:00
{
return m_parameters . channels ;
}
void
2022-06-21 10:25:08 +01:00
R3Stretcher : : reset ( )
2022-05-23 15:04:34 +01:00
{
2023-03-23 10:25:18 +00:00
m_inhop = 1 ;
m_prevInhop = 1 ;
m_prevOuthop = 1 ;
m_unityCount = 0 ;
m_startSkip = 0 ;
m_studyInputDuration = 0 ;
m_suppliedInputDuration = 0 ;
m_totalTargetDuration = 0 ;
m_consumedInputDuration = 0 ;
m_lastKeyFrameSurpassed = 0 ;
m_totalOutputDuration = 0 ;
m_keyFrameMap . clear ( ) ;
m_mode = ProcessMode : : JustCreated ;
2022-05-27 11:17:20 +01:00
m_calculator - > reset ( ) ;
2023-03-22 13:26:15 +00:00
2022-06-24 10:51:40 +01:00
if ( m_resampler ) {
m_resampler - > reset ( ) ;
}
2022-05-27 11:17:20 +01:00
for ( auto & it : m_scaleData ) {
it . second - > guided . reset ( ) ;
}
for ( auto & cd : m_channelData ) {
cd - > reset ( ) ;
}
2023-03-20 10:08:01 +00:00
calculateHop ( ) ;
2022-06-13 16:06:21 +01:00
}
void
2022-06-21 10:25:08 +01:00
R3Stretcher : : study ( const float * const * , size_t samples , bool )
2022-06-13 16:06:21 +01:00
{
2022-07-14 10:02:39 +01:00
Profiler profiler ( " R3Stretcher::study " ) ;
2022-06-13 16:06:21 +01:00
if ( isRealTime ( ) ) {
2022-06-22 09:10:02 +01:00
m_log . log ( 0 , " R3Stretcher::study: Not meaningful in realtime mode " ) ;
2022-06-13 16:06:21 +01:00
return ;
}
if ( m_mode = = ProcessMode : : Processing | | m_mode = = ProcessMode : : Finished ) {
2022-06-22 09:10:02 +01:00
m_log . log ( 0 , " R3Stretcher::study: Cannot study after processing " ) ;
2022-06-13 16:06:21 +01:00
return ;
}
if ( m_mode = = ProcessMode : : JustCreated ) {
m_studyInputDuration = 0 ;
}
m_mode = ProcessMode : : Studying ;
m_studyInputDuration + = samples ;
2022-05-23 15:04:34 +01:00
}
2022-07-07 10:54:23 +01:00
void
R3Stretcher : : setExpectedInputDuration ( size_t samples )
{
m_suppliedInputDuration = samples ;
}
2022-05-23 15:04:34 +01:00
size_t
2022-06-21 10:25:08 +01:00
R3Stretcher : : getSamplesRequired ( ) const
2022-05-23 15:04:34 +01:00
{
2022-05-26 15:08:07 +01:00
if ( available ( ) ! = 0 ) return 0 ;
2022-06-14 15:15:55 +01:00
int rs = m_channelData [ 0 ] - > inbuf - > getReadSpace ( ) ;
2023-02-20 15:26:12 +00:00
m_log . log ( 2 , " getSamplesRequired: read space and window source size " , rs , getWindowSourceSize ( ) ) ;
2022-08-04 10:31:36 +01:00
if ( rs < getWindowSourceSize ( ) ) {
2023-02-20 15:26:12 +00:00
int req = getWindowSourceSize ( ) - rs ;
bool resamplingBefore = false ;
areWeResampling ( & resamplingBefore , nullptr ) ;
if ( ! resamplingBefore ) {
return req ;
} else {
int adjusted = int ( ceil ( double ( req ) * m_pitchScale ) ) ;
m_log . log ( 2 , " getSamplesRequired: resamplingBefore is true, req and adjusted " , req , adjusted ) ;
return adjusted ;
}
2022-05-23 15:04:34 +01:00
} else {
return 0 ;
}
}
2022-06-24 14:01:11 +01:00
void
2023-06-01 14:09:39 +01:00
R3Stretcher : : setMaxProcessSize ( size_t requested )
2022-06-24 14:01:11 +01:00
{
2023-06-01 14:09:39 +01:00
m_log . log ( 2 , " R3Stretcher::setMaxProcessSize " , requested ) ;
int n = m_limits . overallMaxProcessSize ;
if ( requested > size_t ( n ) ) {
m_log . log ( 0 , " R3Stretcher::setMaxProcessSize: request exceeds overall limit " , requested , n ) ;
} else {
n = int ( requested ) ;
}
2023-06-01 10:51:20 +01:00
2023-06-01 14:09:39 +01:00
ensureInbuf ( n * 2 , false ) ;
ensureOutbuf ( n * 8 , false ) ;
}
2022-06-24 14:01:11 +01:00
2023-07-25 13:11:21 +01:00
size_t
R3Stretcher : : getProcessSizeLimit ( ) const
{
return m_limits . overallMaxProcessSize ;
}
2023-06-01 14:09:39 +01:00
void
R3Stretcher : : ensureInbuf ( int required , bool warn )
{
int ws = m_channelData [ 0 ] - > inbuf - > getWriteSpace ( ) ;
if ( required < ws ) {
return ;
}
if ( warn ) {
m_log . log ( 0 , " R3Stretcher::ensureInbuf: WARNING: Forced to increase input buffer size. Either setMaxProcessSize was not properly called, process is being called repeatedly without retrieve, or an internal error has led to an incorrect resampler output calculation. Samples to write and space available " , required , ws ) ;
2023-06-01 10:51:20 +01:00
}
2023-06-01 14:09:39 +01:00
size_t oldSize = m_channelData [ 0 ] - > inbuf - > getSize ( ) ;
size_t newSize = oldSize - ws + required ;
if ( newSize < oldSize * 2 ) newSize = oldSize * 2 ;
m_log . log ( warn ? 0 : 2 , " R3Stretcher::ensureInbuf: old and new sizes " , oldSize , newSize ) ;
for ( int c = 0 ; c < m_parameters . channels ; + + c ) {
auto newBuf = m_channelData [ c ] - > inbuf - > resized ( newSize ) ;
m_channelData [ c ] - > inbuf = std : : unique_ptr < RingBuffer < float > > ( newBuf ) ;
2023-06-07 17:01:47 +01:00
// mixdown is used for mid-side mixing as well as the single
// hop output mix, so it needs to be enough to match the inbuf
m_channelData [ c ] - > mixdown . resize ( newSize , 0.f ) ;
2023-06-01 14:09:39 +01:00
}
}
2023-06-01 10:51:20 +01:00
2023-06-01 14:09:39 +01:00
void
R3Stretcher : : ensureOutbuf ( int required , bool warn )
{
int ws = m_channelData [ 0 ] - > outbuf - > getWriteSpace ( ) ;
if ( required < ws ) {
return ;
}
if ( warn ) {
m_log . log ( 0 , " R3Stretcher::ensureOutbuf: WARNING: Forced to increase output buffer size. Using smaller process blocks or an artificially larger value for setMaxProcessSize may avoid this. Samples to write and space available " , required , ws ) ;
}
size_t oldSize = m_channelData [ 0 ] - > outbuf - > getSize ( ) ;
size_t newSize = oldSize - ws + required ;
if ( newSize < oldSize * 2 ) newSize = oldSize * 2 ;
m_log . log ( warn ? 0 : 2 , " R3Stretcher::ensureOutbuf: old and new sizes " , oldSize , newSize ) ;
for ( int c = 0 ; c < m_parameters . channels ; + + c ) {
auto newBuf = m_channelData [ c ] - > outbuf - > resized ( newSize ) ;
m_channelData [ c ] - > outbuf = std : : unique_ptr < RingBuffer < float > > ( newBuf ) ;
2022-06-24 14:01:11 +01:00
}
}
2022-05-23 15:04:34 +01:00
void
2022-06-21 10:25:08 +01:00
R3Stretcher : : process ( const float * const * input , size_t samples , bool final )
2022-05-23 15:04:34 +01:00
{
2022-07-14 10:02:39 +01:00
Profiler profiler ( " R3Stretcher::process " ) ;
2022-06-13 16:06:21 +01:00
if ( m_mode = = ProcessMode : : Finished ) {
2022-06-22 09:10:02 +01:00
m_log . log ( 0 , " R3Stretcher::process: Cannot process again after final chunk " ) ;
2022-06-13 16:06:21 +01:00
return ;
}
2023-06-01 14:09:39 +01:00
int n = m_limits . overallMaxProcessSize ;
if ( samples > size_t ( n ) ) {
m_log . log ( 0 , " R3Stretcher::process: request exceeds overall limit " , samples , n ) ;
} else {
n = int ( samples ) ;
}
2022-06-24 10:51:40 +01:00
if ( ! isRealTime ( ) ) {
2022-07-07 10:54:23 +01:00
2022-06-29 14:44:21 +01:00
if ( m_mode = = ProcessMode : : Studying ) {
m_totalTargetDuration =
size_t ( round ( m_studyInputDuration * m_timeRatio ) ) ;
m_log . log ( 1 , " study duration and target duration " ,
m_studyInputDuration , m_totalTargetDuration ) ;
2022-07-07 10:54:23 +01:00
} else if ( m_mode = = ProcessMode : : JustCreated ) {
if ( m_suppliedInputDuration ! = 0 ) {
m_totalTargetDuration =
size_t ( round ( m_suppliedInputDuration * m_timeRatio ) ) ;
m_log . log ( 1 , " supplied duration and target duration " ,
m_suppliedInputDuration , m_totalTargetDuration ) ;
}
2022-06-29 14:44:21 +01:00
}
// Update this on every process round, checking whether we've
2022-06-29 16:51:18 +01:00
// surpassed the next key frame yet. This must follow the
// overall target calculation above, which uses the "global"
// time ratio, but precede any other use of the time ratio.
2022-06-29 14:44:21 +01:00
if ( ! m_keyFrameMap . empty ( ) ) {
updateRatioFromMap ( ) ;
}
2022-06-24 10:51:40 +01:00
if ( m_mode = = ProcessMode : : JustCreated | |
m_mode = = ProcessMode : : Studying ) {
if ( m_pitchScale ! = 1.0 & & ! m_resampler ) {
createResampler ( ) ;
}
2022-08-03 14:16:17 +01:00
// Pad to half the frame. As with R2, in real-time mode we
// don't do this -- it's better to start with a swoosh
// than introduce more latency, and we don't want gaps
// when the ratio changes.
2022-08-04 10:31:36 +01:00
int pad = getWindowSourceSize ( ) / 2 ;
2022-06-24 10:51:40 +01:00
m_log . log ( 1 , " offline mode: prefilling with " , pad ) ;
2023-06-01 14:09:39 +01:00
ensureInbuf ( pad ) ;
2022-06-24 10:51:40 +01:00
for ( int c = 0 ; c < m_parameters . channels ; + + c ) {
2023-06-01 14:09:39 +01:00
int zeroed = m_channelData [ c ] - > inbuf - > zero ( pad ) ;
if ( zeroed ! = pad ) {
m_log . log ( 0 , " R3Stretcher: WARNING: too few padding samples written " , zeroed , pad ) ;
}
2022-06-24 10:51:40 +01:00
}
2022-06-27 16:37:52 +01:00
// NB by the time we skip this later we may have resampled
// as well as stretched
2022-06-29 16:51:18 +01:00
m_startSkip = int ( round ( pad / m_pitchScale ) ) ;
2022-07-05 17:53:36 +01:00
m_log . log ( 1 , " start skip is " , m_startSkip ) ;
2022-06-24 10:51:40 +01:00
}
2022-06-13 16:06:21 +01:00
}
2022-09-02 14:49:34 +01:00
bool resamplingBefore = false ;
areWeResampling ( & resamplingBefore , nullptr ) ;
2022-09-02 15:08:29 +01:00
int channels = m_parameters . channels ;
int inputIx = 0 ;
2022-09-02 14:49:34 +01:00
2023-06-01 14:09:39 +01:00
if ( n = = 0 & & final ) {
2023-02-20 15:25:27 +00:00
m_log . log ( 2 , " process: no samples but final specified, consuming " ) ;
2023-06-01 10:51:20 +01:00
consume ( true ) ;
2023-02-20 15:25:27 +00:00
2023-06-01 14:09:39 +01:00
} else while ( inputIx < n ) {
2022-09-02 14:49:34 +01:00
2023-06-01 14:09:39 +01:00
int remaining = n - inputIx ;
2022-09-02 14:49:34 +01:00
2023-06-01 14:09:39 +01:00
int ws = m_channelData [ 0 ] - > inbuf - > getWriteSpace ( ) ;
2022-09-02 14:49:34 +01:00
if ( ws = = 0 ) {
2023-06-01 10:51:20 +01:00
consume ( false ) ;
2022-09-02 14:49:34 +01:00
}
2023-06-01 14:09:39 +01:00
ensureInbuf ( remaining ) ;
ws = m_channelData [ 0 ] - > inbuf - > getWriteSpace ( ) ;
2022-09-02 15:08:29 +01:00
if ( resamplingBefore ) {
2022-09-02 14:49:34 +01:00
2022-09-02 15:08:29 +01:00
for ( int c = 0 ; c < channels ; + + c ) {
auto & cd = m_channelData . at ( c ) ;
m_channelAssembly . resampled [ c ] = cd - > resampled . data ( ) ;
}
2022-09-02 14:49:34 +01:00
2022-09-02 15:08:29 +01:00
int resampleBufSize = int ( m_channelData . at ( 0 ) - > resampled . size ( ) ) ;
int maxResampleOutput = std : : min ( ws , resampleBufSize ) ;
int maxResampleInput = int ( floor ( maxResampleOutput * m_pitchScale ) ) ;
int resampleInput = std : : min ( remaining , maxResampleInput ) ;
if ( resampleInput = = 0 ) resampleInput = 1 ;
2023-03-15 18:01:21 +00:00
2023-06-01 14:09:39 +01:00
m_log . log ( 2 , " R3Stretcher::process: resamplingBefore is true, resampleInput and maxResampleOutput " , resampleInput , maxResampleOutput ) ;
2023-03-15 18:01:21 +00:00
prepareInput ( input , inputIx , resampleInput ) ;
2023-04-05 11:14:04 +01:00
2023-06-01 14:09:39 +01:00
bool finalHop = ( final & & inputIx + resampleInput > = n ) ;
2023-06-01 10:51:20 +01:00
2022-09-02 15:08:29 +01:00
int resampleOutput = m_resampler - > resample
( m_channelAssembly . resampled . data ( ) ,
maxResampleOutput ,
2023-03-15 18:01:21 +00:00
m_channelAssembly . input . data ( ) ,
2022-09-02 15:08:29 +01:00
resampleInput ,
1.0 / m_pitchScale ,
2023-06-01 10:51:20 +01:00
finalHop ) ;
2022-09-02 15:08:29 +01:00
inputIx + = resampleInput ;
2023-06-01 14:09:39 +01:00
2022-09-02 15:08:29 +01:00
for ( int c = 0 ; c < m_parameters . channels ; + + c ) {
2023-06-01 14:09:39 +01:00
int written = m_channelData [ c ] - > inbuf - > write
2022-09-02 15:08:29 +01:00
( m_channelData . at ( c ) - > resampled . data ( ) ,
resampleOutput ) ;
2023-06-01 14:09:39 +01:00
if ( written ! = resampleOutput ) {
m_log . log ( 0 , " R3Stretcher: WARNING: too few samples written to input buffer from resampler " , written , resampleOutput ) ;
}
2022-09-02 14:49:34 +01:00
}
2022-05-23 15:04:34 +01:00
2022-09-02 15:08:29 +01:00
} else {
int toWrite = std : : min ( ws , remaining ) ;
2023-02-20 15:26:12 +00:00
m_log . log ( 2 , " process: resamplingBefore is false, writing to inbuf from supplied data, former read space and samples being added " , m_channelData [ 0 ] - > inbuf - > getReadSpace ( ) , toWrite ) ;
2023-03-15 18:01:21 +00:00
prepareInput ( input , inputIx , toWrite ) ;
2022-09-02 15:08:29 +01:00
for ( int c = 0 ; c < m_parameters . channels ; + + c ) {
2023-06-01 14:09:39 +01:00
int written = m_channelData [ c ] - > inbuf - > write
2023-03-15 18:01:21 +00:00
( m_channelAssembly . input [ c ] , toWrite ) ;
2023-06-01 14:09:39 +01:00
if ( written ! = toWrite ) {
m_log . log ( 0 , " R3Stretcher: WARNING: too few samples written to input buffer " , written , toWrite ) ;
}
2022-09-02 15:08:29 +01:00
}
2023-06-01 14:09:39 +01:00
2022-09-02 15:08:29 +01:00
inputIx + = toWrite ;
}
2023-06-01 14:09:39 +01:00
consume ( final & & inputIx > = n ) ;
2023-06-01 10:51:20 +01:00
}
if ( final ) {
// We don't distinguish between Finished and "draining, but
// haven't yet delivered all the samples" because the
// distinction is meaningless internally - it only affects
// whether available() finds any samples in the buffer
m_log . log ( 1 , " final is set, entering Finished mode " ) ;
m_mode = ProcessMode : : Finished ;
} else {
m_mode = ProcessMode : : Processing ;
2022-05-23 15:04:34 +01:00
}
}
int
2022-06-21 10:25:08 +01:00
R3Stretcher : : available ( ) const
2022-05-23 15:04:34 +01:00
{
2022-05-23 16:45:41 +01:00
int av = int ( m_channelData [ 0 ] - > outbuf - > getReadSpace ( ) ) ;
2022-06-13 16:06:21 +01:00
if ( av = = 0 & & m_mode = = ProcessMode : : Finished ) {
return - 1 ;
} else {
return av ;
}
2022-05-23 15:04:34 +01:00
}
size_t
2022-06-21 10:25:08 +01:00
R3Stretcher : : retrieve ( float * const * output , size_t samples ) const
2022-05-23 15:04:34 +01:00
{
2022-07-14 10:02:39 +01:00
Profiler profiler ( " R3Stretcher::retrieve " ) ;
2022-06-14 15:15:55 +01:00
int got = samples ;
2022-05-23 15:04:34 +01:00
2023-06-01 10:51:20 +01:00
m_log . log ( 2 , " retrieve: requested, outbuf has " , samples , m_channelData [ 0 ] - > outbuf - > getReadSpace ( ) ) ;
2022-06-14 15:15:55 +01:00
for ( int c = 0 ; c < m_parameters . channels ; + + c ) {
int gotHere = m_channelData [ c ] - > outbuf - > read ( output [ c ] , got ) ;
2022-05-23 15:04:34 +01:00
if ( gotHere < got ) {
if ( c > 0 ) {
2022-06-22 09:10:02 +01:00
m_log . log ( 0 , " R3Stretcher::retrieve: WARNING: channel imbalance detected " ) ;
2022-05-23 15:04:34 +01:00
}
2022-06-14 15:15:55 +01:00
got = std : : min ( got , std : : max ( gotHere , 0 ) ) ;
2022-05-23 15:04:34 +01:00
}
}
2023-03-17 13:19:18 +00:00
if ( useMidSide ( ) ) {
2023-03-15 18:01:21 +00:00
for ( int i = 0 ; i < got ; + + i ) {
float m = output [ 0 ] [ i ] ;
float s = output [ 1 ] [ i ] ;
float l = m + s ;
float r = m - s ;
output [ 0 ] [ i ] = l ;
output [ 1 ] [ i ] = r ;
}
}
2023-06-01 10:51:20 +01:00
m_log . log ( 2 , " retrieve: returning, outbuf now has " , got , m_channelData [ 0 ] - > outbuf - > getReadSpace ( ) ) ;
2022-05-23 15:04:34 +01:00
return got ;
}
2023-03-15 18:01:21 +00:00
void
R3Stretcher : : prepareInput ( const float * const * input , int ix , int n )
{
2023-03-17 13:19:18 +00:00
if ( useMidSide ( ) ) {
2023-03-15 18:01:21 +00:00
auto & c0 = m_channelData . at ( 0 ) - > mixdown ;
auto & c1 = m_channelData . at ( 1 ) - > mixdown ;
2023-06-07 17:01:47 +01:00
int bufsize = c0 . size ( ) ;
if ( n > bufsize ) {
m_log . log ( 0 , " R3Stretcher::prepareInput: WARNING: called with size greater than mixdown buffer length " , n , bufsize ) ;
n = bufsize ;
}
2023-03-15 18:01:21 +00:00
for ( int i = 0 ; i < n ; + + i ) {
float l = input [ 0 ] [ i + ix ] ;
float r = input [ 1 ] [ i + ix ] ;
float m = ( l + r ) / 2.f ;
float s = ( l - r ) / 2.f ;
c0 [ i ] = m ;
c1 [ i ] = s ;
}
m_channelAssembly . input [ 0 ] = m_channelData . at ( 0 ) - > mixdown . data ( ) ;
m_channelAssembly . input [ 1 ] = m_channelData . at ( 1 ) - > mixdown . data ( ) ;
} else {
for ( int c = 0 ; c < m_parameters . channels ; + + c ) {
m_channelAssembly . input [ c ] = input [ c ] + ix ;
}
}
}
2022-05-23 15:04:34 +01:00
void
2023-06-01 10:51:20 +01:00
R3Stretcher : : consume ( bool final )
2022-05-23 15:04:34 +01:00
{
2022-07-14 10:02:39 +01:00
Profiler profiler ( " R3Stretcher::consume " ) ;
2022-05-23 15:04:34 +01:00
int longest = m_guideConfiguration . longestFftSize ;
2022-05-25 13:47:40 +01:00
int channels = m_parameters . channels ;
2022-05-27 15:03:40 +01:00
int inhop = m_inhop ;
2022-09-02 14:49:34 +01:00
bool resamplingAfter = false ;
areWeResampling ( nullptr , & resamplingAfter ) ;
2022-05-27 15:03:40 +01:00
double effectivePitchRatio = 1.0 / m_pitchScale ;
if ( m_resampler ) {
2022-06-24 13:25:36 +01:00
effectivePitchRatio =
m_resampler - > getEffectiveRatio ( effectivePitchRatio ) ;
2022-05-27 15:03:40 +01:00
}
2022-05-24 12:00:54 +01:00
2022-05-27 15:03:40 +01:00
int outhop = m_calculator - > calculateSingle ( m_timeRatio ,
effectivePitchRatio ,
2022-05-23 17:59:40 +01:00
1.f ,
2022-05-25 14:10:41 +01:00
inhop ,
2022-05-23 17:59:40 +01:00
longest ,
2022-06-13 17:16:03 +01:00
longest ,
true ) ;
2022-07-07 10:35:01 +01:00
if ( outhop < 1 ) {
m_log . log ( 0 , " R3Stretcher::consume: WARNING: outhop calculated as " , outhop ) ;
outhop = 1 ;
}
2022-06-13 17:16:03 +01:00
2022-05-27 10:06:31 +01:00
// Now inhop is the distance by which the input stream will be
// advanced after our current frame has been read, and outhop is
// the distance by which the output will be advanced after it has
// been emitted; m_prevInhop and m_prevOuthop are the
// corresponding values the last time a frame was processed (*not*
// just the last time this function was called, since we can
// return without doing anything if the output buffer is full).
//
// Our phase adjustments need to be based on the distances we have
// advanced the input and output since the previous frame, not the
// distances we are about to advance them, so they use the m_prev
// values.
2022-06-21 17:03:24 +01:00
2022-05-26 15:08:07 +01:00
if ( inhop ! = m_prevInhop ) {
2022-06-22 09:10:02 +01:00
m_log . log ( 2 , " change in inhop " , double ( m_prevInhop ) , double ( inhop ) ) ;
2022-05-26 15:08:07 +01:00
}
if ( outhop ! = m_prevOuthop ) {
2022-06-22 09:10:02 +01:00
m_log . log ( 2 , " change in outhop " , double ( m_prevOuthop ) , double ( outhop ) ) ;
2022-05-26 15:08:07 +01:00
}
2022-07-07 10:35:01 +01:00
auto & cd0 = m_channelData . at ( 0 ) ;
2023-02-20 15:26:12 +00:00
m_log . log ( 2 , " consume: write space and outhop " , cd0 - > outbuf - > getWriteSpace ( ) , outhop ) ;
2023-06-01 10:51:20 +01:00
// NB our ChannelData, ScaleData, and ChannelScaleData maps
// contain shared_ptrs; whenever we retain one of them in a
// variable, we do so by reference to avoid copying the shared_ptr
// (as that is not realtime safe). Same goes for the map iterators
while ( true ) {
2022-05-23 20:55:56 +01:00
2023-12-15 20:32:57 +00:00
Profiler profiler2 ( " R3Stretcher::consume/loop " ) ;
2022-05-25 11:26:16 +01:00
2022-07-07 10:35:01 +01:00
int readSpace = cd0 - > inbuf - > getReadSpace ( ) ;
2023-06-01 14:09:39 +01:00
m_log . log ( 2 , " consume: read space " , readSpace ) ;
2023-02-20 15:26:12 +00:00
2022-08-04 10:31:36 +01:00
if ( readSpace < getWindowSourceSize ( ) ) {
2023-06-01 10:51:20 +01:00
if ( final ) {
2022-05-23 20:55:56 +01:00
if ( readSpace = = 0 ) {
2022-07-07 10:35:01 +01:00
int fill = cd0 - > scales . at ( longest ) - > accumulatorFill ;
if ( fill = = 0 ) {
break ;
} else {
m_log . log ( 1 , " finished reading input, but samples remaining in output accumulator " , fill ) ;
}
2022-05-23 20:55:56 +01:00
}
} else {
2022-07-07 10:35:01 +01:00
// await more input
2022-05-23 20:55:56 +01:00
break ;
}
2022-05-23 17:59:40 +01:00
}
2023-06-01 14:09:39 +01:00
ensureOutbuf ( outhop ) ;
2023-06-01 10:51:20 +01:00
2022-05-25 11:26:16 +01:00
// Analysis
2022-05-25 09:43:08 +01:00
2022-05-25 13:47:40 +01:00
for ( int c = 0 ; c < channels ; + + c ) {
2022-06-09 15:26:16 +01:00
analyseChannel ( c , inhop , m_prevInhop , m_prevOuthop ) ;
2022-05-23 15:04:34 +01:00
}
2022-05-25 09:43:08 +01:00
// Phase update. This is synchronised across all channels
2022-05-24 17:35:23 +01:00
for ( auto & it : m_channelData [ 0 ] - > scales ) {
2022-05-23 15:04:34 +01:00
int fftSize = it . first ;
2022-05-25 13:47:40 +01:00
for ( int c = 0 ; c < channels ; + + c ) {
2022-05-24 17:35:23 +01:00
auto & cd = m_channelData . at ( c ) ;
2022-05-25 11:26:16 +01:00
auto & scale = cd - > scales . at ( fftSize ) ;
m_channelAssembly . mag [ c ] = scale - > mag . data ( ) ;
m_channelAssembly . phase [ c ] = scale - > phase . data ( ) ;
2022-06-07 12:12:06 +01:00
m_channelAssembly . prevMag [ c ] = scale - > prevMag . data ( ) ;
2022-05-23 15:04:34 +01:00
m_channelAssembly . guidance [ c ] = & cd - > guidance ;
2022-05-25 11:26:16 +01:00
m_channelAssembly . outPhase [ c ] = scale - > advancedPhase . data ( ) ;
2022-05-23 15:04:34 +01:00
}
m_scaleData . at ( fftSize ) - > guided . advance
( m_channelAssembly . outPhase . data ( ) ,
m_channelAssembly . mag . data ( ) ,
m_channelAssembly . phase . data ( ) ,
2022-06-07 12:12:06 +01:00
m_channelAssembly . prevMag . data ( ) ,
2022-05-23 15:04:34 +01:00
m_guideConfiguration ,
m_channelAssembly . guidance . data ( ) ,
2023-03-17 13:19:18 +00:00
useMidSide ( ) ,
2022-05-27 10:06:31 +01:00
m_prevInhop ,
m_prevOuthop ) ;
2022-05-23 15:04:34 +01:00
}
2022-06-13 09:40:26 +01:00
for ( int c = 0 ; c < channels ; + + c ) {
adjustPreKick ( c ) ;
}
2022-05-25 11:26:16 +01:00
// Resynthesis
2022-05-25 09:43:08 +01:00
2022-05-25 13:47:40 +01:00
for ( int c = 0 ; c < channels ; + + c ) {
2022-07-07 10:35:01 +01:00
synthesiseChannel ( c , outhop , readSpace = = 0 ) ;
2022-06-24 13:25:36 +01:00
}
2022-05-25 13:47:40 +01:00
// Resample
2022-07-06 10:22:50 +01:00
int resampledCount = 0 ;
2022-09-02 14:49:34 +01:00
if ( resamplingAfter ) {
2022-05-25 13:47:40 +01:00
for ( int c = 0 ; c < channels ; + + c ) {
auto & cd = m_channelData . at ( c ) ;
m_channelAssembly . mixdown [ c ] = cd - > mixdown . data ( ) ;
m_channelAssembly . resampled [ c ] = cd - > resampled . data ( ) ;
}
2023-04-05 11:14:04 +01:00
2023-06-01 10:51:20 +01:00
bool finalHop = ( final & &
readSpace < inhop & &
cd0 - > scales . at ( longest ) - > accumulatorFill < = outhop ) ;
2023-04-05 11:14:04 +01:00
2022-05-25 13:47:40 +01:00
resampledCount = m_resampler - > resample
( m_channelAssembly . resampled . data ( ) ,
m_channelData [ 0 ] - > resampled . size ( ) ,
m_channelAssembly . mixdown . data ( ) ,
2022-07-07 10:35:01 +01:00
outhop ,
2022-05-25 13:47:40 +01:00
1.0 / m_pitchScale ,
2023-06-01 10:51:20 +01:00
finalHop ) ;
2022-05-25 13:47:40 +01:00
}
// Emit
2022-07-07 10:35:01 +01:00
int writeCount = outhop ;
2022-09-02 14:49:34 +01:00
if ( resamplingAfter ) {
2022-06-29 14:44:21 +01:00
writeCount = resampledCount ;
}
if ( ! isRealTime ( ) ) {
if ( m_totalTargetDuration > 0 & &
m_totalOutputDuration + writeCount > m_totalTargetDuration ) {
m_log . log ( 1 , " writeCount would take output beyond target " ,
m_totalOutputDuration , m_totalTargetDuration ) ;
auto reduced = m_totalTargetDuration - m_totalOutputDuration ;
m_log . log ( 1 , " reducing writeCount from and to " , writeCount , reduced ) ;
writeCount = reduced ;
}
}
2022-07-07 09:43:17 +01:00
int advanceCount = inhop ;
if ( advanceCount > readSpace ) {
2023-06-01 10:51:20 +01:00
// This should happen only when draining
if ( ! final ) {
2022-09-26 16:02:13 +01:00
m_log . log ( 0 , " R3Stretcher: WARNING: readSpace < inhop when processing is not yet finished " , readSpace , inhop ) ;
2022-07-07 09:43:17 +01:00
}
advanceCount = readSpace ;
}
2022-06-29 14:44:21 +01:00
2022-05-25 13:47:40 +01:00
for ( int c = 0 ; c < channels ; + + c ) {
auto & cd = m_channelData . at ( c ) ;
2023-06-01 14:09:39 +01:00
int written = 0 ;
2022-09-02 14:49:34 +01:00
if ( resamplingAfter ) {
2023-06-01 14:09:39 +01:00
written = cd - > outbuf - > write ( cd - > resampled . data ( ) , writeCount ) ;
2022-05-25 13:47:40 +01:00
} else {
2023-06-01 14:09:39 +01:00
written = cd - > outbuf - > write ( cd - > mixdown . data ( ) , writeCount ) ;
}
if ( written ! = writeCount ) {
m_log . log ( 0 , " R3Stretcher: WARNING: too few samples written to output buffer " , written , writeCount ) ;
}
int skipped = cd - > inbuf - > skip ( advanceCount ) ;
if ( skipped ! = advanceCount ) {
m_log . log ( 0 , " R3Stretcher: WARNING: too few samples advanced " , skipped , advanceCount ) ;
2022-05-25 13:47:40 +01:00
}
}
2022-06-10 20:26:37 +01:00
2022-07-07 09:43:17 +01:00
m_consumedInputDuration + = advanceCount ;
2022-06-29 14:44:21 +01:00
m_totalOutputDuration + = writeCount ;
2022-06-24 13:25:36 +01:00
2022-06-10 20:26:37 +01:00
if ( m_startSkip > 0 ) {
2022-07-07 10:35:01 +01:00
int rs = cd0 - > outbuf - > getReadSpace ( ) ;
2022-06-29 14:44:21 +01:00
int toSkip = std : : min ( m_startSkip , rs ) ;
2022-06-10 20:26:37 +01:00
for ( int c = 0 ; c < channels ; + + c ) {
2023-06-01 14:09:39 +01:00
int skipped = m_channelData . at ( c ) - > outbuf - > skip ( toSkip ) ;
if ( skipped ! = toSkip ) {
m_log . log ( 0 , " R3Stretcher: WARNING: too few samples skipped at output " , skipped , toSkip ) ;
}
2022-06-10 20:26:37 +01:00
}
m_startSkip - = toSkip ;
2022-06-29 14:44:21 +01:00
m_totalOutputDuration = rs - toSkip ;
2022-06-10 20:26:37 +01:00
}
2022-06-10 18:15:58 +01:00
2022-05-27 10:06:31 +01:00
m_prevInhop = inhop ;
m_prevOuthop = outhop ;
}
2023-06-01 10:51:20 +01:00
m_log . log ( 2 , " consume: write space reduced to " , cd0 - > outbuf - > getWriteSpace ( ) ) ;
2022-05-25 11:26:16 +01:00
}
void
2022-06-21 10:25:08 +01:00
R3Stretcher : : analyseChannel ( int c , int inhop , int prevInhop , int prevOuthop )
2022-05-25 11:26:16 +01:00
{
2022-07-14 10:02:39 +01:00
Profiler profiler ( " R3Stretcher::analyseChannel " ) ;
2022-05-25 11:26:16 +01:00
auto & cd = m_channelData . at ( c ) ;
2022-08-04 11:19:13 +01:00
int sourceSize = cd - > windowSource . size ( ) ;
2022-08-04 10:31:36 +01:00
process_t * buf = cd - > windowSource . data ( ) ;
2022-05-25 11:26:16 +01:00
int readSpace = cd - > inbuf - > getReadSpace ( ) ;
2022-08-04 11:19:13 +01:00
if ( readSpace < sourceSize ) {
2022-05-25 11:26:16 +01:00
cd - > inbuf - > peek ( buf , readSpace ) ;
2022-08-04 11:19:13 +01:00
v_zero ( buf + readSpace , sourceSize - readSpace ) ;
2022-05-25 11:26:16 +01:00
} else {
2022-08-04 11:19:13 +01:00
cd - > inbuf - > peek ( buf , sourceSize ) ;
2022-05-25 11:26:16 +01:00
}
2022-08-04 10:31:36 +01:00
// We have an unwindowed time-domain frame in buf that is as long
// as required for the union of all FFT sizes and readahead
// hops. Populate the various sizes from it with aligned centres,
// windowing as we copy. The classification scale is handled
// separately because it has readahead, so skip it here. (In
// single-window mode that means we do nothing here, since the
// classification scale is the only one.)
2022-08-04 11:19:13 +01:00
int longest = m_guideConfiguration . longestFftSize ;
2022-08-04 10:31:36 +01:00
int classify = m_guideConfiguration . classificationFftSize ;
2022-05-25 11:26:16 +01:00
2022-08-03 14:16:17 +01:00
for ( auto & it : cd - > scales ) {
int fftSize = it . first ;
2022-08-04 10:31:36 +01:00
if ( fftSize = = classify ) continue ;
2022-08-04 11:19:13 +01:00
int offset = ( longest - fftSize ) / 2 ;
2022-08-03 14:16:17 +01:00
m_scaleData . at ( fftSize ) - > analysisWindow . cut
( buf + offset , it . second - > timeDomain . data ( ) ) ;
2022-05-25 11:26:16 +01:00
}
auto & classifyScale = cd - > scales . at ( classify ) ;
ClassificationReadaheadData & readahead = cd - > readahead ;
2022-08-05 14:58:12 +01:00
bool copyFromReadahead = false ;
2022-08-04 11:19:13 +01:00
2022-08-05 14:58:12 +01:00
if ( m_useReadahead ) {
// The classification scale has a one-hop readahead, so
// populate the readahead from further down the long
// unwindowed frame.
2022-05-26 15:08:07 +01:00
2022-08-05 14:58:12 +01:00
m_scaleData . at ( classify ) - > analysisWindow . cut
( buf + ( longest - classify ) / 2 + inhop ,
readahead . timeDomain . data ( ) ) ;
2022-05-26 15:08:07 +01:00
2022-08-05 14:58:12 +01:00
// If inhop has changed since the previous frame, we must
// populate the classification scale (but for
// analysis/resynthesis rather than classification) anew
// rather than reuse the previous frame's readahead.
2022-05-27 11:17:20 +01:00
2022-08-05 14:58:12 +01:00
copyFromReadahead = cd - > haveReadahead ;
if ( inhop ! = prevInhop ) copyFromReadahead = false ;
}
if ( ! copyFromReadahead ) {
2022-05-26 15:08:07 +01:00
m_scaleData . at ( classify ) - > analysisWindow . cut
2022-08-04 11:19:13 +01:00
( buf + ( longest - classify ) / 2 ,
2022-05-26 15:08:07 +01:00
classifyScale - > timeDomain . data ( ) ) ;
}
2022-05-25 11:26:16 +01:00
// FFT shift, forward FFT, and carry out cartesian-polar
// conversion for each FFT size.
2022-05-26 15:08:07 +01:00
// For the classification scale we need magnitudes for the full
// range (polar only in a subset) and we operate in the readahead,
// pulling current values from the existing readahead (except
// where the inhop has changed as above, in which case we need to
// do both readahead and current)
2022-05-25 11:26:16 +01:00
2022-08-05 14:58:12 +01:00
if ( m_useReadahead ) {
2022-05-25 11:26:16 +01:00
2022-08-05 14:58:12 +01:00
if ( copyFromReadahead ) {
v_copy ( classifyScale - > mag . data ( ) ,
readahead . mag . data ( ) ,
classifyScale - > bufSize ) ;
v_copy ( classifyScale - > phase . data ( ) ,
readahead . phase . data ( ) ,
classifyScale - > bufSize ) ;
}
2022-05-25 11:26:16 +01:00
2022-08-05 14:58:12 +01:00
v_fftshift ( readahead . timeDomain . data ( ) , classify ) ;
m_scaleData . at ( classify ) - > fft . forward ( readahead . timeDomain . data ( ) ,
classifyScale - > real . data ( ) ,
classifyScale - > imag . data ( ) ) ;
for ( int b = 0 ; b < m_guideConfiguration . fftBandLimitCount ; + + b ) {
const auto & band = m_guideConfiguration . fftBandLimits [ b ] ;
if ( band . fftSize = = classify ) {
ToPolarSpec spec ;
spec . magFromBin = 0 ;
spec . magBinCount = classify / 2 + 1 ;
spec . polarFromBin = band . b0min ;
spec . polarBinCount = band . b1max - band . b0min + 1 ;
convertToPolar ( readahead . mag . data ( ) ,
readahead . phase . data ( ) ,
classifyScale - > real . data ( ) ,
classifyScale - > imag . data ( ) ,
spec ) ;
2022-05-25 11:26:16 +01:00
2022-08-05 14:58:12 +01:00
v_scale ( classifyScale - > mag . data ( ) ,
1.0 / double ( classify ) ,
classifyScale - > mag . size ( ) ) ;
break ;
}
2022-05-25 11:26:16 +01:00
}
2022-08-05 14:58:12 +01:00
cd - > haveReadahead = true ;
}
2022-05-27 11:17:20 +01:00
2022-05-27 14:58:42 +01:00
// For the others (and the classify as well, if the inhop has
2022-08-05 14:58:12 +01:00
// changed or we aren't using readahead or haven't filled the
// readahead yet) we operate directly in the scale data and
// restrict the range for cartesian-polar conversion
2022-05-25 11:26:16 +01:00
for ( auto & it : cd - > scales ) {
int fftSize = it . first ;
2022-08-05 14:58:12 +01:00
if ( fftSize = = classify & & copyFromReadahead ) {
2022-06-06 17:44:22 +01:00
continue ;
}
2022-05-25 11:26:16 +01:00
auto & scale = it . second ;
2022-07-14 11:55:21 +01:00
2022-05-25 11:26:16 +01:00
v_fftshift ( scale - > timeDomain . data ( ) , fftSize ) ;
m_scaleData . at ( fftSize ) - > fft . forward ( scale - > timeDomain . data ( ) ,
scale - > real . data ( ) ,
scale - > imag . data ( ) ) ;
2022-08-03 14:16:17 +01:00
for ( int b = 0 ; b < m_guideConfiguration . fftBandLimitCount ; + + b ) {
const auto & band = m_guideConfiguration . fftBandLimits [ b ] ;
if ( band . fftSize = = fftSize ) {
2022-06-17 16:32:14 +01:00
ToPolarSpec spec ;
// For the classify scale we always want the full
2022-06-17 16:56:09 +01:00
// range, as all the magnitudes (though not
// necessarily all phases) are potentially relevant to
// classification and formant analysis. But this case
2022-08-05 14:58:12 +01:00
// here only happens if we don't copyFromReadahead -
// the normal case is above and, er, copies from the
2022-06-17 16:56:09 +01:00
// previous readahead.
2022-06-17 16:32:14 +01:00
if ( fftSize = = classify ) {
spec . magFromBin = 0 ;
spec . magBinCount = classify / 2 + 1 ;
2022-08-03 14:16:17 +01:00
spec . polarFromBin = band . b0min ;
spec . polarBinCount = band . b1max - band . b0min + 1 ;
2022-06-17 16:32:14 +01:00
} else {
2022-08-03 14:16:17 +01:00
spec . magFromBin = band . b0min ;
spec . magBinCount = band . b1max - band . b0min + 1 ;
2022-06-17 16:32:14 +01:00
spec . polarFromBin = spec . magFromBin ;
spec . polarBinCount = spec . magBinCount ;
}
convertToPolar ( scale - > mag . data ( ) ,
scale - > phase . data ( ) ,
scale - > real . data ( ) ,
scale - > imag . data ( ) ,
spec ) ;
v_scale ( scale - > mag . data ( ) + spec . magFromBin ,
2022-05-25 11:26:16 +01:00
1.0 / double ( fftSize ) ,
2022-06-17 16:32:14 +01:00
spec . magBinCount ) ;
2022-05-25 11:26:16 +01:00
break ;
2022-05-23 15:04:34 +01:00
}
2022-05-25 11:26:16 +01:00
}
}
2022-06-06 16:51:02 +01:00
if ( m_parameters . options & RubberBandStretcher : : OptionFormantPreserved ) {
analyseFormant ( c ) ;
adjustFormant ( c ) ;
}
2022-06-06 16:37:44 +01:00
2022-05-25 16:25:03 +01:00
// Use the classification scale to get a bin segmentation and
// calculate the adaptive frequency guide for this channel
2022-05-27 14:58:42 +01:00
v_copy ( cd - > classification . data ( ) , cd - > nextClassification . data ( ) ,
cd - > classification . size ( ) ) ;
2022-08-05 14:58:12 +01:00
if ( m_useReadahead ) {
cd - > classifier - > classify ( readahead . mag . data ( ) ,
cd - > nextClassification . data ( ) ) ;
} else {
cd - > classifier - > classify ( classifyScale - > mag . data ( ) ,
cd - > nextClassification . data ( ) ) ;
}
2022-05-27 14:58:42 +01:00
2022-05-25 11:26:16 +01:00
cd - > prevSegmentation = cd - > segmentation ;
cd - > segmentation = cd - > nextSegmentation ;
2022-05-27 14:58:42 +01:00
cd - > nextSegmentation = cd - > segmenter - > segment ( cd - > nextClassification . data ( ) ) ;
2022-06-09 16:00:27 +01:00
/*
2022-06-09 14:16:49 +01:00
if ( c = = 0 ) {
double pb = cd - > nextSegmentation . percussiveBelow ;
double pa = cd - > nextSegmentation . percussiveAbove ;
double ra = cd - > nextSegmentation . residualAbove ;
2022-06-13 10:08:05 +01:00
int pbb = binForFrequency ( pb , classify , m_parameters . sampleRate ) ;
int pab = binForFrequency ( pa , classify , m_parameters . sampleRate ) ;
int rab = binForFrequency ( ra , classify , m_parameters . sampleRate ) ;
2022-06-09 14:16:49 +01:00
std : : cout < < " pb = " < < pb < < " , pbb = " < < pbb < < std : : endl ;
std : : cout < < " pa = " < < pa < < " , pab = " < < pab < < std : : endl ;
std : : cout < < " ra = " < < ra < < " , rab = " < < rab < < std : : endl ;
std : : cout < < " s: " ;
for ( int i = 0 ; i < = classify / 2 ; + + i ) {
if ( i > 0 ) std : : cout < < " , " ;
if ( i < pbb | | ( i > = pab & & i < = rab ) ) {
std : : cout < < " 1 " ;
} else {
std : : cout < < " 0 " ;
}
}
std : : cout < < std : : endl ;
}
2022-06-13 09:40:26 +01:00
*/
2022-06-17 15:01:26 +01:00
double ratio = getEffectiveRatio ( ) ;
2022-06-24 10:51:40 +01:00
if ( fabs ( ratio - 1.0 ) < 1.0e-7 ) {
2022-06-17 15:01:26 +01:00
+ + m_unityCount ;
} else {
m_unityCount = 0 ;
}
2022-07-04 10:52:50 +01:00
bool tighterChannelLock =
m_parameters . options & RubberBandStretcher : : OptionChannelsTogether ;
2022-08-05 14:58:12 +01:00
double magMean = v_mean ( classifyScale - > mag . data ( ) + 1 , classify / 2 ) ;
2023-03-24 17:51:44 +00:00
bool resetOnSilence = true ;
if ( useMidSide ( ) & & c = = 1 ) {
// Do not phase reset on silence in the side channel - the
// reset is propagated across to the mid channel, giving
// constant resets for e.g. mono material in a stereo
// configuration
resetOnSilence = false ;
}
2022-08-05 14:58:12 +01:00
if ( m_useReadahead ) {
m_guide . updateGuidance ( ratio ,
prevOuthop ,
classifyScale - > mag . data ( ) ,
classifyScale - > prevMag . data ( ) ,
cd - > readahead . mag . data ( ) ,
cd - > segmentation ,
cd - > prevSegmentation ,
cd - > nextSegmentation ,
magMean ,
m_unityCount ,
isRealTime ( ) ,
tighterChannelLock ,
2023-03-24 17:51:44 +00:00
resetOnSilence ,
2022-08-05 14:58:12 +01:00
cd - > guidance ) ;
} else {
m_guide . updateGuidance ( ratio ,
prevOuthop ,
classifyScale - > prevMag . data ( ) ,
classifyScale - > prevMag . data ( ) ,
classifyScale - > mag . data ( ) ,
cd - > segmentation ,
cd - > prevSegmentation ,
cd - > nextSegmentation ,
magMean ,
m_unityCount ,
isRealTime ( ) ,
tighterChannelLock ,
2023-03-24 17:51:44 +00:00
resetOnSilence ,
2022-08-05 14:58:12 +01:00
cd - > guidance ) ;
}
2022-07-04 10:52:50 +01:00
2022-06-13 09:40:26 +01:00
/*
if ( c = = 0 ) {
if ( cd - > guidance . kick . present ) {
std : : cout < < " k:2 " < < std : : endl ;
} else if ( cd - > guidance . preKick . present ) {
std : : cout < < " k:1 " < < std : : endl ;
} else {
std : : cout < < " k:0 " < < std : : endl ;
}
}
*/
2022-05-25 11:26:16 +01:00
}
2022-05-27 14:58:42 +01:00
void
2022-06-21 10:25:08 +01:00
R3Stretcher : : analyseFormant ( int c )
2022-05-27 14:58:42 +01:00
{
2022-07-14 10:02:39 +01:00
Profiler profiler ( " R3Stretcher::analyseFormant " ) ;
2022-06-06 16:51:02 +01:00
auto & cd = m_channelData . at ( c ) ;
auto & f = * cd - > formant ;
2022-05-27 14:58:42 +01:00
2022-06-06 16:51:02 +01:00
int fftSize = f . fftSize ;
int binCount = fftSize / 2 + 1 ;
2022-05-27 14:58:42 +01:00
2022-06-06 16:51:02 +01:00
auto & scale = cd - > scales . at ( fftSize ) ;
auto & scaleData = m_scaleData . at ( fftSize ) ;
2022-05-27 14:58:42 +01:00
2022-06-06 16:51:02 +01:00
scaleData - > fft . inverseCepstral ( scale - > mag . data ( ) , f . cepstra . data ( ) ) ;
2022-05-27 14:58:42 +01:00
2022-06-06 14:00:09 +01:00
int cutoff = int ( floor ( m_parameters . sampleRate / 650.0 ) ) ;
2022-05-27 14:58:42 +01:00
if ( cutoff < 1 ) cutoff = 1 ;
f . cepstra [ 0 ] / = 2.0 ;
f . cepstra [ cutoff - 1 ] / = 2.0 ;
2022-06-06 16:51:02 +01:00
for ( int i = cutoff ; i < fftSize ; + + i ) {
2022-05-27 14:58:42 +01:00
f . cepstra [ i ] = 0.0 ;
}
2022-06-06 16:51:02 +01:00
v_scale ( f . cepstra . data ( ) , 1.0 / double ( fftSize ) , cutoff ) ;
2022-05-27 14:58:42 +01:00
2022-06-06 16:51:02 +01:00
scaleData - > fft . forward ( f . cepstra . data ( ) , f . envelope . data ( ) , f . spare . data ( ) ) ;
2022-05-27 14:58:42 +01:00
v_exp ( f . envelope . data ( ) , binCount ) ;
2022-06-06 13:09:29 +01:00
v_square ( f . envelope . data ( ) , binCount ) ;
2022-05-27 14:58:42 +01:00
for ( int i = 0 ; i < binCount ; + + i ) {
if ( f . envelope [ i ] > 1.0e10 ) f . envelope [ i ] = 1.0e10 ;
}
}
2022-06-06 16:37:44 +01:00
void
2022-06-21 10:25:08 +01:00
R3Stretcher : : adjustFormant ( int c )
2022-06-06 16:37:44 +01:00
{
2022-07-14 10:02:39 +01:00
Profiler profiler ( " R3Stretcher::adjustFormant " ) ;
2022-06-06 16:37:44 +01:00
auto & cd = m_channelData . at ( c ) ;
for ( auto & it : cd - > scales ) {
int fftSize = it . first ;
auto & scale = it . second ;
2022-06-06 17:10:12 +01:00
int highBin = int ( floor ( fftSize * 10000.0 / m_parameters . sampleRate ) ) ;
2022-07-06 10:40:10 +01:00
process_t targetFactor = process_t ( cd - > formant - > fftSize ) / process_t ( fftSize ) ;
process_t formantScale = m_formantScale ;
2022-06-13 10:39:13 +01:00
if ( formantScale = = 0.0 ) formantScale = 1.0 / m_pitchScale ;
2022-07-06 10:40:10 +01:00
process_t sourceFactor = targetFactor / formantScale ;
process_t maxRatio = 60.0 ;
process_t minRatio = 1.0 / maxRatio ;
2022-06-06 17:06:52 +01:00
2022-08-03 14:16:17 +01:00
for ( int b = 0 ; b < m_guideConfiguration . fftBandLimitCount ; + + b ) {
const auto & band = m_guideConfiguration . fftBandLimits [ b ] ;
if ( band . fftSize ! = fftSize ) continue ;
for ( int i = band . b0min ; i < band . b1max & & i < highBin ; + + i ) {
2022-07-06 10:40:10 +01:00
process_t source = cd - > formant - > envelopeAt ( i * sourceFactor ) ;
process_t target = cd - > formant - > envelopeAt ( i * targetFactor ) ;
2022-06-06 17:10:12 +01:00
if ( target > 0.0 ) {
2022-07-06 10:40:10 +01:00
process_t ratio = source / target ;
2022-06-06 17:10:12 +01:00
if ( ratio < minRatio ) ratio = minRatio ;
if ( ratio > maxRatio ) ratio = maxRatio ;
scale - > mag [ i ] * = ratio ;
2022-06-06 17:06:52 +01:00
}
2022-06-06 16:37:44 +01:00
}
}
}
}
2022-06-13 09:40:26 +01:00
void
2022-06-21 10:25:08 +01:00
R3Stretcher : : adjustPreKick ( int c )
2022-06-13 09:40:26 +01:00
{
2022-07-14 13:44:22 +01:00
if ( isSingleWindowed ( ) ) return ;
2022-07-14 11:55:21 +01:00
2022-07-14 10:02:39 +01:00
Profiler profiler ( " R3Stretcher::adjustPreKick " ) ;
2022-06-13 09:40:26 +01:00
auto & cd = m_channelData . at ( c ) ;
auto fftSize = cd - > guidance . fftBands [ 0 ] . fftSize ;
if ( cd - > guidance . preKick . present ) {
auto & scale = cd - > scales . at ( fftSize ) ;
2022-06-13 10:08:05 +01:00
int from = binForFrequency ( cd - > guidance . preKick . f0 ,
fftSize , m_parameters . sampleRate ) ;
int to = binForFrequency ( cd - > guidance . preKick . f1 ,
fftSize , m_parameters . sampleRate ) ;
2022-06-13 09:40:26 +01:00
for ( int i = from ; i < = to ; + + i ) {
2022-07-06 10:40:10 +01:00
process_t diff = scale - > mag [ i ] - scale - > prevMag [ i ] ;
2022-06-13 09:40:26 +01:00
if ( diff > 0.0 ) {
scale - > pendingKick [ i ] = diff ;
scale - > mag [ i ] - = diff ;
}
}
} else if ( cd - > guidance . kick . present ) {
auto & scale = cd - > scales . at ( fftSize ) ;
2022-06-13 10:08:05 +01:00
int from = binForFrequency ( cd - > guidance . preKick . f0 ,
fftSize , m_parameters . sampleRate ) ;
int to = binForFrequency ( cd - > guidance . preKick . f1 ,
fftSize , m_parameters . sampleRate ) ;
2022-06-13 09:40:26 +01:00
for ( int i = from ; i < = to ; + + i ) {
scale - > mag [ i ] + = scale - > pendingKick [ i ] ;
scale - > pendingKick [ i ] = 0.0 ;
}
}
}
2022-05-25 11:26:16 +01:00
void
2022-07-07 10:35:01 +01:00
R3Stretcher : : synthesiseChannel ( int c , int outhop , bool draining )
2022-05-25 11:26:16 +01:00
{
2022-07-14 10:02:39 +01:00
Profiler profiler ( " R3Stretcher::synthesiseChannel " ) ;
2022-05-25 11:26:16 +01:00
int longest = m_guideConfiguration . longestFftSize ;
auto & cd = m_channelData . at ( c ) ;
2022-07-14 11:55:21 +01:00
2022-08-03 14:16:17 +01:00
for ( int b = 0 ; b < cd - > guidance . fftBandCount ; + + b ) {
const auto & band = cd - > guidance . fftBands [ b ] ;
int fftSize = band . fftSize ;
2022-07-14 11:55:21 +01:00
2022-05-25 11:26:16 +01:00
auto & scale = cd - > scales . at ( fftSize ) ;
auto & scaleData = m_scaleData . at ( fftSize ) ;
2022-06-17 16:56:09 +01:00
// copy to prevMag before filtering
v_copy ( scale - > prevMag . data ( ) ,
scale - > mag . data ( ) ,
scale - > bufSize ) ;
2022-07-06 10:40:10 +01:00
process_t winscale = process_t ( outhop ) / scaleData - > windowScaleFactor ;
2022-05-25 09:43:08 +01:00
2022-05-25 16:25:03 +01:00
// The frequency filter is applied naively in the frequency
// domain. Aliasing is reduced by the shorter resynthesis
2022-06-17 16:52:55 +01:00
// window. We resynthesise each scale individually, then sum -
// it's easier to manage scaling for in situations with a
// varying resynthesis hop
2022-06-13 10:08:05 +01:00
int lowBin = binForFrequency ( band . f0 , fftSize , m_parameters . sampleRate ) ;
int highBin = binForFrequency ( band . f1 , fftSize , m_parameters . sampleRate ) ;
2022-06-06 17:50:22 +01:00
if ( highBin % 2 = = 0 & & highBin > 0 ) - - highBin ;
2022-06-06 17:10:12 +01:00
2022-09-26 16:02:13 +01:00
int n = scale - > mag . size ( ) ;
if ( lowBin > = n ) lowBin = n - 1 ;
if ( highBin > = n ) highBin = n - 1 ;
if ( highBin < lowBin ) highBin = lowBin ;
2022-06-17 16:52:55 +01:00
if ( lowBin > 0 ) {
v_zero ( scale - > real . data ( ) , lowBin ) ;
v_zero ( scale - > imag . data ( ) , lowBin ) ;
2022-06-06 12:08:52 +01:00
}
2022-05-25 09:43:08 +01:00
2022-06-17 16:52:55 +01:00
v_scale ( scale - > mag . data ( ) + lowBin , winscale , highBin - lowBin ) ;
v_polar_to_cartesian ( scale - > real . data ( ) + lowBin ,
scale - > imag . data ( ) + lowBin ,
scale - > mag . data ( ) + lowBin ,
scale - > advancedPhase . data ( ) + lowBin ,
highBin - lowBin ) ;
2022-06-17 16:56:09 +01:00
if ( highBin < scale - > bufSize ) {
v_zero ( scale - > real . data ( ) + highBin , scale - > bufSize - highBin ) ;
v_zero ( scale - > imag . data ( ) + highBin , scale - > bufSize - highBin ) ;
2022-05-25 11:26:16 +01:00
}
2022-05-24 16:54:05 +01:00
2022-05-25 11:26:16 +01:00
scaleData - > fft . inverse ( scale - > real . data ( ) ,
scale - > imag . data ( ) ,
scale - > timeDomain . data ( ) ) ;
2022-06-24 10:51:40 +01:00
2022-05-25 11:26:16 +01:00
v_fftshift ( scale - > timeDomain . data ( ) , fftSize ) ;
2022-05-24 16:54:05 +01:00
2022-05-25 16:25:03 +01:00
// Synthesis window may be shorter than analysis window, so
// copy and cut only from the middle of the time-domain frame;
// and the accumulator length always matches the longest FFT
// size, so as to make mixing straightforward, so there is an
// additional offset needed for the target
2022-05-25 09:43:08 +01:00
2022-05-25 11:26:16 +01:00
int synthesisWindowSize = scaleData - > synthesisWindow . getSize ( ) ;
int fromOffset = ( fftSize - synthesisWindowSize ) / 2 ;
int toOffset = ( longest - synthesisWindowSize ) / 2 ;
scaleData - > synthesisWindow . cutAndAdd
( scale - > timeDomain . data ( ) + fromOffset ,
scale - > accumulator . data ( ) + toOffset ) ;
}
2022-05-25 09:43:08 +01:00
2022-05-25 13:47:40 +01:00
// Mix this channel and move the accumulator along
2022-05-24 12:00:54 +01:00
2022-05-25 13:47:40 +01:00
float * mixptr = cd - > mixdown . data ( ) ;
2022-05-25 11:26:16 +01:00
v_zero ( mixptr , outhop ) ;
2022-05-23 15:04:34 +01:00
2022-05-25 11:26:16 +01:00
for ( auto & it : cd - > scales ) {
auto & scale = it . second ;
2022-05-23 20:55:56 +01:00
2022-07-06 10:40:10 +01:00
process_t * accptr = scale - > accumulator . data ( ) ;
2022-05-25 13:47:40 +01:00
for ( int i = 0 ; i < outhop ; + + i ) {
mixptr [ i ] + = float ( accptr [ i ] ) ;
}
2022-05-24 12:00:54 +01:00
2022-05-25 11:26:16 +01:00
int n = scale - > accumulator . size ( ) - outhop ;
v_move ( accptr , accptr + outhop , n ) ;
v_zero ( accptr + n , outhop ) ;
2022-07-07 10:35:01 +01:00
if ( draining ) {
if ( scale - > accumulatorFill > outhop ) {
auto newFill = scale - > accumulatorFill - outhop ;
m_log . log ( 2 , " draining: reducing accumulatorFill from, to " , scale - > accumulatorFill , newFill ) ;
scale - > accumulatorFill = newFill ;
} else {
scale - > accumulatorFill = 0 ;
}
} else {
scale - > accumulatorFill = scale - > accumulator . size ( ) ;
}
2022-05-25 11:26:16 +01:00
}
2022-05-23 15:04:34 +01:00
}
}