From 753848865dfda270c4adaf3095b2ea7117271749 Mon Sep 17 00:00:00 2001 From: oparviai Date: Sat, 15 Oct 2016 19:34:59 +0000 Subject: [PATCH] Added function to get duration ratio between the original input and processed output tracks. --- include/SoundTouch.h | 18 ++++++++++++++++++ source/SoundTouch/RateTransposer.cpp | 2 +- source/SoundTouch/RateTransposer.h | 3 +++ source/SoundTouch/SoundTouch.cpp | 11 ++++++++++- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/SoundTouch.h b/include/SoundTouch.h index 482d312..edbee84 100644 --- a/include/SoundTouch.h +++ b/include/SoundTouch.h @@ -259,6 +259,24 @@ public: /// Sets sample rate. void setSampleRate(uint srate); + /// Get ratio between input and output audio durations, useful for calculating + /// processed output duration: if you'll process a stream of N samples, then + /// you can expect to get out N * getInputOutputSampleRatio() samples. + /// + /// This ratio will give accurate target duration ratio for a full audio track, + /// given that the the whole track is processed with same processing parameters. + /// + /// If this ratio is applied to calculate intermediate offsets inside a processing + /// stream, then this ratio is approximate and can deviate +- some tens of milliseconds + /// from ideal offset, yet by end of the audio stream the duration ratio will become + /// exact. + /// + /// Example: if processing with parameters "-tempo=15 -pitch=-3", the function + /// will return value 0.8695652... Now, if processing an audio stream whose duration + /// is exactly one million audio samples, then you can expect the processed + /// output duration be 0.869565 * 1000000 = 869565 samples. + double getInputOutputSampleRatio(); + /// Flushes the last samples from the processing pipeline to the output. /// Clears also the internal processing buffers. // diff --git a/source/SoundTouch/RateTransposer.cpp b/source/SoundTouch/RateTransposer.cpp index 1b61eae..926adc6 100644 --- a/source/SoundTouch/RateTransposer.cpp +++ b/source/SoundTouch/RateTransposer.cpp @@ -211,7 +211,7 @@ int RateTransposer::isEmpty() const /// Return approximate initial input-output latency int RateTransposer::getLatency() const { - return (bUseAAFilter) ? pAAFilter->getLength() : 0; + return (bUseAAFilter) ? pAAFilter->getLength() : 0; } diff --git a/source/SoundTouch/RateTransposer.h b/source/SoundTouch/RateTransposer.h index b122d4f..87f1dff 100644 --- a/source/SoundTouch/RateTransposer.h +++ b/source/SoundTouch/RateTransposer.h @@ -172,6 +172,9 @@ public: /// Returns nonzero if there aren't any samples available for outputting. int isEmpty() const; + + /// Return approximate initial input-output latency + int getLatency() const; }; } diff --git a/source/SoundTouch/SoundTouch.cpp b/source/SoundTouch/SoundTouch.cpp index b942b80..7617f97 100644 --- a/source/SoundTouch/SoundTouch.cpp +++ b/source/SoundTouch/SoundTouch.cpp @@ -475,7 +475,6 @@ int SoundTouch::getSetting(int settingId) const return size; } - case SETTING_NOMINAL_OUTPUT_SEQUENCE : { int size = pTDStretch->getOutputBatchSize(); @@ -514,6 +513,7 @@ int SoundTouch::getSetting(int settingId) const } + // Clears all the samples in the object's output and internal processing // buffers. void SoundTouch::clear() @@ -567,3 +567,12 @@ uint SoundTouch::receiveSamples(uint maxSamples) samplesOutput += (long)ret; return ret; } + + +/// Get ratio between input and output audio durations, useful for calculating +/// processed output duration: if you'll process a stream of N samples, then +/// you can expect to get out N * getInputOutputSampleRatio() samples. +double SoundTouch::getInputOutputSampleRatio() +{ + return 1.0 / (tempo * rate); +}