From 657d07b5e6c5f2f1eee606241adc0d8edb4bcaff Mon Sep 17 00:00:00 2001
From: oparviai
Date: Wed, 13 Jun 2012 19:29:53 +0000
Subject: [PATCH] Improved flush() for better output stream duration accuracy
---
README.html | 3 +
include/FIFOSampleBuffer.h | 6 +-
include/FIFOSamplePipe.h | 13 ++
source/SoundTouch/FIFOSampleBuffer.cpp | 13 ++
source/SoundTouch/SoundTouch.cpp | 26 ++-
source/SoundTouchDLL/SoundTouchDLL.sln | 36 ++--
source/SoundTouchDLL/SoundTouchDLL.vcproj | 202 ++++++++++++++--------
7 files changed, 205 insertions(+), 94 deletions(-)
diff --git a/README.html b/README.html
index c5ab605..507efae 100644
--- a/README.html
+++ b/README.html
@@ -501,6 +501,8 @@ and estimates the BPM rate:
1.7.0:
- Sound quality improvements
+ - Improved flush() to adjust output sound stream duration to match better with
+ ideal duration
- Rewrote x86 cpu feature check to resolve compatibility problems
- Configure script automatically checks if CPU supports mmx & sse compatibility for GNU platform, and
the script support now "--enable-x86-optimizations" switch to allow disabling x86-specific optimizations.
@@ -712,6 +714,7 @@ submitted bugfixes since SoundTouch v1.3.1:
- Takashi Iwai
- Yuval Naveh
- Paulo Pizarro
+ - Blaise Potard
- RJ Ryan
- John Sheehy
- Tim Shuttleworth
diff --git a/include/FIFOSampleBuffer.h b/include/FIFOSampleBuffer.h
index e69918a..c315e65 100644
--- a/include/FIFOSampleBuffer.h
+++ b/include/FIFOSampleBuffer.h
@@ -89,7 +89,7 @@ private:
/// Returns current capacity.
uint getCapacity() const;
-
+
public:
/// Constructor
@@ -167,6 +167,10 @@ public:
/// Clears all the samples.
virtual void clear();
+
+ /// allow trimming (downwards) amount of samples in pipeline.
+ /// Returns adjusted amount of samples
+ uint adjustAmountOfSamples(uint numSamples);
};
}
diff --git a/include/FIFOSamplePipe.h b/include/FIFOSamplePipe.h
index ad982cb..e06de27 100644
--- a/include/FIFOSamplePipe.h
+++ b/include/FIFOSamplePipe.h
@@ -114,6 +114,11 @@ public:
/// Clears all the samples.
virtual void clear() = 0;
+
+ /// allow trimming (downwards) amount of samples in pipeline.
+ /// Returns adjusted amount of samples
+ virtual uint adjustAmountOfSamples(uint numSamples) = 0;
+
};
@@ -214,6 +219,14 @@ public:
{
return output->isEmpty();
}
+
+ /// allow trimming (downwards) amount of samples in pipeline.
+ /// Returns adjusted amount of samples
+ virtual uint adjustAmountOfSamples(uint numSamples)
+ {
+ return output->adjustAmountOfSamples(numSamples);
+ }
+
};
}
diff --git a/source/SoundTouch/FIFOSampleBuffer.cpp b/source/SoundTouch/FIFOSampleBuffer.cpp
index f9efee6..9e624c9 100644
--- a/source/SoundTouch/FIFOSampleBuffer.cpp
+++ b/source/SoundTouch/FIFOSampleBuffer.cpp
@@ -259,3 +259,16 @@ void FIFOSampleBuffer::clear()
samplesInBuffer = 0;
bufferPos = 0;
}
+
+
+/// allow trimming (downwards) amount of samples in pipeline.
+/// Returns adjusted amount of samples
+uint FIFOSampleBuffer::adjustAmountOfSamples(uint numSamples)
+{
+ if (numSamples < samplesInBuffer)
+ {
+ samplesInBuffer = numSamples;
+ }
+ return samplesInBuffer;
+}
+
diff --git a/source/SoundTouch/SoundTouch.cpp b/source/SoundTouch/SoundTouch.cpp
index b67dd2c..79ce263 100644
--- a/source/SoundTouch/SoundTouch.cpp
+++ b/source/SoundTouch/SoundTouch.cpp
@@ -345,12 +345,19 @@ void SoundTouch::putSamples(const SAMPLETYPE *samples, uint nSamples)
void SoundTouch::flush()
{
int i;
- uint nOut;
- SAMPLETYPE buff[128];
+ int nUnprocessed;
+ int nOut;
+ SAMPLETYPE buff[64*2]; // note: allocate 2*64 to cater 64 sample frames of stereo sound
- nOut = numSamples();
+ // check how many samples still await processing, and scale
+ // that by tempo & rate to get expected output sample count
+ nUnprocessed = numUnprocessedSamples();
+ nUnprocessed = (int)((double)nUnprocessed / (tempo * rate) + 0.5);
- memset(buff, 0, 128 * sizeof(SAMPLETYPE));
+ nOut = numSamples(); // ready samples currently in buffer ...
+ nOut += nUnprocessed; // ... and how many we expect there to be in the end
+
+ memset(buff, 0, 64 * channels * sizeof(SAMPLETYPE));
// "Push" the last active samples out from the processing pipeline by
// feeding blank samples into the processing pipeline until new,
// processed samples appear in the output (not however, more than
@@ -358,7 +365,16 @@ void SoundTouch::flush()
for (i = 0; i < 128; i ++)
{
putSamples(buff, 64);
- if (numSamples() != nOut) break; // new samples have appeared in the output!
+ if ((int)numSamples() >= nOut)
+ {
+ // Enough new samples have appeared into the output!
+ // As samples come from processing with bigger chunks, now truncate it
+ // back to maximum "nOut" samples to improve duration accuracy
+ adjustAmountOfSamples(nOut);
+
+ // finish
+ break;
+ }
}
// Clear working buffers
diff --git a/source/SoundTouchDLL/SoundTouchDLL.sln b/source/SoundTouchDLL/SoundTouchDLL.sln
index 61b1acf..6be4fb0 100644
--- a/source/SoundTouchDLL/SoundTouchDLL.sln
+++ b/source/SoundTouchDLL/SoundTouchDLL.sln
@@ -1,32 +1,28 @@
-Microsoft Visual Studio Solution File, Format Version 8.00
+Microsoft Visual Studio Solution File, Format Version 10.00
+# Visual Studio 2008
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SoundTouchDLL", "SoundTouchDLL.vcproj", "{164DE61D-6391-4265-8273-30740117D356}"
ProjectSection(ProjectDependencies) = postProject
{68A5DD20-7057-448B-8FE0-B6AC8D205509} = {68A5DD20-7057-448B-8FE0-B6AC8D205509}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SoundTouch", "..\SoundTouch\SoundTouch.vcproj", "{68A5DD20-7057-448B-8FE0-B6AC8D205509}"
- ProjectSection(ProjectDependencies) = postProject
- EndProjectSection
EndProject
Global
- GlobalSection(SolutionConfiguration) = preSolution
- Debug = Debug
- Release = Release
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Win32 = Debug|Win32
+ Release|Win32 = Release|Win32
EndGlobalSection
- GlobalSection(ProjectDependencies) = postSolution
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {164DE61D-6391-4265-8273-30740117D356}.Debug|Win32.ActiveCfg = Debug|Win32
+ {164DE61D-6391-4265-8273-30740117D356}.Debug|Win32.Build.0 = Debug|Win32
+ {164DE61D-6391-4265-8273-30740117D356}.Release|Win32.ActiveCfg = Release|Win32
+ {164DE61D-6391-4265-8273-30740117D356}.Release|Win32.Build.0 = Release|Win32
+ {68A5DD20-7057-448B-8FE0-B6AC8D205509}.Debug|Win32.ActiveCfg = Debug|Win32
+ {68A5DD20-7057-448B-8FE0-B6AC8D205509}.Debug|Win32.Build.0 = Debug|Win32
+ {68A5DD20-7057-448B-8FE0-B6AC8D205509}.Release|Win32.ActiveCfg = Release|Win32
+ {68A5DD20-7057-448B-8FE0-B6AC8D205509}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
- GlobalSection(ProjectConfiguration) = postSolution
- {164DE61D-6391-4265-8273-30740117D356}.Debug.ActiveCfg = Debug|Win32
- {164DE61D-6391-4265-8273-30740117D356}.Debug.Build.0 = Debug|Win32
- {164DE61D-6391-4265-8273-30740117D356}.Release.ActiveCfg = Release|Win32
- {164DE61D-6391-4265-8273-30740117D356}.Release.Build.0 = Release|Win32
- {68A5DD20-7057-448B-8FE0-B6AC8D205509}.Debug.ActiveCfg = Debug|Win32
- {68A5DD20-7057-448B-8FE0-B6AC8D205509}.Debug.Build.0 = Debug|Win32
- {68A5DD20-7057-448B-8FE0-B6AC8D205509}.Release.ActiveCfg = Release|Win32
- {68A5DD20-7057-448B-8FE0-B6AC8D205509}.Release.Build.0 = Release|Win32
- EndGlobalSection
- GlobalSection(ExtensibilityGlobals) = postSolution
- EndGlobalSection
- GlobalSection(ExtensibilityAddIns) = postSolution
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
diff --git a/source/SoundTouchDLL/SoundTouchDLL.vcproj b/source/SoundTouchDLL/SoundTouchDLL.vcproj
index 5d93fa8..d01b01a 100644
--- a/source/SoundTouchDLL/SoundTouchDLL.vcproj
+++ b/source/SoundTouchDLL/SoundTouchDLL.vcproj
@@ -1,127 +1,185 @@
+ Keyword="Win32Proj"
+ TargetFrameworkVersion="131072"
+ >
+ Name="Win32"
+ />
+
+
+ InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops"
+ CharacterSet="2"
+ >
+
+
+
+
+
+ Detect64BitPortabilityProblems="true"
+ DebugInformationFormat="3"
+ />
+ Name="VCManagedResourceCompilerTool"
+ />
+
+
+ TargetMachine="1"
+ />
+ Name="VCALinkTool"
+ />
+
+
+
+
+
-
-
-
-
-
-
-
-
+ CommandLine="copy $(OutDir)\*.dll ..\..\lib
copy $(OutDir)\*.lib ..\..\lib
"
+ />
+ InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC71.vsprops"
+ CharacterSet="2"
+ >
+
+
+
+
+
+ CallingConvention="0"
+ />
+ Name="VCManagedResourceCompilerTool"
+ />
+
+
+ TargetMachine="1"
+ />
+ Name="VCALinkTool"
+ />
+
+
+
+
+
-
-
-
-
-
-
-
-
+ CommandLine="copy $(OutDir)\*.dll ..\..\lib
copy $(OutDir)\*.lib ..\..\lib
"
+ />
@@ -130,32 +188,40 @@ copy $(OutDir)\*.lib ..\..\lib
+ UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
+ >
+ RelativePath=".\SoundTouchDLL.cpp"
+ >
+ UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
+ >
+ RelativePath=".\resource.h"
+ >
+ RelativePath=".\SoundTouchDLL.h"
+ >
+ UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
+ >
+ RelativePath=".\SoundTouchDLL.rc"
+ >
+ RelativePath=".\ReadMe.txt"
+ >