soundtouch/0000775000175100017510000000000012577461413012313 5ustar vmdevvmdevsoundtouch/configure.ac0000664000175100017510000002214512577461413014605 0ustar vmdevvmdevdnl SoundTouch configure.ac, by David W. Durham dnl dnl $Id: configure.ac 230 2015-09-20 07:38:32Z oparviai $ dnl dnl This file is part of SoundTouch, an audio processing library for pitch/time adjustments dnl dnl SoundTouch is free software; you can redistribute it and/or modify it under the dnl terms of the GNU General Public License as published by the Free Software dnl Foundation; either version 2 of the License, or (at your option) any later dnl version. dnl dnl SoundTouch is distributed in the hope that it will be useful, but WITHOUT ANY dnl WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS dnl FOR A PARTICULAR PURPOSE. See the GNU General Public License for more dnl details. dnl dnl You should have received a copy of the GNU General Public License along with dnl this program; if not, write to the Free Software Foundation, Inc., 59 Temple dnl Place - Suite 330, Boston, MA 02111-1307, USA # Process this file with autoconf to produce a configure script. AC_INIT([SoundTouch], [1.9.2], [http://www.surina.net/soundtouch]) dnl Default to libSoundTouch.so.$LIB_SONAME.0.0 LIB_SONAME=1 AC_SUBST(LIB_SONAME) AC_CONFIG_AUX_DIR(config) AC_CONFIG_MACRO_DIR([config/m4]) AM_CONFIG_HEADER([config.h include/soundtouch_config.h]) AM_INIT_AUTOMAKE AM_SILENT_RULES([yes]) #AC_DISABLE_SHARED dnl This makes libtool only build static libs AC_DISABLE_STATIC dnl This makes libtool only build shared libs #AC_GNU_SOURCE dnl enable posix extensions in glibc AC_LANG(C++) dnl ############################################################################ dnl # Checks for programs # dnl ############################################################################ AC_PROG_CXX #AC_PROG_AWK AC_PROG_CC AC_PROG_CPP AC_PROG_CXXCPP AC_PROG_INSTALL #AC_PROG_LN_S AC_PROG_MAKE_SET AM_PROG_LIBTOOL dnl turn on using libtool dnl ############################################################################ dnl # Checks for header files # dnl ############################################################################ AC_HEADER_STDC #AC_HEADER_SYS_WAIT # add any others you want to check for here AC_CHECK_HEADERS([cpuid.h]) if test "x$ac_cv_header_cpuid_h" = "xno"; then AC_MSG_WARN([The cpuid.h file was not found therefore the x86 optimizations will be disabled.]) AC_MSG_WARN([If using a x86 architecture and optimizations are desired then please install gcc (>= 4.3).]) AC_MSG_WARN([If using a non-x86 architecture then this is expected and can be ignored.]) fi dnl ############################################################################ dnl # Checks for typedefs, structures, and compiler characteristics $ dnl ############################################################################ AC_C_CONST AC_C_INLINE #AC_TYPE_OFF_T #AC_TYPE_SIZE_T AC_ARG_ENABLE(integer-samples, [AC_HELP_STRING([--enable-integer-samples], [use integer samples instead of floats [default=no]])],, [enable_integer_samples=no]) AC_ARG_ENABLE(openmp, [AC_HELP_STRING([--enable-openmp], [use parallel multicore calculation through OpenMP [default=no]])],, [enable_openmp=no]) # Let the user enable/disable the x86 optimizations. # Useful when compiling on non-x86 architectures. AC_ARG_ENABLE([x86-optimizations], [AS_HELP_STRING([--enable-x86-optimizations], [use MMX or SSE optimization [default=yes]])],[enable_x86_optimizations="${enableval}"], [enable_x86_optimizations=yes]) # Tell the Makefile.am if the user wants to disable optimizations. # Makefile.am will enable them by default if support is available. # Note: We check if optimizations are supported a few lines down. AM_CONDITIONAL([X86_OPTIMIZATIONS], [test "x$enable_x86_optimizations" = "xyes"]) if test "x$enable_integer_samples" = "xyes"; then echo "****** Integer sample type enabled ******" AC_DEFINE(SOUNDTOUCH_INTEGER_SAMPLES,1,[Use Integer as Sample type]) else echo "****** Float sample type enabled ******" AC_DEFINE(SOUNDTOUCH_FLOAT_SAMPLES,1,[Use Float as Sample type]) fi if test "x$enable_openmp" = "xyes"; then echo "****** openmp optimizations enabled ******" AM_CXXFLAGS="-fopenmp $AM_CXXFLAGS" else echo "****** openmp optimizations disabled ******" fi # Check if optimizations are supported in the system at build time. if test "x$enable_x86_optimizations" = "xyes" -a "x$ac_cv_header_cpuid_h" = "xyes"; then echo "****** x86 optimizations enabled ******" original_saved_CXXFLAGS=$CXXFLAGS have_mmx_intrinsics=no CXXFLAGS="-mmmx -Winline $CXXFLAGS" # Check if the user can compile MMX code using intrinsics. # GCC supports MMX intrinsics since version 3.3 # A more recent GCC (>= 4.3) is recommended. AC_COMPILE_IFELSE([AC_LANG_SOURCE([[ #if defined(__GNUC__) && (__GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 3)) #error "Need GCC >= 3.3 for MMX intrinsics" #endif #include int main () { __m64 loop = _mm_cvtsi32_si64 (1); return _mm_cvtsi64_si32 (loop); }]])],[have_mmx_intrinsics=yes]) CXXFLAGS=$original_saved_CXXFLAGS # Inform the user if we did or did not find MMX support. # # If we enable optimization and integer samples we only require MMX. # Disable optimizations in the SSTypes.h file if this is not the case. if test "x$have_mmx_intrinsics" = "xyes" ; then echo "****** MMX support found ******" else echo "****** No MMX support found ******" if test "x$enable_integer_samples" = "xyes"; then echo "****** Disabling optimizations. Using integer samples with no MMX support ******" CPPFLAGS="-DSOUNDTOUCH_DISABLE_X86_OPTIMIZATIONS $CPPFLAGS" fi fi # SSE support original_saved_CXXFLAGS=$CXXFLAGS have_sse_intrinsics=no CXXFLAGS="-msse -Winline $CXXFLAGS" # Check if the user can compile SSE code using intrinsics. # GCC supports SSE intrinsics since version 3.3 # A more recent GCC (>= 4.3) is recommended. AC_COMPILE_IFELSE([AC_LANG_SOURCE([[ #if defined(__GNUC__) && (__GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 3)) #error "Need GCC >= 3.3 for SSE intrinsics" #endif #include int main () { _mm_setzero_ps(); return 0; }]])],[have_sse_intrinsics=yes]) CXXFLAGS=$original_saved_CXXFLAGS # Inform the user if we did or did not find SSE support. # # If we enable optimization and float samples we only require SSE. # Disable optimizations in the SSTypes.h file if this is not the case. if test "x$have_sse_intrinsics" = "xyes" ; then echo "****** SSE support found ******" else echo "****** No SSE support found ******" if test "x$enable_integer_samples" != "xyes"; then echo "****** Disabling optimizations. Using float samples with no SSE support ******" CPPFLAGS="-DSOUNDTOUCH_DISABLE_X86_OPTIMIZATIONS $CPPFLAGS" fi fi else # Disable optimizations in SSTypes.h since the user requested it. echo "****** x86 optimizations disabled ******" CPPFLAGS="-DSOUNDTOUCH_DISABLE_X86_OPTIMIZATIONS $CPPFLAGS" fi # Set AM_CXXFLAGS AC_SUBST([AM_CXXFLAGS], [$AM_CXXFLAGS]) # Empty default CXXFLAGS so user can set them if desirable #AC_SUBST([CXXFLAGS], [ ]) # SSTypes.h by default enables optimizations. Those already got disabled if # the user requested for it or if the system does not support them. # # Now tell the Makefile.am the optimizations that are supported. # Note: # Makefile.am already knows if the user asked for optimizations. We apply # optimizations by default (if support is available) and then disable all of # them if the user requested it. AM_CONDITIONAL([HAVE_MMX], [test "x$have_mmx_intrinsics" = "xyes"]) AM_CONDITIONAL([HAVE_SSE], [test "x$have_sse_intrinsics" = "xyes"]) dnl ############################################################################ dnl # Checks for library functions/classes # dnl ############################################################################ AC_FUNC_MALLOC AC_TYPE_SIGNAL dnl make -lm get added to the LIBS AC_CHECK_LIB(m, sqrt,,AC_MSG_ERROR([compatible libc math library not found])) dnl add whatever functions you might want to check for here #AC_CHECK_FUNCS([floor ftruncate memmove memset mkdir modf pow realpath sqrt strchr strdup strerror strrchr strstr strtol]) dnl ############################################################################ dnl # Internationaliation and Localiation # dnl ############################################################################ #AM_GNU_GETTEXT_VERSION([0.11.5]) #AM_GNU_GETTEXT([external]) dnl ############################################################################ dnl # Final # dnl ############################################################################ AC_CONFIG_FILES([ Makefile source/Makefile source/SoundTouch/Makefile source/SoundStretch/Makefile include/Makefile ]) AC_OUTPUT( soundtouch.pc ) dnl use 'echo' to put stuff here if you want a message to the builder soundtouch/include/0000775000175100017510000000000012577461413013736 5ustar vmdevvmdevsoundtouch/include/FIFOSampleBuffer.h0000664000175100017510000001652312577461413017135 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// A buffer class for temporarily storaging sound samples, operates as a /// first-in-first-out pipe. /// /// Samples are added to the end of the sample buffer with the 'putSamples' /// function, and are received from the beginning of the buffer by calling /// the 'receiveSamples' function. The class automatically removes the /// output samples from the buffer as well as grows the storage size /// whenever necessary. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2014-01-05 23:40:22 +0200 (Sun, 05 Jan 2014) $ // File revision : $Revision: 4 $ // // $Id: FIFOSampleBuffer.h 177 2014-01-05 21:40:22Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef FIFOSampleBuffer_H #define FIFOSampleBuffer_H #include "FIFOSamplePipe.h" namespace soundtouch { /// Sample buffer working in FIFO (first-in-first-out) principle. The class takes /// care of storage size adjustment and data moving during input/output operations. /// /// Notice that in case of stereo audio, one sample is considered to consist of /// both channel data. class FIFOSampleBuffer : public FIFOSamplePipe { private: /// Sample buffer. SAMPLETYPE *buffer; // Raw unaligned buffer memory. 'buffer' is made aligned by pointing it to first // 16-byte aligned location of this buffer SAMPLETYPE *bufferUnaligned; /// Sample buffer size in bytes uint sizeInBytes; /// How many samples are currently in buffer. uint samplesInBuffer; /// Channels, 1=mono, 2=stereo. uint channels; /// Current position pointer to the buffer. This pointer is increased when samples are /// removed from the pipe so that it's necessary to actually rewind buffer (move data) /// only new data when is put to the pipe. uint bufferPos; /// Rewind the buffer by moving data from position pointed by 'bufferPos' to real /// beginning of the buffer. void rewind(); /// Ensures that the buffer has capacity for at least this many samples. void ensureCapacity(uint capacityRequirement); /// Returns current capacity. uint getCapacity() const; public: /// Constructor FIFOSampleBuffer(int numChannels = 2 ///< Number of channels, 1=mono, 2=stereo. ///< Default is stereo. ); /// destructor ~FIFOSampleBuffer(); /// Returns a pointer to the beginning of the output samples. /// This function is provided for accessing the output samples directly. /// Please be careful for not to corrupt the book-keeping! /// /// When using this function to output samples, also remember to 'remove' the /// output samples from the buffer by calling the /// 'receiveSamples(numSamples)' function virtual SAMPLETYPE *ptrBegin(); /// Returns a pointer to the end of the used part of the sample buffer (i.e. /// where the new samples are to be inserted). This function may be used for /// inserting new samples into the sample buffer directly. Please be careful /// not corrupt the book-keeping! /// /// When using this function as means for inserting new samples, also remember /// to increase the sample count afterwards, by calling the /// 'putSamples(numSamples)' function. SAMPLETYPE *ptrEnd( uint slackCapacity ///< How much free capacity (in samples) there _at least_ ///< should be so that the caller can succesfully insert the ///< desired samples to the buffer. If necessary, the function ///< grows the buffer size to comply with this requirement. ); /// Adds 'numSamples' pcs of samples from the 'samples' memory position to /// the sample buffer. virtual void putSamples(const SAMPLETYPE *samples, ///< Pointer to samples. uint numSamples ///< Number of samples to insert. ); /// Adjusts the book-keeping to increase number of samples in the buffer without /// copying any actual samples. /// /// This function is used to update the number of samples in the sample buffer /// when accessing the buffer directly with 'ptrEnd' function. Please be /// careful though! virtual void putSamples(uint numSamples ///< Number of samples been inserted. ); /// Output samples from beginning of the sample buffer. Copies requested samples to /// output buffer and removes them from the sample buffer. If there are less than /// 'numsample' samples in the buffer, returns all that available. /// /// \return Number of samples returned. virtual uint receiveSamples(SAMPLETYPE *output, ///< Buffer where to copy output samples. uint maxSamples ///< How many samples to receive at max. ); /// Adjusts book-keeping so that given number of samples are removed from beginning of the /// sample buffer without copying them anywhere. /// /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly /// with 'ptrBegin' function. virtual uint receiveSamples(uint maxSamples ///< Remove this many samples from the beginning of pipe. ); /// Returns number of samples currently available. virtual uint numSamples() const; /// Sets number of channels, 1 = mono, 2 = stereo. void setChannels(int numChannels); /// Get number of channels int getChannels() { return channels; } /// Returns nonzero if there aren't any samples available for outputting. virtual int isEmpty() const; /// Clears all the samples. virtual void clear(); /// allow trimming (downwards) amount of samples in pipeline. /// Returns adjusted amount of samples uint adjustAmountOfSamples(uint numSamples); }; } #endif soundtouch/include/soundtouch_config.h.in0000664000175100017510000000020312577461413020227 0ustar vmdevvmdev/* Use Float as Sample type */ #undef SOUNDTOUCH_FLOAT_SAMPLES /* Use Integer as Sample type */ #undef SOUNDTOUCH_INTEGER_SAMPLES soundtouch/include/STTypes.h0000664000175100017510000001645412577461413015474 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Common type definitions for SoundTouch audio processing library. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2015-05-18 18:25:07 +0300 (Mon, 18 May 2015) $ // File revision : $Revision: 3 $ // // $Id: STTypes.h 215 2015-05-18 15:25:07Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef STTypes_H #define STTypes_H typedef unsigned int uint; typedef unsigned long ulong; // Patch for MinGW: on Win64 long is 32-bit #ifdef _WIN64 typedef unsigned long long ulongptr; #else typedef ulong ulongptr; #endif // Helper macro for aligning pointer up to next 16-byte boundary #define SOUNDTOUCH_ALIGN_POINTER_16(x) ( ( (ulongptr)(x) + 15 ) & ~(ulongptr)15 ) #if (defined(__GNUC__) && !defined(ANDROID)) // In GCC, include soundtouch_config.h made by config scritps. // Skip this in Android compilation that uses GCC but without configure scripts. #include "soundtouch_config.h" #endif namespace soundtouch { /// Activate these undef's to overrule the possible sampletype /// setting inherited from some other header file: //#undef SOUNDTOUCH_INTEGER_SAMPLES //#undef SOUNDTOUCH_FLOAT_SAMPLES /// If following flag is defined, always uses multichannel processing /// routines also for mono and stero sound. This is for routine testing /// purposes; output should be same with either routines, yet disabling /// the dedicated mono/stereo processing routines will result in slower /// runtime performance so recommendation is to keep this off. // #define USE_MULTICH_ALWAYS #if (defined(__SOFTFP__) && defined(ANDROID)) // For Android compilation: Force use of Integer samples in case that // compilation uses soft-floating point emulation - soft-fp is way too slow #undef SOUNDTOUCH_FLOAT_SAMPLES #define SOUNDTOUCH_INTEGER_SAMPLES 1 #endif #if !(SOUNDTOUCH_INTEGER_SAMPLES || SOUNDTOUCH_FLOAT_SAMPLES) /// Choose either 32bit floating point or 16bit integer sampletype /// by choosing one of the following defines, unless this selection /// has already been done in some other file. //// /// Notes: /// - In Windows environment, choose the sample format with the /// following defines. /// - In GNU environment, the floating point samples are used by /// default, but integer samples can be chosen by giving the /// following switch to the configure script: /// ./configure --enable-integer-samples /// However, if you still prefer to select the sample format here /// also in GNU environment, then please #undef the INTEGER_SAMPLE /// and FLOAT_SAMPLE defines first as in comments above. //#define SOUNDTOUCH_INTEGER_SAMPLES 1 //< 16bit integer samples #define SOUNDTOUCH_FLOAT_SAMPLES 1 //< 32bit float samples #endif #if (_M_IX86 || __i386__ || __x86_64__ || _M_X64) /// Define this to allow X86-specific assembler/intrinsic optimizations. /// Notice that library contains also usual C++ versions of each of these /// these routines, so if you're having difficulties getting the optimized /// routines compiled for whatever reason, you may disable these optimizations /// to make the library compile. #define SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS 1 /// In GNU environment, allow the user to override this setting by /// giving the following switch to the configure script: /// ./configure --disable-x86-optimizations /// ./configure --enable-x86-optimizations=no #ifdef SOUNDTOUCH_DISABLE_X86_OPTIMIZATIONS #undef SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS #endif #else /// Always disable optimizations when not using a x86 systems. #undef SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS #endif // If defined, allows the SIMD-optimized routines to take minor shortcuts // for improved performance. Undefine to require faithfully similar SIMD // calculations as in normal C implementation. #define SOUNDTOUCH_ALLOW_NONEXACT_SIMD_OPTIMIZATION 1 #ifdef SOUNDTOUCH_INTEGER_SAMPLES // 16bit integer sample type typedef short SAMPLETYPE; // data type for sample accumulation: Use 32bit integer to prevent overflows typedef long LONG_SAMPLETYPE; #ifdef SOUNDTOUCH_FLOAT_SAMPLES // check that only one sample type is defined #error "conflicting sample types defined" #endif // SOUNDTOUCH_FLOAT_SAMPLES #ifdef SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS // Allow MMX optimizations #define SOUNDTOUCH_ALLOW_MMX 1 #endif #else // floating point samples typedef float SAMPLETYPE; // data type for sample accumulation: Use double to utilize full precision. typedef double LONG_SAMPLETYPE; #ifdef SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS // Allow SSE optimizations #define SOUNDTOUCH_ALLOW_SSE 1 #endif #endif // SOUNDTOUCH_INTEGER_SAMPLES }; // define ST_NO_EXCEPTION_HANDLING switch to disable throwing std exceptions: // #define ST_NO_EXCEPTION_HANDLING 1 #ifdef ST_NO_EXCEPTION_HANDLING // Exceptions disabled. Throw asserts instead if enabled. #include #define ST_THROW_RT_ERROR(x) {assert((const char *)x);} #else // use c++ standard exceptions #include #include #define ST_THROW_RT_ERROR(x) {throw std::runtime_error(x);} #endif // When this #define is active, eliminates a clicking sound when the "rate" or "pitch" // parameter setting crosses from value <1 to >=1 or vice versa during processing. // Default is off as such crossover is untypical case and involves a slight sound // quality compromise. //#define SOUNDTOUCH_PREVENT_CLICK_AT_RATE_CROSSOVER 1 #endif soundtouch/include/SoundTouch.h0000664000175100017510000003151212577461413016204 0ustar vmdevvmdev////////////////////////////////////////////////////////////////////////////// /// /// SoundTouch - main class for tempo/pitch/rate adjusting routines. /// /// Notes: /// - Initialize the SoundTouch object instance by setting up the sound stream /// parameters with functions 'setSampleRate' and 'setChannels', then set /// desired tempo/pitch/rate settings with the corresponding functions. /// /// - The SoundTouch class behaves like a first-in-first-out pipeline: The /// samples that are to be processed are fed into one of the pipe by calling /// function 'putSamples', while the ready processed samples can be read /// from the other end of the pipeline with function 'receiveSamples'. /// /// - The SoundTouch processing classes require certain sized 'batches' of /// samples in order to process the sound. For this reason the classes buffer /// incoming samples until there are enough of samples available for /// processing, then they carry out the processing step and consequently /// make the processed samples available for outputting. /// /// - For the above reason, the processing routines introduce a certain /// 'latency' between the input and output, so that the samples input to /// SoundTouch may not be immediately available in the output, and neither /// the amount of outputtable samples may not immediately be in direct /// relationship with the amount of previously input samples. /// /// - The tempo/pitch/rate control parameters can be altered during processing. /// Please notice though that they aren't currently protected by semaphores, /// so in multi-thread application external semaphore protection may be /// required. /// /// - This class utilizes classes 'TDStretch' for tempo change (without modifying /// pitch) and 'RateTransposer' for changing the playback rate (that is, both /// tempo and pitch in the same ratio) of the sound. The third available control /// 'pitch' (change pitch but maintain tempo) is produced by a combination of /// combining the two other controls. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2015-09-20 10:38:32 +0300 (Sun, 20 Sep 2015) $ // File revision : $Revision: 4 $ // // $Id: SoundTouch.h 230 2015-09-20 07:38:32Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef SoundTouch_H #define SoundTouch_H #include "FIFOSamplePipe.h" #include "STTypes.h" namespace soundtouch { /// Soundtouch library version string #define SOUNDTOUCH_VERSION "1.9.2" /// SoundTouch library version id #define SOUNDTOUCH_VERSION_ID (10902) // // Available setting IDs for the 'setSetting' & 'get_setting' functions: /// Enable/disable anti-alias filter in pitch transposer (0 = disable) #define SETTING_USE_AA_FILTER 0 /// Pitch transposer anti-alias filter length (8 .. 128 taps, default = 32) #define SETTING_AA_FILTER_LENGTH 1 /// Enable/disable quick seeking algorithm in tempo changer routine /// (enabling quick seeking lowers CPU utilization but causes a minor sound /// quality compromising) #define SETTING_USE_QUICKSEEK 2 /// Time-stretch algorithm single processing sequence length in milliseconds. This determines /// to how long sequences the original sound is chopped in the time-stretch algorithm. /// See "STTypes.h" or README for more information. #define SETTING_SEQUENCE_MS 3 /// Time-stretch algorithm seeking window length in milliseconds for algorithm that finds the /// best possible overlapping location. This determines from how wide window the algorithm /// may look for an optimal joining location when mixing the sound sequences back together. /// See "STTypes.h" or README for more information. #define SETTING_SEEKWINDOW_MS 4 /// Time-stretch algorithm overlap length in milliseconds. When the chopped sound sequences /// are mixed back together, to form a continuous sound stream, this parameter defines over /// how long period the two consecutive sequences are let to overlap each other. /// See "STTypes.h" or README for more information. #define SETTING_OVERLAP_MS 5 /// Call "getSetting" with this ID to query nominal average processing sequence /// size in samples. This value tells approcimate value how many input samples /// SoundTouch needs to gather before it does DSP processing run for the sample batch. /// /// Notices: /// - This is read-only parameter, i.e. setSetting ignores this parameter /// - Returned value is approximate average value, exact processing batch /// size may wary from time to time /// - This parameter value is not constant but may change depending on /// tempo/pitch/rate/samplerate settings. #define SETTING_NOMINAL_INPUT_SEQUENCE 6 /// Call "getSetting" with this ID to query nominal average processing output /// size in samples. This value tells approcimate value how many output samples /// SoundTouch outputs once it does DSP processing run for a batch of input samples. /// /// Notices: /// - This is read-only parameter, i.e. setSetting ignores this parameter /// - Returned value is approximate average value, exact processing batch /// size may wary from time to time /// - This parameter value is not constant but may change depending on /// tempo/pitch/rate/samplerate settings. #define SETTING_NOMINAL_OUTPUT_SEQUENCE 7 class SoundTouch : public FIFOProcessor { private: /// Rate transposer class instance class RateTransposer *pRateTransposer; /// Time-stretch class instance class TDStretch *pTDStretch; /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters. double virtualRate; /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters. double virtualTempo; /// Virtual pitch parameter. Effective rate & tempo are calculated from these parameters. double virtualPitch; /// Flag: Has sample rate been set? bool bSrateSet; /// Accumulator for how many samples in total will be expected as output vs. samples put in, /// considering current processing settings. double samplesExpectedOut; /// Accumulator for how many samples in total have been read out from the processing so far long samplesOutput; /// Calculates effective rate & tempo valuescfrom 'virtualRate', 'virtualTempo' and /// 'virtualPitch' parameters. void calcEffectiveRateAndTempo(); protected : /// Number of channels uint channels; /// Effective 'rate' value calculated from 'virtualRate', 'virtualTempo' and 'virtualPitch' double rate; /// Effective 'tempo' value calculated from 'virtualRate', 'virtualTempo' and 'virtualPitch' double tempo; public: SoundTouch(); virtual ~SoundTouch(); /// Get SoundTouch library version string static const char *getVersionString(); /// Get SoundTouch library version Id static uint getVersionId(); /// Sets new rate control value. Normal rate = 1.0, smaller values /// represent slower rate, larger faster rates. void setRate(double newRate); /// Sets new tempo control value. Normal tempo = 1.0, smaller values /// represent slower tempo, larger faster tempo. void setTempo(double newTempo); /// Sets new rate control value as a difference in percents compared /// to the original rate (-50 .. +100 %) void setRateChange(double newRate); /// Sets new tempo control value as a difference in percents compared /// to the original tempo (-50 .. +100 %) void setTempoChange(double newTempo); /// Sets new pitch control value. Original pitch = 1.0, smaller values /// represent lower pitches, larger values higher pitch. void setPitch(double newPitch); /// Sets pitch change in octaves compared to the original pitch /// (-1.00 .. +1.00) void setPitchOctaves(double newPitch); /// Sets pitch change in semi-tones compared to the original pitch /// (-12 .. +12) void setPitchSemiTones(int newPitch); void setPitchSemiTones(double newPitch); /// Sets the number of channels, 1 = mono, 2 = stereo void setChannels(uint numChannels); /// Sets sample rate. void setSampleRate(uint srate); /// Flushes the last samples from the processing pipeline to the output. /// Clears also the internal processing buffers. // /// Note: This function is meant for extracting the last samples of a sound /// stream. This function may introduce additional blank samples in the end /// of the sound stream, and thus it's not recommended to call this function /// in the middle of a sound stream. void flush(); /// Adds 'numSamples' pcs of samples from the 'samples' memory position into /// the input of the object. Notice that sample rate _has_to_ be set before /// calling this function, otherwise throws a runtime_error exception. virtual void putSamples( const SAMPLETYPE *samples, ///< Pointer to sample buffer. uint numSamples ///< Number of samples in buffer. Notice ///< that in case of stereo-sound a single sample ///< contains data for both channels. ); /// Output samples from beginning of the sample buffer. Copies requested samples to /// output buffer and removes them from the sample buffer. If there are less than /// 'numsample' samples in the buffer, returns all that available. /// /// \return Number of samples returned. virtual uint receiveSamples(SAMPLETYPE *output, ///< Buffer where to copy output samples. uint maxSamples ///< How many samples to receive at max. ); /// Adjusts book-keeping so that given number of samples are removed from beginning of the /// sample buffer without copying them anywhere. /// /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly /// with 'ptrBegin' function. virtual uint receiveSamples(uint maxSamples ///< Remove this many samples from the beginning of pipe. ); /// Clears all the samples in the object's output and internal processing /// buffers. virtual void clear(); /// Changes a setting controlling the processing system behaviour. See the /// 'SETTING_...' defines for available setting ID's. /// /// \return 'true' if the setting was succesfully changed bool setSetting(int settingId, ///< Setting ID number. see SETTING_... defines. int value ///< New setting value. ); /// Reads a setting controlling the processing system behaviour. See the /// 'SETTING_...' defines for available setting ID's. /// /// \return the setting value. int getSetting(int settingId ///< Setting ID number, see SETTING_... defines. ) const; /// Returns number of samples currently unprocessed. virtual uint numUnprocessedSamples() const; /// Other handy functions that are implemented in the ancestor classes (see /// classes 'FIFOProcessor' and 'FIFOSamplePipe') /// /// - receiveSamples() : Use this function to receive 'ready' processed samples from SoundTouch. /// - numSamples() : Get number of 'ready' samples that can be received with /// function 'receiveSamples()' /// - isEmpty() : Returns nonzero if there aren't any 'ready' samples. /// - clear() : Clears all samples from ready/processing buffers. }; } #endif soundtouch/include/Makefile.am0000664000175100017510000000226212577461413015774 0ustar vmdevvmdev## Process this file with automake to create Makefile.in ## ## $Id: Makefile.am 11 2008-02-10 16:26:55Z oparviai $ ## ## Copyright (C) 2003 - David W. Durham ## ## This file is part of SoundTouch, an audio processing library for pitch/time adjustments ## ## SoundTouch 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. ## ## SoundTouch is distributed in the hope that it will be useful, but WITHOUT ANY ## WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR ## A PARTICULAR PURPOSE. See the GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License along with ## this program; if not, write to the Free Software Foundation, Inc., 59 Temple ## Place - Suite 330, Boston, MA 02111-1307, USA ## I used config/am_include.mk for common definitions include $(top_srcdir)/config/am_include.mk pkginclude_HEADERS=FIFOSampleBuffer.h FIFOSamplePipe.h SoundTouch.h STTypes.h BPMDetect.h soundtouch_config.h soundtouch/include/FIFOSamplePipe.h0000664000175100017510000002071712577461413016621 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// 'FIFOSamplePipe' : An abstract base class for classes that manipulate sound /// samples by operating like a first-in-first-out pipe: New samples are fed /// into one end of the pipe with the 'putSamples' function, and the processed /// samples are received from the other end with the 'receiveSamples' function. /// /// 'FIFOProcessor' : A base class for classes the do signal processing with /// the samples while operating like a first-in-first-out pipe. When samples /// are input with the 'putSamples' function, the class processes them /// and moves the processed samples to the given 'output' pipe object, which /// may be either another processing stage, or a fifo sample buffer object. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2012-06-13 22:29:53 +0300 (Wed, 13 Jun 2012) $ // File revision : $Revision: 4 $ // // $Id: FIFOSamplePipe.h 143 2012-06-13 19:29:53Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef FIFOSamplePipe_H #define FIFOSamplePipe_H #include #include #include "STTypes.h" namespace soundtouch { /// Abstract base class for FIFO (first-in-first-out) sample processing classes. class FIFOSamplePipe { public: // virtual default destructor virtual ~FIFOSamplePipe() {} /// Returns a pointer to the beginning of the output samples. /// This function is provided for accessing the output samples directly. /// Please be careful for not to corrupt the book-keeping! /// /// When using this function to output samples, also remember to 'remove' the /// output samples from the buffer by calling the /// 'receiveSamples(numSamples)' function virtual SAMPLETYPE *ptrBegin() = 0; /// Adds 'numSamples' pcs of samples from the 'samples' memory position to /// the sample buffer. virtual void putSamples(const SAMPLETYPE *samples, ///< Pointer to samples. uint numSamples ///< Number of samples to insert. ) = 0; // Moves samples from the 'other' pipe instance to this instance. void moveSamples(FIFOSamplePipe &other ///< Other pipe instance where from the receive the data. ) { int oNumSamples = other.numSamples(); putSamples(other.ptrBegin(), oNumSamples); other.receiveSamples(oNumSamples); }; /// Output samples from beginning of the sample buffer. Copies requested samples to /// output buffer and removes them from the sample buffer. If there are less than /// 'numsample' samples in the buffer, returns all that available. /// /// \return Number of samples returned. virtual uint receiveSamples(SAMPLETYPE *output, ///< Buffer where to copy output samples. uint maxSamples ///< How many samples to receive at max. ) = 0; /// Adjusts book-keeping so that given number of samples are removed from beginning of the /// sample buffer without copying them anywhere. /// /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly /// with 'ptrBegin' function. virtual uint receiveSamples(uint maxSamples ///< Remove this many samples from the beginning of pipe. ) = 0; /// Returns number of samples currently available. virtual uint numSamples() const = 0; // Returns nonzero if there aren't any samples available for outputting. virtual int isEmpty() const = 0; /// 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; }; /// Base-class for sound processing routines working in FIFO principle. With this base /// class it's easy to implement sound processing stages that can be chained together, /// so that samples that are fed into beginning of the pipe automatically go through /// all the processing stages. /// /// When samples are input to this class, they're first processed and then put to /// the FIFO pipe that's defined as output of this class. This output pipe can be /// either other processing stage or a FIFO sample buffer. class FIFOProcessor :public FIFOSamplePipe { protected: /// Internal pipe where processed samples are put. FIFOSamplePipe *output; /// Sets output pipe. void setOutPipe(FIFOSamplePipe *pOutput) { assert(output == NULL); assert(pOutput != NULL); output = pOutput; } /// Constructor. Doesn't define output pipe; it has to be set be /// 'setOutPipe' function. FIFOProcessor() { output = NULL; } /// Constructor. Configures output pipe. FIFOProcessor(FIFOSamplePipe *pOutput ///< Output pipe. ) { output = pOutput; } /// Destructor. virtual ~FIFOProcessor() { } /// Returns a pointer to the beginning of the output samples. /// This function is provided for accessing the output samples directly. /// Please be careful for not to corrupt the book-keeping! /// /// When using this function to output samples, also remember to 'remove' the /// output samples from the buffer by calling the /// 'receiveSamples(numSamples)' function virtual SAMPLETYPE *ptrBegin() { return output->ptrBegin(); } public: /// Output samples from beginning of the sample buffer. Copies requested samples to /// output buffer and removes them from the sample buffer. If there are less than /// 'numsample' samples in the buffer, returns all that available. /// /// \return Number of samples returned. virtual uint receiveSamples(SAMPLETYPE *outBuffer, ///< Buffer where to copy output samples. uint maxSamples ///< How many samples to receive at max. ) { return output->receiveSamples(outBuffer, maxSamples); } /// Adjusts book-keeping so that given number of samples are removed from beginning of the /// sample buffer without copying them anywhere. /// /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly /// with 'ptrBegin' function. virtual uint receiveSamples(uint maxSamples ///< Remove this many samples from the beginning of pipe. ) { return output->receiveSamples(maxSamples); } /// Returns number of samples currently available. virtual uint numSamples() const { return output->numSamples(); } /// Returns nonzero if there aren't any samples available for outputting. virtual int isEmpty() const { 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); } }; } #endif soundtouch/include/BPMDetect.h0000664000175100017510000001456512577461413015671 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Beats-per-minute (BPM) detection routine. /// /// The beat detection algorithm works as follows: /// - Use function 'inputSamples' to input a chunks of samples to the class for /// analysis. It's a good idea to enter a large sound file or stream in smallish /// chunks of around few kilosamples in order not to extinguish too much RAM memory. /// - Input sound data is decimated to approx 500 Hz to reduce calculation burden, /// which is basically ok as low (bass) frequencies mostly determine the beat rate. /// Simple averaging is used for anti-alias filtering because the resulting signal /// quality isn't of that high importance. /// - Decimated sound data is enveloped, i.e. the amplitude shape is detected by /// taking absolute value that's smoothed by sliding average. Signal levels that /// are below a couple of times the general RMS amplitude level are cut away to /// leave only notable peaks there. /// - Repeating sound patterns (e.g. beats) are detected by calculating short-term /// autocorrelation function of the enveloped signal. /// - After whole sound data file has been analyzed as above, the bpm level is /// detected by function 'getBpm' that finds the highest peak of the autocorrelation /// function, calculates it's precise location and converts this reading to bpm's. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2012-08-30 22:53:44 +0300 (Thu, 30 Aug 2012) $ // File revision : $Revision: 4 $ // // $Id: BPMDetect.h 150 2012-08-30 19:53:44Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef _BPMDetect_H_ #define _BPMDetect_H_ #include "STTypes.h" #include "FIFOSampleBuffer.h" namespace soundtouch { /// Minimum allowed BPM rate. Used to restrict accepted result above a reasonable limit. #define MIN_BPM 29 /// Maximum allowed BPM rate. Used to restrict accepted result below a reasonable limit. #define MAX_BPM 200 /// Class for calculating BPM rate for audio data. class BPMDetect { protected: /// Auto-correlation accumulator bins. float *xcorr; /// Amplitude envelope sliding average approximation level accumulator double envelopeAccu; /// RMS volume sliding average approximation level accumulator double RMSVolumeAccu; /// Sample average counter. int decimateCount; /// Sample average accumulator for FIFO-like decimation. soundtouch::LONG_SAMPLETYPE decimateSum; /// Decimate sound by this coefficient to reach approx. 500 Hz. int decimateBy; /// Auto-correlation window length int windowLen; /// Number of channels (1 = mono, 2 = stereo) int channels; /// sample rate int sampleRate; /// Beginning of auto-correlation window: Autocorrelation isn't being updated for /// the first these many correlation bins. int windowStart; /// FIFO-buffer for decimated processing samples. soundtouch::FIFOSampleBuffer *buffer; /// Updates auto-correlation function for given number of decimated samples that /// are read from the internal 'buffer' pipe (samples aren't removed from the pipe /// though). void updateXCorr(int process_samples /// How many samples are processed. ); /// Decimates samples to approx. 500 Hz. /// /// \return Number of output samples. int decimate(soundtouch::SAMPLETYPE *dest, ///< Destination buffer const soundtouch::SAMPLETYPE *src, ///< Source sample buffer int numsamples ///< Number of source samples. ); /// Calculates amplitude envelope for the buffer of samples. /// Result is output to 'samples'. void calcEnvelope(soundtouch::SAMPLETYPE *samples, ///< Pointer to input/output data buffer int numsamples ///< Number of samples in buffer ); /// remove constant bias from xcorr data void removeBias(); public: /// Constructor. BPMDetect(int numChannels, ///< Number of channels in sample data. int sampleRate ///< Sample rate in Hz. ); /// Destructor. virtual ~BPMDetect(); /// Inputs a block of samples for analyzing: Envelopes the samples and then /// updates the autocorrelation estimation. When whole song data has been input /// in smaller blocks using this function, read the resulting bpm with 'getBpm' /// function. /// /// Notice that data in 'samples' array can be disrupted in processing. void inputSamples(const soundtouch::SAMPLETYPE *samples, ///< Pointer to input/working data buffer int numSamples ///< Number of samples in buffer ); /// Analyzes the results and returns the BPM rate. Use this function to read result /// after whole song data has been input to the class by consecutive calls of /// 'inputSamples' function. /// /// \return Beats-per-minute rate, or zero if detection failed. float getBpm(); }; } #endif // _BPMDetect_H_ soundtouch/configure-file-README.txt0000664000175100017510000000041312577461413016703 0ustar vmdevvmdevStarting from SoundTouch 1.6.0, the "configure" file is removed from the source code package due to autoconf/automake version conflicts. Instead, generate the "configure" file using local tools by invoking "./bootstrap" script, then configure & compile as usual. soundtouch/README.html0000664000175100017510000011441412577461413014143 0ustar vmdevvmdev SoundTouch library README

SoundTouch audio processing library v1.9.2

SoundTouch library Copyright Olli Parviainen 2001-2015


1. Introduction

SoundTouch is an open-source audio processing library that allows changing the sound tempo, pitch and playback rate parameters independently from each other, i.e.:

  • Sound tempo can be increased or decreased while maintaining the original pitch
  • Sound pitch can be increased or decreased while maintaining the original tempo
  • Change playback rate that affects both tempo and pitch at the same time
  • Choose any combination of tempo/pitch/rate

1.1 Contact information

Author email: oparviai 'at' iki.fi

SoundTouch WWW page: http://soundtouch.surina.net


2. Compiling SoundTouch

Before compiling, notice that you can choose the sample data format if it's desirable to use floating point sample data instead of 16bit integers. See section "sample data format" for more information.

Also notice that SoundTouch can use OpenMP instructions for parallel computation to accelerate the runtime processing speed in multi-core systems, however, these improvements need to be separately enabled before compiling. See OpenMP notes in Chapter 3 below.

2.1. Building in Microsoft Windows

Project files for Microsoft Visual C++ are supplied with the source code package. Go to Microsoft WWW page to download Microsoft Visual Studio Express version for free.

To build the binaries with Visual C++ compiler, either run "make-win.bat" script, or open the appropriate project files in source code directories with Visual Studio. The final executable will appear under the "SoundTouch\bin" directory. If using the Visual Studio IDE instead of the make-win.bat script, directories bin and lib may need to be created manually to the SoundTouch package root for the final executables. The make-win.bat script creates these directories automatically.

OpenMP NOTE: If activating the OpenMP parallel computing in the compilation, the target program will require additional vcomp dll library to properly run. In Visual C++ 9.0 these libraries can be found in the following folders.

  • x86 32bit: C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.OPENMP\vcomp90.dll
  • x64 64bit: C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\redist\amd64\Microsoft.VC90.OPENMP\vcomp90.dll

In Visual Studio 2008, a SP1 version may be required for these libraries. In other VC++ versions the required library will be expectedly found in similar "redist" location.

Notice that as minor demonstration of a "dll hell" phenomenon both the 32-bit and 64-bit version of vcomp90.dll have the same filename but different contents, thus choose the proper version to allow the program start.

2.2. Building in Gnu platforms

The SoundTouch library compiles in practically any platform supporting GNU compiler (GCC) tools. SoundTouch requires GCC version 4.3 or later.

To build and install the binaries, run the following commands in /soundtouch directory:

./bootstrap  -
Creates "configure" file with local autoconf/automake toolset.
./configure  -

Configures the SoundTouch package for the local environment. Notice that "configure" file is not available before running the "./bootstrap" command as above.

make         -

Builds the SoundTouch library & SoundStretch utility. You can optionally add "-j" switch after "make" to speed up the compilation in multi-core systems.

make install -

Installs the SoundTouch & BPM libraries to /usr/local/lib and SoundStretch utility to /usr/local/bin. Please notice that 'root' privileges may be required to install the binaries to the destination locations.

2.2.1 Required GNU tools 

Bash shell, GNU C++ compiler, libtool, autoconf and automake tools are required for compiling the SoundTouch library. These are usually included with the GNU/Linux distribution, but if not, install these packages first. For example, Ubuntu Linux can acquire and install these with the following command:

sudo apt-get install automake autoconf libtool build-essential

2.2.2 Problems with GCC compiler compatibility

At the release time the SoundTouch package has been tested to compile in GNU/Linux platform. However, If you have problems getting the SoundTouch library compiled, try disabling optimizations that are specific for x86 processors by running ./configure script with switch

--enable-x86-optimizations=no
Alternatively, if you don't use GNU Configure system, edit file "include/STTypes.h" directly and remove the following definition:
#define SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS 1

2.2.3 Compiling Shared Library / DLL version in Cygwin

The GNU compilation does not automatically create a shared-library version of SoundTouch (.so or .dll). If such is desired, then you can create it as follows after running the usual compilation:

g++ -shared -static -DDLL_EXPORTS -I../../include -o SoundTouch.dll \
     SoundTouchDLL.cpp ../SoundTouch/.libs/libSoundTouch.a
sstrip SoundTouch.dll

2.1. Building in Android

Android compilation instructions are within the source code package, see file "source/Android-lib/README-SoundTouch-Android.html" in the source code package.

The Android compilation automatically builds separate .so library binaries for ARM, X86 and MIPS processor architectures. For optimal device support, include all these .so library binaries into the Android .apk application package, so the target Android device can automatically choose the proper library binary version to use.

The source/Android-lib folder includes also an Android example application that processes WAV audio files using SoundTouch library in Android devices.


3. About implementation & Usage tips

3.1. Supported sample data formats

The sample data format can be chosen between 16bit signed integer and 32bit floating point values. The default is 32bit floating point format, which will also provide slightly better sound quality over the integer format.

In Windows environment, the sample data format is chosen in file "STTypes.h" by choosing one of the following defines:

  • #define SOUNDTOUCH_INTEGER_SAMPLES for 16bit signed integer
  • #define SOUNDTOUCH_FLOAT_SAMPLES for 32bit floating point

In GNU environment, the floating sample format is used by default, but integer sample format can be chosen by giving the following switch to the configure script:

./configure --enable-integer-samples

The sample data can have either single (mono) or double (stereo) audio channel. Stereo data is interleaved so that every other data value is for left channel and every second for right channel. Notice that while it'd be possible in theory to process stereo sound as two separate mono channels, this isn't recommended because processing the channels separately would result in losing the phase coherency between the channels, which consequently would ruin the stereo effect.

Sample rates between 8000-48000H are supported.

3.2. Processing latency

The processing and latency constraints of the SoundTouch library are:

  • Input/output processing latency for the SoundTouch processor is around 100 ms. This is when time-stretching is used. If the rate transposing effect alone is used, the latency requirement is much shorter, see section 'About algorithms'.
  • Processing CD-quality sound (16bit stereo sound with 44100H sample rate) in real-time or faster is possible starting from processors equivalent to Intel Pentium 133Mh or better, if using the "quick" processing algorithm. If not using the "quick" mode or if floating point sample data are being used, several times more CPU power is typically required.

3.3. About algorithms

SoundTouch provides three seemingly independent effects: tempo, pitch and playback rate control. These three controls are implemented as combination of two primary effects, sample rate transposing and time-stretching.

Sample rate transposing affects both the audio stream duration and pitch. It's implemented simply by converting the original audio sample stream to the  desired duration by interpolating from the original audio samples. In SoundTouch, linear interpolation with anti-alias filtering is used. Theoretically a higher-order interpolation provide better result than 1st order linear interpolation, but in audio application linear interpolation together with anti-alias filtering performs subjectively about as well as higher-order filtering would.

Time-stretching means changing the audio stream duration without affecting it's pitch. SoundTouch uses WSOLA-like time-stretching routines that operate in the time domain. Compared to sample rate transposing, time-stretching is a much heavier operation and also requires a longer processing "window" of sound samples used by the processing algorithm, thus increasing the algorithm input/output latency. Typical i/o latency for the SoundTouch time-stretch algorithm is around 100 ms.

Sample rate transposing and time-stretching are then used together to produce the tempo, pitch and rate controls:

  • 'Tempo' control is implemented purely by time-stretching.
  • 'Rate' control is implemented purely by sample rate transposing.
  • 'Pitch' control is implemented as a combination of time-stretching and sample rate transposing. For example, to increase pitch the audio stream is first time-stretched to longer duration (without affecting pitch) and then transposed back to original duration by sample rate transposing, which simultaneously reduces duration and increases pitch. The result is original duration but increased pitch.

3.4 Tuning the algorithm parameters

The time-stretch algorithm has few parameters that can be tuned to optimize sound quality for certain application. The current default parameters have been chosen by iterative if-then analysis (read: "trial and error") to obtain best subjective sound quality in pop/rock music processing, but in applications processing different kind of sound the default parameter set may result into a sub-optimal result.

The time-stretch algorithm default parameter values are set by the following #defines in file "TDStretch.h":

#define DEFAULT_SEQUENCE_MS     AUTOMATIC
#define DEFAULT_SEEKWINDOW_MS AUTOMATIC
#define DEFAULT_OVERLAP_MS 8

These parameters affect to the time-stretch algorithm as follows:

  • DEFAULT_SEQUENCE_MS: This is the default length of a single processing sequence in milliseconds which determines the how the original sound is chopped in the time-stretch algorithm. Larger values mean fewer sequences are used in processing. In principle a larger value sounds better when slowing down the tempo, but worse when increasing the tempo and vice versa. 

    By default, this setting value is calculated automatically according to tempo value.
  • DEFAULT_SEEKWINDOW_MS: The seeking window default length in milliseconds is for the algorithm that seeks the best possible overlapping location. This determines from how wide a sample "window" the algorithm can use to find an optimal mixing location when the sound sequences are to be linked back together. 

    The bigger this window setting is, the higher the possibility to find a better mixing position becomes, but at the same time large values may cause a "drifting" sound artifact because neighboring sequences can be chosen at more uneven intervals. If there's a disturbing artifact that sounds as if a constant frequency was drifting around, try reducing this setting.

    By default, this setting value is calculated automatically according to tempo value.
  • DEFAULT_OVERLAP_MS: Overlap length in milliseconds. When the sound sequences are mixed back together to form again a continuous sound stream, this parameter defines how much the ends of the consecutive sequences will overlap with each other.

    This shouldn't be that critical parameter. If you reduce the DEFAULT_SEQUENCE_MS setting by a large amount, you might wish to try a smaller value on this.

Notice that these parameters can also be set during execution time with functions "TDStretch::setParameters()" and "SoundTouch::setSetting()".

The table below summaries how the parameters can be adjusted for different applications:

Parameter name Default value magnitude Larger value affects... Smaller value affects... Effect to CPU burden
SEQUENCE_MS
Default value is relatively large, chosen for slowing down music tempo Larger value is usually better for slowing down tempo. Growing the value decelerates the "echoing" artifact when slowing down the tempo. Smaller value might be better for speeding up tempo. Reducing the value accelerates the "echoing" artifact when slowing down the tempo Increasing the parameter value reduces computation burden
SEEKWINDOW_MS
Default value is relatively large, chosen for slowing down music tempo Larger value eases finding a good mixing position, but may cause a "drifting" artifact Smaller reduce possibility to find a good mixing position, but reduce the "drifting" artifact. Increasing the parameter value increases computation burden
OVERLAP_MS
Default value is relatively large, chosen to suit with above parameters.   If you reduce the "sequence ms" setting, you might wish to try a smaller value. Increasing the parameter value increases computation burden

3.5 Performance Optimizations

General optimizations:

The time-stretch routine has a 'quick' mode that substantially speeds up the algorithm but may slightly compromise the sound quality. This mode is activated by calling SoundTouch::setSetting() function with parameter  id of SETTING_USE_QUICKSEEK and value "1", i.e.

setSetting(SETTING_USE_QUICKSEEK, 1);

CPU-specific optimizations:

Intel x86 specific SIMD optimizations are implemented using compiler intrinsics, providing about a 3x processing speedup for x86 compatible processors vs. non-SIMD implementation:

  • Intel MMX optimized routines are used with x86 CPUs when 16bit integer sample type is used
  • Intel SSE optimized routines are used with x86 CPUs when 32bit floating point sample type is used

3.5 OpenMP parallel computation

SoundTouch 1.9 onwards support running the algorithms parallel in several CPU cores. Based on benchmark the experienced multi-core processing speed-up gain ranges between +30% (on a high-spec dual-core x86 Windows PC) to 215% (on a moderately low-spec quad-core ARM of Raspberry Pi2).

The parallel computing support is implemented using OpenMP spec 3.0 instructions. These instructions are supported by Visual C++ 2008 and later, and GCC v4.2 and later. Compilers that do not supporting OpenMP will ignore these optimizations and routines will still work properly. Possible warnings about unknown #pragmas are related to OpenMP support and can be safely ignored.

The OpenMP improvements are disabled by default, and need to be enabled by developer during compile-time. Reason for this is that parallel processing adds moderate runtime overhead in managing the multi-threading, so it may not be necessary nor desirable in all applications. For example real-time processing that is not constrained by CPU power will not benefit of speed-up provided by the parallel processing, in the contrary it may increase power consumption due to the increased overhead.

However, applications that run on low-spec multi-core CPUs and may otherwise have possibly constrained performance will benefit of the OpenMP improvements. This include for example multi-core embedded devices.

OpenMP parallel computation can be enabled before compiling SoundTouch library as follows:

  • Visual Studio: Open properties for the SoundTouch sub-project, browse to C/C++ and Language settings. Set there "OpenMP support" to "Yes". Alternatively add /openmp switch to command-line parameters
  • GNU: Run the configure script with "./configure --enable-openmp" switch, then run make as usually
  • Android: Add "-fopenmp" switches to compiler & linker options, see README-SoundTouch-Android.html in the source code package for more detailed instructions.

4. SoundStretch audio processing utility

SoundStretch audio processing utility
Copyright (c) Olli Parviainen 2002-2015

SoundStretch is a simple command-line application that can change tempo, pitch and playback rates of WAV sound files. This program is intended primarily to demonstrate how the "SoundTouch" library can be used to process sound in your own program, but it can as well be used for processing sound files.

4.1. SoundStretch Usage Instructions

SoundStretch Usage syntax:

soundstretch infilename outfilename [switches]

Where:

"infilename"
Name of the input sound data file (in .WAV audio file format). Give "stdin" as filename to use standard input pipe.
"outfilename"
Name of the output sound file where the resulting sound is saved (in .WAV audio file format). This parameter may be omitted if you  don't want to save the output (e.g. when only calculating BPM rate with '-bpm' switch). Give "stdout" as filename to use standard output pipe.
 [switches]
Are one or more control switches.

Available control switches are:

-tempo=n 
Change the sound tempo by n percents (n = -95.0 .. +5000.0 %)
-pitch=n
Change the sound pitch by n semitones (n = -60.0 .. + 60.0 semitones)
-rate=n
Change the sound playback rate by n percents (n = -95.0 .. +5000.0 %)
-bpm=n
Detect the Beats-Per-Minute (BPM) rate of the sound and adjust the tempo to meet 'n' BPMs. When this switch is applied, the "-tempo" switch is ignored. If "=n" is omitted, i.e. switch "-bpm" is used alone, then the BPM rate is estimated and displayed, but tempo not adjusted according to the BPM value.
-quick
Use quicker tempo change algorithm. Gains speed but loses sound quality.
-naa
Don't use anti-alias filtering in sample rate transposing. Gains speed but loses sound quality.
-license
Displays the program license text (LGPL)

Notes:

  • To use standard input/output pipes for processing, give "stdin" and "stdout" as input/output filenames correspondingly. The standard input/output pipes will still carry the audio data in .wav audio file format.
  • The numerical switches allow both integer (e.g. "-tempo=123") and decimal (e.g. "-tempo=123.45") numbers.
  • The "-naa" and/or "-quick" switches can be used to reduce CPU usage while compromising some sound quality
  • The BPM detection algorithm works by detecting repeating bass or drum patterns at low frequencies of <250Hz. A lower-than-expected BPM figure may be reported for music with uneven or complex bass patterns.

4.2. SoundStretch usage examples

Example 1

The following command increases tempo of the sound file "originalfile.wav" by 12.5% and stores result to file "destinationfile.wav":

soundstretch originalfile.wav destinationfile.wav -tempo=12.5

Example 2

The following command decreases the sound pitch (key) of the sound file "orig.wav" by two semitones and stores the result to file "dest.wav":

soundstretch orig.wav dest.wav -pitch=-2

Example 3

The following command processes the file "orig.wav" by decreasing the sound tempo by 25.3% and increasing the sound pitch (key) by 1.5 semitones. Resulting .wav audio data is directed to standard output pipe:

soundstretch orig.wav stdout -tempo=-25.3 -pitch=1.5

Example 4

The following command detects the BPM rate of the file "orig.wav" and adjusts the tempo to match 100 beats per minute. Result is stored to file "dest.wav":

soundstretch orig.wav dest.wav -bpm=100

Example 5

The following command reads .wav sound data from standard input pipe and estimates the BPM rate:

soundstretch stdin -bpm

Example 6

The following command tunes song from original 440Hz tuning to 432Hz tuning: this corresponds to lowering the pitch by -0.318 semitones:

soundstretch original.wav output.wav -pitch=-0.318

5. Change History

5.1. SoundTouch library Change History

1.9.2:

  • Fix in GNU package configuration

1.9.1:

  • Improved SoundTouch::flush() function so that it returns precisely the desired amount of samples for exact output duration control
  • Redesigned quickseek algorithm for improved sound quality when using the quickseek mode. The new quickseek algorithm can find 99% as good results as the default full-scan mode, while the quickseek algorithm is remarkable less CPU intensive.
  • Added adaptive integer divider scaling for improved sound quality when using integer processing algorithm

1.9:

  • Added support for parallel computation support via OpenMP primitives for better performance in multicore systems. Benchmarks show that achieved parallel processing speedup improvement typically range from +30% (x86 dual-core) to +180% (ARM quad-core). The OpenMP optimizations are disabled by default, see OpenMP notes above in this readme file how to enabled these optimizations.
  • Android: Added support for Android devices featuring X86 and MIPS CPUs, in addition to ARM CPUs.
  • Android: More versatile Android example application that processes WAV audio files with SoundTouch library
  • Replaced Windows-like 'BOOL' types with native 'bool'
  • Changed documentation token to "dist_doc_DATA" in Makefile.am file
  • Miscellaneous small fixes and improvements

1.8.0:

  • Added support for multi-channel audio processing
  • Added support for cubic and shannon interpolation for rate and pitch shift effects besides the original linear interpolation, to reduce aliasing at high frequencies due to interpolation. Cubic interpolation is used as default for floating point processing, and linear interpolation for integer processing.
  • Fixed bug in anti-alias filtering that limited stop-band attenuation to -10 dB instead of <-50dB, and increased filter length from 32 to 64 taps to further reduce aliasing due to frequency folding.
  • Performance improvements in cross-correlation algorithm
  • Other bug and compatibility fixes

1.7.1:

  • Added files for Android compilation

1.7.0:

  • Sound quality improvements/li>
  • 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.
  • Revised #define conditions for 32bit/64bit compatibility
  • gnu autoconf/automake script compatibility fixes
  • Tuned beat-per-minute detection algorithm

1.6.0:

  • Added automatic cutoff threshold adaptation to beat detection routine to better adapt BPM calculation to different types of music
  • Retired 3DNow! optimization support as 3DNow! is nowadays obsoleted and assembler code is nuisance to maintain
  • Retired "configure" file from source code package due to autoconf/automake versio conflicts, so that it is from now on to be generated by invoking "boostrap" script that uses locally available toolchain version for generating the "configure" file
  • Resolved namespace/label naming conflicts with other libraries by replacing global labels such as INTEGER_SAMPLES with more specific SOUNDTOUCH_INTEGER_SAMPLES etc.
  • Updated windows build scripts & project files for Visual Studio 2008 support
  • Updated SoundTouch.dll API for .NET compatibility
  • Added API for querying nominal processing input & output sample batch sizes

1.5.0:

  • Added normalization to correlation calculation and improvement automatic seek/sequence parameter calculation to improve sound quality
  • Bugfixes: 
    • Fixed negative array indexing in quick seek algorithm
    • FIR autoalias filter running too far in processing buffer
    • Check against zero sample count in rate transposing
    • Fix for x86-64 support: Removed pop/push instructions from the cpu detection algorithm. 
    • Check against empty buffers in FIFOSampleBuffer
    • Other minor fixes & code cleanup
  • Fixes in compilation scripts for non-Intel platforms
  • Added Dynamic-Link-Library (DLL) version of SoundTouch library build, provided with Delphi/Pascal wrapper for calling the dll routines
  • Added #define PREVENT_CLICK_AT_RATE_CROSSOVER that prevents a click artifact when crossing the nominal pitch from either positive to negative side or vice versa

1.4.1:

  • Fixed a buffer overflow bug in BPM detect algorithm routines if processing more than 2048 samples at one call 

1.4.0:

  • Improved sound quality by automatic calculation of time stretch algorithm processing parameters according to tempo setting
  • Moved BPM detection routines from SoundStretch application into SoundTouch library
  • Bugfixes: Usage of uninitialied variables, GNU build scripts, compiler errors due to 'const' keyword mismatch.
  • Source code cleanup

1.3.1:

  • Changed static class declaration to GCC 4.x compiler compatible syntax.
  • Enabled MMX/SSE-optimized routines also for GCC compilers. Earlier the MMX/SSE-optimized routines were written in compiler-specific inline assembler, now these routines are migrated to use compiler intrinsic syntax which allows compiling the same MMX/SSE-optimized source code with both Visual C++ and GCC compilers.
  • Set floating point as the default sample format and added switch to the GNU configure script for selecting the other sample format.

1.3.0:

  • Fixed tempo routine output duration inaccuracy due to rounding error
  • Implemented separate processing routines for integer and floating arithmetic to allow improvements to floating point routines (earlier used algorithms mostly optimized for integer arithmetic also for floating point samples)
  • Fixed a bug that distorts sound if sample rate changes during the sound stream
  • Fixed a memory leak that appeared in MMX/SSE/3DNow! optimized routines
  • Reduced redundant code pieces in MMX/SSE/3DNow! optimized routines vs. the standard C routines.
  • MMX routine incompatibility with new gcc compiler versions
  • Other miscellaneous bug fixes

1.2.1:

  • Added automake/autoconf scripts for GNU platforms (in courtesy of David Durham)
  • Fixed SCALE overflow bug in rate transposer routine.
  • Fixed 64bit address space bugs.
  • Created a 'soundtouch' namespace for SAMPLETYPE definitions.

1.2.0:

  • Added support for 32bit floating point sample data type with SSE/3DNow! optimizations for Win32 platform (SSE/3DNow! optimizations currently not supported in GCC environment)
  • Replaced 'make-gcc' script for GNU environment by master Makefile
  • Added time-stretch routine configurability to SoundTouch main class
  • Bugfixes

1.1.1:

  • Moved SoundTouch under lesser GPL license (LGPL). This allows using SoundTouch library in programs that aren't released under GPL license.
  • Changed MMX routine organiation so that MMX optimized routines are now implemented in classes that are derived from the basic classes having the standard non-mmx routines.
  • MMX routines to support gcc version 3.
  • Replaced windows makefiles by script using the .dsw files

1.0.1:

  • "mmx_gcc.cpp": Added "using namespace std" and removed "return 0" from a function with void return value to fix compiler errors when compiling the library in Solaris environment.
  • Moved file "FIFOSampleBuffer.h" to "include" directory to allow accessing the FIFOSampleBuffer class from external files.

1.0:

  • Initial release

 

5.2. SoundStretch application Change History

1.9:

  • Added support for WAV file 'fact' information chunk.

1.7.0:

  • Bugfixes in Wavfile: exception string formatting, avoid getLengthMs() integer precision overflow, support WAV files using 24/32bit sample format.

1.5.0:

  • Added "-speech" switch to activate algorithm parameters more suitable for speech processing than the default parameters tuned for music processing.

1.4.0:

  • Moved BPM detection routines from SoundStretch application into SoundTouch library
  • Allow using standard input/output pipes as audio processing input/output streams

1.3.0:

  • Simplified accessing WAV files with floating point sample format.

1.2.1:

  • Fixed 64bit address space bugs.

1.2.0:

  • Added support for 32bit floating point sample data type
  • Restructured the BPM routines into separate library
  • Fixed big-endian conversion bugs in WAV file routines (hopefully :)

1.1.1:

  • Fixed bugs in WAV file reading & added byte-order conversion for big-endian processors.
  • Moved SoundStretch source code under 'example' directory to highlight difference from SoundTouch stuff.
  • Replaced windows makefiles by script using the .dsw files
  • Output file name isn't required if output isn't desired (e.g. if using the switch '-bpm' in plain format only)

1.1:

  • Fixed "Release" settings in Microsoft Visual C++ project file (.dsp)
  • Added beats-per-minute (BPM) detection routine and command-line switch "-bpm"

1.01:

  • Initial release

6. Acknowledgements

Kudos for these people who have contributed to development or submitted bugfixes:

  • Arthur A
  • Richard Ash
  • Stanislav Brabec
  • Christian Budde
  • Chris Bryan
  • Jacek Caban
  • Brian Cameron
  • Jason Champion
  • David Clark
  • Patrick Colis
  • Miquel Colon
  • Jim Credland
  • Sandro Cumerlato
  • Justin Frankel
  • Masa H.
  • Jason Garland
  • Takashi Iwai
  • Thomas Klausner
  • Mathias Mhl
  • Yuval Naveh
  • Paulo Pizarro
  • Blaise Potard
  • Michael Pruett
  • Rajeev Puran
  • RJ Ryan
  • John Sheehy
  • Tim Shuttleworth
  • Albert Sirvent
  • John Stumpo
  • Katja Vetter

Moral greetings to all other contributors and users also!


7. LICENSE

SoundTouch audio processing library
Copyright (c) Olli Parviainen

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA


README.html file updated in Sep-2015

soundtouch/soundtouch.pc.in0000664000175100017510000000051212577461413015435 0ustar vmdevvmdevprefix=@prefix@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=@includedir@ Name: SoundTouch Description: SoundTouch is an open-source audio processing library for changing the Tempo, Pitch and Playback Rates of audio streams or files Version: @VERSION@ Libs: -L${libdir} -lSoundTouch Cflags: -I${includedir}/soundtouch soundtouch/Makefile.am0000664000175100017510000000541712577461413014356 0ustar vmdevvmdev## Process this file with automake to create Makefile.in ## ## $Id: Makefile.am 199 2014-10-05 15:33:08Z oparviai $ ## ## Copyright (C) 2003 - David W. Durham ## ## This file is part of SoundTouch, an audio processing library for pitch/time adjustments ## ## SoundTouch 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. ## ## SoundTouch is distributed in the hope that it will be useful, but WITHOUT ANY ## WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR ## A PARTICULAR PURPOSE. See the GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License along with ## this program; if not, write to the Free Software Foundation, Inc., 59 Temple ## Place - Suite 330, Boston, MA 02111-1307, USA ## I used config/am_include.mk for common definitions include $(top_srcdir)/config/am_include.mk ## Descend into SUBDIRS and run make. Look at the Makefile.am files in the ## subdirectories Start at frontend_fox/Makefile.am to see how everything works. SUBDIRS=include source # list files that are documentation to be packaged in a release tarball and installed dist_doc_DATA=COPYING.TXT README.html # extra data files that are to be pacakged in a release tarball and installed into the data directory #pkgdata_DATA= # sets up for soundtouch.m4 to be installed m4datadir=$(datadir)/aclocal m4data_DATA=soundtouch.m4 ## These extra files and directories will be included in the distribution. by ## using make-dist by default many common filenames are automatically included ## such as AUTHORS, COPYING, etc the bootstrap script really shouldn't be a part ## of a final package, but it is useful for developers who might want to make ## changes to the configure scripts or makefiles. # NOTE: wouldn't have to list the .TXT file if it were named without the .TXT EXTRA_DIST= \ soundtouch.m4 \ config/m4 \ bootstrap \ make-win.bat \ COPYING.TXT \ README.html pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = soundtouch.pc ## This removes stuff from the distribution which may be present ## from a cvs checkout or other build reasons dist-hook: rm -rf `find $(distdir) -type d -name CVS` # remove all CVS directories echo rm -rf `find $(distdir) -type f -name \.\#\*` # CVS revision files left around for some reason ## This happens at 'make distclean' #distcleancheck: # rm -rf files-that-may-also-need-to-be-deleted-on-'make distclean' # flag to aclocal where to find m4 macros for tests ACLOCAL_AMFLAGS = -I config/m4 AUTOMAKE_OPTIONS = foreign soundtouch/COPYING.TXT0000664000175100017510000005750412577461413014037 0ustar vmdevvmdev GNU LESSER GENERAL PUBLIC LICENSE Version 2.1, February 1999 Copyright (C) 1991, 1999 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. [This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public Licenses are intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This license, the Lesser General Public License, applies to some specially designated software packages--typically libraries--of the Free Software Foundation and other authors who decide to use it. You can use it too, but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular case, based on the explanations below. When we speak of free software, we are referring to freedom of use, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish); that you receive source code or can get it if you want it; that you can change the software and use pieces of it in new free programs; and that you are informed that you can do these things. To protect your rights, we need to make restrictions that forbid distributors to deny you these rights or to ask you to surrender these rights. These restrictions translate to certain responsibilities for you if you distribute copies of the library or if you modify it. For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link other code with the library, you must provide complete object files to the recipients, so that they can relink them with the library after making changes to the library and recompiling it. And you must show them these terms so they know their rights. We protect your rights with a two-step method: (1) we copyright the library, and (2) we offer you this license, which gives you legal permission to copy, distribute and/or modify the library. To protect each distributor, we want to make it very clear that there is no warranty for the free library. Also, if the library is modified by someone else and passed on, the recipients should know that what they have is not the original version, so that the original author's reputation will not be affected by problems that might be introduced by others. Finally, software patents pose a constant threat to the existence of any free program. We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent holder. Therefore, we insist that any patent license obtained for a version of the library must be consistent with the full freedom of use specified in this license. Most GNU software, including some libraries, is covered by the ordinary GNU General Public License. This license, the GNU Lesser General Public License, applies to certain designated libraries, and is quite different from the ordinary General Public License. We use this license for certain libraries in order to permit linking those libraries into non-free programs. When a program is linked with a library, whether statically or using a shared library, the combination of the two is legally speaking a combined work, a derivative of the original library. The ordinary General Public License therefore permits such linking only if the entire combination fits its criteria of freedom. The Lesser General Public License permits more lax criteria for linking other code with the library. We call this license the "Lesser" General Public License because it does Less to protect the user's freedom than the ordinary General Public License. It also provides other free software developers Less of an advantage over competing non-free programs. These disadvantages are the reason we use the ordinary General Public License for many libraries. However, the Lesser license provides advantages in certain special circumstances. For example, on rare occasions, there may be a special need to encourage the widest possible use of a certain library, so that it becomes a de-facto standard. To achieve this, non-free programs must be allowed to use the library. A more frequent case is that a free library does the same job as widely used non-free libraries. In this case, there is little to gain by limiting the free library to free software only, so we use the Lesser General Public License. In other cases, permission to use a particular library in non-free programs enables a greater number of people to use a large body of free software. For example, permission to use the GNU C Library in non-free programs enables many more people to use the whole GNU operating system, as well as its variant, the GNU/Linux operating system. Although the Lesser General Public License is Less protective of the users' freedom, it does ensure that the user of a program that is linked with the Library has the freedom and the wherewithal to run that program using a modified version of the Library. The precise terms and conditions for copying, distribution and modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, whereas the latter must be combined with the library in order to run. GNU LESSER GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any software library or other program which contains a notice placed by the copyright holder or other authoried party saying it may be distributed under the terms of this Lesser General Public License (also called "this License"). Each licensee is addressed as "you". A "library" means a collection of software functions and/or data prepared so as to be conveniently linked with application programs (which use some of those functions and data) to form executables. The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the Library" means either the Library or any derivative work under copyright law: that is to say, a work containing the Library or a portion of it, either verbatim or with modifications and/or translated straightforwardly into another language. (Hereinafter, translation is included without limitation in the term "modification".) "Source code" for a work means the preferred form of the work for making modifications to it. For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library. Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running a program using the Library is not restricted, and output from such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. 1. You may copy and distribute verbatim copies of the Library's complete source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and distribute a copy of this License along with the Library. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) The modified work must itself be a software library. b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. c) You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License. d) If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility, other than as an argument passed when the facility is invoked, then you must make a good faith effort to ensure that, in the event an application does not supply such function or table, the facility still operates, and performs whatever part of its purpose remains meaningful. (For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that any application-supplied function or table used by this function must be optional: if the application does not supply it, the square root function must still compute square roots.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Library, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Library. In addition, mere aggregation of another work not based on the Library with the Library (or with a work based on the Library) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library. To do this, you must alter all the notices that refer to this License, so that they refer to the ordinary GNU General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange. If distribution of object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code, even though third parties are not compelled to copy the source along with the object code. 5. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. However, linking a "work that uses the Library" with the Library creates an executable that is a derivative of the Library (because it contains portions of the Library), rather than a "work that uses the library". The executable is therefore covered by this License. Section 6 states terms for distribution of such executables. When a "work that uses the Library" uses material from a header file that is part of the Library, the object code for the work may be a derivative work of the Library even though the source code is not. Whether this is true is especially significant if the work can be linked without the Library, or if the work is itself a library. The threshold for this to be true is not precisely defined by law. If such an object file uses only numerical parameters, data structure layouts and accessors, and small macros and small inline functions (ten lines or less in length), then the use of the object file is unrestricted, regardless of whether it is legally a derivative work. (Executables containing this object code plus portions of the Library will still fall under Section 6.) Otherwise, if the work is a derivative of the Library, you may distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. 6. As an exception to the Sections above, you may also combine or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work under terms of your choice, provided that the terms permit modification of the work for the customer's own use and reverse engineering for debugging such modifications. You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License. You must supply a copy of this License. If the work during execution displays copyright notices, you must include the copyright notice for the Library among them, as well as a reference directing the user to the copy of this License. Also, you must do one of these things: a) Accompany the work with the complete corresponding machine-readable source code for the Library including whatever changes were used in the work (which must be distributed under Sections 1 and 2 above); and, if the work is an executable linked with the Library, with the complete machine-readable "work that uses the Library", as object code and/or source code, so that the user can modify the Library and then relink to produce a modified executable containing the modified Library. (It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions.) b) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (1) uses at run time a copy of the library already present on the user's computer system, rather than copying library functions into the executable, and (2) will operate properly with a modified version of the library, if the user installs one, as long as the modified version is interface-compatible with the version that the work was made with. c) Accompany the work with a written offer, valid for at least three years, to give the same user the materials specified in Subsection 6a, above, for a charge no more than the cost of performing this distribution. d) If distribution of the work is made by offering access to copy from a designated place, offer equivalent access to copy the above specified materials from the same place. e) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. For an executable, the required form of the "work that uses the Library" must include any data and utility programs needed for reproducing the executable from it. However, as a special exception, the materials to be distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. It may happen that this requirement contradicts the license restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined library, provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted, and provided that you do these two things: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities. This must be distributed under the terms of the Sections above. b) Give prominent notice with the combined library of the fact that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 8. You may not copy, modify, sublicense, link with, or distribute the Library except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, link with, or distribute the Library is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 9. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Library or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Library (or any work based on the Library), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Library or works based on it. 10. Each time you redistribute the Library (or any work based on the Library), the recipient automatically receives a license from the original licensor to copy, distribute, link with or modify the Library subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties with this License. 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Library at all. For example, if a patent license would not permit royalty-free redistribution of the Library by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 12. If the distribution and/or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Library under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 13. The Free Software Foundation may publish revised and/or new versions of the Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS soundtouch/soundtouch-1.4.pc.in0000664000175100017510000000066112577461413015742 0ustar vmdevvmdev# This file is obsoleted but provided for backwards compatibility # with legacy package environments prefix=@prefix@ exec_prefix=@exec_prefix@ libdir=@libdir@ includedir=@includedir@ Name: SoundTouch Description: SoundTouch is an open-source audio processing library for changing the Tempo, Pitch and Playback Rates of audio streams or files Version: @VERSION@ Libs: -L${libdir} -lSoundTouch Cflags: -I${includedir}/soundtouch soundtouch/bootstrap0000775000175100017510000000231312577461413014255 0ustar vmdevvmdev# $Id: bootstrap 43 2008-12-25 17:54:41Z oparviai $ #!/bin/sh if [ "$1" = "--clean" ] then if [ -a Makefile ] then make maintainer-clean elif [ -a configure ] then configure && $0 --clean else bootstrap && configure && $0 --clean fi rm -rf configure libtool aclocal.m4 `find . -name Makefile.in` autom4te*.cache config/config.guess config/config.h.in config/config.sub config/depcomp config/install-sh config/ltmain.sh config/missing config/mkinstalldirs config/stamp-h config/stamp-h.in #gettextie files #rm -f ABOUT-NLS config/config.rpath config/m4/codeset.m4 config/m4/gettext.m4 config/m4/glibc21.m4 config/m4/iconv.m4 config/m4/intdiv0.m4 config/m4/inttypes-pri.m4 config/m4/inttypes.m4 config/m4/inttypes_h.m4 config/m4/isc-posix.m4 config/m4/lcmessage.m4 config/m4/lib-ld.m4 config/m4/lib-link.m4 config/m4/lib-prefix.m4 config/m4/progtest.m4 config/m4/stdint_h.m4 config/m4/uintmax_t.m4 config/m4/ulonglong.m4 po/Makefile.in.in po/Rules-quot po/boldquot.sed po/en@boldquot.header po/en@quot.header po/insert-header.sin po/quot.sed po/remove-potcdate.sin else export AUTOMAKE="automake --add-missing --foreign --copy" autoreconf -fisv && rm -f `find . -name "*~"` && rm -f ChangeLog exit $? fi soundtouch/config/0000775000175100017510000000000012577461413013560 5ustar vmdevvmdevsoundtouch/config/am_include.mk0000664000175100017510000000235012577461413016211 0ustar vmdevvmdev## vim:tw=78 ## Process this file with automake to create Makefile.in ## ## $Id: am_include.mk 218 2015-05-18 17:04:47Z oparviai $ ## ## This file is part of SoundTouch, an audio processing library for pitch/time adjustments ## ## SoundTouch 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. ## ## SoundTouch is distributed in the hope that it will be useful, but WITHOUT ANY ## WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR ## A PARTICULAR PURPOSE. See the GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License along with ## this program; if not, write to the Free Software Foundation, Inc., 59 Temple ## Place - Suite 330, Boston, MA 02111-1307, USA ## These are common definitions used in all Makefiles ## It is actually included when a makefile.am is coverted to Makefile.in ## by automake, so it's ok to have @MACROS@ that will be set by configure AM_CPPFLAGS=-I$(top_srcdir)/include # doc directory pkgdocdir=$(prefix)/doc/@PACKAGE@ soundtouch/config/README.TXT0000664000175100017510000000020112577461413015107 0ustar vmdevvmdevFiles in this directory are used by GNU autoconf/automake system. These files aren't used/needed in the Windows environment. soundtouch/config/m4/0000775000175100017510000000000012577461413014100 5ustar vmdevvmdevsoundtouch/source/0000775000175100017510000000000012577461413013613 5ustar vmdevvmdevsoundtouch/source/SoundStretch/0000775000175100017510000000000012577461413016240 5ustar vmdevvmdevsoundtouch/source/SoundStretch/main.cpp0000664000175100017510000002505612577461413017700 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// SoundStretch main routine. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2015-05-18 20:32:21 +0300 (Mon, 18 May 2015) $ // File revision : $Revision: 4 $ // // $Id: main.cpp 219 2015-05-18 17:32:21Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include "RunParameters.h" #include "WavFile.h" #include "SoundTouch.h" #include "BPMDetect.h" using namespace soundtouch; using namespace std; // Processing chunk size (size chosen to be divisible by 2, 4, 6, 8, 10, 12, 14, 16 channels ...) #define BUFF_SIZE 6720 #if _WIN32 #include #include // Macro for Win32 standard input/output stream support: Sets a file stream into binary mode #define SET_STREAM_TO_BIN_MODE(f) (_setmode(_fileno(f), _O_BINARY)) #else // Not needed for GNU environment... #define SET_STREAM_TO_BIN_MODE(f) {} #endif static const char _helloText[] = "\n" " SoundStretch v%s - Written by Olli Parviainen 2001 - 2015\n" "==================================================================\n" "author e-mail: - WWW: http://www.surina.net/soundtouch\n" "\n" "This program is subject to (L)GPL license. Run \"soundstretch -license\" for\n" "more information.\n" "\n"; static void openFiles(WavInFile **inFile, WavOutFile **outFile, const RunParameters *params) { int bits, samplerate, channels; if (strcmp(params->inFileName, "stdin") == 0) { // used 'stdin' as input file SET_STREAM_TO_BIN_MODE(stdin); *inFile = new WavInFile(stdin); } else { // open input file... *inFile = new WavInFile(params->inFileName); } // ... open output file with same sound parameters bits = (int)(*inFile)->getNumBits(); samplerate = (int)(*inFile)->getSampleRate(); channels = (int)(*inFile)->getNumChannels(); if (params->outFileName) { if (strcmp(params->outFileName, "stdout") == 0) { SET_STREAM_TO_BIN_MODE(stdout); *outFile = new WavOutFile(stdout, samplerate, bits, channels); } else { *outFile = new WavOutFile(params->outFileName, samplerate, bits, channels); } } else { *outFile = NULL; } } // Sets the 'SoundTouch' object up according to input file sound format & // command line parameters static void setup(SoundTouch *pSoundTouch, const WavInFile *inFile, const RunParameters *params) { int sampleRate; int channels; sampleRate = (int)inFile->getSampleRate(); channels = (int)inFile->getNumChannels(); pSoundTouch->setSampleRate(sampleRate); pSoundTouch->setChannels(channels); pSoundTouch->setTempoChange(params->tempoDelta); pSoundTouch->setPitchSemiTones(params->pitchDelta); pSoundTouch->setRateChange(params->rateDelta); pSoundTouch->setSetting(SETTING_USE_QUICKSEEK, params->quick); pSoundTouch->setSetting(SETTING_USE_AA_FILTER, !(params->noAntiAlias)); if (params->speech) { // use settings for speech processing pSoundTouch->setSetting(SETTING_SEQUENCE_MS, 40); pSoundTouch->setSetting(SETTING_SEEKWINDOW_MS, 15); pSoundTouch->setSetting(SETTING_OVERLAP_MS, 8); fprintf(stderr, "Tune processing parameters for speech processing.\n"); } // print processing information if (params->outFileName) { #ifdef SOUNDTOUCH_INTEGER_SAMPLES fprintf(stderr, "Uses 16bit integer sample type in processing.\n\n"); #else #ifndef SOUNDTOUCH_FLOAT_SAMPLES #error "Sampletype not defined" #endif fprintf(stderr, "Uses 32bit floating point sample type in processing.\n\n"); #endif // print processing information only if outFileName given i.e. some processing will happen fprintf(stderr, "Processing the file with the following changes:\n"); fprintf(stderr, " tempo change = %+g %%\n", params->tempoDelta); fprintf(stderr, " pitch change = %+g semitones\n", params->pitchDelta); fprintf(stderr, " rate change = %+g %%\n\n", params->rateDelta); fprintf(stderr, "Working..."); } else { // outFileName not given fprintf(stderr, "Warning: output file name missing, won't output anything.\n\n"); } fflush(stderr); } // Processes the sound static void process(SoundTouch *pSoundTouch, WavInFile *inFile, WavOutFile *outFile) { int nSamples; int nChannels; int buffSizeSamples; SAMPLETYPE sampleBuffer[BUFF_SIZE]; if ((inFile == NULL) || (outFile == NULL)) return; // nothing to do. nChannels = (int)inFile->getNumChannels(); assert(nChannels > 0); buffSizeSamples = BUFF_SIZE / nChannels; // Process samples read from the input file while (inFile->eof() == 0) { int num; // Read a chunk of samples from the input file num = inFile->read(sampleBuffer, BUFF_SIZE); nSamples = num / (int)inFile->getNumChannels(); // Feed the samples into SoundTouch processor pSoundTouch->putSamples(sampleBuffer, nSamples); // Read ready samples from SoundTouch processor & write them output file. // NOTES: // - 'receiveSamples' doesn't necessarily return any samples at all // during some rounds! // - On the other hand, during some round 'receiveSamples' may have more // ready samples than would fit into 'sampleBuffer', and for this reason // the 'receiveSamples' call is iterated for as many times as it // outputs samples. do { nSamples = pSoundTouch->receiveSamples(sampleBuffer, buffSizeSamples); outFile->write(sampleBuffer, nSamples * nChannels); } while (nSamples != 0); } // Now the input file is processed, yet 'flush' few last samples that are // hiding in the SoundTouch's internal processing pipeline. pSoundTouch->flush(); do { nSamples = pSoundTouch->receiveSamples(sampleBuffer, buffSizeSamples); outFile->write(sampleBuffer, nSamples * nChannels); } while (nSamples != 0); } // Detect BPM rate of inFile and adjust tempo setting accordingly if necessary static void detectBPM(WavInFile *inFile, RunParameters *params) { float bpmValue; int nChannels; BPMDetect bpm(inFile->getNumChannels(), inFile->getSampleRate()); SAMPLETYPE sampleBuffer[BUFF_SIZE]; // detect bpm rate fprintf(stderr, "Detecting BPM rate..."); fflush(stderr); nChannels = (int)inFile->getNumChannels(); assert(BUFF_SIZE % nChannels == 0); // Process the 'inFile' in small blocks, repeat until whole file has // been processed while (inFile->eof() == 0) { int num, samples; // Read sample data from input file num = inFile->read(sampleBuffer, BUFF_SIZE); // Enter the new samples to the bpm analyzer class samples = num / nChannels; bpm.inputSamples(sampleBuffer, samples); } // Now the whole song data has been analyzed. Read the resulting bpm. bpmValue = bpm.getBpm(); fprintf(stderr, "Done!\n"); // rewind the file after bpm detection inFile->rewind(); if (bpmValue > 0) { fprintf(stderr, "Detected BPM rate %.1f\n\n", bpmValue); } else { fprintf(stderr, "Couldn't detect BPM rate.\n\n"); return; } if (params->goalBPM > 0) { // adjust tempo to given bpm params->tempoDelta = (params->goalBPM / bpmValue - 1.0f) * 100.0f; fprintf(stderr, "The file will be converted to %.1f BPM\n\n", params->goalBPM); } } int main(const int nParams, const char * const paramStr[]) { WavInFile *inFile; WavOutFile *outFile; RunParameters *params; SoundTouch soundTouch; fprintf(stderr, _helloText, SoundTouch::getVersionString()); try { // Parse command line parameters params = new RunParameters(nParams, paramStr); // Open input & output files openFiles(&inFile, &outFile, params); if (params->detectBPM == true) { // detect sound BPM (and adjust processing parameters // accordingly if necessary) detectBPM(inFile, params); } // Setup the 'SoundTouch' object for processing the sound setup(&soundTouch, inFile, params); // clock_t cs = clock(); // for benchmarking processing duration // Process the sound process(&soundTouch, inFile, outFile); // clock_t ce = clock(); // for benchmarking processing duration // printf("duration: %lf\n", (double)(ce-cs)/CLOCKS_PER_SEC); // Close WAV file handles & dispose of the objects delete inFile; delete outFile; delete params; fprintf(stderr, "Done!\n"); } catch (const runtime_error &e) { // An exception occurred during processing, display an error message fprintf(stderr, "%s\n", e.what()); return -1; } return 0; } soundtouch/source/SoundStretch/soundstretch.dsp0000664000175100017510000001133412577461413021477 0ustar vmdevvmdev# Microsoft Developer Studio Project File - Name="soundstretch" - Package Owner=<4> # Microsoft Developer Studio Generated Build File, Format Version 6.00 # ** DO NOT EDIT ** # TARGTYPE "Win32 (x86) Console Application" 0x0103 CFG=soundstretch - Win32 Debug !MESSAGE This is not a valid makefile. To build this project using NMAKE, !MESSAGE use the Export Makefile command and run !MESSAGE !MESSAGE NMAKE /f "soundstretch.mak". !MESSAGE !MESSAGE You can specify a configuration when running NMAKE !MESSAGE by defining the macro CFG on the command line. For example: !MESSAGE !MESSAGE NMAKE /f "soundstretch.mak" CFG="soundstretch - Win32 Debug" !MESSAGE !MESSAGE Possible choices for configuration are: !MESSAGE !MESSAGE "soundstretch - Win32 Release" (based on "Win32 (x86) Console Application") !MESSAGE "soundstretch - Win32 Debug" (based on "Win32 (x86) Console Application") !MESSAGE # Begin Project # PROP AllowPerConfigDependencies 0 # PROP Scc_ProjName "" # PROP Scc_LocalPath "" CPP=cl.exe RSC=rc.exe !IF "$(CFG)" == "soundstretch - Win32 Release" # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 0 # PROP BASE Output_Dir "Release" # PROP BASE Intermediate_Dir "Release" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 0 # PROP Output_Dir "Release" # PROP Intermediate_Dir "Release" # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c # ADD CPP /nologo /W3 /GX /Zi /O2 /I "..\..\include" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /FD /c # SUBTRACT CPP /YX # ADD BASE RSC /l 0x40b /d "NDEBUG" # ADD RSC /l 0x40b /d "NDEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 # ADD LINK32 SoundTouch.lib /nologo /subsystem:console /profile /map /debug /machine:I386 /libpath:"..\..\lib" # Begin Special Build Tool SOURCE="$(InputPath)" PostBuild_Cmds=copy Release\soundstretch.exe ..\..\bin\ # End Special Build Tool !ELSEIF "$(CFG)" == "soundstretch - Win32 Debug" # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 1 # PROP BASE Output_Dir "Debug" # PROP BASE Intermediate_Dir "Debug" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 1 # PROP Output_Dir "Debug" # PROP Intermediate_Dir "Debug" # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c # ADD CPP /nologo /Zp16 /W3 /Gm /GX /ZI /Od /I "..\..\include" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FR /FD /GZ /c # SUBTRACT CPP /YX # ADD BASE RSC /l 0x40b /d "_DEBUG" # ADD RSC /l 0x40b /d "_DEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept # ADD LINK32 SoundTouchD.lib /nologo /subsystem:console /map /debug /machine:I386 /nodefaultlib:"libc" /out:"Debug/soundstretchD.exe" /pdbtype:sept /libpath:"..\..\lib" # SUBTRACT LINK32 /pdb:none # Begin Special Build Tool SOURCE="$(InputPath)" PostBuild_Cmds=copy Debug\soundstretchD.exe ..\..\bin\ # End Special Build Tool !ENDIF # Begin Target # Name "soundstretch - Win32 Release" # Name "soundstretch - Win32 Debug" # Begin Group "Source Files" # PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" # Begin Source File SOURCE=.\main.cpp # End Source File # Begin Source File SOURCE=.\RunParameters.cpp # End Source File # Begin Source File SOURCE=.\WavFile.cpp # End Source File # End Group # Begin Group "Header Files" # PROP Default_Filter "h;hpp;hxx;hm;inl" # Begin Source File SOURCE=.\RunParameters.h # End Source File # Begin Source File SOURCE=..\..\..\include\SoundTouch.h # End Source File # Begin Source File SOURCE=..\..\..\include\STTypes.h # End Source File # Begin Source File SOURCE=.\WavFile.h # End Source File # End Group # Begin Group "Resource Files" # PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" # End Group # End Target # End Project soundtouch/source/SoundStretch/Makefile.am0000664000175100017510000000446212577461413020302 0ustar vmdevvmdev## Process this file with automake to create Makefile.in ## ## $Id: Makefile.am 209 2015-05-01 07:55:47Z oparviai $ ## ## Copyright (C) 2003 - David W. Durham ## ## This file is part of SoundTouch, an audio processing library for pitch/time adjustments ## ## SoundTouch 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. ## ## SoundTouch is distributed in the hope that it will be useful, but WITHOUT ANY ## WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR ## A PARTICULAR PURPOSE. See the GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License along with ## this program; if not, write to the Free Software Foundation, Inc., 59 Temple ## Place - Suite 330, Boston, MA 02111-1307, USA include $(top_srcdir)/config/am_include.mk ## bin_PROGRAMS is the macro that tells automake the name of the programs to ## install in the bin directory (/usr/local/bin) by default. By setting ## --prefix= at configure time the user can change this (eg: ./configure ## --prefix=/usr will install soundstretch under /usr/bin/soundstretch ) bin_PROGRAMS=soundstretch noinst_HEADERS=RunParameters.h WavFile.h # extra files to include in distrubution tarball EXTRA_DIST=soundstretch.dsp soundstretch.dsw soundstretch.sln soundstretch.vcproj ## for every name listed under bin_PROGRAMS, you have a _SOURCES. This lists ## all the sources in the current directory that are used to build soundstretch. soundstretch_SOURCES=main.cpp RunParameters.cpp WavFile.cpp ## soundstretch_LDADD is a list of extras to pass at link time. All the objects ## created by the above soundstretch_SOURCES are automatically linked in, so here I ## list object files from other directories as well as flags passed to the ## linker. soundstretch_LDADD=../SoundTouch/libSoundTouch.la -lm ## linker flags. # OP 2011-7-17 Linker flag -s disabled to prevent stripping symbols by default #soundstretch_LDFLAGS=-s ## additional compiler flags soundstretch_CXXFLAGS=-O3 $(AM_CXXFLAGS) #clean-local: # -rm -f additional-files-to-remove-on-make-clean soundtouch/source/SoundStretch/RunParameters.cpp0000664000175100017510000002052612577461413021541 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// A class for parsing the 'soundstretch' application command line parameters /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2014-04-06 18:57:21 +0300 (Sun, 06 Apr 2014) $ // File revision : $Revision: 4 $ // // $Id: RunParameters.cpp 195 2014-04-06 15:57:21Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include #include #include "RunParameters.h" using namespace std; // Program usage instructions static const char licenseText[] = " LICENSE:\n" " ========\n" " \n" " SoundTouch sound processing library\n" " Copyright (c) Olli Parviainen\n" " \n" " This library is free software; you can redistribute it and/or\n" " modify it under the terms of the GNU Lesser General Public\n" " License version 2.1 as published by the Free Software Foundation.\n" " \n" " This library is distributed in the hope that it will be useful,\n" " but WITHOUT ANY WARRANTY; without even the implied warranty of\n" " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n" " Lesser General Public License for more details.\n" " \n" " You should have received a copy of the GNU Lesser General Public\n" " License along with this library; if not, write to the Free Software\n" " Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n" " \n" "This application is distributed with full source codes; however, if you\n" "didn't receive them, please visit the author's homepage (see the link above)."; static const char whatText[] = "This application processes WAV audio files by modifying the sound tempo,\n" "pitch and playback rate properties independently from each other.\n" "\n"; static const char usage[] = "Usage :\n" " soundstretch infilename outfilename [switches]\n" "\n" "To use standard input/output pipes, give 'stdin' and 'stdout' as filenames.\n" "\n" "Available switches are:\n" " -tempo=n : Change sound tempo by n percents (n=-95..+5000 %)\n" " -pitch=n : Change sound pitch by n semitones (n=-60..+60 semitones)\n" " -rate=n : Change sound rate by n percents (n=-95..+5000 %)\n" " -bpm=n : Detect the BPM rate of sound and adjust tempo to meet 'n' BPMs.\n" " If '=n' is omitted, just detects the BPM rate.\n" " -quick : Use quicker tempo change algorithm (gain speed, lose quality)\n" " -naa : Don't use anti-alias filtering (gain speed, lose quality)\n" " -speech : Tune algorithm for speech processing (default is for music)\n" " -license : Display the program license text (LGPL)\n"; // Converts a char into lower case static int _toLowerCase(int c) { if (c >= 'A' && c <= 'Z') { c += 'a' - 'A'; } return c; } // Constructor RunParameters::RunParameters(const int nParams, const char * const paramStr[]) { int i; int nFirstParam; if (nParams < 3) { // Too few parameters if (nParams > 1 && paramStr[1][0] == '-' && _toLowerCase(paramStr[1][1]) == 'l') { // '-license' switch throwLicense(); } string msg = whatText; msg += usage; ST_THROW_RT_ERROR(msg.c_str()); } inFileName = NULL; outFileName = NULL; tempoDelta = 0; pitchDelta = 0; rateDelta = 0; quick = 0; noAntiAlias = 0; goalBPM = 0; speech = false; detectBPM = false; // Get input & output file names inFileName = (char*)paramStr[1]; outFileName = (char*)paramStr[2]; if (outFileName[0] == '-') { // no outputfile name was given but parameters outFileName = NULL; nFirstParam = 2; } else { nFirstParam = 3; } // parse switch parameters for (i = nFirstParam; i < nParams; i ++) { parseSwitchParam(paramStr[i]); } checkLimits(); } // Checks parameter limits void RunParameters::checkLimits() { if (tempoDelta < -95.0f) { tempoDelta = -95.0f; } else if (tempoDelta > 5000.0f) { tempoDelta = 5000.0f; } if (pitchDelta < -60.0f) { pitchDelta = -60.0f; } else if (pitchDelta > 60.0f) { pitchDelta = 60.0f; } if (rateDelta < -95.0f) { rateDelta = -95.0f; } else if (rateDelta > 5000.0f) { rateDelta = 5000.0f; } } // Unknown switch parameter -- throws an exception with an error message void RunParameters::throwIllegalParamExp(const string &str) const { string msg = "ERROR : Illegal parameter \""; msg += str; msg += "\".\n\n"; msg += usage; ST_THROW_RT_ERROR(msg.c_str()); } void RunParameters::throwLicense() const { ST_THROW_RT_ERROR(licenseText); } float RunParameters::parseSwitchValue(const string &str) const { int pos; pos = (int)str.find_first_of('='); if (pos < 0) { // '=' missing throwIllegalParamExp(str); } // Read numerical parameter value after '=' return (float)atof(str.substr(pos + 1).c_str()); } // Interprets a single switch parameter string of format "-switch=xx" // Valid switches are "-tempo=xx", "-pitch=xx" and "-rate=xx". Stores // switch values into 'params' structure. void RunParameters::parseSwitchParam(const string &str) { int upS; if (str[0] != '-') { // leading hyphen missing => not a valid parameter throwIllegalParamExp(str); } // Take the first character of switch name & change to lower case upS = _toLowerCase(str[1]); // interpret the switch name & operate accordingly switch (upS) { case 't' : // switch '-tempo=xx' tempoDelta = parseSwitchValue(str); break; case 'p' : // switch '-pitch=xx' pitchDelta = parseSwitchValue(str); break; case 'r' : // switch '-rate=xx' rateDelta = parseSwitchValue(str); break; case 'b' : // switch '-bpm=xx' detectBPM = true; try { goalBPM = parseSwitchValue(str); } catch (const runtime_error) { // illegal or missing bpm value => just calculate bpm goalBPM = 0; } break; case 'q' : // switch '-quick' quick = 1; break; case 'n' : // switch '-naa' noAntiAlias = 1; break; case 'l' : // switch '-license' throwLicense(); break; case 's' : // switch '-speech' speech = true; break; default: // unknown switch throwIllegalParamExp(str); } } soundtouch/source/SoundStretch/soundstretch.vcproj0000664000175100017510000004273212577461413022222 0ustar vmdevvmdev soundtouch/source/SoundStretch/WavFile.h0000664000175100017510000002200712577461413017747 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Classes for easy reading & writing of WAV sound files. /// /// For big-endian CPU, define BIG_ENDIAN during compile-time to correctly /// parse the WAV files with such processors. /// /// Admittingly, more complete WAV reader routines may exist in public domain, but /// the reason for 'yet another' one is that those generic WAV reader libraries are /// exhaustingly large and cumbersome! Wanted to have something simpler here, i.e. /// something that's not already larger than rest of the SoundTouch/SoundStretch program... /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2014-10-05 19:20:24 +0300 (Sun, 05 Oct 2014) $ // File revision : $Revision: 4 $ // // $Id: WavFile.h 200 2014-10-05 16:20:24Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef WAVFILE_H #define WAVFILE_H #include #ifndef uint typedef unsigned int uint; #endif /// WAV audio file 'riff' section header typedef struct { char riff_char[4]; int package_len; char wave[4]; } WavRiff; /// WAV audio file 'format' section header typedef struct { char fmt[4]; int format_len; short fixed; short channel_number; int sample_rate; int byte_rate; short byte_per_sample; short bits_per_sample; } WavFormat; /// WAV audio file 'fact' section header typedef struct { char fact_field[4]; int fact_len; uint fact_sample_len; } WavFact; /// WAV audio file 'data' section header typedef struct { char data_field[4]; uint data_len; } WavData; /// WAV audio file header typedef struct { WavRiff riff; WavFormat format; WavFact fact; WavData data; } WavHeader; /// Base class for processing WAV audio files. class WavFileBase { private: /// Conversion working buffer; char *convBuff; int convBuffSize; protected: WavFileBase(); virtual ~WavFileBase(); /// Get pointer to conversion buffer of at min. given size void *getConvBuffer(int sizeByte); }; /// Class for reading WAV audio files. class WavInFile : protected WavFileBase { private: /// File pointer. FILE *fptr; /// Position within the audio stream long position; /// Counter of how many bytes of sample data have been read from the file. long dataRead; /// WAV header information WavHeader header; /// Init the WAV file stream void init(); /// Read WAV file headers. /// \return zero if all ok, nonzero if file format is invalid. int readWavHeaders(); /// Checks WAV file header tags. /// \return zero if all ok, nonzero if file format is invalid. int checkCharTags() const; /// Reads a single WAV file header block. /// \return zero if all ok, nonzero if file format is invalid. int readHeaderBlock(); /// Reads WAV file 'riff' block int readRIFFBlock(); public: /// Constructor: Opens the given WAV file. If the file can't be opened, /// throws 'runtime_error' exception. WavInFile(const char *filename); WavInFile(FILE *file); /// Destructor: Closes the file. ~WavInFile(); /// Rewind to beginning of the file void rewind(); /// Get sample rate. uint getSampleRate() const; /// Get number of bits per sample, i.e. 8 or 16. uint getNumBits() const; /// Get sample data size in bytes. Ahem, this should return same information as /// 'getBytesPerSample'... uint getDataSizeInBytes() const; /// Get total number of samples in file. uint getNumSamples() const; /// Get number of bytes per audio sample (e.g. 16bit stereo = 4 bytes/sample) uint getBytesPerSample() const; /// Get number of audio channels in the file (1=mono, 2=stereo) uint getNumChannels() const; /// Get the audio file length in milliseconds uint getLengthMS() const; /// Returns how many milliseconds of audio have so far been read from the file /// /// \return elapsed duration in milliseconds uint getElapsedMS() const; /// Reads audio samples from the WAV file. This routine works only for 8 bit samples. /// Reads given number of elements from the file or if end-of-file reached, as many /// elements as are left in the file. /// /// \return Number of 8-bit integers read from the file. int read(unsigned char *buffer, int maxElems); /// Reads audio samples from the WAV file to 16 bit integer format. Reads given number /// of elements from the file or if end-of-file reached, as many elements as are /// left in the file. /// /// \return Number of 16-bit integers read from the file. int read(short *buffer, ///< Pointer to buffer where to read data. int maxElems ///< Size of 'buffer' array (number of array elements). ); /// Reads audio samples from the WAV file to floating point format, converting /// sample values to range [-1,1[. Reads given number of elements from the file /// or if end-of-file reached, as many elements as are left in the file. /// Notice that reading in float format supports 8/16/24/32bit sample formats. /// /// \return Number of elements read from the file. int read(float *buffer, ///< Pointer to buffer where to read data. int maxElems ///< Size of 'buffer' array (number of array elements). ); /// Check end-of-file. /// /// \return Nonzero if end-of-file reached. int eof() const; }; /// Class for writing WAV audio files. class WavOutFile : protected WavFileBase { private: /// Pointer to the WAV file FILE *fptr; /// WAV file header data. WavHeader header; /// Counter of how many bytes have been written to the file so far. int bytesWritten; /// Fills in WAV file header information. void fillInHeader(const uint sampleRate, const uint bits, const uint channels); /// Finishes the WAV file header by supplementing information of amount of /// data written to file etc void finishHeader(); /// Writes the WAV file header. void writeHeader(); public: /// Constructor: Creates a new WAV file. Throws a 'runtime_error' exception /// if file creation fails. WavOutFile(const char *fileName, ///< Filename int sampleRate, ///< Sample rate (e.g. 44100 etc) int bits, ///< Bits per sample (8 or 16 bits) int channels ///< Number of channels (1=mono, 2=stereo) ); WavOutFile(FILE *file, int sampleRate, int bits, int channels); /// Destructor: Finalizes & closes the WAV file. ~WavOutFile(); /// Write data to WAV file. This function works only with 8bit samples. /// Throws a 'runtime_error' exception if writing to file fails. void write(const unsigned char *buffer, ///< Pointer to sample data buffer. int numElems ///< How many array items are to be written to file. ); /// Write data to WAV file. Throws a 'runtime_error' exception if writing to /// file fails. void write(const short *buffer, ///< Pointer to sample data buffer. int numElems ///< How many array items are to be written to file. ); /// Write data to WAV file in floating point format, saturating sample values to range /// [-1..+1[. Throws a 'runtime_error' exception if writing to file fails. void write(const float *buffer, ///< Pointer to sample data buffer. int numElems ///< How many array items are to be written to file. ); }; #endif soundtouch/source/SoundStretch/WavFile.cpp0000664000175100017510000006413612577461413020313 0ustar vmdevvmdev //////////////////////////////////////////////////////////////////////////////// /// /// Classes for easy reading & writing of WAV sound files. /// /// For big-endian CPU, define _BIG_ENDIAN_ during compile-time to correctly /// parse the WAV files with such processors. /// /// Admittingly, more complete WAV reader routines may exist in public domain, /// but the reason for 'yet another' one is that those generic WAV reader /// libraries are exhaustingly large and cumbersome! Wanted to have something /// simpler here, i.e. something that's not already larger than rest of the /// SoundTouch/SoundStretch program... /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2014-10-05 19:20:24 +0300 (Sun, 05 Oct 2014) $ // File revision : $Revision: 4 $ // // $Id: WavFile.cpp 200 2014-10-05 16:20:24Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include "WavFile.h" #include "STTypes.h" using namespace std; static const char riffStr[] = "RIFF"; static const char waveStr[] = "WAVE"; static const char fmtStr[] = "fmt "; static const char factStr[] = "fact"; static const char dataStr[] = "data"; ////////////////////////////////////////////////////////////////////////////// // // Helper functions for swapping byte order to correctly read/write WAV files // with big-endian CPU's: Define compile-time definition _BIG_ENDIAN_ to // turn-on the conversion if it appears necessary. // // For example, Intel x86 is little-endian and doesn't require conversion, // while PowerPC of Mac's and many other RISC cpu's are big-endian. #ifdef BYTE_ORDER // In gcc compiler detect the byte order automatically #if BYTE_ORDER == BIG_ENDIAN // big-endian platform. #define _BIG_ENDIAN_ #endif #endif #ifdef _BIG_ENDIAN_ // big-endian CPU, swap bytes in 16 & 32 bit words // helper-function to swap byte-order of 32bit integer static inline int _swap32(int &dwData) { dwData = ((dwData >> 24) & 0x000000FF) | ((dwData >> 8) & 0x0000FF00) | ((dwData << 8) & 0x00FF0000) | ((dwData << 24) & 0xFF000000); return dwData; } // helper-function to swap byte-order of 16bit integer static inline short _swap16(short &wData) { wData = ((wData >> 8) & 0x00FF) | ((wData << 8) & 0xFF00); return wData; } // helper-function to swap byte-order of buffer of 16bit integers static inline void _swap16Buffer(short *pData, int numWords) { int i; for (i = 0; i < numWords; i ++) { pData[i] = _swap16(pData[i]); } } #else // BIG_ENDIAN // little-endian CPU, WAV file is ok as such // dummy helper-function static inline int _swap32(int &dwData) { // do nothing return dwData; } // dummy helper-function static inline short _swap16(short &wData) { // do nothing return wData; } // dummy helper-function static inline void _swap16Buffer(short *pData, int numBytes) { // do nothing } #endif // BIG_ENDIAN ////////////////////////////////////////////////////////////////////////////// // // Class WavFileBase // WavFileBase::WavFileBase() { convBuff = NULL; convBuffSize = 0; } WavFileBase::~WavFileBase() { delete[] convBuff; convBuffSize = 0; } /// Get pointer to conversion buffer of at min. given size void *WavFileBase::getConvBuffer(int sizeBytes) { if (convBuffSize < sizeBytes) { delete[] convBuff; convBuffSize = (sizeBytes + 15) & -8; // round up to following 8-byte bounday convBuff = new char[convBuffSize]; } return convBuff; } ////////////////////////////////////////////////////////////////////////////// // // Class WavInFile // WavInFile::WavInFile(const char *fileName) { // Try to open the file for reading fptr = fopen(fileName, "rb"); if (fptr == NULL) { // didn't succeed string msg = "Error : Unable to open file \""; msg += fileName; msg += "\" for reading."; ST_THROW_RT_ERROR(msg.c_str()); } init(); } WavInFile::WavInFile(FILE *file) { // Try to open the file for reading fptr = file; if (!file) { // didn't succeed string msg = "Error : Unable to access input stream for reading"; ST_THROW_RT_ERROR(msg.c_str()); } init(); } /// Init the WAV file stream void WavInFile::init() { int hdrsOk; // assume file stream is already open assert(fptr); // Read the file headers hdrsOk = readWavHeaders(); if (hdrsOk != 0) { // Something didn't match in the wav file headers string msg = "Input file is corrupt or not a WAV file"; ST_THROW_RT_ERROR(msg.c_str()); } /* Ignore 'fixed' field value as 32bit signed linear data can have other value than 1. if (header.format.fixed != 1) { string msg = "Input file uses unsupported encoding."; ST_THROW_RT_ERROR(msg.c_str()); } */ dataRead = 0; } WavInFile::~WavInFile() { if (fptr) fclose(fptr); fptr = NULL; } void WavInFile::rewind() { int hdrsOk; fseek(fptr, 0, SEEK_SET); hdrsOk = readWavHeaders(); assert(hdrsOk == 0); dataRead = 0; } int WavInFile::checkCharTags() const { // header.format.fmt should equal to 'fmt ' if (memcmp(fmtStr, header.format.fmt, 4) != 0) return -1; // header.data.data_field should equal to 'data' if (memcmp(dataStr, header.data.data_field, 4) != 0) return -1; return 0; } int WavInFile::read(unsigned char *buffer, int maxElems) { int numBytes; uint afterDataRead; // ensure it's 8 bit format if (header.format.bits_per_sample != 8) { ST_THROW_RT_ERROR("Error: WavInFile::read(char*, int) works only with 8bit samples."); } assert(sizeof(char) == 1); numBytes = maxElems; afterDataRead = dataRead + numBytes; if (afterDataRead > header.data.data_len) { // Don't read more samples than are marked available in header numBytes = (int)header.data.data_len - (int)dataRead; assert(numBytes >= 0); } assert(buffer); numBytes = (int)fread(buffer, 1, numBytes, fptr); dataRead += numBytes; return numBytes; } int WavInFile::read(short *buffer, int maxElems) { unsigned int afterDataRead; int numBytes; int numElems; assert(buffer); switch (header.format.bits_per_sample) { case 8: { // 8 bit format unsigned char *temp = (unsigned char*)getConvBuffer(maxElems); int i; numElems = read(temp, maxElems); // convert from 8 to 16 bit for (i = 0; i < numElems; i ++) { buffer[i] = (short)(((short)temp[i] - 128) * 256); } break; } case 16: { // 16 bit format assert(sizeof(short) == 2); numBytes = maxElems * 2; afterDataRead = dataRead + numBytes; if (afterDataRead > header.data.data_len) { // Don't read more samples than are marked available in header numBytes = (int)header.data.data_len - (int)dataRead; assert(numBytes >= 0); } numBytes = (int)fread(buffer, 1, numBytes, fptr); dataRead += numBytes; numElems = numBytes / 2; // 16bit samples, swap byte order if necessary _swap16Buffer((short *)buffer, numElems); break; } default: { stringstream ss; ss << "\nOnly 8/16 bit sample WAV files supported in integer compilation. Can't open WAV file with "; ss << (int)header.format.bits_per_sample; ss << " bit sample format. "; ST_THROW_RT_ERROR(ss.str().c_str()); } }; return numElems; } /// Read data in float format. Notice that when reading in float format /// 8/16/24/32 bit sample formats are supported int WavInFile::read(float *buffer, int maxElems) { unsigned int afterDataRead; int numBytes; int numElems; int bytesPerSample; assert(buffer); bytesPerSample = header.format.bits_per_sample / 8; if ((bytesPerSample < 1) || (bytesPerSample > 4)) { stringstream ss; ss << "\nOnly 8/16/24/32 bit sample WAV files supported. Can't open WAV file with "; ss << (int)header.format.bits_per_sample; ss << " bit sample format. "; ST_THROW_RT_ERROR(ss.str().c_str()); } numBytes = maxElems * bytesPerSample; afterDataRead = dataRead + numBytes; if (afterDataRead > header.data.data_len) { // Don't read more samples than are marked available in header numBytes = (int)header.data.data_len - (int)dataRead; assert(numBytes >= 0); } // read raw data into temporary buffer char *temp = (char*)getConvBuffer(numBytes); numBytes = (int)fread(temp, 1, numBytes, fptr); dataRead += numBytes; numElems = numBytes / bytesPerSample; // swap byte ordert & convert to float, depending on sample format switch (bytesPerSample) { case 1: { unsigned char *temp2 = (unsigned char*)temp; double conv = 1.0 / 128.0; for (int i = 0; i < numElems; i ++) { buffer[i] = (float)(temp2[i] * conv - 1.0); } break; } case 2: { short *temp2 = (short*)temp; double conv = 1.0 / 32768.0; for (int i = 0; i < numElems; i ++) { short value = temp2[i]; buffer[i] = (float)(_swap16(value) * conv); } break; } case 3: { char *temp2 = (char *)temp; double conv = 1.0 / 8388608.0; for (int i = 0; i < numElems; i ++) { int value = *((int*)temp2); value = _swap32(value) & 0x00ffffff; // take 24 bits value |= (value & 0x00800000) ? 0xff000000 : 0; // extend minus sign bits buffer[i] = (float)(value * conv); temp2 += 3; } break; } case 4: { int *temp2 = (int *)temp; double conv = 1.0 / 2147483648.0; assert(sizeof(int) == 4); for (int i = 0; i < numElems; i ++) { int value = temp2[i]; buffer[i] = (float)(_swap32(value) * conv); } break; } } return numElems; } int WavInFile::eof() const { // return true if all data has been read or file eof has reached return (dataRead == header.data.data_len || feof(fptr)); } // test if character code is between a white space ' ' and little 'z' static int isAlpha(char c) { return (c >= ' ' && c <= 'z') ? 1 : 0; } // test if all characters are between a white space ' ' and little 'z' static int isAlphaStr(const char *str) { char c; c = str[0]; while (c) { if (isAlpha(c) == 0) return 0; str ++; c = str[0]; } return 1; } int WavInFile::readRIFFBlock() { if (fread(&(header.riff), sizeof(WavRiff), 1, fptr) != 1) return -1; // swap 32bit data byte order if necessary _swap32((int &)header.riff.package_len); // header.riff.riff_char should equal to 'RIFF'); if (memcmp(riffStr, header.riff.riff_char, 4) != 0) return -1; // header.riff.wave should equal to 'WAVE' if (memcmp(waveStr, header.riff.wave, 4) != 0) return -1; return 0; } int WavInFile::readHeaderBlock() { char label[5]; string sLabel; // lead label string if (fread(label, 1, 4, fptr) !=4) return -1; label[4] = 0; if (isAlphaStr(label) == 0) return -1; // not a valid label // Decode blocks according to their label if (strcmp(label, fmtStr) == 0) { int nLen, nDump; // 'fmt ' block memcpy(header.format.fmt, fmtStr, 4); // read length of the format field if (fread(&nLen, sizeof(int), 1, fptr) != 1) return -1; // swap byte order if necessary _swap32(nLen); // int format_len; header.format.format_len = nLen; // calculate how much length differs from expected nDump = nLen - ((int)sizeof(header.format) - 8); // if format_len is larger than expected, read only as much data as we've space for if (nDump > 0) { nLen = sizeof(header.format) - 8; } // read data if (fread(&(header.format.fixed), nLen, 1, fptr) != 1) return -1; // swap byte order if necessary _swap16(header.format.fixed); // short int fixed; _swap16(header.format.channel_number); // short int channel_number; _swap32((int &)header.format.sample_rate); // int sample_rate; _swap32((int &)header.format.byte_rate); // int byte_rate; _swap16(header.format.byte_per_sample); // short int byte_per_sample; _swap16(header.format.bits_per_sample); // short int bits_per_sample; // if format_len is larger than expected, skip the extra data if (nDump > 0) { fseek(fptr, nDump, SEEK_CUR); } return 0; } else if (strcmp(label, factStr) == 0) { int nLen, nDump; // 'fact' block memcpy(header.fact.fact_field, factStr, 4); // read length of the fact field if (fread(&nLen, sizeof(int), 1, fptr) != 1) return -1; // swap byte order if necessary _swap32(nLen); // int fact_len; header.fact.fact_len = nLen; // calculate how much length differs from expected nDump = nLen - ((int)sizeof(header.fact) - 8); // if format_len is larger than expected, read only as much data as we've space for if (nDump > 0) { nLen = sizeof(header.fact) - 8; } // read data if (fread(&(header.fact.fact_sample_len), nLen, 1, fptr) != 1) return -1; // swap byte order if necessary _swap32((int &)header.fact.fact_sample_len); // int sample_length; // if fact_len is larger than expected, skip the extra data if (nDump > 0) { fseek(fptr, nDump, SEEK_CUR); } return 0; } else if (strcmp(label, dataStr) == 0) { // 'data' block memcpy(header.data.data_field, dataStr, 4); if (fread(&(header.data.data_len), sizeof(uint), 1, fptr) != 1) return -1; // swap byte order if necessary _swap32((int &)header.data.data_len); return 1; } else { uint len, i; uint temp; // unknown block // read length if (fread(&len, sizeof(len), 1, fptr) != 1) return -1; // scan through the block for (i = 0; i < len; i ++) { if (fread(&temp, 1, 1, fptr) != 1) return -1; if (feof(fptr)) return -1; // unexpected eof } } return 0; } int WavInFile::readWavHeaders() { int res; memset(&header, 0, sizeof(header)); res = readRIFFBlock(); if (res) return 1; // read header blocks until data block is found do { // read header blocks res = readHeaderBlock(); if (res < 0) return 1; // error in file structure } while (res == 0); // check that all required tags are legal return checkCharTags(); } uint WavInFile::getNumChannels() const { return header.format.channel_number; } uint WavInFile::getNumBits() const { return header.format.bits_per_sample; } uint WavInFile::getBytesPerSample() const { return getNumChannels() * getNumBits() / 8; } uint WavInFile::getSampleRate() const { return header.format.sample_rate; } uint WavInFile::getDataSizeInBytes() const { return header.data.data_len; } uint WavInFile::getNumSamples() const { if (header.format.byte_per_sample == 0) return 0; if (header.format.fixed > 1) return header.fact.fact_sample_len; return header.data.data_len / (unsigned short)header.format.byte_per_sample; } uint WavInFile::getLengthMS() const { double numSamples; double sampleRate; numSamples = (double)getNumSamples(); sampleRate = (double)getSampleRate(); return (uint)(1000.0 * numSamples / sampleRate + 0.5); } /// Returns how many milliseconds of audio have so far been read from the file uint WavInFile::getElapsedMS() const { return (uint)(1000.0 * (double)dataRead / (double)header.format.byte_rate); } ////////////////////////////////////////////////////////////////////////////// // // Class WavOutFile // WavOutFile::WavOutFile(const char *fileName, int sampleRate, int bits, int channels) { bytesWritten = 0; fptr = fopen(fileName, "wb"); if (fptr == NULL) { string msg = "Error : Unable to open file \""; msg += fileName; msg += "\" for writing."; //pmsg = msg.c_str; ST_THROW_RT_ERROR(msg.c_str()); } fillInHeader(sampleRate, bits, channels); writeHeader(); } WavOutFile::WavOutFile(FILE *file, int sampleRate, int bits, int channels) { bytesWritten = 0; fptr = file; if (fptr == NULL) { string msg = "Error : Unable to access output file stream."; ST_THROW_RT_ERROR(msg.c_str()); } fillInHeader(sampleRate, bits, channels); writeHeader(); } WavOutFile::~WavOutFile() { finishHeader(); if (fptr) fclose(fptr); fptr = NULL; } void WavOutFile::fillInHeader(uint sampleRate, uint bits, uint channels) { // fill in the 'riff' part.. // copy string 'RIFF' to riff_char memcpy(&(header.riff.riff_char), riffStr, 4); // package_len unknown so far header.riff.package_len = 0; // copy string 'WAVE' to wave memcpy(&(header.riff.wave), waveStr, 4); // fill in the 'format' part.. // copy string 'fmt ' to fmt memcpy(&(header.format.fmt), fmtStr, 4); header.format.format_len = 0x10; header.format.fixed = 1; header.format.channel_number = (short)channels; header.format.sample_rate = (int)sampleRate; header.format.bits_per_sample = (short)bits; header.format.byte_per_sample = (short)(bits * channels / 8); header.format.byte_rate = header.format.byte_per_sample * (int)sampleRate; header.format.sample_rate = (int)sampleRate; // fill in the 'fact' part... memcpy(&(header.fact.fact_field), factStr, 4); header.fact.fact_len = 4; header.fact.fact_sample_len = 0; // fill in the 'data' part.. // copy string 'data' to data_field memcpy(&(header.data.data_field), dataStr, 4); // data_len unknown so far header.data.data_len = 0; } void WavOutFile::finishHeader() { // supplement the file length into the header structure header.riff.package_len = bytesWritten + sizeof(WavHeader) - sizeof(WavRiff) + 4; header.data.data_len = bytesWritten; header.fact.fact_sample_len = bytesWritten / header.format.byte_per_sample; writeHeader(); } void WavOutFile::writeHeader() { WavHeader hdrTemp; int res; // swap byte order if necessary hdrTemp = header; _swap32((int &)hdrTemp.riff.package_len); _swap32((int &)hdrTemp.format.format_len); _swap16((short &)hdrTemp.format.fixed); _swap16((short &)hdrTemp.format.channel_number); _swap32((int &)hdrTemp.format.sample_rate); _swap32((int &)hdrTemp.format.byte_rate); _swap16((short &)hdrTemp.format.byte_per_sample); _swap16((short &)hdrTemp.format.bits_per_sample); _swap32((int &)hdrTemp.data.data_len); _swap32((int &)hdrTemp.fact.fact_len); _swap32((int &)hdrTemp.fact.fact_sample_len); // write the supplemented header in the beginning of the file fseek(fptr, 0, SEEK_SET); res = (int)fwrite(&hdrTemp, sizeof(hdrTemp), 1, fptr); if (res != 1) { ST_THROW_RT_ERROR("Error while writing to a wav file."); } // jump back to the end of the file fseek(fptr, 0, SEEK_END); } void WavOutFile::write(const unsigned char *buffer, int numElems) { int res; if (header.format.bits_per_sample != 8) { ST_THROW_RT_ERROR("Error: WavOutFile::write(const char*, int) accepts only 8bit samples."); } assert(sizeof(char) == 1); res = (int)fwrite(buffer, 1, numElems, fptr); if (res != numElems) { ST_THROW_RT_ERROR("Error while writing to a wav file."); } bytesWritten += numElems; } void WavOutFile::write(const short *buffer, int numElems) { int res; // 16 bit samples if (numElems < 1) return; // nothing to do switch (header.format.bits_per_sample) { case 8: { int i; unsigned char *temp = (unsigned char *)getConvBuffer(numElems); // convert from 16bit format to 8bit format for (i = 0; i < numElems; i ++) { temp[i] = (unsigned char)(buffer[i] / 256 + 128); } // write in 8bit format write(temp, numElems); break; } case 16: { // 16bit format // use temp buffer to swap byte order if necessary short *pTemp = (short *)getConvBuffer(numElems * sizeof(short)); memcpy(pTemp, buffer, numElems * 2); _swap16Buffer(pTemp, numElems); res = (int)fwrite(pTemp, 2, numElems, fptr); if (res != numElems) { ST_THROW_RT_ERROR("Error while writing to a wav file."); } bytesWritten += 2 * numElems; break; } default: { stringstream ss; ss << "\nOnly 8/16 bit sample WAV files supported in integer compilation. Can't open WAV file with "; ss << (int)header.format.bits_per_sample; ss << " bit sample format. "; ST_THROW_RT_ERROR(ss.str().c_str()); } } } /// Convert from float to integer and saturate inline int saturate(float fvalue, float minval, float maxval) { if (fvalue > maxval) { fvalue = maxval; } else if (fvalue < minval) { fvalue = minval; } return (int)fvalue; } void WavOutFile::write(const float *buffer, int numElems) { int numBytes; int bytesPerSample; if (numElems == 0) return; bytesPerSample = header.format.bits_per_sample / 8; numBytes = numElems * bytesPerSample; short *temp = (short*)getConvBuffer(numBytes); switch (bytesPerSample) { case 1: { unsigned char *temp2 = (unsigned char *)temp; for (int i = 0; i < numElems; i ++) { temp2[i] = (unsigned char)saturate(buffer[i] * 128.0f + 128.0f, 0.0f, 255.0f); } break; } case 2: { short *temp2 = (short *)temp; for (int i = 0; i < numElems; i ++) { short value = (short)saturate(buffer[i] * 32768.0f, -32768.0f, 32767.0f); temp2[i] = _swap16(value); } break; } case 3: { char *temp2 = (char *)temp; for (int i = 0; i < numElems; i ++) { int value = saturate(buffer[i] * 8388608.0f, -8388608.0f, 8388607.0f); *((int*)temp2) = _swap32(value); temp2 += 3; } break; } case 4: { int *temp2 = (int *)temp; for (int i = 0; i < numElems; i ++) { int value = saturate(buffer[i] * 2147483648.0f, -2147483648.0f, 2147483647.0f); temp2[i] = _swap32(value); } break; } default: assert(false); } int res = (int)fwrite(temp, 1, numBytes, fptr); if (res != numBytes) { ST_THROW_RT_ERROR("Error while writing to a wav file."); } bytesWritten += numBytes; } soundtouch/source/SoundStretch/RunParameters.h0000664000175100017510000000447512577461413021213 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// A class for parsing the 'soundstretch' application command line parameters /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2014-04-06 18:57:21 +0300 (Sun, 06 Apr 2014) $ // File revision : $Revision: 4 $ // // $Id: RunParameters.h 195 2014-04-06 15:57:21Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef RUNPARAMETERS_H #define RUNPARAMETERS_H #include "STTypes.h" #include using namespace std; /// Parses command line parameters into program parameters class RunParameters { private: void throwIllegalParamExp(const string &str) const; void throwLicense() const; void parseSwitchParam(const string &str); void checkLimits(); float parseSwitchValue(const string &str) const; public: char *inFileName; char *outFileName; float tempoDelta; float pitchDelta; float rateDelta; int quick; int noAntiAlias; float goalBPM; bool detectBPM; bool speech; RunParameters(const int nParams, const char * const paramStr[]); }; #endif soundtouch/source/SoundStretch/soundstretch.dsw0000664000175100017510000000151512577461413021506 0ustar vmdevvmdevMicrosoft Developer Studio Workspace File, Format Version 6.00 # WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE! ############################################################################### Project: "SoundTouch"=..\SoundTouch\SoundTouch.dsp - Package Owner=<4> Package=<5> {{{ }}} Package=<4> {{{ }}} ############################################################################### Project: "soundstretch"=.\soundstretch.dsp - Package Owner=<4> Package=<5> {{{ }}} Package=<4> {{{ Begin Project Dependency Project_Dep_Name SoundTouch End Project Dependency }}} ############################################################################### Global: Package=<5> {{{ }}} Package=<3> {{{ }}} ############################################################################### soundtouch/source/SoundStretch/soundstretch.sln0000664000175100017510000000354112577461413021506 0ustar vmdevvmdevMicrosoft Visual Studio Solution File, Format Version 10.00 # Visual Studio 2008 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "soundstretch", "soundstretch.vcproj", "{5AACDFFA-D491-44B8-A332-DA7ACCAAF2AF}" 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}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 Release|Win32 = Release|Win32 ReleaseX64|Win32 = ReleaseX64|Win32 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {5AACDFFA-D491-44B8-A332-DA7ACCAAF2AF}.Debug|Win32.ActiveCfg = Debug|Win32 {5AACDFFA-D491-44B8-A332-DA7ACCAAF2AF}.Debug|Win32.Build.0 = Debug|Win32 {5AACDFFA-D491-44B8-A332-DA7ACCAAF2AF}.Release|Win32.ActiveCfg = Release|Win32 {5AACDFFA-D491-44B8-A332-DA7ACCAAF2AF}.Release|Win32.Build.0 = Release|Win32 {5AACDFFA-D491-44B8-A332-DA7ACCAAF2AF}.ReleaseX64|Win32.ActiveCfg = ReleaseX64|x64 {5AACDFFA-D491-44B8-A332-DA7ACCAAF2AF}.ReleaseX64|Win32.Build.0 = ReleaseX64|x64 {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 {68A5DD20-7057-448B-8FE0-B6AC8D205509}.ReleaseX64|Win32.ActiveCfg = ReleaseX64|x64 {68A5DD20-7057-448B-8FE0-B6AC8D205509}.ReleaseX64|Win32.Build.0 = ReleaseX64|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection EndGlobal soundtouch/source/SoundTouchDLL/0000775000175100017510000000000012577461413016242 5ustar vmdevvmdevsoundtouch/source/SoundTouchDLL/SoundTouchDLL.pas0000664000175100017510000004267012577461413021407 0ustar vmdevvmdevunit SoundTouchDLL; ////////////////////////////////////////////////////////////////////////////// // // SoundTouch.dll wrapper for accessing SoundTouch routines from Delphi/Pascal // // Module Author : Christian Budde // // 2014-01-12 fixes by Sandro Cumerlato // //////////////////////////////////////////////////////////////////////////////// // // $Id: SoundTouchDLL.pas 198 2014-04-06 18:06:50Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// interface uses Windows; type TSoundTouchHandle = THandle; // Create a new instance of SoundTouch processor. TSoundTouchCreateInstance = function : TSoundTouchHandle; cdecl; // Destroys a SoundTouch processor instance. TSoundTouchDestroyInstance = procedure (Handle: TSoundTouchHandle); cdecl; // Get SoundTouch library version string TSoundTouchGetVersionString = function : PAnsiChar; cdecl; // Get SoundTouch library version string 2 TSoundTouchGetVersionString2 = procedure(VersionString : PAnsiChar; BufferSize : Integer); cdecl; // Get SoundTouch library version Id TSoundTouchGetVersionId = function : Cardinal; cdecl; // Sets new rate control value. Normal rate = 1.0, smaller values // represent slower rate, larger faster rates. TSoundTouchSetRate = procedure (Handle: TSoundTouchHandle; NewRate: Single); cdecl; // Sets new tempo control value. Normal tempo = 1.0, smaller values // represent slower tempo, larger faster tempo. TSoundTouchSetTempo = procedure (Handle: TSoundTouchHandle; NewTempo: Single); cdecl; // Sets new rate control value as a difference in percents compared // to the original rate (-50 .. +100 %); TSoundTouchSetRateChange = procedure (Handle: TSoundTouchHandle; NewRate: Single); cdecl; // Sets new tempo control value as a difference in percents compared // to the original tempo (-50 .. +100 %); TSoundTouchSetTempoChange = procedure (Handle: TSoundTouchHandle; NewTempo: Single); cdecl; // Sets new pitch control value. Original pitch = 1.0, smaller values // represent lower pitches, larger values higher pitch. TSoundTouchSetPitch = procedure (Handle: TSoundTouchHandle; NewPitch: Single); cdecl; // Sets pitch change in octaves compared to the original pitch // (-1.00 .. +1.00); TSoundTouchSetPitchOctaves = procedure (Handle: TSoundTouchHandle; NewPitch: Single); cdecl; // Sets pitch change in semi-tones compared to the original pitch // (-12 .. +12); TSoundTouchSetPitchSemiTones = procedure (Handle: TSoundTouchHandle; NewPitch: Single); cdecl; // Sets the number of channels, 1 = mono, 2 = stereo TSoundTouchSetChannels = procedure (Handle: TSoundTouchHandle; NumChannels: Cardinal); cdecl; // Sets sample rate. TSoundTouchSetSampleRate = procedure (Handle: TSoundTouchHandle; SampleRate: Cardinal); cdecl; // Flushes the last samples from the processing pipeline to the output. // Clears also the internal processing buffers. // // Note: This function is meant for extracting the last samples of a sound // stream. This function may introduce additional blank samples in the end // of the sound stream, and thus it // in the middle of a sound stream. TSoundTouchFlush = procedure (Handle: TSoundTouchHandle); cdecl; // Adds 'numSamples' pcs of samples from the 'samples' memory position into // the input of the object. Notice that sample rate _has_to_ be set before // calling this function, otherwise throws a runtime_error exception. TSoundTouchPutSamples = procedure (Handle: TSoundTouchHandle; const Samples: PSingle; //< Pointer to sample buffer. NumSamples: Cardinal //< Number of samples in buffer. Notice //< that in case of stereo-sound a single sample //< contains data for both channels. ); cdecl; // Clears all the samples in the object's output and internal processing // buffers. TSoundTouchClear = procedure (Handle: TSoundTouchHandle); cdecl; // Changes a setting controlling the processing system behaviour. See the // 'SETTING_...' defines for available setting ID's. // // \return 'TRUE' if the setting was succesfully changed TSoundTouchSetSetting = function (Handle: TSoundTouchHandle; SettingId: Integer; //< Setting ID number. see SETTING_... defines. Value: Integer //< New setting value. ): Boolean; cdecl; // Reads a setting controlling the processing system behaviour. See the // 'SETTING_...' defines for available setting ID's. // // \return the setting value. TSoundTouchGetSetting = function (Handle: TSoundTouchHandle; SettingId: Integer //< Setting ID number, see SETTING_... defines. ): Integer; cdecl; // Returns number of samples currently unprocessed. TSoundTouchNumUnprocessedSamples = function (Handle: TSoundTouchHandle): Cardinal; cdecl; // Adjusts book-keeping so that given number of samples are removed from beginning of the // sample buffer without copying them anywhere. // // Used to reduce the number of samples in the buffer when accessing the sample buffer directly // with 'ptrBegin' function. TSoundTouchReceiveSamples = function (Handle: TSoundTouchHandle; OutBuffer: PSingle; //< Buffer where to copy output samples. MaxSamples: Integer //< How many samples to receive at max. ): Cardinal; cdecl; // Returns number of samples currently available. TSoundTouchNumSamples = function (Handle: TSoundTouchHandle): Cardinal; cdecl; // Returns nonzero if there aren't any samples available for outputting. TSoundTouchIsEmpty = function (Handle: TSoundTouchHandle): Integer; cdecl; var SoundTouchCreateInstance : TSoundTouchCreateInstance; SoundTouchDestroyInstance : TSoundTouchDestroyInstance; SoundTouchGetVersionString : TSoundTouchGetVersionString; SoundTouchGetVersionString2 : TSoundTouchGetVersionString2; SoundTouchGetVersionId : TSoundTouchGetVersionId; SoundTouchSetRate : TSoundTouchSetRate; SoundTouchSetTempo : TSoundTouchSetTempo; SoundTouchSetRateChange : TSoundTouchSetRateChange; SoundTouchSetTempoChange : TSoundTouchSetTempoChange; SoundTouchSetPitch : TSoundTouchSetPitch; SoundTouchSetPitchOctaves : TSoundTouchSetPitchOctaves; SoundTouchSetPitchSemiTones : TSoundTouchSetPitchSemiTones; SoundTouchSetChannels : TSoundTouchSetChannels; SoundTouchSetSampleRate : TSoundTouchSetSampleRate; SoundTouchFlush : TSoundTouchFlush; SoundTouchPutSamples : TSoundTouchPutSamples; SoundTouchClear : TSoundTouchClear; SoundTouchSetSetting : TSoundTouchSetSetting; SoundTouchGetSetting : TSoundTouchGetSetting; SoundTouchNumUnprocessedSamples : TSoundTouchNumUnprocessedSamples; SoundTouchReceiveSamples : TSoundTouchReceiveSamples; SoundTouchNumSamples : TSoundTouchNumSamples; SoundTouchIsEmpty : TSoundTouchIsEmpty; type TSoundTouch = class private FHandle : TSoundTouchHandle; FRate : Single; FPitch : Single; FTempo : Single; FSampleRate : Single; FChannels : Cardinal; function GetNumSamples: Cardinal; function GetNumUnprocessedSamples: Cardinal; function GetIsEmpty: Integer; function GetPitchChange: Single; function GetRateChange: Single; function GetTempoChange: Single; procedure SetRate(const Value: Single); procedure SetPitch(const Value: Single); procedure SetTempo(const Value: Single); procedure SetPitchChange(const Value: Single); procedure SetRateChange(const Value: Single); procedure SetTempoChange(const Value: Single); procedure SetChannels(const Value: Cardinal); procedure SetSampleRate(const Value: Single); protected procedure SamplerateChanged; virtual; procedure ChannelsChanged; virtual; procedure PitchChanged; virtual; procedure TempoChanged; virtual; procedure RateChanged; virtual; public class function GetVersionString: string; class function GetVersionId: Cardinal; constructor Create; virtual; destructor Destroy; override; procedure Flush; virtual; procedure Clear; virtual; procedure PutSamples(const Samples: PSingle; const NumSamples: Cardinal); function ReceiveSamples(const OutBuffer: PSingle; const MaxSamples: Integer): Cardinal; function SetSetting(const SettingId: Integer; const Value: Integer): Boolean; function GetSetting(const SettingId: Integer): Integer; property VersionString: string read GetVersionString; property VersionID: Cardinal read GetVersionId; property Channels: Cardinal read FChannels write SetChannels; property Rate: Single read FRate write SetRate; property RateChange: Single read GetRateChange write SetRateChange; property Tempo: Single read FTempo write SetTempo; property TempoChange: Single read GetTempoChange write SetTempoChange; property Pitch: Single read FPitch write SetPitch; property PitchChange: Single read GetPitchChange write SetPitchChange; property SampleRate: Single read FSampleRate write SetSampleRate; property NumSamples: Cardinal read GetNumSamples; property NumUnprocessedSamples: Cardinal read GetNumUnprocessedSamples; property IsEmpty: Integer read GetIsEmpty; end; implementation { TSoundTouch } constructor TSoundTouch.Create; begin inherited; FHandle := SoundTouchCreateInstance(); FRate := 1; FTempo := 1; FPitch := 1; FChannels := 1; FSampleRate := 44100; SamplerateChanged; ChannelsChanged; end; destructor TSoundTouch.Destroy; begin SoundTouchDestroyInstance(FHandle); inherited; end; procedure TSoundTouch.Flush; begin SoundTouchFlush(FHandle); end; procedure TSoundTouch.Clear; begin SoundTouchClear(FHandle); end; function TSoundTouch.GetIsEmpty: Integer; begin result := SoundTouchIsEmpty(FHandle); end; function TSoundTouch.GetNumSamples: Cardinal; begin result := SoundTouchNumSamples(FHandle); end; function TSoundTouch.GetNumUnprocessedSamples: Cardinal; begin result := SoundTouchNumUnprocessedSamples(FHandle); end; function TSoundTouch.GetPitchChange: Single; begin result := 100 * (FPitch - 1.0); end; function TSoundTouch.GetRateChange: Single; begin result := 100 * (FRate - 1.0); end; function TSoundTouch.GetTempoChange: Single; begin result := 100 * (FTempo - 1.0); end; class function TSoundTouch.GetVersionId: Cardinal; begin result := SoundTouchGetVersionId(); end; class function TSoundTouch.GetVersionString: string; begin result := StrPas(SoundTouchGetVersionString()); end; procedure TSoundTouch.SetChannels(const Value: Cardinal); begin if FChannels <> Value then begin FChannels := Value; ChannelsChanged; end; end; procedure TSoundTouch.ChannelsChanged; begin assert(FChannels in [1, 2]); SoundTouchSetChannels(FHandle, FChannels); end; procedure TSoundTouch.SetPitch(const Value: Single); begin if FPitch <> Value then begin FPitch := Value; PitchChanged; end; end; procedure TSoundTouch.PitchChanged; begin SoundTouchSetPitch(FHandle, FPitch); end; procedure TSoundTouch.putSamples(const Samples: PSingle; const NumSamples: Cardinal); begin SoundTouchPutSamples(FHandle, Samples, NumSamples); end; procedure TSoundTouch.RateChanged; begin SoundTouchSetRate(FHandle, FRate); end; function TSoundTouch.ReceiveSamples(const OutBuffer: PSingle; const MaxSamples: Integer): Cardinal; begin result := SoundTouchReceiveSamples(FHandle, OutBuffer, MaxSamples); end; procedure TSoundTouch.SetPitchChange(const Value: Single); begin Pitch := 1.0 + 0.01 * Value; end; procedure TSoundTouch.SetRate(const Value: Single); begin if FRate <> Value then begin FRate := Value; RateChanged; end; end; procedure TSoundTouch.SetRateChange(const Value: Single); begin Rate := 1.0 + 0.01 * Value; end; procedure TSoundTouch.SetSampleRate(const Value: Single); begin if FSampleRate <> Value then begin FSampleRate := Value; SamplerateChanged; end; end; procedure TSoundTouch.SamplerateChanged; begin assert(FSampleRate > 0); SoundTouchsetSampleRate(FHandle, round(FSampleRate)); end; procedure TSoundTouch.SetTempo(const Value: Single); begin if FTempo <> Value then begin FTempo := Value; TempoChanged; end; end; procedure TSoundTouch.SetTempoChange(const Value: Single); begin Tempo := 1.0 + 0.01 * Value; end; function TSoundTouch.GetSetting(const SettingId: Integer): Integer; begin result := SoundTouchGetSetting(FHandle, SettingId); end; function TSoundTouch.SetSetting(const SettingId: Integer; const Value: Integer): Boolean; begin result := SoundTouchSetSetting(FHandle, SettingId, Value); end; procedure TSoundTouch.TempoChanged; begin SoundTouchsetTempo(FHandle, FTempo); end; var SoundTouchLibHandle: HINST; SoundTouchDLLFile: PAnsiChar = 'SoundTouch.dll'; procedure InitDLL; begin SoundTouchLibHandle := LoadLibrary(SoundTouchDLLFile); if SoundTouchLibHandle <> 0 then try Pointer(SoundTouchCreateInstance) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_createInstance'); Pointer(SoundTouchDestroyInstance) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_destroyInstance'); Pointer(SoundTouchGetVersionString) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_getVersionString'); Pointer(SoundTouchGetVersionString2) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_getVersionString2'); Pointer(SoundTouchGetVersionId) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_getVersionId'); Pointer(SoundTouchSetRate) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_setRate'); Pointer(SoundTouchSetTempo) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_setTempo'); Pointer(SoundTouchSetRateChange) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_setRateChange'); Pointer(SoundTouchSetTempoChange) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_setTempoChange'); Pointer(SoundTouchSetPitch) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_setPitch'); Pointer(SoundTouchSetPitchOctaves) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_setPitchOctaves'); Pointer(SoundTouchSetPitchSemiTones) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_setPitchSemiTones'); Pointer(SoundTouchSetChannels) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_setChannels'); Pointer(SoundTouchSetSampleRate) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_setSampleRate'); Pointer(SoundTouchFlush) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_flush'); Pointer(SoundTouchPutSamples) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_putSamples'); Pointer(SoundTouchClear) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_clear'); Pointer(SoundTouchSetSetting) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_SetSetting'); Pointer(SoundTouchGetSetting) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_setSetting'); Pointer(SoundTouchNumUnprocessedSamples) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_numUnprocessedSamples'); Pointer(SoundTouchReceiveSamples) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_receiveSamples'); Pointer(SoundTouchNumSamples) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_numSamples'); Pointer(SoundTouchIsEmpty) := GetProcAddress(SoundTouchLibHandle, 'soundtouch_isEmpty'); except FreeLibrary(SoundTouchLibHandle); SoundTouchLibHandle := 0; end; end; procedure FreeDLL; begin if SoundTouchLibHandle <> 0 then FreeLibrary(SoundTouchLibHandle); end; initialization InitDLL; finalization FreeDLL; end. soundtouch/source/SoundTouchDLL/resource.h0000664000175100017510000000063312577461413020244 0ustar vmdevvmdev//{{NO_DEPENDENCIES}} // Microsoft Visual C++ generated include file. // Used by SoundTouchDLL.rc // // Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 101 #define _APS_NEXT_COMMAND_VALUE 40001 #define _APS_NEXT_CONTROL_VALUE 1000 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif soundtouch/source/SoundTouchDLL/SoundTouchDLL.h0000664000175100017510000001615712577461413021054 0ustar vmdevvmdev////////////////////////////////////////////////////////////////////////////// /// /// SoundTouch DLL wrapper - wraps SoundTouch routines into a Dynamic Load /// Library interface. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // $Id: SoundTouchDLL.h 198 2014-04-06 18:06:50Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef _SoundTouchDLL_h_ #define _SoundTouchDLL_h_ #ifdef __cplusplus #ifdef DLL_EXPORTS #define SOUNDTOUCHDLL_API extern "C" __declspec(dllexport) #else #define SOUNDTOUCHDLL_API extern "C" __declspec(dllimport) #endif #else #ifdef DLL_EXPORTS #define SOUNDTOUCHDLL_API __declspec(dllexport) #else #define SOUNDTOUCHDLL_API __declspec(dllimport) #endif #endif // __cplusplus typedef void * HANDLE; /// Create a new instance of SoundTouch processor. SOUNDTOUCHDLL_API HANDLE __cdecl soundtouch_createInstance(); /// Destroys a SoundTouch processor instance. SOUNDTOUCHDLL_API void __cdecl soundtouch_destroyInstance(HANDLE h); /// Get SoundTouch library version string SOUNDTOUCHDLL_API const char *__cdecl soundtouch_getVersionString(); /// Get SoundTouch library version string - alternative function for /// environments that can't properly handle character string as return value SOUNDTOUCHDLL_API void __cdecl soundtouch_getVersionString2(char* versionString, int bufferSize); /// Get SoundTouch library version Id SOUNDTOUCHDLL_API unsigned int __cdecl soundtouch_getVersionId(); /// Sets new rate control value. Normal rate = 1.0, smaller values /// represent slower rate, larger faster rates. SOUNDTOUCHDLL_API void __cdecl soundtouch_setRate(HANDLE h, float newRate); /// Sets new tempo control value. Normal tempo = 1.0, smaller values /// represent slower tempo, larger faster tempo. SOUNDTOUCHDLL_API void __cdecl soundtouch_setTempo(HANDLE h, float newTempo); /// Sets new rate control value as a difference in percents compared /// to the original rate (-50 .. +100 %); SOUNDTOUCHDLL_API void __cdecl soundtouch_setRateChange(HANDLE h, float newRate); /// Sets new tempo control value as a difference in percents compared /// to the original tempo (-50 .. +100 %); SOUNDTOUCHDLL_API void __cdecl soundtouch_setTempoChange(HANDLE h, float newTempo); /// Sets new pitch control value. Original pitch = 1.0, smaller values /// represent lower pitches, larger values higher pitch. SOUNDTOUCHDLL_API void __cdecl soundtouch_setPitch(HANDLE h, float newPitch); /// Sets pitch change in octaves compared to the original pitch /// (-1.00 .. +1.00); SOUNDTOUCHDLL_API void __cdecl soundtouch_setPitchOctaves(HANDLE h, float newPitch); /// Sets pitch change in semi-tones compared to the original pitch /// (-12 .. +12); SOUNDTOUCHDLL_API void __cdecl soundtouch_setPitchSemiTones(HANDLE h, float newPitch); /// Sets the number of channels, 1 = mono, 2 = stereo SOUNDTOUCHDLL_API void __cdecl soundtouch_setChannels(HANDLE h, unsigned int numChannels); /// Sets sample rate. SOUNDTOUCHDLL_API void __cdecl soundtouch_setSampleRate(HANDLE h, unsigned int srate); /// Flushes the last samples from the processing pipeline to the output. /// Clears also the internal processing buffers. // /// Note: This function is meant for extracting the last samples of a sound /// stream. This function may introduce additional blank samples in the end /// of the sound stream, and thus it's not recommended to call this function /// in the middle of a sound stream. SOUNDTOUCHDLL_API void __cdecl soundtouch_flush(HANDLE h); /// Adds 'numSamples' pcs of samples from the 'samples' memory position into /// the input of the object. Notice that sample rate _has_to_ be set before /// calling this function, otherwise throws a runtime_error exception. SOUNDTOUCHDLL_API void __cdecl soundtouch_putSamples(HANDLE h, const float *samples, ///< Pointer to sample buffer. unsigned int numSamples ///< Number of samples in buffer. Notice ///< that in case of stereo-sound a single sample ///< contains data for both channels. ); /// Clears all the samples in the object's output and internal processing /// buffers. SOUNDTOUCHDLL_API void __cdecl soundtouch_clear(HANDLE h); /// Changes a setting controlling the processing system behaviour. See the /// 'SETTING_...' defines for available setting ID's. /// /// \return 'TRUE' if the setting was succesfully changed SOUNDTOUCHDLL_API BOOL __cdecl soundtouch_setSetting(HANDLE h, int settingId, ///< Setting ID number. see SETTING_... defines. int value ///< New setting value. ); /// Reads a setting controlling the processing system behaviour. See the /// 'SETTING_...' defines for available setting ID's. /// /// \return the setting value. SOUNDTOUCHDLL_API int __cdecl soundtouch_getSetting(HANDLE h, int settingId ///< Setting ID number, see SETTING_... defines. ); /// Returns number of samples currently unprocessed. SOUNDTOUCHDLL_API unsigned int __cdecl soundtouch_numUnprocessedSamples(HANDLE h); /// Adjusts book-keeping so that given number of samples are removed from beginning of the /// sample buffer without copying them anywhere. /// /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly /// with 'ptrBegin' function. SOUNDTOUCHDLL_API unsigned int __cdecl soundtouch_receiveSamples(HANDLE h, float *outBuffer, ///< Buffer where to copy output samples. unsigned int maxSamples ///< How many samples to receive at max. ); /// Returns number of samples currently available. SOUNDTOUCHDLL_API unsigned int __cdecl soundtouch_numSamples(HANDLE h); /// Returns nonzero if there aren't any samples available for outputting. SOUNDTOUCHDLL_API int __cdecl soundtouch_isEmpty(HANDLE h); #endif // _SoundTouchDLL_h_ soundtouch/source/SoundTouchDLL/SoundTouchDLL.sln0000664000175100017510000000354312577461413021414 0ustar vmdevvmdevMicrosoft 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}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 Release|Win32 = Release|Win32 ReleaseX64|Win32 = ReleaseX64|Win32 EndGlobalSection 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 {164DE61D-6391-4265-8273-30740117D356}.ReleaseX64|Win32.ActiveCfg = ReleaseX64|x64 {164DE61D-6391-4265-8273-30740117D356}.ReleaseX64|Win32.Build.0 = ReleaseX64|x64 {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 {68A5DD20-7057-448B-8FE0-B6AC8D205509}.ReleaseX64|Win32.ActiveCfg = ReleaseX64|x64 {68A5DD20-7057-448B-8FE0-B6AC8D205509}.ReleaseX64|Win32.Build.0 = ReleaseX64|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection EndGlobal soundtouch/source/SoundTouchDLL/SoundTouchDLL.vcproj0000664000175100017510000002754612577461413022134 0ustar vmdevvmdev soundtouch/source/SoundTouchDLL/SoundTouchDLL.cpp0000664000175100017510000002416212577461413021402 0ustar vmdevvmdev////////////////////////////////////////////////////////////////////////////// /// /// SoundTouch DLL wrapper - wraps SoundTouch routines into a Dynamic Load /// Library interface. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // $Id: SoundTouchDLL.cpp 207 2015-02-22 15:16:48Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include #include #include "SoundTouchDLL.h" #include "soundtouch.h" using namespace soundtouch; BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } ////////////// typedef struct { DWORD dwMagic; SoundTouch *pst; } STHANDLE; #define STMAGIC 0x1770C001 SOUNDTOUCHDLL_API HANDLE __cdecl soundtouch_createInstance() { STHANDLE *tmp = new STHANDLE; if (tmp) { tmp->dwMagic = STMAGIC; tmp->pst = new SoundTouch(); if (tmp->pst == NULL) { delete tmp; tmp = NULL; } } return (HANDLE)tmp; } SOUNDTOUCHDLL_API void __cdecl soundtouch_destroyInstance(HANDLE h) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return; sth->dwMagic = 0; if (sth->pst) delete sth->pst; sth->pst = NULL; delete sth; } /// Get SoundTouch library version string SOUNDTOUCHDLL_API const char *__cdecl soundtouch_getVersionString() { return SoundTouch::getVersionString(); } /// Get SoundTouch library version string - alternative function for /// environments that can't properly handle character string as return value SOUNDTOUCHDLL_API void __cdecl soundtouch_getVersionString2(char* versionString, int bufferSize) { strncpy(versionString, SoundTouch::getVersionString(), bufferSize - 1); versionString[bufferSize - 1] = 0; } /// Get SoundTouch library version Id SOUNDTOUCHDLL_API uint __cdecl soundtouch_getVersionId() { return SoundTouch::getVersionId(); } /// Sets new rate control value. Normal rate = 1.0, smaller values /// represent slower rate, larger faster rates. SOUNDTOUCHDLL_API void __cdecl soundtouch_setRate(HANDLE h, float newRate) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return; sth->pst->setRate(newRate); } /// Sets new tempo control value. Normal tempo = 1.0, smaller values /// represent slower tempo, larger faster tempo. SOUNDTOUCHDLL_API void __cdecl soundtouch_setTempo(HANDLE h, float newTempo) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return; sth->pst->setTempo(newTempo); } /// Sets new rate control value as a difference in percents compared /// to the original rate (-50 .. +100 %) SOUNDTOUCHDLL_API void __cdecl soundtouch_setRateChange(HANDLE h, float newRate) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return; sth->pst->setRateChange(newRate); } /// Sets new tempo control value as a difference in percents compared /// to the original tempo (-50 .. +100 %) SOUNDTOUCHDLL_API void __cdecl soundtouch_setTempoChange(HANDLE h, float newTempo) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return; sth->pst->setTempoChange(newTempo); } /// Sets new pitch control value. Original pitch = 1.0, smaller values /// represent lower pitches, larger values higher pitch. SOUNDTOUCHDLL_API void __cdecl soundtouch_setPitch(HANDLE h, float newPitch) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return; sth->pst->setPitch(newPitch); } /// Sets pitch change in octaves compared to the original pitch /// (-1.00 .. +1.00) SOUNDTOUCHDLL_API void __cdecl soundtouch_setPitchOctaves(HANDLE h, float newPitch) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return; sth->pst->setPitchOctaves(newPitch); } /// Sets pitch change in semi-tones compared to the original pitch /// (-12 .. +12) SOUNDTOUCHDLL_API void __cdecl soundtouch_setPitchSemiTones(HANDLE h, float newPitch) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return; sth->pst->setPitchSemiTones(newPitch); } /// Sets the number of channels, 1 = mono, 2 = stereo SOUNDTOUCHDLL_API void __cdecl soundtouch_setChannels(HANDLE h, uint numChannels) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return; sth->pst->setChannels(numChannels); } /// Sets sample rate. SOUNDTOUCHDLL_API void __cdecl soundtouch_setSampleRate(HANDLE h, uint srate) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return; sth->pst->setSampleRate(srate); } /// Flushes the last samples from the processing pipeline to the output. /// Clears also the internal processing buffers. // /// Note: This function is meant for extracting the last samples of a sound /// stream. This function may introduce additional blank samples in the end /// of the sound stream, and thus it's not recommended to call this function /// in the middle of a sound stream. SOUNDTOUCHDLL_API void __cdecl soundtouch_flush(HANDLE h) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return; sth->pst->flush(); } /// Adds 'numSamples' pcs of samples from the 'samples' memory position into /// the input of the object. Notice that sample rate _has_to_ be set before /// calling this function, otherwise throws a runtime_error exception. SOUNDTOUCHDLL_API void __cdecl soundtouch_putSamples(HANDLE h, const SAMPLETYPE *samples, ///< Pointer to sample buffer. uint numSamples ///< Number of samples in buffer. Notice ///< that in case of stereo-sound a single sample ///< contains data for both channels. ) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return; sth->pst->putSamples(samples, numSamples); } /// Clears all the samples in the object's output and internal processing /// buffers. SOUNDTOUCHDLL_API void __cdecl soundtouch_clear(HANDLE h) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return; sth->pst->clear(); } /// Changes a setting controlling the processing system behaviour. See the /// 'SETTING_...' defines for available setting ID's. /// /// \return 'TRUE' if the setting was succesfully changed SOUNDTOUCHDLL_API BOOL __cdecl soundtouch_setSetting(HANDLE h, int settingId, ///< Setting ID number. see SETTING_... defines. int value ///< New setting value. ) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return FALSE; return sth->pst->setSetting(settingId, value); } /// Reads a setting controlling the processing system behaviour. See the /// 'SETTING_...' defines for available setting ID's. /// /// \return the setting value. SOUNDTOUCHDLL_API int __cdecl soundtouch_getSetting(HANDLE h, int settingId ///< Setting ID number, see SETTING_... defines. ) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return -1; return sth->pst->getSetting(settingId); } /// Returns number of samples currently unprocessed. SOUNDTOUCHDLL_API uint __cdecl soundtouch_numUnprocessedSamples(HANDLE h) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return 0; return sth->pst->numUnprocessedSamples(); } /// Adjusts book-keeping so that given number of samples are removed from beginning of the /// sample buffer without copying them anywhere. /// /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly /// with 'ptrBegin' function. SOUNDTOUCHDLL_API uint __cdecl soundtouch_receiveSamples(HANDLE h, SAMPLETYPE *outBuffer, ///< Buffer where to copy output samples. uint maxSamples ///< How many samples to receive at max. ) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return 0; if (outBuffer) { return sth->pst->receiveSamples(outBuffer, maxSamples); } else { return sth->pst->receiveSamples(maxSamples); } } /// Returns number of samples currently available. SOUNDTOUCHDLL_API uint __cdecl soundtouch_numSamples(HANDLE h) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return 0; return sth->pst->numSamples(); } /// Returns nonzero if there aren't any samples available for outputting. SOUNDTOUCHDLL_API int __cdecl soundtouch_isEmpty(HANDLE h) { STHANDLE *sth = (STHANDLE*)h; if (sth->dwMagic != STMAGIC) return -1; return sth->pst->isEmpty(); } soundtouch/source/SoundTouchDLL/SoundTouchDLL.rc0000664000175100017510000000554312577461413021226 0ustar vmdevvmdev// Microsoft Visual C++ generated resource script. // #include "resource.h" #define APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // // Generated from the TEXTINCLUDE 2 resource. // #include "afxres.h" ///////////////////////////////////////////////////////////////////////////// #undef APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // English (U.S.) resources #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) #ifdef _WIN32 LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US #pragma code_page(1252) #endif //_WIN32 #ifdef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // TEXTINCLUDE // 1 TEXTINCLUDE BEGIN "resource.h\0" END 2 TEXTINCLUDE BEGIN "#include ""afxres.h""\r\n" "\0" END 3 TEXTINCLUDE BEGIN "\r\n" "\0" END #endif // APSTUDIO_INVOKED #endif // English (U.S.) resources ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// // Finnish resources #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FIN) #ifdef _WIN32 LANGUAGE LANG_FINNISH, SUBLANG_DEFAULT #pragma code_page(1252) #endif //_WIN32 ///////////////////////////////////////////////////////////////////////////// // // Version // VS_VERSION_INFO VERSIONINFO FILEVERSION 1,9,2,0 PRODUCTVERSION 1,9,2,0 FILEFLAGSMASK 0x17L #ifdef _DEBUG FILEFLAGS 0x1L #else FILEFLAGS 0x0L #endif FILEOS 0x4L FILETYPE 0x2L FILESUBTYPE 0x0L BEGIN BLOCK "StringFileInfo" BEGIN BLOCK "000004b0" BEGIN VALUE "Comments", "SoundTouch Library licensed for 3rd party applications subject to LGPL license v2.1. Visit http://www.surina.net/soundtouch for more information about the SoundTouch library." VALUE "FileDescription", "SoundTouch Dynamic Link Library" VALUE "FileVersion", "1, 9, 2, 0" VALUE "InternalName", "SoundTouch" VALUE "LegalCopyright", "Copyright (C) Olli Parviainen 1999-2015" VALUE "OriginalFilename", "SoundTouch.dll" VALUE "ProductName", " SoundTouch Dynamic Link Library" VALUE "ProductVersion", "1, 9, 2, 0" END END BLOCK "VarFileInfo" BEGIN VALUE "Translation", 0x0, 1200 END END #endif // Finnish resources ///////////////////////////////////////////////////////////////////////////// #ifndef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // Generated from the TEXTINCLUDE 3 resource. // ///////////////////////////////////////////////////////////////////////////// #endif // not APSTUDIO_INVOKED soundtouch/source/Makefile.am0000664000175100017510000000222412577461413015647 0ustar vmdevvmdev## Process this file with automake to create Makefile.in ## ## $Id: Makefile.am 38 2008-12-25 17:00:23Z oparviai $ ## ## Copyright (C) 2003 - David W. Durham ## ## This file is part of SoundTouch, an audio processing library for pitch/time adjustments ## ## SoundTouch 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. ## ## SoundTouch is distributed in the hope that it will be useful, but WITHOUT ANY ## WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR ## A PARTICULAR PURPOSE. See the GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License along with ## this program; if not, write to the Free Software Foundation, Inc., 59 Temple ## Place - Suite 330, Boston, MA 02111-1307, USA include $(top_srcdir)/config/am_include.mk SUBDIRS=SoundTouch SoundStretch # set to something if you want other stuff to be included in the distribution tarball #EXTRA_DIST= soundtouch/source/SoundTouch/0000775000175100017510000000000012577461413015706 5ustar vmdevvmdevsoundtouch/source/SoundTouch/RateTransposer.h0000664000175100017510000001357412577461413021045 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Sample rate transposer. Changes sample rate by using linear interpolation /// together with anti-alias filtering (first order interpolation with anti- /// alias filtering should be quite adequate for this application). /// /// Use either of the derived classes of 'RateTransposerInteger' or /// 'RateTransposerFloat' for corresponding integer/floating point tranposing /// algorithm implementation. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2015-07-26 17:45:48 +0300 (Sun, 26 Jul 2015) $ // File revision : $Revision: 4 $ // // $Id: RateTransposer.h 225 2015-07-26 14:45:48Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef RateTransposer_H #define RateTransposer_H #include #include "AAFilter.h" #include "FIFOSamplePipe.h" #include "FIFOSampleBuffer.h" #include "STTypes.h" namespace soundtouch { /// Abstract base class for transposer implementations (linear, advanced vs integer, float etc) class TransposerBase { public: enum ALGORITHM { LINEAR = 0, CUBIC, SHANNON }; protected: virtual void resetRegisters() = 0; virtual int transposeMono(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) = 0; virtual int transposeStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) = 0; virtual int transposeMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) = 0; static ALGORITHM algorithm; public: double rate; int numChannels; TransposerBase(); virtual ~TransposerBase(); virtual int transpose(FIFOSampleBuffer &dest, FIFOSampleBuffer &src); virtual void setRate(double newRate); virtual void setChannels(int channels); // static factory function static TransposerBase *newInstance(); // static function to set interpolation algorithm static void setAlgorithm(ALGORITHM a); }; /// A common linear samplerate transposer class. /// class RateTransposer : public FIFOProcessor { protected: /// Anti-alias filter object AAFilter *pAAFilter; TransposerBase *pTransposer; /// Buffer for collecting samples to feed the anti-alias filter between /// two batches FIFOSampleBuffer inputBuffer; /// Buffer for keeping samples between transposing & anti-alias filter FIFOSampleBuffer midBuffer; /// Output sample buffer FIFOSampleBuffer outputBuffer; bool bUseAAFilter; /// Transposes sample rate by applying anti-alias filter to prevent folding. /// Returns amount of samples returned in the "dest" buffer. /// The maximum amount of samples that can be returned at a time is set by /// the 'set_returnBuffer_size' function. void processSamples(const SAMPLETYPE *src, uint numSamples); public: RateTransposer(); virtual ~RateTransposer(); /// Operator 'new' is overloaded so that it automatically creates a suitable instance /// depending on if we're to use integer or floating point arithmetics. // static void *operator new(size_t s); /// Use this function instead of "new" operator to create a new instance of this class. /// This function automatically chooses a correct implementation, depending on if /// integer ot floating point arithmetics are to be used. // static RateTransposer *newInstance(); /// Returns the output buffer object FIFOSamplePipe *getOutput() { return &outputBuffer; }; /// Returns the store buffer object // FIFOSamplePipe *getStore() { return &storeBuffer; }; /// Return anti-alias filter object AAFilter *getAAFilter(); /// Enables/disables the anti-alias filter. Zero to disable, nonzero to enable void enableAAFilter(bool newMode); /// Returns nonzero if anti-alias filter is enabled. bool isAAFilterEnabled() const; /// Sets new target rate. Normal rate = 1.0, smaller values represent slower /// rate, larger faster rates. virtual void setRate(double newRate); /// Sets the number of channels, 1 = mono, 2 = stereo void setChannels(int channels); /// Adds 'numSamples' pcs of samples from the 'samples' memory position into /// the input of the object. void putSamples(const SAMPLETYPE *samples, uint numSamples); /// Clears all the samples in the object void clear(); /// Returns nonzero if there aren't any samples available for outputting. int isEmpty() const; }; } #endif soundtouch/source/SoundTouch/PeakFinder.cpp0000664000175100017510000002042712577461413020427 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Peak detection routine. /// /// The routine detects highest value on an array of values and calculates the /// precise peak location as a mass-center of the 'hump' around the peak value. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2015-05-18 18:22:02 +0300 (Mon, 18 May 2015) $ // File revision : $Revision: 4 $ // // $Id: PeakFinder.cpp 213 2015-05-18 15:22:02Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include #include #include "PeakFinder.h" using namespace soundtouch; #define max(x, y) (((x) > (y)) ? (x) : (y)) PeakFinder::PeakFinder() { minPos = maxPos = 0; } // Finds real 'top' of a peak hump from neighnourhood of the given 'peakpos'. int PeakFinder::findTop(const float *data, int peakpos) const { int i; int start, end; float refvalue; refvalue = data[peakpos]; // seek within 10 points start = peakpos - 10; if (start < minPos) start = minPos; end = peakpos + 10; if (end > maxPos) end = maxPos; for (i = start; i <= end; i ++) { if (data[i] > refvalue) { peakpos = i; refvalue = data[i]; } } // failure if max value is at edges of seek range => it's not peak, it's at slope. if ((peakpos == start) || (peakpos == end)) return 0; return peakpos; } // Finds 'ground level' of a peak hump by starting from 'peakpos' and proceeding // to direction defined by 'direction' until next 'hump' after minimum value will // begin int PeakFinder::findGround(const float *data, int peakpos, int direction) const { int lowpos; int pos; int climb_count; float refvalue; float delta; climb_count = 0; refvalue = data[peakpos]; lowpos = peakpos; pos = peakpos; while ((pos > minPos+1) && (pos < maxPos-1)) { int prevpos; prevpos = pos; pos += direction; // calculate derivate delta = data[pos] - data[prevpos]; if (delta <= 0) { // going downhill, ok if (climb_count) { climb_count --; // decrease climb count } // check if new minimum found if (data[pos] < refvalue) { // new minimum found lowpos = pos; refvalue = data[pos]; } } else { // going uphill, increase climbing counter climb_count ++; if (climb_count > 5) break; // we've been climbing too long => it's next uphill => quit } } return lowpos; } // Find offset where the value crosses the given level, when starting from 'peakpos' and // proceeds to direction defined in 'direction' int PeakFinder::findCrossingLevel(const float *data, float level, int peakpos, int direction) const { float peaklevel; int pos; peaklevel = data[peakpos]; assert(peaklevel >= level); pos = peakpos; while ((pos >= minPos) && (pos < maxPos)) { if (data[pos + direction] < level) return pos; // crossing found pos += direction; } return -1; // not found } // Calculates the center of mass location of 'data' array items between 'firstPos' and 'lastPos' double PeakFinder::calcMassCenter(const float *data, int firstPos, int lastPos) const { int i; float sum; float wsum; sum = 0; wsum = 0; for (i = firstPos; i <= lastPos; i ++) { sum += (float)i * data[i]; wsum += data[i]; } if (wsum < 1e-6) return 0; return sum / wsum; } /// get exact center of peak near given position by calculating local mass of center double PeakFinder::getPeakCenter(const float *data, int peakpos) const { float peakLevel; // peak level int crosspos1, crosspos2; // position where the peak 'hump' crosses cutting level float cutLevel; // cutting value float groundLevel; // ground level of the peak int gp1, gp2; // bottom positions of the peak 'hump' // find ground positions. gp1 = findGround(data, peakpos, -1); gp2 = findGround(data, peakpos, 1); peakLevel = data[peakpos]; if (gp1 == gp2) { // avoid rounding errors when all are equal assert(gp1 == peakpos); cutLevel = groundLevel = peakLevel; } else { // get average of the ground levels groundLevel = 0.5f * (data[gp1] + data[gp2]); // calculate 70%-level of the peak cutLevel = 0.70f * peakLevel + 0.30f * groundLevel; } // find mid-level crossings crosspos1 = findCrossingLevel(data, cutLevel, peakpos, -1); crosspos2 = findCrossingLevel(data, cutLevel, peakpos, 1); if ((crosspos1 < 0) || (crosspos2 < 0)) return 0; // no crossing, no peak.. // calculate mass center of the peak surroundings return calcMassCenter(data, crosspos1, crosspos2); } double PeakFinder::detectPeak(const float *data, int aminPos, int amaxPos) { int i; int peakpos; // position of peak level double highPeak, peak; this->minPos = aminPos; this->maxPos = amaxPos; // find absolute peak peakpos = minPos; peak = data[minPos]; for (i = minPos + 1; i < maxPos; i ++) { if (data[i] > peak) { peak = data[i]; peakpos = i; } } // Calculate exact location of the highest peak mass center highPeak = getPeakCenter(data, peakpos); peak = highPeak; // Now check if the highest peak were in fact harmonic of the true base beat peak // - sometimes the highest peak can be Nth harmonic of the true base peak yet // just a slightly higher than the true base for (i = 3; i < 10; i ++) { double peaktmp, harmonic; int i1,i2; harmonic = (double)i * 0.5; peakpos = (int)(highPeak / harmonic + 0.5f); if (peakpos < minPos) break; peakpos = findTop(data, peakpos); // seek true local maximum index if (peakpos == 0) continue; // no local max here // calculate mass-center of possible harmonic peak peaktmp = getPeakCenter(data, peakpos); // accept harmonic peak if // (a) it is found // (b) is within 4% of the expected harmonic interval // (c) has at least half x-corr value of the max. peak double diff = harmonic * peaktmp / highPeak; if ((diff < 0.96) || (diff > 1.04)) continue; // peak too afar from expected // now compare to highest detected peak i1 = (int)(highPeak + 0.5); i2 = (int)(peaktmp + 0.5); if (data[i2] >= 0.4*data[i1]) { // The harmonic is at least half as high primary peak, // thus use the harmonic peak instead peak = peaktmp; } } return peak; } soundtouch/source/SoundTouch/InterpolateLinear.cpp0000664000175100017510000002007112577461413022033 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Linear interpolation algorithm. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // $Id: InterpolateLinear.cpp 225 2015-07-26 14:45:48Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include #include #include "InterpolateLinear.h" using namespace soundtouch; ////////////////////////////////////////////////////////////////////////////// // // InterpolateLinearInteger - integer arithmetic implementation // /// fixed-point interpolation routine precision #define SCALE 65536 // Constructor InterpolateLinearInteger::InterpolateLinearInteger() : TransposerBase() { // Notice: use local function calling syntax for sake of clarity, // to indicate the fact that C++ constructor can't call virtual functions. resetRegisters(); setRate(1.0f); } void InterpolateLinearInteger::resetRegisters() { iFract = 0; } // Transposes the sample rate of the given samples using linear interpolation. // 'Mono' version of the routine. Returns the number of samples returned in // the "dest" buffer int InterpolateLinearInteger::transposeMono(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) { int i; int srcSampleEnd = srcSamples - 1; int srcCount = 0; i = 0; while (srcCount < srcSampleEnd) { LONG_SAMPLETYPE temp; assert(iFract < SCALE); temp = (SCALE - iFract) * src[0] + iFract * src[1]; dest[i] = (SAMPLETYPE)(temp / SCALE); i++; iFract += iRate; int iWhole = iFract / SCALE; iFract -= iWhole * SCALE; srcCount += iWhole; src += iWhole; } srcSamples = srcCount; return i; } // Transposes the sample rate of the given samples using linear interpolation. // 'Stereo' version of the routine. Returns the number of samples returned in // the "dest" buffer int InterpolateLinearInteger::transposeStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) { int i; int srcSampleEnd = srcSamples - 1; int srcCount = 0; i = 0; while (srcCount < srcSampleEnd) { LONG_SAMPLETYPE temp0; LONG_SAMPLETYPE temp1; assert(iFract < SCALE); temp0 = (SCALE - iFract) * src[0] + iFract * src[2]; temp1 = (SCALE - iFract) * src[1] + iFract * src[3]; dest[0] = (SAMPLETYPE)(temp0 / SCALE); dest[1] = (SAMPLETYPE)(temp1 / SCALE); dest += 2; i++; iFract += iRate; int iWhole = iFract / SCALE; iFract -= iWhole * SCALE; srcCount += iWhole; src += 2*iWhole; } srcSamples = srcCount; return i; } int InterpolateLinearInteger::transposeMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) { int i; int srcSampleEnd = srcSamples - 1; int srcCount = 0; i = 0; while (srcCount < srcSampleEnd) { LONG_SAMPLETYPE temp, vol1; assert(iFract < SCALE); vol1 = (SCALE - iFract); for (int c = 0; c < numChannels; c ++) { temp = vol1 * src[c] + iFract * src[c + numChannels]; dest[0] = (SAMPLETYPE)(temp / SCALE); dest ++; } i++; iFract += iRate; int iWhole = iFract / SCALE; iFract -= iWhole * SCALE; srcCount += iWhole; src += iWhole * numChannels; } srcSamples = srcCount; return i; } // Sets new target iRate. Normal iRate = 1.0, smaller values represent slower // iRate, larger faster iRates. void InterpolateLinearInteger::setRate(double newRate) { iRate = (int)(newRate * SCALE + 0.5); TransposerBase::setRate(newRate); } ////////////////////////////////////////////////////////////////////////////// // // InterpolateLinearFloat - floating point arithmetic implementation // ////////////////////////////////////////////////////////////////////////////// // Constructor InterpolateLinearFloat::InterpolateLinearFloat() : TransposerBase() { // Notice: use local function calling syntax for sake of clarity, // to indicate the fact that C++ constructor can't call virtual functions. resetRegisters(); setRate(1.0); } void InterpolateLinearFloat::resetRegisters() { fract = 0; } // Transposes the sample rate of the given samples using linear interpolation. // 'Mono' version of the routine. Returns the number of samples returned in // the "dest" buffer int InterpolateLinearFloat::transposeMono(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) { int i; int srcSampleEnd = srcSamples - 1; int srcCount = 0; i = 0; while (srcCount < srcSampleEnd) { double out; assert(fract < 1.0); out = (1.0 - fract) * src[0] + fract * src[1]; dest[i] = (SAMPLETYPE)out; i ++; // update position fraction fract += rate; // update whole positions int whole = (int)fract; fract -= whole; src += whole; srcCount += whole; } srcSamples = srcCount; return i; } // Transposes the sample rate of the given samples using linear interpolation. // 'Mono' version of the routine. Returns the number of samples returned in // the "dest" buffer int InterpolateLinearFloat::transposeStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) { int i; int srcSampleEnd = srcSamples - 1; int srcCount = 0; i = 0; while (srcCount < srcSampleEnd) { double out0, out1; assert(fract < 1.0); out0 = (1.0 - fract) * src[0] + fract * src[2]; out1 = (1.0 - fract) * src[1] + fract * src[3]; dest[2*i] = (SAMPLETYPE)out0; dest[2*i+1] = (SAMPLETYPE)out1; i ++; // update position fraction fract += rate; // update whole positions int whole = (int)fract; fract -= whole; src += 2*whole; srcCount += whole; } srcSamples = srcCount; return i; } int InterpolateLinearFloat::transposeMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples) { int i; int srcSampleEnd = srcSamples - 1; int srcCount = 0; i = 0; while (srcCount < srcSampleEnd) { float temp, vol1, fract_float; vol1 = (float)(1.0 - fract); fract_float = (float)fract; for (int c = 0; c < numChannels; c ++) { temp = vol1 * src[c] + fract_float * src[c + numChannels]; *dest = (SAMPLETYPE)temp; dest ++; } i++; fract += rate; int iWhole = (int)fract; fract -= iWhole; srcCount += iWhole; src += iWhole * numChannels; } srcSamples = srcCount; return i; } soundtouch/source/SoundTouch/FIFOSampleBuffer.cpp0000664000175100017510000002120412577461413021430 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// A buffer class for temporarily storaging sound samples, operates as a /// first-in-first-out pipe. /// /// Samples are added to the end of the sample buffer with the 'putSamples' /// function, and are received from the beginning of the buffer by calling /// the 'receiveSamples' function. The class automatically removes the /// outputted samples from the buffer, as well as grows the buffer size /// whenever necessary. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2012-11-08 20:53:01 +0200 (Thu, 08 Nov 2012) $ // File revision : $Revision: 4 $ // // $Id: FIFOSampleBuffer.cpp 160 2012-11-08 18:53:01Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include "FIFOSampleBuffer.h" using namespace soundtouch; // Constructor FIFOSampleBuffer::FIFOSampleBuffer(int numChannels) { assert(numChannels > 0); sizeInBytes = 0; // reasonable initial value buffer = NULL; bufferUnaligned = NULL; samplesInBuffer = 0; bufferPos = 0; channels = (uint)numChannels; ensureCapacity(32); // allocate initial capacity } // destructor FIFOSampleBuffer::~FIFOSampleBuffer() { delete[] bufferUnaligned; bufferUnaligned = NULL; buffer = NULL; } // Sets number of channels, 1 = mono, 2 = stereo void FIFOSampleBuffer::setChannels(int numChannels) { uint usedBytes; assert(numChannels > 0); usedBytes = channels * samplesInBuffer; channels = (uint)numChannels; samplesInBuffer = usedBytes / channels; } // if output location pointer 'bufferPos' isn't zero, 'rewinds' the buffer and // zeroes this pointer by copying samples from the 'bufferPos' pointer // location on to the beginning of the buffer. void FIFOSampleBuffer::rewind() { if (buffer && bufferPos) { memmove(buffer, ptrBegin(), sizeof(SAMPLETYPE) * channels * samplesInBuffer); bufferPos = 0; } } // Adds 'numSamples' pcs of samples from the 'samples' memory position to // the sample buffer. void FIFOSampleBuffer::putSamples(const SAMPLETYPE *samples, uint nSamples) { memcpy(ptrEnd(nSamples), samples, sizeof(SAMPLETYPE) * nSamples * channels); samplesInBuffer += nSamples; } // Increases the number of samples in the buffer without copying any actual // samples. // // This function is used to update the number of samples in the sample buffer // when accessing the buffer directly with 'ptrEnd' function. Please be // careful though! void FIFOSampleBuffer::putSamples(uint nSamples) { uint req; req = samplesInBuffer + nSamples; ensureCapacity(req); samplesInBuffer += nSamples; } // Returns a pointer to the end of the used part of the sample buffer (i.e. // where the new samples are to be inserted). This function may be used for // inserting new samples into the sample buffer directly. Please be careful! // // Parameter 'slackCapacity' tells the function how much free capacity (in // terms of samples) there _at least_ should be, in order to the caller to // succesfully insert all the required samples to the buffer. When necessary, // the function grows the buffer size to comply with this requirement. // // When using this function as means for inserting new samples, also remember // to increase the sample count afterwards, by calling the // 'putSamples(numSamples)' function. SAMPLETYPE *FIFOSampleBuffer::ptrEnd(uint slackCapacity) { ensureCapacity(samplesInBuffer + slackCapacity); return buffer + samplesInBuffer * channels; } // Returns a pointer to the beginning of the currently non-outputted samples. // This function is provided for accessing the output samples directly. // Please be careful! // // When using this function to output samples, also remember to 'remove' the // outputted samples from the buffer by calling the // 'receiveSamples(numSamples)' function SAMPLETYPE *FIFOSampleBuffer::ptrBegin() { assert(buffer); return buffer + bufferPos * channels; } // Ensures that the buffer has enought capacity, i.e. space for _at least_ // 'capacityRequirement' number of samples. The buffer is grown in steps of // 4 kilobytes to eliminate the need for frequently growing up the buffer, // as well as to round the buffer size up to the virtual memory page size. void FIFOSampleBuffer::ensureCapacity(uint capacityRequirement) { SAMPLETYPE *tempUnaligned, *temp; if (capacityRequirement > getCapacity()) { // enlarge the buffer in 4kbyte steps (round up to next 4k boundary) sizeInBytes = (capacityRequirement * channels * sizeof(SAMPLETYPE) + 4095) & (uint)-4096; assert(sizeInBytes % 2 == 0); tempUnaligned = new SAMPLETYPE[sizeInBytes / sizeof(SAMPLETYPE) + 16 / sizeof(SAMPLETYPE)]; if (tempUnaligned == NULL) { ST_THROW_RT_ERROR("Couldn't allocate memory!\n"); } // Align the buffer to begin at 16byte cache line boundary for optimal performance temp = (SAMPLETYPE *)SOUNDTOUCH_ALIGN_POINTER_16(tempUnaligned); if (samplesInBuffer) { memcpy(temp, ptrBegin(), samplesInBuffer * channels * sizeof(SAMPLETYPE)); } delete[] bufferUnaligned; buffer = temp; bufferUnaligned = tempUnaligned; bufferPos = 0; } else { // simply rewind the buffer (if necessary) rewind(); } } // Returns the current buffer capacity in terms of samples uint FIFOSampleBuffer::getCapacity() const { return sizeInBytes / (channels * sizeof(SAMPLETYPE)); } // Returns the number of samples currently in the buffer uint FIFOSampleBuffer::numSamples() const { return samplesInBuffer; } // Output samples from beginning of the sample buffer. Copies demanded number // of samples to output and removes them from the sample buffer. If there // are less than 'numsample' samples in the buffer, returns all available. // // Returns number of samples copied. uint FIFOSampleBuffer::receiveSamples(SAMPLETYPE *output, uint maxSamples) { uint num; num = (maxSamples > samplesInBuffer) ? samplesInBuffer : maxSamples; memcpy(output, ptrBegin(), channels * sizeof(SAMPLETYPE) * num); return receiveSamples(num); } // Removes samples from the beginning of the sample buffer without copying them // anywhere. Used to reduce the number of samples in the buffer, when accessing // the sample buffer with the 'ptrBegin' function. uint FIFOSampleBuffer::receiveSamples(uint maxSamples) { if (maxSamples >= samplesInBuffer) { uint temp; temp = samplesInBuffer; samplesInBuffer = 0; return temp; } samplesInBuffer -= maxSamples; bufferPos += maxSamples; return maxSamples; } // Returns nonzero if the sample buffer is empty int FIFOSampleBuffer::isEmpty() const { return (samplesInBuffer == 0) ? 1 : 0; } // Clears the sample buffer 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; } soundtouch/source/SoundTouch/SoundTouch.cpp0000664000175100017510000003773212577461413020521 0ustar vmdevvmdev////////////////////////////////////////////////////////////////////////////// /// /// SoundTouch - main class for tempo/pitch/rate adjusting routines. /// /// Notes: /// - Initialize the SoundTouch object instance by setting up the sound stream /// parameters with functions 'setSampleRate' and 'setChannels', then set /// desired tempo/pitch/rate settings with the corresponding functions. /// /// - The SoundTouch class behaves like a first-in-first-out pipeline: The /// samples that are to be processed are fed into one of the pipe by calling /// function 'putSamples', while the ready processed samples can be read /// from the other end of the pipeline with function 'receiveSamples'. /// /// - The SoundTouch processing classes require certain sized 'batches' of /// samples in order to process the sound. For this reason the classes buffer /// incoming samples until there are enough of samples available for /// processing, then they carry out the processing step and consequently /// make the processed samples available for outputting. /// /// - For the above reason, the processing routines introduce a certain /// 'latency' between the input and output, so that the samples input to /// SoundTouch may not be immediately available in the output, and neither /// the amount of outputtable samples may not immediately be in direct /// relationship with the amount of previously input samples. /// /// - The tempo/pitch/rate control parameters can be altered during processing. /// Please notice though that they aren't currently protected by semaphores, /// so in multi-thread application external semaphore protection may be /// required. /// /// - This class utilizes classes 'TDStretch' for tempo change (without modifying /// pitch) and 'RateTransposer' for changing the playback rate (that is, both /// tempo and pitch in the same ratio) of the sound. The third available control /// 'pitch' (change pitch but maintain tempo) is produced by a combination of /// combining the two other controls. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2015-07-26 17:45:48 +0300 (Sun, 26 Jul 2015) $ // File revision : $Revision: 4 $ // // $Id: SoundTouch.cpp 225 2015-07-26 14:45:48Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include "SoundTouch.h" #include "TDStretch.h" #include "RateTransposer.h" #include "cpu_detect.h" using namespace soundtouch; /// test if two floating point numbers are equal #define TEST_FLOAT_EQUAL(a, b) (fabs(a - b) < 1e-10) /// Print library version string for autoconf extern "C" void soundtouch_ac_test() { printf("SoundTouch Version: %s\n",SOUNDTOUCH_VERSION); } SoundTouch::SoundTouch() { // Initialize rate transposer and tempo changer instances pRateTransposer = new RateTransposer(); pTDStretch = TDStretch::newInstance(); setOutPipe(pTDStretch); rate = tempo = 0; virtualPitch = virtualRate = virtualTempo = 1.0; calcEffectiveRateAndTempo(); samplesExpectedOut = 0; samplesOutput = 0; channels = 0; bSrateSet = false; } SoundTouch::~SoundTouch() { delete pRateTransposer; delete pTDStretch; } /// Get SoundTouch library version string const char *SoundTouch::getVersionString() { static const char *_version = SOUNDTOUCH_VERSION; return _version; } /// Get SoundTouch library version Id uint SoundTouch::getVersionId() { return SOUNDTOUCH_VERSION_ID; } // Sets the number of channels, 1 = mono, 2 = stereo void SoundTouch::setChannels(uint numChannels) { /*if (numChannels != 1 && numChannels != 2) { //ST_THROW_RT_ERROR("Illegal number of channels"); return; }*/ channels = numChannels; pRateTransposer->setChannels((int)numChannels); pTDStretch->setChannels((int)numChannels); } // Sets new rate control value. Normal rate = 1.0, smaller values // represent slower rate, larger faster rates. void SoundTouch::setRate(double newRate) { virtualRate = newRate; calcEffectiveRateAndTempo(); } // Sets new rate control value as a difference in percents compared // to the original rate (-50 .. +100 %) void SoundTouch::setRateChange(double newRate) { virtualRate = 1.0 + 0.01 * newRate; calcEffectiveRateAndTempo(); } // Sets new tempo control value. Normal tempo = 1.0, smaller values // represent slower tempo, larger faster tempo. void SoundTouch::setTempo(double newTempo) { virtualTempo = newTempo; calcEffectiveRateAndTempo(); } // Sets new tempo control value as a difference in percents compared // to the original tempo (-50 .. +100 %) void SoundTouch::setTempoChange(double newTempo) { virtualTempo = 1.0 + 0.01 * newTempo; calcEffectiveRateAndTempo(); } // Sets new pitch control value. Original pitch = 1.0, smaller values // represent lower pitches, larger values higher pitch. void SoundTouch::setPitch(double newPitch) { virtualPitch = newPitch; calcEffectiveRateAndTempo(); } // Sets pitch change in octaves compared to the original pitch // (-1.00 .. +1.00) void SoundTouch::setPitchOctaves(double newPitch) { virtualPitch = exp(0.69314718056 * newPitch); calcEffectiveRateAndTempo(); } // Sets pitch change in semi-tones compared to the original pitch // (-12 .. +12) void SoundTouch::setPitchSemiTones(int newPitch) { setPitchOctaves((double)newPitch / 12.0); } void SoundTouch::setPitchSemiTones(double newPitch) { setPitchOctaves(newPitch / 12.0); } // Calculates 'effective' rate and tempo values from the // nominal control values. void SoundTouch::calcEffectiveRateAndTempo() { double oldTempo = tempo; double oldRate = rate; tempo = virtualTempo / virtualPitch; rate = virtualPitch * virtualRate; if (!TEST_FLOAT_EQUAL(rate,oldRate)) pRateTransposer->setRate(rate); if (!TEST_FLOAT_EQUAL(tempo, oldTempo)) pTDStretch->setTempo(tempo); #ifndef SOUNDTOUCH_PREVENT_CLICK_AT_RATE_CROSSOVER if (rate <= 1.0f) { if (output != pTDStretch) { FIFOSamplePipe *tempoOut; assert(output == pRateTransposer); // move samples in the current output buffer to the output of pTDStretch tempoOut = pTDStretch->getOutput(); tempoOut->moveSamples(*output); // move samples in pitch transposer's store buffer to tempo changer's input // deprecated : pTDStretch->moveSamples(*pRateTransposer->getStore()); output = pTDStretch; } } else #endif { if (output != pRateTransposer) { FIFOSamplePipe *transOut; assert(output == pTDStretch); // move samples in the current output buffer to the output of pRateTransposer transOut = pRateTransposer->getOutput(); transOut->moveSamples(*output); // move samples in tempo changer's input to pitch transposer's input pRateTransposer->moveSamples(*pTDStretch->getInput()); output = pRateTransposer; } } } // Sets sample rate. void SoundTouch::setSampleRate(uint srate) { bSrateSet = true; // set sample rate, leave other tempo changer parameters as they are. pTDStretch->setParameters((int)srate); } // Adds 'numSamples' pcs of samples from the 'samples' memory position into // the input of the object. void SoundTouch::putSamples(const SAMPLETYPE *samples, uint nSamples) { if (bSrateSet == false) { ST_THROW_RT_ERROR("SoundTouch : Sample rate not defined"); } else if (channels == 0) { ST_THROW_RT_ERROR("SoundTouch : Number of channels not defined"); } // Transpose the rate of the new samples if necessary /* Bypass the nominal setting - can introduce a click in sound when tempo/pitch control crosses the nominal value... if (rate == 1.0f) { // The rate value is same as the original, simply evaluate the tempo changer. assert(output == pTDStretch); if (pRateTransposer->isEmpty() == 0) { // yet flush the last samples in the pitch transposer buffer // (may happen if 'rate' changes from a non-zero value to zero) pTDStretch->moveSamples(*pRateTransposer); } pTDStretch->putSamples(samples, nSamples); } */ // accumulate how many samples are expected out from processing, given the current // processing setting samplesExpectedOut += (double)nSamples / ((double)rate * (double)tempo); #ifndef SOUNDTOUCH_PREVENT_CLICK_AT_RATE_CROSSOVER if (rate <= 1.0f) { // transpose the rate down, output the transposed sound to tempo changer buffer assert(output == pTDStretch); pRateTransposer->putSamples(samples, nSamples); pTDStretch->moveSamples(*pRateTransposer); } else #endif { // evaluate the tempo changer, then transpose the rate up, assert(output == pRateTransposer); pTDStretch->putSamples(samples, nSamples); pRateTransposer->moveSamples(*pTDStretch); } } // Flushes the last samples from the processing pipeline to the output. // Clears also the internal processing buffers. // // Note: This function is meant for extracting the last samples of a sound // stream. This function may introduce additional blank samples in the end // of the sound stream, and thus it's not recommended to call this function // in the middle of a sound stream. void SoundTouch::flush() { int i; int numStillExpected; SAMPLETYPE *buff = new SAMPLETYPE[128 * channels]; // how many samples are still expected to output numStillExpected = (int)((long)(samplesExpectedOut + 0.5) - samplesOutput); memset(buff, 0, 128 * 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 // 24ksamples in any case) for (i = 0; (numStillExpected > (int)numSamples()) && (i < 200); i ++) { putSamples(buff, 128); } adjustAmountOfSamples(numStillExpected); delete[] buff; // Clear input buffers // pRateTransposer->clearInput(); pTDStretch->clearInput(); // yet leave the output intouched as that's where the // flushed samples are! } // Changes a setting controlling the processing system behaviour. See the // 'SETTING_...' defines for available setting ID's. bool SoundTouch::setSetting(int settingId, int value) { int sampleRate, sequenceMs, seekWindowMs, overlapMs; // read current tdstretch routine parameters pTDStretch->getParameters(&sampleRate, &sequenceMs, &seekWindowMs, &overlapMs); switch (settingId) { case SETTING_USE_AA_FILTER : // enables / disabless anti-alias filter pRateTransposer->enableAAFilter((value != 0) ? true : false); return true; case SETTING_AA_FILTER_LENGTH : // sets anti-alias filter length pRateTransposer->getAAFilter()->setLength(value); return true; case SETTING_USE_QUICKSEEK : // enables / disables tempo routine quick seeking algorithm pTDStretch->enableQuickSeek((value != 0) ? true : false); return true; case SETTING_SEQUENCE_MS: // change time-stretch sequence duration parameter pTDStretch->setParameters(sampleRate, value, seekWindowMs, overlapMs); return true; case SETTING_SEEKWINDOW_MS: // change time-stretch seek window length parameter pTDStretch->setParameters(sampleRate, sequenceMs, value, overlapMs); return true; case SETTING_OVERLAP_MS: // change time-stretch overlap length parameter pTDStretch->setParameters(sampleRate, sequenceMs, seekWindowMs, value); return true; default : return false; } } // Reads a setting controlling the processing system behaviour. See the // 'SETTING_...' defines for available setting ID's. // // Returns the setting value. int SoundTouch::getSetting(int settingId) const { int temp; switch (settingId) { case SETTING_USE_AA_FILTER : return (uint)pRateTransposer->isAAFilterEnabled(); case SETTING_AA_FILTER_LENGTH : return pRateTransposer->getAAFilter()->getLength(); case SETTING_USE_QUICKSEEK : return (uint) pTDStretch->isQuickSeekEnabled(); case SETTING_SEQUENCE_MS: pTDStretch->getParameters(NULL, &temp, NULL, NULL); return temp; case SETTING_SEEKWINDOW_MS: pTDStretch->getParameters(NULL, NULL, &temp, NULL); return temp; case SETTING_OVERLAP_MS: pTDStretch->getParameters(NULL, NULL, NULL, &temp); return temp; case SETTING_NOMINAL_INPUT_SEQUENCE : return pTDStretch->getInputSampleReq(); case SETTING_NOMINAL_OUTPUT_SEQUENCE : return pTDStretch->getOutputBatchSize(); default : return 0; } } // Clears all the samples in the object's output and internal processing // buffers. void SoundTouch::clear() { samplesExpectedOut = 0; pRateTransposer->clear(); pTDStretch->clear(); } /// Returns number of samples currently unprocessed. uint SoundTouch::numUnprocessedSamples() const { FIFOSamplePipe * psp; if (pTDStretch) { psp = pTDStretch->getInput(); if (psp) { return psp->numSamples(); } } return 0; } /// Output samples from beginning of the sample buffer. Copies requested samples to /// output buffer and removes them from the sample buffer. If there are less than /// 'numsample' samples in the buffer, returns all that available. /// /// \return Number of samples returned. uint SoundTouch::receiveSamples(SAMPLETYPE *output, uint maxSamples) { uint ret = FIFOProcessor::receiveSamples(output, maxSamples); samplesOutput += (long)ret; return ret; } /// Adjusts book-keeping so that given number of samples are removed from beginning of the /// sample buffer without copying them anywhere. /// /// Used to reduce the number of samples in the buffer when accessing the sample buffer directly /// with 'ptrBegin' function. uint SoundTouch::receiveSamples(uint maxSamples) { uint ret = FIFOProcessor::receiveSamples(maxSamples); samplesOutput += (long)ret; return ret; } soundtouch/source/SoundTouch/InterpolateShannon.h0000664000175100017510000000462612577461413021702 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Sample interpolation routine using 8-tap band-limited Shannon interpolation /// with kaiser window. /// /// Notice. This algorithm is remarkably much heavier than linear or cubic /// interpolation, and not remarkably better than cubic algorithm. Thus mostly /// for experimental purposes /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // $Id: InterpolateShannon.h 225 2015-07-26 14:45:48Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef _InterpolateShannon_H_ #define _InterpolateShannon_H_ #include "RateTransposer.h" #include "STTypes.h" namespace soundtouch { class InterpolateShannon : public TransposerBase { protected: void resetRegisters(); int transposeMono(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples); int transposeStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples); int transposeMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples); double fract; public: InterpolateShannon(); }; } #endif soundtouch/source/SoundTouch/TDStretch.cpp0000664000175100017510000010414012577461413020256 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Sampled sound tempo changer/time stretch algorithm. Changes the sound tempo /// while maintaining the original pitch by using a time domain WSOLA-like /// method with several performance-increasing tweaks. /// /// Note : MMX optimized functions reside in a separate, platform-specific /// file, e.g. 'mmx_win.cpp' or 'mmx_gcc.cpp' /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2015-08-09 00:00:15 +0300 (Sun, 09 Aug 2015) $ // File revision : $Revision: 1.12 $ // // $Id: TDStretch.cpp 226 2015-08-08 21:00:15Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include "STTypes.h" #include "cpu_detect.h" #include "TDStretch.h" using namespace soundtouch; #define max(x, y) (((x) > (y)) ? (x) : (y)) /***************************************************************************** * * Constant definitions * *****************************************************************************/ // Table for the hierarchical mixing position seeking algorithm const short _scanOffsets[5][24]={ { 124, 186, 248, 310, 372, 434, 496, 558, 620, 682, 744, 806, 868, 930, 992, 1054, 1116, 1178, 1240, 1302, 1364, 1426, 1488, 0}, {-100, -75, -50, -25, 25, 50, 75, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { -20, -15, -10, -5, 5, 10, 15, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { -4, -3, -2, -1, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 121, 114, 97, 114, 98, 105, 108, 32, 104, 99, 117, 111, 116, 100, 110, 117, 111, 115, 0, 0, 0, 0, 0, 0}}; /***************************************************************************** * * Implementation of the class 'TDStretch' * *****************************************************************************/ TDStretch::TDStretch() : FIFOProcessor(&outputBuffer) { bQuickSeek = false; channels = 2; pMidBuffer = NULL; pMidBufferUnaligned = NULL; overlapLength = 0; bAutoSeqSetting = true; bAutoSeekSetting = true; maxnorm = 0; maxnormf = 1e8; skipFract = 0; tempo = 1.0f; setParameters(44100, DEFAULT_SEQUENCE_MS, DEFAULT_SEEKWINDOW_MS, DEFAULT_OVERLAP_MS); setTempo(1.0f); clear(); } TDStretch::~TDStretch() { delete[] pMidBufferUnaligned; } // Sets routine control parameters. These control are certain time constants // defining how the sound is stretched to the desired duration. // // 'sampleRate' = sample rate of the sound // 'sequenceMS' = one processing sequence length in milliseconds (default = 82 ms) // 'seekwindowMS' = seeking window length for scanning the best overlapping // position (default = 28 ms) // 'overlapMS' = overlapping length (default = 12 ms) void TDStretch::setParameters(int aSampleRate, int aSequenceMS, int aSeekWindowMS, int aOverlapMS) { // accept only positive parameter values - if zero or negative, use old values instead if (aSampleRate > 0) this->sampleRate = aSampleRate; if (aOverlapMS > 0) this->overlapMs = aOverlapMS; if (aSequenceMS > 0) { this->sequenceMs = aSequenceMS; bAutoSeqSetting = false; } else if (aSequenceMS == 0) { // if zero, use automatic setting bAutoSeqSetting = true; } if (aSeekWindowMS > 0) { this->seekWindowMs = aSeekWindowMS; bAutoSeekSetting = false; } else if (aSeekWindowMS == 0) { // if zero, use automatic setting bAutoSeekSetting = true; } calcSeqParameters(); calculateOverlapLength(overlapMs); // set tempo to recalculate 'sampleReq' setTempo(tempo); } /// Get routine control parameters, see setParameters() function. /// Any of the parameters to this function can be NULL, in such case corresponding parameter /// value isn't returned. void TDStretch::getParameters(int *pSampleRate, int *pSequenceMs, int *pSeekWindowMs, int *pOverlapMs) const { if (pSampleRate) { *pSampleRate = sampleRate; } if (pSequenceMs) { *pSequenceMs = (bAutoSeqSetting) ? (USE_AUTO_SEQUENCE_LEN) : sequenceMs; } if (pSeekWindowMs) { *pSeekWindowMs = (bAutoSeekSetting) ? (USE_AUTO_SEEKWINDOW_LEN) : seekWindowMs; } if (pOverlapMs) { *pOverlapMs = overlapMs; } } // Overlaps samples in 'midBuffer' with the samples in 'pInput' void TDStretch::overlapMono(SAMPLETYPE *pOutput, const SAMPLETYPE *pInput) const { int i; SAMPLETYPE m1, m2; m1 = (SAMPLETYPE)0; m2 = (SAMPLETYPE)overlapLength; for (i = 0; i < overlapLength ; i ++) { pOutput[i] = (pInput[i] * m1 + pMidBuffer[i] * m2 ) / overlapLength; m1 += 1; m2 -= 1; } } void TDStretch::clearMidBuffer() { memset(pMidBuffer, 0, channels * sizeof(SAMPLETYPE) * overlapLength); } void TDStretch::clearInput() { inputBuffer.clear(); clearMidBuffer(); } // Clears the sample buffers void TDStretch::clear() { outputBuffer.clear(); clearInput(); } // Enables/disables the quick position seeking algorithm. Zero to disable, nonzero // to enable void TDStretch::enableQuickSeek(bool enable) { bQuickSeek = enable; } // Returns nonzero if the quick seeking algorithm is enabled. bool TDStretch::isQuickSeekEnabled() const { return bQuickSeek; } // Seeks for the optimal overlap-mixing position. int TDStretch::seekBestOverlapPosition(const SAMPLETYPE *refPos) { if (bQuickSeek) { return seekBestOverlapPositionQuick(refPos); } else { return seekBestOverlapPositionFull(refPos); } } // Overlaps samples in 'midBuffer' with the samples in 'pInputBuffer' at position // of 'ovlPos'. inline void TDStretch::overlap(SAMPLETYPE *pOutput, const SAMPLETYPE *pInput, uint ovlPos) const { #ifndef USE_MULTICH_ALWAYS if (channels == 1) { // mono sound. overlapMono(pOutput, pInput + ovlPos); } else if (channels == 2) { // stereo sound overlapStereo(pOutput, pInput + 2 * ovlPos); } else #endif // USE_MULTICH_ALWAYS { assert(channels > 0); overlapMulti(pOutput, pInput + channels * ovlPos); } } // Seeks for the optimal overlap-mixing position. The 'stereo' version of the // routine // // The best position is determined as the position where the two overlapped // sample sequences are 'most alike', in terms of the highest cross-correlation // value over the overlapping period int TDStretch::seekBestOverlapPositionFull(const SAMPLETYPE *refPos) { int bestOffs; double bestCorr; int i; double norm; bestCorr = FLT_MIN; bestOffs = 0; // Scans for the best correlation value by testing each possible position // over the permitted range. bestCorr = calcCrossCorr(refPos, pMidBuffer, norm); #pragma omp parallel for for (i = 1; i < seekLength; i ++) { double corr; // Calculates correlation value for the mixing position corresponding to 'i' #ifdef _OPENMP // in parallel OpenMP mode, can't use norm accumulator version as parallel executor won't // iterate the loop in sequential order corr = calcCrossCorr(refPos + channels * i, pMidBuffer, norm); #else // In non-parallel version call "calcCrossCorrAccumulate" that is otherwise same // as "calcCrossCorr", but saves time by reusing & updating previously stored // "norm" value corr = calcCrossCorrAccumulate(refPos + channels * i, pMidBuffer, norm); #endif // heuristic rule to slightly favour values close to mid of the range double tmp = (double)(2 * i - seekLength) / (double)seekLength; corr = ((corr + 0.1) * (1.0 - 0.25 * tmp * tmp)); // Checks for the highest correlation value if (corr > bestCorr) { // For optimal performance, enter critical section only in case that best value found. // in such case repeat 'if' condition as it's possible that parallel execution may have // updated the bestCorr value in the mean time #pragma omp critical if (corr > bestCorr) { bestCorr = corr; bestOffs = i; } } } #ifdef SOUNDTOUCH_INTEGER_SAMPLES adaptNormalizer(); #endif // clear cross correlation routine state if necessary (is so e.g. in MMX routines). clearCrossCorrState(); return bestOffs; } // Quick seek algorithm for improved runtime-performance: First roughly scans through the // correlation area, and then scan surroundings of two best preliminary correlation candidates // with improved precision // // Based on testing: // - This algorithm gives on average 99% as good match as the full algorith // - this quick seek algorithm finds the best match on ~90% of cases // - on those 10% of cases when this algorithm doesn't find best match, // it still finds on average ~90% match vs. the best possible match int TDStretch::seekBestOverlapPositionQuick(const SAMPLETYPE *refPos) { #define _MIN(a, b) (((a) < (b)) ? (a) : (b)) #define SCANSTEP 16 #define SCANWIND 8 int bestOffs; int i; int bestOffs2; float bestCorr, corr; float bestCorr2; double norm; // note: 'float' types used in this function in case that the platform would need to use software-fp bestCorr = FLT_MIN; bestOffs = SCANWIND; bestCorr2 = FLT_MIN; bestOffs2 = 0; int best = 0; // Scans for the best correlation value by testing each possible position // over the permitted range. Look for two best matches on the first pass to // increase possibility of ideal match. // // Begin from "SCANSTEP" instead of SCANWIND to make the calculation // catch the 'middlepoint' of seekLength vector as that's the a-priori // expected best match position // // Roughly: // - 15% of cases find best result directly on the first round, // - 75% cases find better match on 2nd round around the best match from 1st round // - 10% cases find better match on 2nd round around the 2nd-best-match from 1st round for (i = SCANSTEP; i < seekLength - SCANWIND - 1; i += SCANSTEP) { // Calculates correlation value for the mixing position corresponding // to 'i' corr = (float)calcCrossCorr(refPos + channels*i, pMidBuffer, norm); // heuristic rule to slightly favour values close to mid of the seek range float tmp = (float)(2 * i - seekLength - 1) / (float)seekLength; corr = ((corr + 0.1f) * (1.0f - 0.25f * tmp * tmp)); // Checks for the highest correlation value if (corr > bestCorr) { // found new best match. keep the previous best as 2nd best match bestCorr2 = bestCorr; bestOffs2 = bestOffs; bestCorr = corr; bestOffs = i; } else if (corr > bestCorr2) { // not new best, but still new 2nd best match bestCorr2 = corr; bestOffs2 = i; } } // Scans surroundings of the found best match with small stepping int end = _MIN(bestOffs + SCANWIND + 1, seekLength); for (i = bestOffs - SCANWIND; i < end; i++) { if (i == bestOffs) continue; // this offset already calculated, thus skip // Calculates correlation value for the mixing position corresponding // to 'i' corr = (float)calcCrossCorr(refPos + channels*i, pMidBuffer, norm); // heuristic rule to slightly favour values close to mid of the range float tmp = (float)(2 * i - seekLength - 1) / (float)seekLength; corr = ((corr + 0.1f) * (1.0f - 0.25f * tmp * tmp)); // Checks for the highest correlation value if (corr > bestCorr) { bestCorr = corr; bestOffs = i; best = 1; } } // Scans surroundings of the 2nd best match with small stepping end = _MIN(bestOffs2 + SCANWIND + 1, seekLength); for (i = bestOffs2 - SCANWIND; i < end; i++) { if (i == bestOffs2) continue; // this offset already calculated, thus skip // Calculates correlation value for the mixing position corresponding // to 'i' corr = (float)calcCrossCorr(refPos + channels*i, pMidBuffer, norm); // heuristic rule to slightly favour values close to mid of the range float tmp = (float)(2 * i - seekLength - 1) / (float)seekLength; corr = ((corr + 0.1f) * (1.0f - 0.25f * tmp * tmp)); // Checks for the highest correlation value if (corr > bestCorr) { bestCorr = corr; bestOffs = i; best = 2; } } // clear cross correlation routine state if necessary (is so e.g. in MMX routines). clearCrossCorrState(); #ifdef SOUNDTOUCH_INTEGER_SAMPLES adaptNormalizer(); #endif return bestOffs; } /// For integer algorithm: adapt normalization factor divider with music so that /// it'll not be pessimistically restrictive that can degrade quality on quieter sections /// yet won't cause integer overflows either void TDStretch::adaptNormalizer() { // Do not adapt normalizer over too silent sequences to avoid averaging filter depleting to // too low values during pauses in music if ((maxnorm > 1000) || (maxnormf > 40000000)) { //norm averaging filter maxnormf = 0.9f * maxnormf + 0.1f * (float)maxnorm; if ((maxnorm > 800000000) && (overlapDividerBitsNorm < 16)) { // large values, so increase divider overlapDividerBitsNorm++; if (maxnorm > 1600000000) overlapDividerBitsNorm++; // extra large value => extra increase } else if ((maxnormf < 1000000) && (overlapDividerBitsNorm > 0)) { // extra small values, decrease divider overlapDividerBitsNorm--; } } maxnorm = 0; } /// clear cross correlation routine state if necessary void TDStretch::clearCrossCorrState() { // default implementation is empty. } /// Calculates processing sequence length according to tempo setting void TDStretch::calcSeqParameters() { // Adjust tempo param according to tempo, so that variating processing sequence length is used // at varius tempo settings, between the given low...top limits #define AUTOSEQ_TEMPO_LOW 0.5 // auto setting low tempo range (-50%) #define AUTOSEQ_TEMPO_TOP 2.0 // auto setting top tempo range (+100%) // sequence-ms setting values at above low & top tempo #define AUTOSEQ_AT_MIN 125.0 #define AUTOSEQ_AT_MAX 50.0 #define AUTOSEQ_K ((AUTOSEQ_AT_MAX - AUTOSEQ_AT_MIN) / (AUTOSEQ_TEMPO_TOP - AUTOSEQ_TEMPO_LOW)) #define AUTOSEQ_C (AUTOSEQ_AT_MIN - (AUTOSEQ_K) * (AUTOSEQ_TEMPO_LOW)) // seek-window-ms setting values at above low & top tempoq #define AUTOSEEK_AT_MIN 25.0 #define AUTOSEEK_AT_MAX 15.0 #define AUTOSEEK_K ((AUTOSEEK_AT_MAX - AUTOSEEK_AT_MIN) / (AUTOSEQ_TEMPO_TOP - AUTOSEQ_TEMPO_LOW)) #define AUTOSEEK_C (AUTOSEEK_AT_MIN - (AUTOSEEK_K) * (AUTOSEQ_TEMPO_LOW)) #define CHECK_LIMITS(x, mi, ma) (((x) < (mi)) ? (mi) : (((x) > (ma)) ? (ma) : (x))) double seq, seek; if (bAutoSeqSetting) { seq = AUTOSEQ_C + AUTOSEQ_K * tempo; seq = CHECK_LIMITS(seq, AUTOSEQ_AT_MAX, AUTOSEQ_AT_MIN); sequenceMs = (int)(seq + 0.5); } if (bAutoSeekSetting) { seek = AUTOSEEK_C + AUTOSEEK_K * tempo; seek = CHECK_LIMITS(seek, AUTOSEEK_AT_MAX, AUTOSEEK_AT_MIN); seekWindowMs = (int)(seek + 0.5); } // Update seek window lengths seekWindowLength = (sampleRate * sequenceMs) / 1000; if (seekWindowLength < 2 * overlapLength) { seekWindowLength = 2 * overlapLength; } seekLength = (sampleRate * seekWindowMs) / 1000; } // Sets new target tempo. Normal tempo = 'SCALE', smaller values represent slower // tempo, larger faster tempo. void TDStretch::setTempo(double newTempo) { int intskip; tempo = newTempo; // Calculate new sequence duration calcSeqParameters(); // Calculate ideal skip length (according to tempo value) nominalSkip = tempo * (seekWindowLength - overlapLength); intskip = (int)(nominalSkip + 0.5); // Calculate how many samples are needed in the 'inputBuffer' to // process another batch of samples //sampleReq = max(intskip + overlapLength, seekWindowLength) + seekLength / 2; sampleReq = max(intskip + overlapLength, seekWindowLength) + seekLength; } // Sets the number of channels, 1 = mono, 2 = stereo void TDStretch::setChannels(int numChannels) { assert(numChannels > 0); if (channels == numChannels) return; // assert(numChannels == 1 || numChannels == 2); channels = numChannels; inputBuffer.setChannels(channels); outputBuffer.setChannels(channels); // re-init overlap/buffer overlapLength=0; setParameters(sampleRate); } // nominal tempo, no need for processing, just pass the samples through // to outputBuffer /* void TDStretch::processNominalTempo() { assert(tempo == 1.0f); if (bMidBufferDirty) { // If there are samples in pMidBuffer waiting for overlapping, // do a single sliding overlapping with them in order to prevent a // clicking distortion in the output sound if (inputBuffer.numSamples() < overlapLength) { // wait until we've got overlapLength input samples return; } // Mix the samples in the beginning of 'inputBuffer' with the // samples in 'midBuffer' using sliding overlapping overlap(outputBuffer.ptrEnd(overlapLength), inputBuffer.ptrBegin(), 0); outputBuffer.putSamples(overlapLength); inputBuffer.receiveSamples(overlapLength); clearMidBuffer(); // now we've caught the nominal sample flow and may switch to // bypass mode } // Simply bypass samples from input to output outputBuffer.moveSamples(inputBuffer); } */ // Processes as many processing frames of the samples 'inputBuffer', store // the result into 'outputBuffer' void TDStretch::processSamples() { int ovlSkip, offset; int temp; /* Removed this small optimization - can introduce a click to sound when tempo setting crosses the nominal value if (tempo == 1.0f) { // tempo not changed from the original, so bypass the processing processNominalTempo(); return; } */ // Process samples as long as there are enough samples in 'inputBuffer' // to form a processing frame. while ((int)inputBuffer.numSamples() >= sampleReq) { // If tempo differs from the normal ('SCALE'), scan for the best overlapping // position offset = seekBestOverlapPosition(inputBuffer.ptrBegin()); // Mix the samples in the 'inputBuffer' at position of 'offset' with the // samples in 'midBuffer' using sliding overlapping // ... first partially overlap with the end of the previous sequence // (that's in 'midBuffer') overlap(outputBuffer.ptrEnd((uint)overlapLength), inputBuffer.ptrBegin(), (uint)offset); outputBuffer.putSamples((uint)overlapLength); // ... then copy sequence samples from 'inputBuffer' to output: // length of sequence temp = (seekWindowLength - 2 * overlapLength); // crosscheck that we don't have buffer overflow... if ((int)inputBuffer.numSamples() < (offset + temp + overlapLength * 2)) { continue; // just in case, shouldn't really happen } outputBuffer.putSamples(inputBuffer.ptrBegin() + channels * (offset + overlapLength), (uint)temp); // Copies the end of the current sequence from 'inputBuffer' to // 'midBuffer' for being mixed with the beginning of the next // processing sequence and so on assert((offset + temp + overlapLength * 2) <= (int)inputBuffer.numSamples()); memcpy(pMidBuffer, inputBuffer.ptrBegin() + channels * (offset + temp + overlapLength), channels * sizeof(SAMPLETYPE) * overlapLength); // Remove the processed samples from the input buffer. Update // the difference between integer & nominal skip step to 'skipFract' // in order to prevent the error from accumulating over time. skipFract += nominalSkip; // real skip size ovlSkip = (int)skipFract; // rounded to integer skip skipFract -= ovlSkip; // maintain the fraction part, i.e. real vs. integer skip inputBuffer.receiveSamples((uint)ovlSkip); } } // Adds 'numsamples' pcs of samples from the 'samples' memory position into // the input of the object. void TDStretch::putSamples(const SAMPLETYPE *samples, uint nSamples) { // Add the samples into the input buffer inputBuffer.putSamples(samples, nSamples); // Process the samples in input buffer processSamples(); } /// Set new overlap length parameter & reallocate RefMidBuffer if necessary. void TDStretch::acceptNewOverlapLength(int newOverlapLength) { int prevOvl; assert(newOverlapLength >= 0); prevOvl = overlapLength; overlapLength = newOverlapLength; if (overlapLength > prevOvl) { delete[] pMidBufferUnaligned; pMidBufferUnaligned = new SAMPLETYPE[overlapLength * channels + 16 / sizeof(SAMPLETYPE)]; // ensure that 'pMidBuffer' is aligned to 16 byte boundary for efficiency pMidBuffer = (SAMPLETYPE *)SOUNDTOUCH_ALIGN_POINTER_16(pMidBufferUnaligned); clearMidBuffer(); } } // Operator 'new' is overloaded so that it automatically creates a suitable instance // depending on if we've a MMX/SSE/etc-capable CPU available or not. void * TDStretch::operator new(size_t s) { // Notice! don't use "new TDStretch" directly, use "newInstance" to create a new instance instead! ST_THROW_RT_ERROR("Error in TDStretch::new: Don't use 'new TDStretch' directly, use 'newInstance' member instead!"); return newInstance(); } TDStretch * TDStretch::newInstance() { uint uExtensions; uExtensions = detectCPUextensions(); // Check if MMX/SSE instruction set extensions supported by CPU #ifdef SOUNDTOUCH_ALLOW_MMX // MMX routines available only with integer sample types if (uExtensions & SUPPORT_MMX) { return ::new TDStretchMMX; } else #endif // SOUNDTOUCH_ALLOW_MMX #ifdef SOUNDTOUCH_ALLOW_SSE if (uExtensions & SUPPORT_SSE) { // SSE support return ::new TDStretchSSE; } else #endif // SOUNDTOUCH_ALLOW_SSE { // ISA optimizations not supported, use plain C version return ::new TDStretch; } } ////////////////////////////////////////////////////////////////////////////// // // Integer arithmetics specific algorithm implementations. // ////////////////////////////////////////////////////////////////////////////// #ifdef SOUNDTOUCH_INTEGER_SAMPLES // Overlaps samples in 'midBuffer' with the samples in 'input'. The 'Stereo' // version of the routine. void TDStretch::overlapStereo(short *poutput, const short *input) const { int i; short temp; int cnt2; for (i = 0; i < overlapLength ; i ++) { temp = (short)(overlapLength - i); cnt2 = 2 * i; poutput[cnt2] = (input[cnt2] * i + pMidBuffer[cnt2] * temp ) / overlapLength; poutput[cnt2 + 1] = (input[cnt2 + 1] * i + pMidBuffer[cnt2 + 1] * temp ) / overlapLength; } } // Overlaps samples in 'midBuffer' with the samples in 'input'. The 'Multi' // version of the routine. void TDStretch::overlapMulti(SAMPLETYPE *poutput, const SAMPLETYPE *input) const { SAMPLETYPE m1=(SAMPLETYPE)0; SAMPLETYPE m2; int i=0; for (m2 = (SAMPLETYPE)overlapLength; m2; m2 --) { for (int c = 0; c < channels; c ++) { poutput[i] = (input[i] * m1 + pMidBuffer[i] * m2) / overlapLength; i++; } m1++; } } // Calculates the x having the closest 2^x value for the given value static int _getClosest2Power(double value) { return (int)(log(value) / log(2.0) + 0.5); } /// Calculates overlap period length in samples. /// Integer version rounds overlap length to closest power of 2 /// for a divide scaling operation. void TDStretch::calculateOverlapLength(int aoverlapMs) { int newOvl; assert(aoverlapMs >= 0); // calculate overlap length so that it's power of 2 - thus it's easy to do // integer division by right-shifting. Term "-1" at end is to account for // the extra most significatnt bit left unused in result by signed multiplication overlapDividerBitsPure = _getClosest2Power((sampleRate * aoverlapMs) / 1000.0) - 1; if (overlapDividerBitsPure > 9) overlapDividerBitsPure = 9; if (overlapDividerBitsPure < 3) overlapDividerBitsPure = 3; newOvl = (int)pow(2.0, (int)overlapDividerBitsPure + 1); // +1 => account for -1 above acceptNewOverlapLength(newOvl); overlapDividerBitsNorm = overlapDividerBitsPure; // calculate sloping divider so that crosscorrelation operation won't // overflow 32-bit register. Max. sum of the crosscorrelation sum without // divider would be 2^30*(N^3-N)/3, where N = overlap length slopingDivider = (newOvl * newOvl - 1) / 3; } double TDStretch::calcCrossCorr(const short *mixingPos, const short *compare, double &norm) { long corr; unsigned long lnorm; int i; corr = lnorm = 0; // Same routine for stereo and mono. For stereo, unroll loop for better // efficiency and gives slightly better resolution against rounding. // For mono it same routine, just unrolls loop by factor of 4 for (i = 0; i < channels * overlapLength; i += 4) { corr += (mixingPos[i] * compare[i] + mixingPos[i + 1] * compare[i + 1]) >> overlapDividerBitsNorm; // notice: do intermediate division here to avoid integer overflow corr += (mixingPos[i + 2] * compare[i + 2] + mixingPos[i + 3] * compare[i + 3]) >> overlapDividerBitsNorm; lnorm += (mixingPos[i] * mixingPos[i] + mixingPos[i + 1] * mixingPos[i + 1]) >> overlapDividerBitsNorm; // notice: do intermediate division here to avoid integer overflow lnorm += (mixingPos[i + 2] * mixingPos[i + 2] + mixingPos[i + 3] * mixingPos[i + 3]) >> overlapDividerBitsNorm; } if (lnorm > maxnorm) { maxnorm = lnorm; } // Normalize result by dividing by sqrt(norm) - this step is easiest // done using floating point operation norm = (double)lnorm; return (double)corr / sqrt((norm < 1e-9) ? 1.0 : norm); } /// Update cross-correlation by accumulating "norm" coefficient by previously calculated value double TDStretch::calcCrossCorrAccumulate(const short *mixingPos, const short *compare, double &norm) { long corr; unsigned long lnorm; int i; // cancel first normalizer tap from previous round lnorm = 0; for (i = 1; i <= channels; i ++) { lnorm -= (mixingPos[-i] * mixingPos[-i]) >> overlapDividerBitsNorm; } corr = 0; // Same routine for stereo and mono. For stereo, unroll loop for better // efficiency and gives slightly better resolution against rounding. // For mono it same routine, just unrolls loop by factor of 4 for (i = 0; i < channels * overlapLength; i += 4) { corr += (mixingPos[i] * compare[i] + mixingPos[i + 1] * compare[i + 1]) >> overlapDividerBitsNorm; // notice: do intermediate division here to avoid integer overflow corr += (mixingPos[i + 2] * compare[i + 2] + mixingPos[i + 3] * compare[i + 3]) >> overlapDividerBitsNorm; } // update normalizer with last samples of this round for (int j = 0; j < channels; j ++) { i --; lnorm += (mixingPos[i] * mixingPos[i]) >> overlapDividerBitsNorm; } norm += (double)lnorm; if (norm > maxnorm) { maxnorm = (unsigned long)norm; } // Normalize result by dividing by sqrt(norm) - this step is easiest // done using floating point operation return (double)corr / sqrt((norm < 1e-9) ? 1.0 : norm); } #endif // SOUNDTOUCH_INTEGER_SAMPLES ////////////////////////////////////////////////////////////////////////////// // // Floating point arithmetics specific algorithm implementations. // #ifdef SOUNDTOUCH_FLOAT_SAMPLES // Overlaps samples in 'midBuffer' with the samples in 'pInput' void TDStretch::overlapStereo(float *pOutput, const float *pInput) const { int i; float fScale; float f1; float f2; fScale = 1.0f / (float)overlapLength; f1 = 0; f2 = 1.0f; for (i = 0; i < 2 * (int)overlapLength ; i += 2) { pOutput[i + 0] = pInput[i + 0] * f1 + pMidBuffer[i + 0] * f2; pOutput[i + 1] = pInput[i + 1] * f1 + pMidBuffer[i + 1] * f2; f1 += fScale; f2 -= fScale; } } // Overlaps samples in 'midBuffer' with the samples in 'input'. void TDStretch::overlapMulti(float *pOutput, const float *pInput) const { int i; float fScale; float f1; float f2; fScale = 1.0f / (float)overlapLength; f1 = 0; f2 = 1.0f; i=0; for (int i2 = 0; i2 < overlapLength; i2 ++) { // note: Could optimize this slightly by taking into account that always channels > 2 for (int c = 0; c < channels; c ++) { pOutput[i] = pInput[i] * f1 + pMidBuffer[i] * f2; i++; } f1 += fScale; f2 -= fScale; } } /// Calculates overlapInMsec period length in samples. void TDStretch::calculateOverlapLength(int overlapInMsec) { int newOvl; assert(overlapInMsec >= 0); newOvl = (sampleRate * overlapInMsec) / 1000; if (newOvl < 16) newOvl = 16; // must be divisible by 8 newOvl -= newOvl % 8; acceptNewOverlapLength(newOvl); } /// Calculate cross-correlation double TDStretch::calcCrossCorr(const float *mixingPos, const float *compare, double &anorm) { double corr; double norm; int i; corr = norm = 0; // Same routine for stereo and mono. For Stereo, unroll by factor of 2. // For mono it's same routine yet unrollsd by factor of 4. for (i = 0; i < channels * overlapLength; i += 4) { corr += mixingPos[i] * compare[i] + mixingPos[i + 1] * compare[i + 1]; norm += mixingPos[i] * mixingPos[i] + mixingPos[i + 1] * mixingPos[i + 1]; // unroll the loop for better CPU efficiency: corr += mixingPos[i + 2] * compare[i + 2] + mixingPos[i + 3] * compare[i + 3]; norm += mixingPos[i + 2] * mixingPos[i + 2] + mixingPos[i + 3] * mixingPos[i + 3]; } anorm = norm; return corr / sqrt((norm < 1e-9 ? 1.0 : norm)); } /// Update cross-correlation by accumulating "norm" coefficient by previously calculated value double TDStretch::calcCrossCorrAccumulate(const float *mixingPos, const float *compare, double &norm) { double corr; int i; corr = 0; // cancel first normalizer tap from previous round for (i = 1; i <= channels; i ++) { norm -= mixingPos[-i] * mixingPos[-i]; } // Same routine for stereo and mono. For Stereo, unroll by factor of 2. // For mono it's same routine yet unrollsd by factor of 4. for (i = 0; i < channels * overlapLength; i += 4) { corr += mixingPos[i] * compare[i] + mixingPos[i + 1] * compare[i + 1] + mixingPos[i + 2] * compare[i + 2] + mixingPos[i + 3] * compare[i + 3]; } // update normalizer with last samples of this round for (int j = 0; j < channels; j ++) { i --; norm += mixingPos[i] * mixingPos[i]; } return corr / sqrt((norm < 1e-9 ? 1.0 : norm)); } #endif // SOUNDTOUCH_FLOAT_SAMPLES soundtouch/source/SoundTouch/InterpolateCubic.h0000664000175100017510000000424312577461413021316 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Cubic interpolation routine. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // $Id: InterpolateCubic.h 225 2015-07-26 14:45:48Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef _InterpolateCubic_H_ #define _InterpolateCubic_H_ #include "RateTransposer.h" #include "STTypes.h" namespace soundtouch { class InterpolateCubic : public TransposerBase { protected: virtual void resetRegisters(); virtual int transposeMono(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples); virtual int transposeStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples); virtual int transposeMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples); double fract; public: InterpolateCubic(); }; } #endif soundtouch/source/SoundTouch/FIRFilter.cpp0000664000175100017510000002303112577461413020177 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// General FIR digital filter routines with MMX optimization. /// /// Note : MMX optimized functions reside in a separate, platform-specific file, /// e.g. 'mmx_win.cpp' or 'mmx_gcc.cpp' /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2015-02-21 23:24:29 +0200 (Sat, 21 Feb 2015) $ // File revision : $Revision: 4 $ // // $Id: FIRFilter.cpp 202 2015-02-21 21:24:29Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include "FIRFilter.h" #include "cpu_detect.h" using namespace soundtouch; /***************************************************************************** * * Implementation of the class 'FIRFilter' * *****************************************************************************/ FIRFilter::FIRFilter() { resultDivFactor = 0; resultDivider = 0; length = 0; lengthDiv8 = 0; filterCoeffs = NULL; } FIRFilter::~FIRFilter() { delete[] filterCoeffs; } // Usual C-version of the filter routine for stereo sound uint FIRFilter::evaluateFilterStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples) const { int j, end; #ifdef SOUNDTOUCH_FLOAT_SAMPLES // when using floating point samples, use a scaler instead of a divider // because division is much slower operation than multiplying. double dScaler = 1.0 / (double)resultDivider; #endif assert(length != 0); assert(src != NULL); assert(dest != NULL); assert(filterCoeffs != NULL); end = 2 * (numSamples - length); #pragma omp parallel for for (j = 0; j < end; j += 2) { const SAMPLETYPE *ptr; LONG_SAMPLETYPE suml, sumr; uint i; suml = sumr = 0; ptr = src + j; for (i = 0; i < length; i += 4) { // loop is unrolled by factor of 4 here for efficiency suml += ptr[2 * i + 0] * filterCoeffs[i + 0] + ptr[2 * i + 2] * filterCoeffs[i + 1] + ptr[2 * i + 4] * filterCoeffs[i + 2] + ptr[2 * i + 6] * filterCoeffs[i + 3]; sumr += ptr[2 * i + 1] * filterCoeffs[i + 0] + ptr[2 * i + 3] * filterCoeffs[i + 1] + ptr[2 * i + 5] * filterCoeffs[i + 2] + ptr[2 * i + 7] * filterCoeffs[i + 3]; } #ifdef SOUNDTOUCH_INTEGER_SAMPLES suml >>= resultDivFactor; sumr >>= resultDivFactor; // saturate to 16 bit integer limits suml = (suml < -32768) ? -32768 : (suml > 32767) ? 32767 : suml; // saturate to 16 bit integer limits sumr = (sumr < -32768) ? -32768 : (sumr > 32767) ? 32767 : sumr; #else suml *= dScaler; sumr *= dScaler; #endif // SOUNDTOUCH_INTEGER_SAMPLES dest[j] = (SAMPLETYPE)suml; dest[j + 1] = (SAMPLETYPE)sumr; } return numSamples - length; } // Usual C-version of the filter routine for mono sound uint FIRFilter::evaluateFilterMono(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples) const { int j, end; #ifdef SOUNDTOUCH_FLOAT_SAMPLES // when using floating point samples, use a scaler instead of a divider // because division is much slower operation than multiplying. double dScaler = 1.0 / (double)resultDivider; #endif assert(length != 0); end = numSamples - length; #pragma omp parallel for for (j = 0; j < end; j ++) { const SAMPLETYPE *pSrc = src + j; LONG_SAMPLETYPE sum; uint i; sum = 0; for (i = 0; i < length; i += 4) { // loop is unrolled by factor of 4 here for efficiency sum += pSrc[i + 0] * filterCoeffs[i + 0] + pSrc[i + 1] * filterCoeffs[i + 1] + pSrc[i + 2] * filterCoeffs[i + 2] + pSrc[i + 3] * filterCoeffs[i + 3]; } #ifdef SOUNDTOUCH_INTEGER_SAMPLES sum >>= resultDivFactor; // saturate to 16 bit integer limits sum = (sum < -32768) ? -32768 : (sum > 32767) ? 32767 : sum; #else sum *= dScaler; #endif // SOUNDTOUCH_INTEGER_SAMPLES dest[j] = (SAMPLETYPE)sum; } return end; } uint FIRFilter::evaluateFilterMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels) { int j, end; #ifdef SOUNDTOUCH_FLOAT_SAMPLES // when using floating point samples, use a scaler instead of a divider // because division is much slower operation than multiplying. double dScaler = 1.0 / (double)resultDivider; #endif assert(length != 0); assert(src != NULL); assert(dest != NULL); assert(filterCoeffs != NULL); assert(numChannels < 16); end = numChannels * (numSamples - length); #pragma omp parallel for for (j = 0; j < end; j += numChannels) { const SAMPLETYPE *ptr; LONG_SAMPLETYPE sums[16]; uint c, i; for (c = 0; c < numChannels; c ++) { sums[c] = 0; } ptr = src + j; for (i = 0; i < length; i ++) { SAMPLETYPE coef=filterCoeffs[i]; for (c = 0; c < numChannels; c ++) { sums[c] += ptr[0] * coef; ptr ++; } } for (c = 0; c < numChannels; c ++) { #ifdef SOUNDTOUCH_INTEGER_SAMPLES sums[c] >>= resultDivFactor; #else sums[c] *= dScaler; #endif // SOUNDTOUCH_INTEGER_SAMPLES dest[j+c] = (SAMPLETYPE)sums[c]; } } return numSamples - length; } // Set filter coeffiecients and length. // // Throws an exception if filter length isn't divisible by 8 void FIRFilter::setCoefficients(const SAMPLETYPE *coeffs, uint newLength, uint uResultDivFactor) { assert(newLength > 0); if (newLength % 8) ST_THROW_RT_ERROR("FIR filter length not divisible by 8"); lengthDiv8 = newLength / 8; length = lengthDiv8 * 8; assert(length == newLength); resultDivFactor = uResultDivFactor; resultDivider = (SAMPLETYPE)::pow(2.0, (int)resultDivFactor); delete[] filterCoeffs; filterCoeffs = new SAMPLETYPE[length]; memcpy(filterCoeffs, coeffs, length * sizeof(SAMPLETYPE)); } uint FIRFilter::getLength() const { return length; } // Applies the filter to the given sequence of samples. // // Note : The amount of outputted samples is by value of 'filter_length' // smaller than the amount of input samples. uint FIRFilter::evaluate(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels) { assert(length > 0); assert(lengthDiv8 * 8 == length); if (numSamples < length) return 0; #ifndef USE_MULTICH_ALWAYS if (numChannels == 1) { return evaluateFilterMono(dest, src, numSamples); } else if (numChannels == 2) { return evaluateFilterStereo(dest, src, numSamples); } else #endif // USE_MULTICH_ALWAYS { assert(numChannels > 0); return evaluateFilterMulti(dest, src, numSamples, numChannels); } } // Operator 'new' is overloaded so that it automatically creates a suitable instance // depending on if we've a MMX-capable CPU available or not. void * FIRFilter::operator new(size_t s) { // Notice! don't use "new FIRFilter" directly, use "newInstance" to create a new instance instead! ST_THROW_RT_ERROR("Error in FIRFilter::new: Don't use 'new FIRFilter', use 'newInstance' member instead!"); return newInstance(); } FIRFilter * FIRFilter::newInstance() { uint uExtensions; uExtensions = detectCPUextensions(); // Check if MMX/SSE instruction set extensions supported by CPU #ifdef SOUNDTOUCH_ALLOW_MMX // MMX routines available only with integer sample types if (uExtensions & SUPPORT_MMX) { return ::new FIRFilterMMX; } else #endif // SOUNDTOUCH_ALLOW_MMX #ifdef SOUNDTOUCH_ALLOW_SSE if (uExtensions & SUPPORT_SSE) { // SSE support return ::new FIRFilterSSE; } else #endif // SOUNDTOUCH_ALLOW_SSE { // ISA optimizations not supported, use plain C version return ::new FIRFilter; } } soundtouch/source/SoundTouch/FIRFilter.h0000664000175100017510000001137512577461413017654 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// General FIR digital filter routines with MMX optimization. /// /// Note : MMX optimized functions reside in a separate, platform-specific file, /// e.g. 'mmx_win.cpp' or 'mmx_gcc.cpp' /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2015-02-21 23:24:29 +0200 (Sat, 21 Feb 2015) $ // File revision : $Revision: 4 $ // // $Id: FIRFilter.h 202 2015-02-21 21:24:29Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef FIRFilter_H #define FIRFilter_H #include #include "STTypes.h" namespace soundtouch { class FIRFilter { protected: // Number of FIR filter taps uint length; // Number of FIR filter taps divided by 8 uint lengthDiv8; // Result divider factor in 2^k format uint resultDivFactor; // Result divider value. SAMPLETYPE resultDivider; // Memory for filter coefficients SAMPLETYPE *filterCoeffs; virtual uint evaluateFilterStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples) const; virtual uint evaluateFilterMono(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples) const; virtual uint evaluateFilterMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels); public: FIRFilter(); virtual ~FIRFilter(); /// Operator 'new' is overloaded so that it automatically creates a suitable instance /// depending on if we've a MMX-capable CPU available or not. static void * operator new(size_t s); static FIRFilter *newInstance(); /// Applies the filter to the given sequence of samples. /// Note : The amount of outputted samples is by value of 'filter_length' /// smaller than the amount of input samples. /// /// \return Number of samples copied to 'dest'. uint evaluate(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels); uint getLength() const; virtual void setCoefficients(const SAMPLETYPE *coeffs, uint newLength, uint uResultDivFactor); }; // Optional subclasses that implement CPU-specific optimizations: #ifdef SOUNDTOUCH_ALLOW_MMX /// Class that implements MMX optimized functions exclusive for 16bit integer samples type. class FIRFilterMMX : public FIRFilter { protected: short *filterCoeffsUnalign; short *filterCoeffsAlign; virtual uint evaluateFilterStereo(short *dest, const short *src, uint numSamples) const; public: FIRFilterMMX(); ~FIRFilterMMX(); virtual void setCoefficients(const short *coeffs, uint newLength, uint uResultDivFactor); }; #endif // SOUNDTOUCH_ALLOW_MMX #ifdef SOUNDTOUCH_ALLOW_SSE /// Class that implements SSE optimized functions exclusive for floating point samples type. class FIRFilterSSE : public FIRFilter { protected: float *filterCoeffsUnalign; float *filterCoeffsAlign; virtual uint evaluateFilterStereo(float *dest, const float *src, uint numSamples) const; public: FIRFilterSSE(); ~FIRFilterSSE(); virtual void setCoefficients(const float *coeffs, uint newLength, uint uResultDivFactor); }; #endif // SOUNDTOUCH_ALLOW_SSE } #endif // FIRFilter_H soundtouch/source/SoundTouch/BPMDetect.cpp0000664000175100017510000002743712577461413020176 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Beats-per-minute (BPM) detection routine. /// /// The beat detection algorithm works as follows: /// - Use function 'inputSamples' to input a chunks of samples to the class for /// analysis. It's a good idea to enter a large sound file or stream in smallish /// chunks of around few kilosamples in order not to extinguish too much RAM memory. /// - Inputted sound data is decimated to approx 500 Hz to reduce calculation burden, /// which is basically ok as low (bass) frequencies mostly determine the beat rate. /// Simple averaging is used for anti-alias filtering because the resulting signal /// quality isn't of that high importance. /// - Decimated sound data is enveloped, i.e. the amplitude shape is detected by /// taking absolute value that's smoothed by sliding average. Signal levels that /// are below a couple of times the general RMS amplitude level are cut away to /// leave only notable peaks there. /// - Repeating sound patterns (e.g. beats) are detected by calculating short-term /// autocorrelation function of the enveloped signal. /// - After whole sound data file has been analyzed as above, the bpm level is /// detected by function 'getBpm' that finds the highest peak of the autocorrelation /// function, calculates it's precise location and converts this reading to bpm's. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2015-02-21 23:24:29 +0200 (Sat, 21 Feb 2015) $ // File revision : $Revision: 4 $ // // $Id: BPMDetect.cpp 202 2015-02-21 21:24:29Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include "FIFOSampleBuffer.h" #include "PeakFinder.h" #include "BPMDetect.h" using namespace soundtouch; #define INPUT_BLOCK_SAMPLES 2048 #define DECIMATED_BLOCK_SAMPLES 256 /// decay constant for calculating RMS volume sliding average approximation /// (time constant is about 10 sec) const float avgdecay = 0.99986f; /// Normalization coefficient for calculating RMS sliding average approximation. const float avgnorm = (1 - avgdecay); //////////////////////////////////////////////////////////////////////////////// // Enable following define to create bpm analysis file: // #define _CREATE_BPM_DEBUG_FILE #ifdef _CREATE_BPM_DEBUG_FILE #define DEBUGFILE_NAME "c:\\temp\\soundtouch-bpm-debug.txt" static void _SaveDebugData(const float *data, int minpos, int maxpos, double coeff) { FILE *fptr = fopen(DEBUGFILE_NAME, "wt"); int i; if (fptr) { printf("\n\nWriting BPM debug data into file " DEBUGFILE_NAME "\n\n"); for (i = minpos; i < maxpos; i ++) { fprintf(fptr, "%d\t%.1lf\t%f\n", i, coeff / (double)i, data[i]); } fclose(fptr); } } #else #define _SaveDebugData(a,b,c,d) #endif //////////////////////////////////////////////////////////////////////////////// BPMDetect::BPMDetect(int numChannels, int aSampleRate) { this->sampleRate = aSampleRate; this->channels = numChannels; decimateSum = 0; decimateCount = 0; envelopeAccu = 0; // Initialize RMS volume accumulator to RMS level of 1500 (out of 32768) that's // safe initial RMS signal level value for song data. This value is then adapted // to the actual level during processing. #ifdef SOUNDTOUCH_INTEGER_SAMPLES // integer samples RMSVolumeAccu = (1500 * 1500) / avgnorm; #else // float samples, scaled to range [-1..+1[ RMSVolumeAccu = (0.045f * 0.045f) / avgnorm; #endif // choose decimation factor so that result is approx. 1000 Hz decimateBy = sampleRate / 1000; assert(decimateBy > 0); assert(INPUT_BLOCK_SAMPLES < decimateBy * DECIMATED_BLOCK_SAMPLES); // Calculate window length & starting item according to desired min & max bpms windowLen = (60 * sampleRate) / (decimateBy * MIN_BPM); windowStart = (60 * sampleRate) / (decimateBy * MAX_BPM); assert(windowLen > windowStart); // allocate new working objects xcorr = new float[windowLen]; memset(xcorr, 0, windowLen * sizeof(float)); // allocate processing buffer buffer = new FIFOSampleBuffer(); // we do processing in mono mode buffer->setChannels(1); buffer->clear(); } BPMDetect::~BPMDetect() { delete[] xcorr; delete buffer; } /// convert to mono, low-pass filter & decimate to about 500 Hz. /// return number of outputted samples. /// /// Decimation is used to remove the unnecessary frequencies and thus to reduce /// the amount of data needed to be processed as calculating autocorrelation /// function is a very-very heavy operation. /// /// Anti-alias filtering is done simply by averaging the samples. This is really a /// poor-man's anti-alias filtering, but it's not so critical in this kind of application /// (it'd also be difficult to design a high-quality filter with steep cut-off at very /// narrow band) int BPMDetect::decimate(SAMPLETYPE *dest, const SAMPLETYPE *src, int numsamples) { int count, outcount; LONG_SAMPLETYPE out; assert(channels > 0); assert(decimateBy > 0); outcount = 0; for (count = 0; count < numsamples; count ++) { int j; // convert to mono and accumulate for (j = 0; j < channels; j ++) { decimateSum += src[j]; } src += j; decimateCount ++; if (decimateCount >= decimateBy) { // Store every Nth sample only out = (LONG_SAMPLETYPE)(decimateSum / (decimateBy * channels)); decimateSum = 0; decimateCount = 0; #ifdef SOUNDTOUCH_INTEGER_SAMPLES // check ranges for sure (shouldn't actually be necessary) if (out > 32767) { out = 32767; } else if (out < -32768) { out = -32768; } #endif // SOUNDTOUCH_INTEGER_SAMPLES dest[outcount] = (SAMPLETYPE)out; outcount ++; } } return outcount; } // Calculates autocorrelation function of the sample history buffer void BPMDetect::updateXCorr(int process_samples) { int offs; SAMPLETYPE *pBuffer; assert(buffer->numSamples() >= (uint)(process_samples + windowLen)); pBuffer = buffer->ptrBegin(); #pragma omp parallel for for (offs = windowStart; offs < windowLen; offs ++) { LONG_SAMPLETYPE sum; int i; sum = 0; for (i = 0; i < process_samples; i ++) { sum += pBuffer[i] * pBuffer[i + offs]; // scaling the sub-result shouldn't be necessary } // xcorr[offs] *= xcorr_decay; // decay 'xcorr' here with suitable coefficients // if it's desired that the system adapts automatically to // various bpms, e.g. in processing continouos music stream. // The 'xcorr_decay' should be a value that's smaller than but // close to one, and should also depend on 'process_samples' value. xcorr[offs] += (float)sum; } } // Calculates envelope of the sample data void BPMDetect::calcEnvelope(SAMPLETYPE *samples, int numsamples) { const static double decay = 0.7f; // decay constant for smoothing the envelope const static double norm = (1 - decay); int i; LONG_SAMPLETYPE out; double val; for (i = 0; i < numsamples; i ++) { // calc average RMS volume RMSVolumeAccu *= avgdecay; val = (float)fabs((float)samples[i]); RMSVolumeAccu += val * val; // cut amplitudes that are below cutoff ~2 times RMS volume // (we're interested in peak values, not the silent moments) if (val < 0.5 * sqrt(RMSVolumeAccu * avgnorm)) { val = 0; } // smooth amplitude envelope envelopeAccu *= decay; envelopeAccu += val; out = (LONG_SAMPLETYPE)(envelopeAccu * norm); #ifdef SOUNDTOUCH_INTEGER_SAMPLES // cut peaks (shouldn't be necessary though) if (out > 32767) out = 32767; #endif // SOUNDTOUCH_INTEGER_SAMPLES samples[i] = (SAMPLETYPE)out; } } void BPMDetect::inputSamples(const SAMPLETYPE *samples, int numSamples) { SAMPLETYPE decimated[DECIMATED_BLOCK_SAMPLES]; // iterate so that max INPUT_BLOCK_SAMPLES processed per iteration while (numSamples > 0) { int block; int decSamples; block = (numSamples > INPUT_BLOCK_SAMPLES) ? INPUT_BLOCK_SAMPLES : numSamples; // decimate. note that converts to mono at the same time decSamples = decimate(decimated, samples, block); samples += block * channels; numSamples -= block; // envelope new samples and add them to buffer calcEnvelope(decimated, decSamples); buffer->putSamples(decimated, decSamples); } // when the buffer has enought samples for processing... if ((int)buffer->numSamples() > windowLen) { int processLength; // how many samples are processed processLength = (int)buffer->numSamples() - windowLen; // ... calculate autocorrelations for oldest samples... updateXCorr(processLength); // ... and remove them from the buffer buffer->receiveSamples(processLength); } } void BPMDetect::removeBias() { int i; float minval = 1e12f; // arbitrary large number for (i = windowStart; i < windowLen; i ++) { if (xcorr[i] < minval) { minval = xcorr[i]; } } for (i = windowStart; i < windowLen; i ++) { xcorr[i] -= minval; } } float BPMDetect::getBpm() { double peakPos; double coeff; PeakFinder peakFinder; coeff = 60.0 * ((double)sampleRate / (double)decimateBy); // save bpm debug analysis data if debug data enabled _SaveDebugData(xcorr, windowStart, windowLen, coeff); // remove bias from xcorr data removeBias(); // find peak position peakPos = peakFinder.detectPeak(xcorr, windowStart, windowLen); assert(decimateBy != 0); if (peakPos < 1e-9) return 0.0; // detection failed. // calculate BPM return (float) (coeff / peakPos); } soundtouch/source/SoundTouch/sse_optimized.cpp0000664000175100017510000003224412577461413021275 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// SSE optimized routines for Pentium-III, Athlon-XP and later CPUs. All SSE /// optimized functions have been gathered into this single source /// code file, regardless to their class or original source code file, in order /// to ease porting the library to other compiler and processor platforms. /// /// The SSE-optimizations are programmed using SSE compiler intrinsics that /// are supported both by Microsoft Visual C++ and GCC compilers, so this file /// should compile with both toolsets. /// /// NOTICE: If using Visual Studio 6.0, you'll need to install the "Visual C++ /// 6.0 processor pack" update to support SSE instruction set. The update is /// available for download at Microsoft Developers Network, see here: /// http://msdn.microsoft.com/en-us/vstudio/aa718349.aspx /// /// If the above URL is expired or removed, go to "http://msdn.microsoft.com" and /// perform a search with keywords "processor pack". /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2015-08-09 00:00:15 +0300 (Sun, 09 Aug 2015) $ // File revision : $Revision: 4 $ // // $Id: sse_optimized.cpp 226 2015-08-08 21:00:15Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include "cpu_detect.h" #include "STTypes.h" using namespace soundtouch; #ifdef SOUNDTOUCH_ALLOW_SSE // SSE routines available only with float sample type ////////////////////////////////////////////////////////////////////////////// // // implementation of SSE optimized functions of class 'TDStretchSSE' // ////////////////////////////////////////////////////////////////////////////// #include "TDStretch.h" #include #include // Calculates cross correlation of two buffers double TDStretchSSE::calcCrossCorr(const float *pV1, const float *pV2, double &anorm) { int i; const float *pVec1; const __m128 *pVec2; __m128 vSum, vNorm; // Note. It means a major slow-down if the routine needs to tolerate // unaligned __m128 memory accesses. It's way faster if we can skip // unaligned slots and use _mm_load_ps instruction instead of _mm_loadu_ps. // This can mean up to ~ 10-fold difference (incl. part of which is // due to skipping every second round for stereo sound though). // // Compile-time define SOUNDTOUCH_ALLOW_NONEXACT_SIMD_OPTIMIZATION is provided // for choosing if this little cheating is allowed. #ifdef SOUNDTOUCH_ALLOW_NONEXACT_SIMD_OPTIMIZATION // Little cheating allowed, return valid correlation only for // aligned locations, meaning every second round for stereo sound. #define _MM_LOAD _mm_load_ps if (((ulongptr)pV1) & 15) return -1e50; // skip unaligned locations #else // No cheating allowed, use unaligned load & take the resulting // performance hit. #define _MM_LOAD _mm_loadu_ps #endif // ensure overlapLength is divisible by 8 assert((overlapLength % 8) == 0); // Calculates the cross-correlation value between 'pV1' and 'pV2' vectors // Note: pV2 _must_ be aligned to 16-bit boundary, pV1 need not. pVec1 = (const float*)pV1; pVec2 = (const __m128*)pV2; vSum = vNorm = _mm_setzero_ps(); // Unroll the loop by factor of 4 * 4 operations. Use same routine for // stereo & mono, for mono it just means twice the amount of unrolling. for (i = 0; i < channels * overlapLength / 16; i ++) { __m128 vTemp; // vSum += pV1[0..3] * pV2[0..3] vTemp = _MM_LOAD(pVec1); vSum = _mm_add_ps(vSum, _mm_mul_ps(vTemp ,pVec2[0])); vNorm = _mm_add_ps(vNorm, _mm_mul_ps(vTemp ,vTemp)); // vSum += pV1[4..7] * pV2[4..7] vTemp = _MM_LOAD(pVec1 + 4); vSum = _mm_add_ps(vSum, _mm_mul_ps(vTemp, pVec2[1])); vNorm = _mm_add_ps(vNorm, _mm_mul_ps(vTemp ,vTemp)); // vSum += pV1[8..11] * pV2[8..11] vTemp = _MM_LOAD(pVec1 + 8); vSum = _mm_add_ps(vSum, _mm_mul_ps(vTemp, pVec2[2])); vNorm = _mm_add_ps(vNorm, _mm_mul_ps(vTemp ,vTemp)); // vSum += pV1[12..15] * pV2[12..15] vTemp = _MM_LOAD(pVec1 + 12); vSum = _mm_add_ps(vSum, _mm_mul_ps(vTemp, pVec2[3])); vNorm = _mm_add_ps(vNorm, _mm_mul_ps(vTemp ,vTemp)); pVec1 += 16; pVec2 += 4; } // return value = vSum[0] + vSum[1] + vSum[2] + vSum[3] float *pvNorm = (float*)&vNorm; float norm = (pvNorm[0] + pvNorm[1] + pvNorm[2] + pvNorm[3]); anorm = norm; float *pvSum = (float*)&vSum; return (double)(pvSum[0] + pvSum[1] + pvSum[2] + pvSum[3]) / sqrt(norm < 1e-9 ? 1.0 : norm); /* This is approximately corresponding routine in C-language yet without normalization: double corr, norm; uint i; // Calculates the cross-correlation value between 'pV1' and 'pV2' vectors corr = norm = 0.0; for (i = 0; i < channels * overlapLength / 16; i ++) { corr += pV1[0] * pV2[0] + pV1[1] * pV2[1] + pV1[2] * pV2[2] + pV1[3] * pV2[3] + pV1[4] * pV2[4] + pV1[5] * pV2[5] + pV1[6] * pV2[6] + pV1[7] * pV2[7] + pV1[8] * pV2[8] + pV1[9] * pV2[9] + pV1[10] * pV2[10] + pV1[11] * pV2[11] + pV1[12] * pV2[12] + pV1[13] * pV2[13] + pV1[14] * pV2[14] + pV1[15] * pV2[15]; for (j = 0; j < 15; j ++) norm += pV1[j] * pV1[j]; pV1 += 16; pV2 += 16; } return corr / sqrt(norm); */ } double TDStretchSSE::calcCrossCorrAccumulate(const float *pV1, const float *pV2, double &norm) { // call usual calcCrossCorr function because SSE does not show big benefit of // accumulating "norm" value, and also the "norm" rolling algorithm would get // complicated due to SSE-specific alignment-vs-nonexact correlation rules. return calcCrossCorr(pV1, pV2, norm); } ////////////////////////////////////////////////////////////////////////////// // // implementation of SSE optimized functions of class 'FIRFilter' // ////////////////////////////////////////////////////////////////////////////// #include "FIRFilter.h" FIRFilterSSE::FIRFilterSSE() : FIRFilter() { filterCoeffsAlign = NULL; filterCoeffsUnalign = NULL; } FIRFilterSSE::~FIRFilterSSE() { delete[] filterCoeffsUnalign; filterCoeffsAlign = NULL; filterCoeffsUnalign = NULL; } // (overloaded) Calculates filter coefficients for SSE routine void FIRFilterSSE::setCoefficients(const float *coeffs, uint newLength, uint uResultDivFactor) { uint i; float fDivider; FIRFilter::setCoefficients(coeffs, newLength, uResultDivFactor); // Scale the filter coefficients so that it won't be necessary to scale the filtering result // also rearrange coefficients suitably for SSE // Ensure that filter coeffs array is aligned to 16-byte boundary delete[] filterCoeffsUnalign; filterCoeffsUnalign = new float[2 * newLength + 4]; filterCoeffsAlign = (float *)SOUNDTOUCH_ALIGN_POINTER_16(filterCoeffsUnalign); fDivider = (float)resultDivider; // rearrange the filter coefficients for mmx routines for (i = 0; i < newLength; i ++) { filterCoeffsAlign[2 * i + 0] = filterCoeffsAlign[2 * i + 1] = coeffs[i + 0] / fDivider; } } // SSE-optimized version of the filter routine for stereo sound uint FIRFilterSSE::evaluateFilterStereo(float *dest, const float *source, uint numSamples) const { int count = (int)((numSamples - length) & (uint)-2); int j; assert(count % 2 == 0); if (count < 2) return 0; assert(source != NULL); assert(dest != NULL); assert((length % 8) == 0); assert(filterCoeffsAlign != NULL); assert(((ulongptr)filterCoeffsAlign) % 16 == 0); // filter is evaluated for two stereo samples with each iteration, thus use of 'j += 2' #pragma omp parallel for for (j = 0; j < count; j += 2) { const float *pSrc; float *pDest; const __m128 *pFil; __m128 sum1, sum2; uint i; pSrc = (const float*)source + j * 2; // source audio data pDest = dest + j * 2; // destination audio data pFil = (const __m128*)filterCoeffsAlign; // filter coefficients. NOTE: Assumes coefficients // are aligned to 16-byte boundary sum1 = sum2 = _mm_setzero_ps(); for (i = 0; i < length / 8; i ++) { // Unroll loop for efficiency & calculate filter for 2*2 stereo samples // at each pass // sum1 is accu for 2*2 filtered stereo sound data at the primary sound data offset // sum2 is accu for 2*2 filtered stereo sound data for the next sound sample offset. sum1 = _mm_add_ps(sum1, _mm_mul_ps(_mm_loadu_ps(pSrc) , pFil[0])); sum2 = _mm_add_ps(sum2, _mm_mul_ps(_mm_loadu_ps(pSrc + 2), pFil[0])); sum1 = _mm_add_ps(sum1, _mm_mul_ps(_mm_loadu_ps(pSrc + 4), pFil[1])); sum2 = _mm_add_ps(sum2, _mm_mul_ps(_mm_loadu_ps(pSrc + 6), pFil[1])); sum1 = _mm_add_ps(sum1, _mm_mul_ps(_mm_loadu_ps(pSrc + 8) , pFil[2])); sum2 = _mm_add_ps(sum2, _mm_mul_ps(_mm_loadu_ps(pSrc + 10), pFil[2])); sum1 = _mm_add_ps(sum1, _mm_mul_ps(_mm_loadu_ps(pSrc + 12), pFil[3])); sum2 = _mm_add_ps(sum2, _mm_mul_ps(_mm_loadu_ps(pSrc + 14), pFil[3])); pSrc += 16; pFil += 4; } // Now sum1 and sum2 both have a filtered 2-channel sample each, but we still need // to sum the two hi- and lo-floats of these registers together. // post-shuffle & add the filtered values and store to dest. _mm_storeu_ps(pDest, _mm_add_ps( _mm_shuffle_ps(sum1, sum2, _MM_SHUFFLE(1,0,3,2)), // s2_1 s2_0 s1_3 s1_2 _mm_shuffle_ps(sum1, sum2, _MM_SHUFFLE(3,2,1,0)) // s2_3 s2_2 s1_1 s1_0 )); } // Ideas for further improvement: // 1. If it could be guaranteed that 'source' were always aligned to 16-byte // boundary, a faster aligned '_mm_load_ps' instruction could be used. // 2. If it could be guaranteed that 'dest' were always aligned to 16-byte // boundary, a faster '_mm_store_ps' instruction could be used. return (uint)count; /* original routine in C-language. please notice the C-version has differently organized coefficients though. double suml1, suml2; double sumr1, sumr2; uint i, j; for (j = 0; j < count; j += 2) { const float *ptr; const float *pFil; suml1 = sumr1 = 0.0; suml2 = sumr2 = 0.0; ptr = src; pFil = filterCoeffs; for (i = 0; i < lengthLocal; i ++) { // unroll loop for efficiency. suml1 += ptr[0] * pFil[0] + ptr[2] * pFil[2] + ptr[4] * pFil[4] + ptr[6] * pFil[6]; sumr1 += ptr[1] * pFil[1] + ptr[3] * pFil[3] + ptr[5] * pFil[5] + ptr[7] * pFil[7]; suml2 += ptr[8] * pFil[0] + ptr[10] * pFil[2] + ptr[12] * pFil[4] + ptr[14] * pFil[6]; sumr2 += ptr[9] * pFil[1] + ptr[11] * pFil[3] + ptr[13] * pFil[5] + ptr[15] * pFil[7]; ptr += 16; pFil += 8; } dest[0] = (float)suml1; dest[1] = (float)sumr1; dest[2] = (float)suml2; dest[3] = (float)sumr2; src += 4; dest += 4; } */ } #endif // SOUNDTOUCH_ALLOW_SSE soundtouch/source/SoundTouch/InterpolateCubic.cpp0000664000175100017510000001456612577461413021662 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Cubic interpolation routine. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // $Id: InterpolateCubic.cpp 179 2014-01-06 18:41:42Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include #include #include "InterpolateCubic.h" #include "STTypes.h" using namespace soundtouch; // cubic interpolation coefficients static const float _coeffs[]= { -0.5f, 1.0f, -0.5f, 0.0f, 1.5f, -2.5f, 0.0f, 1.0f, -1.5f, 2.0f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f}; InterpolateCubic::InterpolateCubic() { fract = 0; } void InterpolateCubic::resetRegisters() { fract = 0; } /// Transpose mono audio. Returns number of produced output samples, and /// updates "srcSamples" to amount of consumed source samples int InterpolateCubic::transposeMono(SAMPLETYPE *pdest, const SAMPLETYPE *psrc, int &srcSamples) { int i; int srcSampleEnd = srcSamples - 4; int srcCount = 0; i = 0; while (srcCount < srcSampleEnd) { float out; const float x3 = 1.0f; const float x2 = (float)fract; // x const float x1 = x2*x2; // x^2 const float x0 = x1*x2; // x^3 float y0, y1, y2, y3; assert(fract < 1.0); y0 = _coeffs[0] * x0 + _coeffs[1] * x1 + _coeffs[2] * x2 + _coeffs[3] * x3; y1 = _coeffs[4] * x0 + _coeffs[5] * x1 + _coeffs[6] * x2 + _coeffs[7] * x3; y2 = _coeffs[8] * x0 + _coeffs[9] * x1 + _coeffs[10] * x2 + _coeffs[11] * x3; y3 = _coeffs[12] * x0 + _coeffs[13] * x1 + _coeffs[14] * x2 + _coeffs[15] * x3; out = y0 * psrc[0] + y1 * psrc[1] + y2 * psrc[2] + y3 * psrc[3]; pdest[i] = (SAMPLETYPE)out; i ++; // update position fraction fract += rate; // update whole positions int whole = (int)fract; fract -= whole; psrc += whole; srcCount += whole; } srcSamples = srcCount; return i; } /// Transpose stereo audio. Returns number of produced output samples, and /// updates "srcSamples" to amount of consumed source samples int InterpolateCubic::transposeStereo(SAMPLETYPE *pdest, const SAMPLETYPE *psrc, int &srcSamples) { int i; int srcSampleEnd = srcSamples - 4; int srcCount = 0; i = 0; while (srcCount < srcSampleEnd) { const float x3 = 1.0f; const float x2 = (float)fract; // x const float x1 = x2*x2; // x^2 const float x0 = x1*x2; // x^3 float y0, y1, y2, y3; float out0, out1; assert(fract < 1.0); y0 = _coeffs[0] * x0 + _coeffs[1] * x1 + _coeffs[2] * x2 + _coeffs[3] * x3; y1 = _coeffs[4] * x0 + _coeffs[5] * x1 + _coeffs[6] * x2 + _coeffs[7] * x3; y2 = _coeffs[8] * x0 + _coeffs[9] * x1 + _coeffs[10] * x2 + _coeffs[11] * x3; y3 = _coeffs[12] * x0 + _coeffs[13] * x1 + _coeffs[14] * x2 + _coeffs[15] * x3; out0 = y0 * psrc[0] + y1 * psrc[2] + y2 * psrc[4] + y3 * psrc[6]; out1 = y0 * psrc[1] + y1 * psrc[3] + y2 * psrc[5] + y3 * psrc[7]; pdest[2*i] = (SAMPLETYPE)out0; pdest[2*i+1] = (SAMPLETYPE)out1; i ++; // update position fraction fract += rate; // update whole positions int whole = (int)fract; fract -= whole; psrc += 2*whole; srcCount += whole; } srcSamples = srcCount; return i; } /// Transpose multi-channel audio. Returns number of produced output samples, and /// updates "srcSamples" to amount of consumed source samples int InterpolateCubic::transposeMulti(SAMPLETYPE *pdest, const SAMPLETYPE *psrc, int &srcSamples) { int i; int srcSampleEnd = srcSamples - 4; int srcCount = 0; i = 0; while (srcCount < srcSampleEnd) { const float x3 = 1.0f; const float x2 = (float)fract; // x const float x1 = x2*x2; // x^2 const float x0 = x1*x2; // x^3 float y0, y1, y2, y3; assert(fract < 1.0); y0 = _coeffs[0] * x0 + _coeffs[1] * x1 + _coeffs[2] * x2 + _coeffs[3] * x3; y1 = _coeffs[4] * x0 + _coeffs[5] * x1 + _coeffs[6] * x2 + _coeffs[7] * x3; y2 = _coeffs[8] * x0 + _coeffs[9] * x1 + _coeffs[10] * x2 + _coeffs[11] * x3; y3 = _coeffs[12] * x0 + _coeffs[13] * x1 + _coeffs[14] * x2 + _coeffs[15] * x3; for (int c = 0; c < numChannels; c ++) { float out; out = y0 * psrc[c] + y1 * psrc[c + numChannels] + y2 * psrc[c + 2 * numChannels] + y3 * psrc[c + 3 * numChannels]; pdest[0] = (SAMPLETYPE)out; pdest ++; } i ++; // update position fraction fract += rate; // update whole positions int whole = (int)fract; fract -= whole; psrc += numChannels*whole; srcCount += whole; } srcSamples = srcCount; return i; } soundtouch/source/SoundTouch/Makefile.am0000664000175100017510000000551512577461413017750 0ustar vmdevvmdev## Process this file with automake to create Makefile.in ## ## $Id: Makefile.am 230 2015-09-20 07:38:32Z oparviai $ ## ## This file is part of SoundTouch, an audio processing library for pitch/time adjustments ## ## SoundTouch 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. ## ## SoundTouch is distributed in the hope that it will be useful, but WITHOUT ANY ## WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR ## A PARTICULAR PURPOSE. See the GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License along with ## this program; if not, write to the Free Software Foundation, Inc., 59 Temple ## Place - Suite 330, Boston, MA 02111-1307, USA include $(top_srcdir)/config/am_include.mk # set to something if you want other stuff to be included in the distribution tarball EXTRA_DIST=SoundTouch.dsp SoundTouch.dsw SoundTouch.sln SoundTouch.vcproj noinst_HEADERS=AAFilter.h cpu_detect.h cpu_detect_x86.cpp FIRFilter.h RateTransposer.h TDStretch.h PeakFinder.h lib_LTLIBRARIES=libSoundTouch.la # libSoundTouch_la_SOURCES=AAFilter.cpp FIRFilter.cpp FIFOSampleBuffer.cpp \ RateTransposer.cpp SoundTouch.cpp TDStretch.cpp cpu_detect_x86.cpp \ BPMDetect.cpp PeakFinder.cpp InterpolateLinear.cpp InterpolateCubic.cpp \ InterpolateShannon.cpp # Compiler flags AM_CXXFLAGS+=-O3 # Compile the files that need MMX and SSE individually. libSoundTouch_la_LIBADD=libSoundTouchMMX.la libSoundTouchSSE.la noinst_LTLIBRARIES=libSoundTouchMMX.la libSoundTouchSSE.la libSoundTouchMMX_la_SOURCES=mmx_optimized.cpp libSoundTouchSSE_la_SOURCES=sse_optimized.cpp # We enable optimizations by default. # If MMX is supported compile with -mmmx. # Do not assume -msse is also supported. if HAVE_MMX libSoundTouchMMX_la_CXXFLAGS = -mmmx $(AM_CXXFLAGS) else libSoundTouchMMX_la_CXXFLAGS = $(AM_CXXFLAGS) endif # We enable optimizations by default. # If SSE is supported compile with -msse. if HAVE_SSE libSoundTouchSSE_la_CXXFLAGS = -msse $(AM_CXXFLAGS) else libSoundTouchSSE_la_CXXFLAGS = $(AM_CXXFLAGS) endif # Let the user disable optimizations if he wishes to. if !X86_OPTIMIZATIONS libSoundTouchMMX_la_CXXFLAGS = $(AM_CXXFLAGS) libSoundTouchSSE_la_CXXFLAGS = $(AM_CXXFLAGS) endif # Modify the default 0.0.0 to LIB_SONAME.0.0 libSoundTouch_la_LDFLAGS=-version-info @LIB_SONAME@ # other linking flags to add # noinst_LTLIBRARIES = libSoundTouchOpt.la # libSoundTouch_la_LIBADD = libSoundTouchOpt.la # libSoundTouchOpt_la_SOURCES = mmx_optimized.cpp sse_optimized.cpp # libSoundTouchOpt_la_CXXFLAGS = -O3 -msse -fcheck-new -I../../include soundtouch/source/SoundTouch/InterpolateLinear.h0000664000175100017510000000573512577461413021512 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Linear interpolation routine. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // $Id: InterpolateLinear.h 225 2015-07-26 14:45:48Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef _InterpolateLinear_H_ #define _InterpolateLinear_H_ #include "RateTransposer.h" #include "STTypes.h" namespace soundtouch { /// Linear transposer class that uses integer arithmetics class InterpolateLinearInteger : public TransposerBase { protected: int iFract; int iRate; virtual void resetRegisters(); virtual int transposeMono(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples); virtual int transposeStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples); virtual int transposeMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples); public: InterpolateLinearInteger(); /// Sets new target rate. Normal rate = 1.0, smaller values represent slower /// rate, larger faster rates. virtual void setRate(double newRate); }; /// Linear transposer class that uses floating point arithmetics class InterpolateLinearFloat : public TransposerBase { protected: double fract; virtual void resetRegisters(); virtual int transposeMono(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples); virtual int transposeStereo(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples); virtual int transposeMulti(SAMPLETYPE *dest, const SAMPLETYPE *src, int &srcSamples); public: InterpolateLinearFloat(); }; } #endif soundtouch/source/SoundTouch/SoundTouch.vcproj0000664000175100017510000005165512577461413021242 0ustar vmdevvmdev soundtouch/source/SoundTouch/InterpolateShannon.cpp0000664000175100017510000001325012577461413022226 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Sample interpolation routine using 8-tap band-limited Shannon interpolation /// with kaiser window. /// /// Notice. This algorithm is remarkably much heavier than linear or cubic /// interpolation, and not remarkably better than cubic algorithm. Thus mostly /// for experimental purposes /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // $Id: InterpolateShannon.cpp 195 2014-04-06 15:57:21Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include #include "InterpolateShannon.h" #include "STTypes.h" using namespace soundtouch; /// Kaiser window with beta = 2.0 /// Values scaled down by 5% to avoid overflows static const double _kaiser8[8] = { 0.41778693317814, 0.64888025049173, 0.83508562409944, 0.93887857733412, 0.93887857733412, 0.83508562409944, 0.64888025049173, 0.41778693317814 }; InterpolateShannon::InterpolateShannon() { fract = 0; } void InterpolateShannon::resetRegisters() { fract = 0; } #define PI 3.1415926536 #define sinc(x) (sin(PI * (x)) / (PI * (x))) /// Transpose mono audio. Returns number of produced output samples, and /// updates "srcSamples" to amount of consumed source samples int InterpolateShannon::transposeMono(SAMPLETYPE *pdest, const SAMPLETYPE *psrc, int &srcSamples) { int i; int srcSampleEnd = srcSamples - 8; int srcCount = 0; i = 0; while (srcCount < srcSampleEnd) { double out; assert(fract < 1.0); out = psrc[0] * sinc(-3.0 - fract) * _kaiser8[0]; out += psrc[1] * sinc(-2.0 - fract) * _kaiser8[1]; out += psrc[2] * sinc(-1.0 - fract) * _kaiser8[2]; if (fract < 1e-6) { out += psrc[3] * _kaiser8[3]; // sinc(0) = 1 } else { out += psrc[3] * sinc(- fract) * _kaiser8[3]; } out += psrc[4] * sinc( 1.0 - fract) * _kaiser8[4]; out += psrc[5] * sinc( 2.0 - fract) * _kaiser8[5]; out += psrc[6] * sinc( 3.0 - fract) * _kaiser8[6]; out += psrc[7] * sinc( 4.0 - fract) * _kaiser8[7]; pdest[i] = (SAMPLETYPE)out; i ++; // update position fraction fract += rate; // update whole positions int whole = (int)fract; fract -= whole; psrc += whole; srcCount += whole; } srcSamples = srcCount; return i; } /// Transpose stereo audio. Returns number of produced output samples, and /// updates "srcSamples" to amount of consumed source samples int InterpolateShannon::transposeStereo(SAMPLETYPE *pdest, const SAMPLETYPE *psrc, int &srcSamples) { int i; int srcSampleEnd = srcSamples - 8; int srcCount = 0; i = 0; while (srcCount < srcSampleEnd) { double out0, out1, w; assert(fract < 1.0); w = sinc(-3.0 - fract) * _kaiser8[0]; out0 = psrc[0] * w; out1 = psrc[1] * w; w = sinc(-2.0 - fract) * _kaiser8[1]; out0 += psrc[2] * w; out1 += psrc[3] * w; w = sinc(-1.0 - fract) * _kaiser8[2]; out0 += psrc[4] * w; out1 += psrc[5] * w; w = _kaiser8[3] * ((fract < 1e-5) ? 1.0 : sinc(- fract)); // sinc(0) = 1 out0 += psrc[6] * w; out1 += psrc[7] * w; w = sinc( 1.0 - fract) * _kaiser8[4]; out0 += psrc[8] * w; out1 += psrc[9] * w; w = sinc( 2.0 - fract) * _kaiser8[5]; out0 += psrc[10] * w; out1 += psrc[11] * w; w = sinc( 3.0 - fract) * _kaiser8[6]; out0 += psrc[12] * w; out1 += psrc[13] * w; w = sinc( 4.0 - fract) * _kaiser8[7]; out0 += psrc[14] * w; out1 += psrc[15] * w; pdest[2*i] = (SAMPLETYPE)out0; pdest[2*i+1] = (SAMPLETYPE)out1; i ++; // update position fraction fract += rate; // update whole positions int whole = (int)fract; fract -= whole; psrc += 2*whole; srcCount += whole; } srcSamples = srcCount; return i; } /// Transpose stereo audio. Returns number of produced output samples, and /// updates "srcSamples" to amount of consumed source samples int InterpolateShannon::transposeMulti(SAMPLETYPE *pdest, const SAMPLETYPE *psrc, int &srcSamples) { // not implemented assert(false); return 0; } soundtouch/source/SoundTouch/PeakFinder.h0000664000175100017510000001022512577461413020067 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// The routine detects highest value on an array of values and calculates the /// precise peak location as a mass-center of the 'hump' around the peak value. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2011-12-30 22:33:46 +0200 (Fri, 30 Dec 2011) $ // File revision : $Revision: 4 $ // // $Id: PeakFinder.h 132 2011-12-30 20:33:46Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef _PeakFinder_H_ #define _PeakFinder_H_ namespace soundtouch { class PeakFinder { protected: /// Min, max allowed peak positions within the data vector int minPos, maxPos; /// Calculates the mass center between given vector items. double calcMassCenter(const float *data, ///< Data vector. int firstPos, ///< Index of first vector item beloging to the peak. int lastPos ///< Index of last vector item beloging to the peak. ) const; /// Finds the data vector index where the monotoniously decreasing signal crosses the /// given level. int findCrossingLevel(const float *data, ///< Data vector. float level, ///< Goal crossing level. int peakpos, ///< Peak position index within the data vector. int direction /// Direction where to proceed from the peak: 1 = right, -1 = left. ) const; // Finds real 'top' of a peak hump from neighnourhood of the given 'peakpos'. int findTop(const float *data, int peakpos) const; /// Finds the 'ground' level, i.e. smallest level between two neighbouring peaks, to right- /// or left-hand side of the given peak position. int findGround(const float *data, /// Data vector. int peakpos, /// Peak position index within the data vector. int direction /// Direction where to proceed from the peak: 1 = right, -1 = left. ) const; /// get exact center of peak near given position by calculating local mass of center double getPeakCenter(const float *data, int peakpos) const; public: /// Constructor. PeakFinder(); /// Detect exact peak position of the data vector by finding the largest peak 'hump' /// and calculating the mass-center location of the peak hump. /// /// \return The location of the largest base harmonic peak hump. double detectPeak(const float *data, /// Data vector to be analyzed. The data vector has /// to be at least 'maxPos' items long. int minPos, ///< Min allowed peak location within the vector data. int maxPos ///< Max allowed peak location within the vector data. ); }; } #endif // _PeakFinder_H_ soundtouch/source/SoundTouch/cpu_detect_x86.cpp0000664000175100017510000001134512577461413021242 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Generic version of the x86 CPU extension detection routine. /// /// This file is for GNU & other non-Windows compilers, see 'cpu_detect_x86_win.cpp' /// for the Microsoft compiler version. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2014-01-07 20:24:28 +0200 (Tue, 07 Jan 2014) $ // File revision : $Revision: 4 $ // // $Id: cpu_detect_x86.cpp 183 2014-01-07 18:24:28Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include "cpu_detect.h" #include "STTypes.h" #if defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS) #if defined(__GNUC__) && defined(__i386__) // gcc #include "cpuid.h" #elif defined(_M_IX86) // windows non-gcc #include #endif #define bit_MMX (1 << 23) #define bit_SSE (1 << 25) #define bit_SSE2 (1 << 26) #endif ////////////////////////////////////////////////////////////////////////////// // // processor instructions extension detection routines // ////////////////////////////////////////////////////////////////////////////// // Flag variable indicating whick ISA extensions are disabled (for debugging) static uint _dwDisabledISA = 0x00; // 0xffffffff; //<- use this to disable all extensions // Disables given set of instruction extensions. See SUPPORT_... defines. void disableExtensions(uint dwDisableMask) { _dwDisabledISA = dwDisableMask; } /// Checks which instruction set extensions are supported by the CPU. uint detectCPUextensions(void) { /// If building for a 64bit system (no Itanium) and the user wants optimizations. /// Return the OR of SUPPORT_{MMX,SSE,SSE2}. 11001 or 0x19. /// Keep the _dwDisabledISA test (2 more operations, could be eliminated). #if ((defined(__GNUC__) && defined(__x86_64__)) \ || defined(_M_X64)) \ && defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS) return 0x19 & ~_dwDisabledISA; /// If building for a 32bit system and the user wants optimizations. /// Keep the _dwDisabledISA test (2 more operations, could be eliminated). #elif ((defined(__GNUC__) && defined(__i386__)) \ || defined(_M_IX86)) \ && defined(SOUNDTOUCH_ALLOW_X86_OPTIMIZATIONS) if (_dwDisabledISA == 0xffffffff) return 0; uint res = 0; #if defined(__GNUC__) // GCC version of cpuid. Requires GCC 4.3.0 or later for __cpuid intrinsic support. uint eax, ebx, ecx, edx; // unsigned int is the standard type. uint is defined by the compiler and not guaranteed to be portable. // Check if no cpuid support. if (!__get_cpuid (1, &eax, &ebx, &ecx, &edx)) return 0; // always disable extensions. if (edx & bit_MMX) res = res | SUPPORT_MMX; if (edx & bit_SSE) res = res | SUPPORT_SSE; if (edx & bit_SSE2) res = res | SUPPORT_SSE2; #else // Window / VS version of cpuid. Notice that Visual Studio 2005 or later required // for __cpuid intrinsic support. int reg[4] = {-1}; // Check if no cpuid support. __cpuid(reg,0); if ((unsigned int)reg[0] == 0) return 0; // always disable extensions. __cpuid(reg,1); if ((unsigned int)reg[3] & bit_MMX) res = res | SUPPORT_MMX; if ((unsigned int)reg[3] & bit_SSE) res = res | SUPPORT_SSE; if ((unsigned int)reg[3] & bit_SSE2) res = res | SUPPORT_SSE2; #endif return res & ~_dwDisabledISA; #else /// One of these is true: /// 1) We don't want optimizations. /// 2) Using an unsupported compiler. /// 3) Running on a non-x86 platform. return 0; #endif } soundtouch/source/SoundTouch/SoundTouch.dsw0000664000175100017510000000113412577461413020517 0ustar vmdevvmdevMicrosoft Developer Studio Workspace File, Format Version 6.00 # WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE! # # $Id: SoundTouch.dsw 11 2008-02-10 16:26:55Z oparviai $ ############################################################################### Project: "SoundTouch"=.\SoundTouch.dsp - Package Owner=<4> Package=<5> {{{ }}} Package=<4> {{{ }}} ############################################################################### Global: Package=<5> {{{ }}} Package=<3> {{{ }}} ############################################################################### soundtouch/source/SoundTouch/AAFilter.cpp0000664000175100017510000001504612577461413020047 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// FIR low-pass (anti-alias) filter with filter coefficient design routine and /// MMX optimization. /// /// Anti-alias filter is used to prevent folding of high frequencies when /// transposing the sample rate with interpolation. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2014-01-05 23:40:22 +0200 (Sun, 05 Jan 2014) $ // File revision : $Revision: 4 $ // // $Id: AAFilter.cpp 177 2014-01-05 21:40:22Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include "AAFilter.h" #include "FIRFilter.h" using namespace soundtouch; #define PI 3.141592655357989 #define TWOPI (2 * PI) // define this to save AA filter coefficients to a file // #define _DEBUG_SAVE_AAFILTER_COEFFICIENTS 1 #ifdef _DEBUG_SAVE_AAFILTER_COEFFICIENTS #include static void _DEBUG_SAVE_AAFIR_COEFFS(SAMPLETYPE *coeffs, int len) { FILE *fptr = fopen("aa_filter_coeffs.txt", "wt"); if (fptr == NULL) return; for (int i = 0; i < len; i ++) { double temp = coeffs[i]; fprintf(fptr, "%lf\n", temp); } fclose(fptr); } #else #define _DEBUG_SAVE_AAFIR_COEFFS(x, y) #endif /***************************************************************************** * * Implementation of the class 'AAFilter' * *****************************************************************************/ AAFilter::AAFilter(uint len) { pFIR = FIRFilter::newInstance(); cutoffFreq = 0.5; setLength(len); } AAFilter::~AAFilter() { delete pFIR; } // Sets new anti-alias filter cut-off edge frequency, scaled to // sampling frequency (nyquist frequency = 0.5). // The filter will cut frequencies higher than the given frequency. void AAFilter::setCutoffFreq(double newCutoffFreq) { cutoffFreq = newCutoffFreq; calculateCoeffs(); } // Sets number of FIR filter taps void AAFilter::setLength(uint newLength) { length = newLength; calculateCoeffs(); } // Calculates coefficients for a low-pass FIR filter using Hamming window void AAFilter::calculateCoeffs() { uint i; double cntTemp, temp, tempCoeff,h, w; double wc; double scaleCoeff, sum; double *work; SAMPLETYPE *coeffs; assert(length >= 2); assert(length % 4 == 0); assert(cutoffFreq >= 0); assert(cutoffFreq <= 0.5); work = new double[length]; coeffs = new SAMPLETYPE[length]; wc = 2.0 * PI * cutoffFreq; tempCoeff = TWOPI / (double)length; sum = 0; for (i = 0; i < length; i ++) { cntTemp = (double)i - (double)(length / 2); temp = cntTemp * wc; if (temp != 0) { h = sin(temp) / temp; // sinc function } else { h = 1.0; } w = 0.54 + 0.46 * cos(tempCoeff * cntTemp); // hamming window temp = w * h; work[i] = temp; // calc net sum of coefficients sum += temp; } // ensure the sum of coefficients is larger than zero assert(sum > 0); // ensure we've really designed a lowpass filter... assert(work[length/2] > 0); assert(work[length/2 + 1] > -1e-6); assert(work[length/2 - 1] > -1e-6); // Calculate a scaling coefficient in such a way that the result can be // divided by 16384 scaleCoeff = 16384.0f / sum; for (i = 0; i < length; i ++) { temp = work[i] * scaleCoeff; //#if SOUNDTOUCH_INTEGER_SAMPLES // scale & round to nearest integer temp += (temp >= 0) ? 0.5 : -0.5; // ensure no overfloods assert(temp >= -32768 && temp <= 32767); //#endif coeffs[i] = (SAMPLETYPE)temp; } // Set coefficients. Use divide factor 14 => divide result by 2^14 = 16384 pFIR->setCoefficients(coeffs, length, 14); _DEBUG_SAVE_AAFIR_COEFFS(coeffs, length); delete[] work; delete[] coeffs; } // Applies the filter to the given sequence of samples. // Note : The amount of outputted samples is by value of 'filter length' // smaller than the amount of input samples. uint AAFilter::evaluate(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels) const { return pFIR->evaluate(dest, src, numSamples, numChannels); } /// Applies the filter to the given src & dest pipes, so that processed amount of /// samples get removed from src, and produced amount added to dest /// Note : The amount of outputted samples is by value of 'filter length' /// smaller than the amount of input samples. uint AAFilter::evaluate(FIFOSampleBuffer &dest, FIFOSampleBuffer &src) const { SAMPLETYPE *pdest; const SAMPLETYPE *psrc; uint numSrcSamples; uint result; int numChannels = src.getChannels(); assert(numChannels == dest.getChannels()); numSrcSamples = src.numSamples(); psrc = src.ptrBegin(); pdest = dest.ptrEnd(numSrcSamples); result = pFIR->evaluate(pdest, psrc, numSrcSamples, numChannels); src.receiveSamples(result); dest.putSamples(result); return result; } uint AAFilter::getLength() const { return pFIR->getLength(); } soundtouch/source/SoundTouch/AAFilter.h0000664000175100017510000000666212577461413017520 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Sampled sound tempo changer/time stretch algorithm. Changes the sound tempo /// while maintaining the original pitch by using a time domain WSOLA-like method /// with several performance-increasing tweaks. /// /// Anti-alias filter is used to prevent folding of high frequencies when /// transposing the sample rate with interpolation. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2014-01-07 21:41:23 +0200 (Tue, 07 Jan 2014) $ // File revision : $Revision: 4 $ // // $Id: AAFilter.h 187 2014-01-07 19:41:23Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef AAFilter_H #define AAFilter_H #include "STTypes.h" #include "FIFOSampleBuffer.h" namespace soundtouch { class AAFilter { protected: class FIRFilter *pFIR; /// Low-pass filter cut-off frequency, negative = invalid double cutoffFreq; /// num of filter taps uint length; /// Calculate the FIR coefficients realizing the given cutoff-frequency void calculateCoeffs(); public: AAFilter(uint length); ~AAFilter(); /// Sets new anti-alias filter cut-off edge frequency, scaled to sampling /// frequency (nyquist frequency = 0.5). The filter will cut off the /// frequencies than that. void setCutoffFreq(double newCutoffFreq); /// Sets number of FIR filter taps, i.e. ~filter complexity void setLength(uint newLength); uint getLength() const; /// Applies the filter to the given sequence of samples. /// Note : The amount of outputted samples is by value of 'filter length' /// smaller than the amount of input samples. uint evaluate(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSamples, uint numChannels) const; /// Applies the filter to the given src & dest pipes, so that processed amount of /// samples get removed from src, and produced amount added to dest /// Note : The amount of outputted samples is by value of 'filter length' /// smaller than the amount of input samples. uint evaluate(FIFOSampleBuffer &dest, FIFOSampleBuffer &src) const; }; } #endif soundtouch/source/SoundTouch/cpu_detect.h0000664000175100017510000000452212577461413020201 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// A header file for detecting the Intel MMX instructions set extension. /// /// Please see 'mmx_win.cpp', 'mmx_cpp.cpp' and 'mmx_non_x86.cpp' for the /// routine implementations for x86 Windows, x86 gnu version and non-x86 /// platforms, respectively. /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2008-02-10 18:26:55 +0200 (Sun, 10 Feb 2008) $ // File revision : $Revision: 4 $ // // $Id: cpu_detect.h 11 2008-02-10 16:26:55Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef _CPU_DETECT_H_ #define _CPU_DETECT_H_ #include "STTypes.h" #define SUPPORT_MMX 0x0001 #define SUPPORT_3DNOW 0x0002 #define SUPPORT_ALTIVEC 0x0004 #define SUPPORT_SSE 0x0008 #define SUPPORT_SSE2 0x0010 /// Checks which instruction set extensions are supported by the CPU. /// /// \return A bitmask of supported extensions, see SUPPORT_... defines. uint detectCPUextensions(void); /// Disables given set of instruction extensions. See SUPPORT_... defines. void disableExtensions(uint wDisableMask); #endif // _CPU_DETECT_H_ soundtouch/source/SoundTouch/RateTransposer.cpp0000664000175100017510000002003012577461413021361 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Sample rate transposer. Changes sample rate by using linear interpolation /// together with anti-alias filtering (first order interpolation with anti- /// alias filtering should be quite adequate for this application) /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2015-07-26 17:45:48 +0300 (Sun, 26 Jul 2015) $ // File revision : $Revision: 4 $ // // $Id: RateTransposer.cpp 225 2015-07-26 14:45:48Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include "RateTransposer.h" #include "InterpolateLinear.h" #include "InterpolateCubic.h" #include "InterpolateShannon.h" #include "AAFilter.h" using namespace soundtouch; // Define default interpolation algorithm here TransposerBase::ALGORITHM TransposerBase::algorithm = TransposerBase::CUBIC; // Constructor RateTransposer::RateTransposer() : FIFOProcessor(&outputBuffer) { bUseAAFilter = true; // Instantiates the anti-alias filter pAAFilter = new AAFilter(64); pTransposer = TransposerBase::newInstance(); } RateTransposer::~RateTransposer() { delete pAAFilter; delete pTransposer; } /// Enables/disables the anti-alias filter. Zero to disable, nonzero to enable void RateTransposer::enableAAFilter(bool newMode) { bUseAAFilter = newMode; } /// Returns nonzero if anti-alias filter is enabled. bool RateTransposer::isAAFilterEnabled() const { return bUseAAFilter; } AAFilter *RateTransposer::getAAFilter() { return pAAFilter; } // Sets new target iRate. Normal iRate = 1.0, smaller values represent slower // iRate, larger faster iRates. void RateTransposer::setRate(double newRate) { double fCutoff; pTransposer->setRate(newRate); // design a new anti-alias filter if (newRate > 1.0) { fCutoff = 0.5 / newRate; } else { fCutoff = 0.5 * newRate; } pAAFilter->setCutoffFreq(fCutoff); } // Adds 'nSamples' pcs of samples from the 'samples' memory position into // the input of the object. void RateTransposer::putSamples(const SAMPLETYPE *samples, uint nSamples) { processSamples(samples, nSamples); } // Transposes sample rate by applying anti-alias filter to prevent folding. // Returns amount of samples returned in the "dest" buffer. // The maximum amount of samples that can be returned at a time is set by // the 'set_returnBuffer_size' function. void RateTransposer::processSamples(const SAMPLETYPE *src, uint nSamples) { uint count; if (nSamples == 0) return; // Store samples to input buffer inputBuffer.putSamples(src, nSamples); // If anti-alias filter is turned off, simply transpose without applying // the filter if (bUseAAFilter == false) { count = pTransposer->transpose(outputBuffer, inputBuffer); return; } assert(pAAFilter); // Transpose with anti-alias filter if (pTransposer->rate < 1.0f) { // If the parameter 'Rate' value is smaller than 1, first transpose // the samples and then apply the anti-alias filter to remove aliasing. // Transpose the samples, store the result to end of "midBuffer" pTransposer->transpose(midBuffer, inputBuffer); // Apply the anti-alias filter for transposed samples in midBuffer pAAFilter->evaluate(outputBuffer, midBuffer); } else { // If the parameter 'Rate' value is larger than 1, first apply the // anti-alias filter to remove high frequencies (prevent them from folding // over the lover frequencies), then transpose. // Apply the anti-alias filter for samples in inputBuffer pAAFilter->evaluate(midBuffer, inputBuffer); // Transpose the AA-filtered samples in "midBuffer" pTransposer->transpose(outputBuffer, midBuffer); } } // Sets the number of channels, 1 = mono, 2 = stereo void RateTransposer::setChannels(int nChannels) { assert(nChannels > 0); if (pTransposer->numChannels == nChannels) return; pTransposer->setChannels(nChannels); inputBuffer.setChannels(nChannels); midBuffer.setChannels(nChannels); outputBuffer.setChannels(nChannels); } // Clears all the samples in the object void RateTransposer::clear() { outputBuffer.clear(); midBuffer.clear(); inputBuffer.clear(); } // Returns nonzero if there aren't any samples available for outputting. int RateTransposer::isEmpty() const { int res; res = FIFOProcessor::isEmpty(); if (res == 0) return 0; return inputBuffer.isEmpty(); } ////////////////////////////////////////////////////////////////////////////// // // TransposerBase - Base class for interpolation // // static function to set interpolation algorithm void TransposerBase::setAlgorithm(TransposerBase::ALGORITHM a) { TransposerBase::algorithm = a; } // Transposes the sample rate of the given samples using linear interpolation. // Returns the number of samples returned in the "dest" buffer int TransposerBase::transpose(FIFOSampleBuffer &dest, FIFOSampleBuffer &src) { int numSrcSamples = src.numSamples(); int sizeDemand = (int)((double)numSrcSamples / rate) + 8; int numOutput; SAMPLETYPE *psrc = src.ptrBegin(); SAMPLETYPE *pdest = dest.ptrEnd(sizeDemand); #ifndef USE_MULTICH_ALWAYS if (numChannels == 1) { numOutput = transposeMono(pdest, psrc, numSrcSamples); } else if (numChannels == 2) { numOutput = transposeStereo(pdest, psrc, numSrcSamples); } else #endif // USE_MULTICH_ALWAYS { assert(numChannels > 0); numOutput = transposeMulti(pdest, psrc, numSrcSamples); } dest.putSamples(numOutput); src.receiveSamples(numSrcSamples); return numOutput; } TransposerBase::TransposerBase() { numChannels = 0; rate = 1.0f; } TransposerBase::~TransposerBase() { } void TransposerBase::setChannels(int channels) { numChannels = channels; resetRegisters(); } void TransposerBase::setRate(double newRate) { rate = newRate; } // static factory function TransposerBase *TransposerBase::newInstance() { #ifdef SOUNDTOUCH_INTEGER_SAMPLES // Notice: For integer arithmetics support only linear algorithm (due to simplest calculus) return ::new InterpolateLinearInteger; #else switch (algorithm) { case LINEAR: return new InterpolateLinearFloat; case CUBIC: return new InterpolateCubic; case SHANNON: return new InterpolateShannon; default: assert(false); return NULL; } #endif } soundtouch/source/SoundTouch/SoundTouch.dsp0000664000175100017510000001116112577461413020511 0ustar vmdevvmdev# Microsoft Developer Studio Project File - Name="SoundTouch" - Package Owner=<4> # Microsoft Developer Studio Generated Build File, Format Version 6.00 # ** DO NOT EDIT ** # TARGTYPE "Win32 (x86) Static Library" 0x0104 CFG=SoundTouch - Win32 Debug !MESSAGE This is not a valid makefile. To build this project using NMAKE, !MESSAGE use the Export Makefile command and run !MESSAGE !MESSAGE NMAKE /f "SoundTouch.mak". !MESSAGE !MESSAGE You can specify a configuration when running NMAKE !MESSAGE by defining the macro CFG on the command line. For example: !MESSAGE !MESSAGE NMAKE /f "SoundTouch.mak" CFG="SoundTouch - Win32 Debug" !MESSAGE !MESSAGE Possible choices for configuration are: !MESSAGE !MESSAGE "SoundTouch - Win32 Release" (based on "Win32 (x86) Static Library") !MESSAGE "SoundTouch - Win32 Debug" (based on "Win32 (x86) Static Library") !MESSAGE # Begin Project # PROP AllowPerConfigDependencies 0 # PROP Scc_ProjName "" # PROP Scc_LocalPath "" CPP=cl.exe RSC=rc.exe !IF "$(CFG)" == "SoundTouch - Win32 Release" # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 0 # PROP BASE Output_Dir "Release" # PROP BASE Intermediate_Dir "Release" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 0 # PROP Output_Dir "Release" # PROP Intermediate_Dir "Release" # PROP Target_Dir "" # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c # ADD CPP /nologo /W3 /GX /Zi /O2 /I "..\..\include" /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c # ADD BASE RSC /l 0x40b /d "NDEBUG" # ADD RSC /l 0x40b /d "NDEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LIB32=link.exe -lib # ADD BASE LIB32 /nologo # ADD LIB32 /nologo # Begin Special Build Tool SOURCE="$(InputPath)" PostBuild_Cmds=copy .\Release\SoundTouch.lib ..\..\lib\ # End Special Build Tool !ELSEIF "$(CFG)" == "SoundTouch - Win32 Debug" # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 1 # PROP BASE Output_Dir "Debug" # PROP BASE Intermediate_Dir "Debug" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 1 # PROP Output_Dir "Debug" # PROP Intermediate_Dir "Debug" # PROP Target_Dir "" # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c # ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "..\..\include" /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /FR /YX /FD /GZ /c # ADD BASE RSC /l 0x40b /d "_DEBUG" # ADD RSC /l 0x40b /d "_DEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LIB32=link.exe -lib # ADD BASE LIB32 /nologo # ADD LIB32 /nologo /out:"Debug\SoundTouchD.lib" # Begin Special Build Tool SOURCE="$(InputPath)" PostBuild_Cmds=copy .\Debug\SoundTouchD.lib ..\..\lib\ # End Special Build Tool !ENDIF # Begin Target # Name "SoundTouch - Win32 Release" # Name "SoundTouch - Win32 Debug" # Begin Group "Source Files" # PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" # Begin Group "bpm" # PROP Default_Filter "" # Begin Source File SOURCE=.\BPMDetect.cpp # End Source File # Begin Source File SOURCE=.\PeakFinder.cpp # End Source File # End Group # Begin Source File SOURCE=.\3dnow_win.cpp # End Source File # Begin Source File SOURCE=.\AAFilter.cpp # End Source File # Begin Source File SOURCE=.\cpu_detect_x86_win.cpp # End Source File # Begin Source File SOURCE=.\FIFOSampleBuffer.cpp # End Source File # Begin Source File SOURCE=.\FIRFilter.cpp # End Source File # Begin Source File SOURCE=.\mmx_optimized.cpp # End Source File # Begin Source File SOURCE=.\RateTransposer.cpp # End Source File # Begin Source File SOURCE=.\SoundTouch.cpp # End Source File # Begin Source File SOURCE=.\sse_optimized.cpp # End Source File # Begin Source File SOURCE=.\TDStretch.cpp # End Source File # End Group # Begin Group "Header Files" # PROP Default_Filter "h;hpp;hxx;hm;inl" # Begin Source File SOURCE=.\AAFilter.h # End Source File # Begin Source File SOURCE=.\cpu_detect.h # End Source File # Begin Source File SOURCE=..\..\include\FIFOSampleBuffer.h # End Source File # Begin Source File SOURCE=..\..\include\FIFOSamplePipe.h # End Source File # Begin Source File SOURCE=.\FIRFilter.h # End Source File # Begin Source File SOURCE=.\RateTransposer.h # End Source File # Begin Source File SOURCE=..\..\include\SoundTouch.h # End Source File # Begin Source File SOURCE=..\..\include\STTypes.h # End Source File # Begin Source File SOURCE=.\TDStretch.h # End Source File # End Group # End Target # End Project soundtouch/source/SoundTouch/TDStretch.h0000664000175100017510000002563212577461413017733 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Sampled sound tempo changer/time stretch algorithm. Changes the sound tempo /// while maintaining the original pitch by using a time domain WSOLA-like method /// with several performance-increasing tweaks. /// /// Note : MMX/SSE optimized functions reside in separate, platform-specific files /// 'mmx_optimized.cpp' and 'sse_optimized.cpp' /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2015-08-09 00:00:15 +0300 (Sun, 09 Aug 2015) $ // File revision : $Revision: 4 $ // // $Id: TDStretch.h 226 2015-08-08 21:00:15Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #ifndef TDStretch_H #define TDStretch_H #include #include "STTypes.h" #include "RateTransposer.h" #include "FIFOSamplePipe.h" namespace soundtouch { /// Default values for sound processing parameters: /// Notice that the default parameters are tuned for contemporary popular music /// processing. For speech processing applications these parameters suit better: /// #define DEFAULT_SEQUENCE_MS 40 /// #define DEFAULT_SEEKWINDOW_MS 15 /// #define DEFAULT_OVERLAP_MS 8 /// /// Default length of a single processing sequence, in milliseconds. This determines to how /// long sequences the original sound is chopped in the time-stretch algorithm. /// /// The larger this value is, the lesser sequences are used in processing. In principle /// a bigger value sounds better when slowing down tempo, but worse when increasing tempo /// and vice versa. /// /// Increasing this value reduces computational burden & vice versa. //#define DEFAULT_SEQUENCE_MS 40 #define DEFAULT_SEQUENCE_MS USE_AUTO_SEQUENCE_LEN /// Giving this value for the sequence length sets automatic parameter value /// according to tempo setting (recommended) #define USE_AUTO_SEQUENCE_LEN 0 /// Seeking window default length in milliseconds for algorithm that finds the best possible /// overlapping location. This determines from how wide window the algorithm may look for an /// optimal joining location when mixing the sound sequences back together. /// /// The bigger this window setting is, the higher the possibility to find a better mixing /// position will become, but at the same time large values may cause a "drifting" artifact /// because consequent sequences will be taken at more uneven intervals. /// /// If there's a disturbing artifact that sounds as if a constant frequency was drifting /// around, try reducing this setting. /// /// Increasing this value increases computational burden & vice versa. //#define DEFAULT_SEEKWINDOW_MS 15 #define DEFAULT_SEEKWINDOW_MS USE_AUTO_SEEKWINDOW_LEN /// Giving this value for the seek window length sets automatic parameter value /// according to tempo setting (recommended) #define USE_AUTO_SEEKWINDOW_LEN 0 /// Overlap length in milliseconds. When the chopped sound sequences are mixed back together, /// to form a continuous sound stream, this parameter defines over how long period the two /// consecutive sequences are let to overlap each other. /// /// This shouldn't be that critical parameter. If you reduce the DEFAULT_SEQUENCE_MS setting /// by a large amount, you might wish to try a smaller value on this. /// /// Increasing this value increases computational burden & vice versa. #define DEFAULT_OVERLAP_MS 8 /// Class that does the time-stretch (tempo change) effect for the processed /// sound. class TDStretch : public FIFOProcessor { protected: int channels; int sampleReq; int overlapLength; int seekLength; int seekWindowLength; int overlapDividerBitsNorm; int overlapDividerBitsPure; int slopingDivider; int sampleRate; int sequenceMs; int seekWindowMs; int overlapMs; unsigned long maxnorm; float maxnormf; double tempo; double nominalSkip; double skipFract; bool bQuickSeek; bool bAutoSeqSetting; bool bAutoSeekSetting; SAMPLETYPE *pMidBuffer; SAMPLETYPE *pMidBufferUnaligned; FIFOSampleBuffer outputBuffer; FIFOSampleBuffer inputBuffer; void acceptNewOverlapLength(int newOverlapLength); virtual void clearCrossCorrState(); void calculateOverlapLength(int overlapMs); virtual double calcCrossCorr(const SAMPLETYPE *mixingPos, const SAMPLETYPE *compare, double &norm); virtual double calcCrossCorrAccumulate(const SAMPLETYPE *mixingPos, const SAMPLETYPE *compare, double &norm); virtual int seekBestOverlapPositionFull(const SAMPLETYPE *refPos); virtual int seekBestOverlapPositionQuick(const SAMPLETYPE *refPos); virtual int seekBestOverlapPosition(const SAMPLETYPE *refPos); virtual void overlapStereo(SAMPLETYPE *output, const SAMPLETYPE *input) const; virtual void overlapMono(SAMPLETYPE *output, const SAMPLETYPE *input) const; virtual void overlapMulti(SAMPLETYPE *output, const SAMPLETYPE *input) const; void clearMidBuffer(); void overlap(SAMPLETYPE *output, const SAMPLETYPE *input, uint ovlPos) const; void calcSeqParameters(); void adaptNormalizer(); /// Changes the tempo of the given sound samples. /// Returns amount of samples returned in the "output" buffer. /// The maximum amount of samples that can be returned at a time is set by /// the 'set_returnBuffer_size' function. void processSamples(); public: TDStretch(); virtual ~TDStretch(); /// Operator 'new' is overloaded so that it automatically creates a suitable instance /// depending on if we've a MMX/SSE/etc-capable CPU available or not. static void *operator new(size_t s); /// Use this function instead of "new" operator to create a new instance of this class. /// This function automatically chooses a correct feature set depending on if the CPU /// supports MMX/SSE/etc extensions. static TDStretch *newInstance(); /// Returns the output buffer object FIFOSamplePipe *getOutput() { return &outputBuffer; }; /// Returns the input buffer object FIFOSamplePipe *getInput() { return &inputBuffer; }; /// Sets new target tempo. Normal tempo = 'SCALE', smaller values represent slower /// tempo, larger faster tempo. void setTempo(double newTempo); /// Returns nonzero if there aren't any samples available for outputting. virtual void clear(); /// Clears the input buffer void clearInput(); /// Sets the number of channels, 1 = mono, 2 = stereo void setChannels(int numChannels); /// Enables/disables the quick position seeking algorithm. Zero to disable, /// nonzero to enable void enableQuickSeek(bool enable); /// Returns nonzero if the quick seeking algorithm is enabled. bool isQuickSeekEnabled() const; /// Sets routine control parameters. These control are certain time constants /// defining how the sound is stretched to the desired duration. // /// 'sampleRate' = sample rate of the sound /// 'sequenceMS' = one processing sequence length in milliseconds /// 'seekwindowMS' = seeking window length for scanning the best overlapping /// position /// 'overlapMS' = overlapping length void setParameters(int sampleRate, ///< Samplerate of sound being processed (Hz) int sequenceMS = -1, ///< Single processing sequence length (ms) int seekwindowMS = -1, ///< Offset seeking window length (ms) int overlapMS = -1 ///< Sequence overlapping length (ms) ); /// Get routine control parameters, see setParameters() function. /// Any of the parameters to this function can be NULL, in such case corresponding parameter /// value isn't returned. void getParameters(int *pSampleRate, int *pSequenceMs, int *pSeekWindowMs, int *pOverlapMs) const; /// Adds 'numsamples' pcs of samples from the 'samples' memory position into /// the input of the object. virtual void putSamples( const SAMPLETYPE *samples, ///< Input sample data uint numSamples ///< Number of samples in 'samples' so that one sample ///< contains both channels if stereo ); /// return nominal input sample requirement for triggering a processing batch int getInputSampleReq() const { return (int)(nominalSkip + 0.5); } /// return nominal output sample amount when running a processing batch int getOutputBatchSize() const { return seekWindowLength - overlapLength; } }; // Implementation-specific class declarations: #ifdef SOUNDTOUCH_ALLOW_MMX /// Class that implements MMX optimized routines for 16bit integer samples type. class TDStretchMMX : public TDStretch { protected: double calcCrossCorr(const short *mixingPos, const short *compare, double &norm); double calcCrossCorrAccumulate(const short *mixingPos, const short *compare, double &norm); virtual void overlapStereo(short *output, const short *input) const; virtual void clearCrossCorrState(); }; #endif /// SOUNDTOUCH_ALLOW_MMX #ifdef SOUNDTOUCH_ALLOW_SSE /// Class that implements SSE optimized routines for floating point samples type. class TDStretchSSE : public TDStretch { protected: double calcCrossCorr(const float *mixingPos, const float *compare, double &norm); double calcCrossCorrAccumulate(const float *mixingPos, const float *compare, double &norm); }; #endif /// SOUNDTOUCH_ALLOW_SSE } #endif /// TDStretch_H soundtouch/source/SoundTouch/SoundTouch.sln0000664000175100017510000000250012577461413020514 0ustar vmdevvmdevMicrosoft Visual Studio Solution File, Format Version 10.00 # Visual Studio 2008 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SoundTouch", "SoundTouch.vcproj", "{68A5DD20-7057-448B-8FE0-B6AC8D205509}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 Debug|x64 = Debug|x64 Release|Win32 = Release|Win32 Release|x64 = Release|x64 ReleaseX64|Win32 = ReleaseX64|Win32 ReleaseX64|x64 = ReleaseX64|x64 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {68A5DD20-7057-448B-8FE0-B6AC8D205509}.Debug|Win32.ActiveCfg = Debug|Win32 {68A5DD20-7057-448B-8FE0-B6AC8D205509}.Debug|x64.ActiveCfg = Debug|x64 {68A5DD20-7057-448B-8FE0-B6AC8D205509}.Release|Win32.ActiveCfg = Release|Win32 {68A5DD20-7057-448B-8FE0-B6AC8D205509}.Release|x64.ActiveCfg = Release|x64 {68A5DD20-7057-448B-8FE0-B6AC8D205509}.ReleaseX64|Win32.ActiveCfg = ReleaseX64|Win32 {68A5DD20-7057-448B-8FE0-B6AC8D205509}.ReleaseX64|Win32.Build.0 = ReleaseX64|Win32 {68A5DD20-7057-448B-8FE0-B6AC8D205509}.ReleaseX64|x64.ActiveCfg = ReleaseX64|x64 {68A5DD20-7057-448B-8FE0-B6AC8D205509}.ReleaseX64|x64.Build.0 = ReleaseX64|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection EndGlobal soundtouch/source/SoundTouch/mmx_optimized.cpp0000664000175100017510000003300712577461413021302 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// MMX optimized routines. All MMX optimized functions have been gathered into /// this single source code file, regardless to their class or original source /// code file, in order to ease porting the library to other compiler and /// processor platforms. /// /// The MMX-optimizations are programmed using MMX compiler intrinsics that /// are supported both by Microsoft Visual C++ and GCC compilers, so this file /// should compile with both toolsets. /// /// NOTICE: If using Visual Studio 6.0, you'll need to install the "Visual C++ /// 6.0 processor pack" update to support compiler intrinsic syntax. The update /// is available for download at Microsoft Developers Network, see here: /// http://msdn.microsoft.com/en-us/vstudio/aa718349.aspx /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// SoundTouch WWW: http://www.surina.net/soundtouch /// //////////////////////////////////////////////////////////////////////////////// // // Last changed : $Date: 2015-08-09 00:00:15 +0300 (Sun, 09 Aug 2015) $ // File revision : $Revision: 4 $ // // $Id: mmx_optimized.cpp 226 2015-08-08 21:00:15Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// // // License : // // SoundTouch audio processing library // Copyright (c) Olli Parviainen // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // //////////////////////////////////////////////////////////////////////////////// #include "STTypes.h" #ifdef SOUNDTOUCH_ALLOW_MMX // MMX routines available only with integer sample type using namespace soundtouch; ////////////////////////////////////////////////////////////////////////////// // // implementation of MMX optimized functions of class 'TDStretchMMX' // ////////////////////////////////////////////////////////////////////////////// #include "TDStretch.h" #include #include #include // Calculates cross correlation of two buffers double TDStretchMMX::calcCrossCorr(const short *pV1, const short *pV2, double &dnorm) { const __m64 *pVec1, *pVec2; __m64 shifter; __m64 accu, normaccu; long corr, norm; int i; pVec1 = (__m64*)pV1; pVec2 = (__m64*)pV2; shifter = _m_from_int(overlapDividerBitsNorm); normaccu = accu = _mm_setzero_si64(); // Process 4 parallel sets of 2 * stereo samples or 4 * mono samples // during each round for improved CPU-level parallellization. for (i = 0; i < channels * overlapLength / 16; i ++) { __m64 temp, temp2; // dictionary of instructions: // _m_pmaddwd : 4*16bit multiply-add, resulting two 32bits = [a0*b0+a1*b1 ; a2*b2+a3*b3] // _mm_add_pi32 : 2*32bit add // _m_psrad : 32bit right-shift temp = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[0], pVec2[0]), shifter), _mm_sra_pi32(_mm_madd_pi16(pVec1[1], pVec2[1]), shifter)); temp2 = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[0], pVec1[0]), shifter), _mm_sra_pi32(_mm_madd_pi16(pVec1[1], pVec1[1]), shifter)); accu = _mm_add_pi32(accu, temp); normaccu = _mm_add_pi32(normaccu, temp2); temp = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[2], pVec2[2]), shifter), _mm_sra_pi32(_mm_madd_pi16(pVec1[3], pVec2[3]), shifter)); temp2 = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[2], pVec1[2]), shifter), _mm_sra_pi32(_mm_madd_pi16(pVec1[3], pVec1[3]), shifter)); accu = _mm_add_pi32(accu, temp); normaccu = _mm_add_pi32(normaccu, temp2); pVec1 += 4; pVec2 += 4; } // copy hi-dword of mm0 to lo-dword of mm1, then sum mmo+mm1 // and finally store the result into the variable "corr" accu = _mm_add_pi32(accu, _mm_srli_si64(accu, 32)); corr = _m_to_int(accu); normaccu = _mm_add_pi32(normaccu, _mm_srli_si64(normaccu, 32)); norm = _m_to_int(normaccu); // Clear MMS state _m_empty(); if (norm > (long)maxnorm) { maxnorm = norm; } // Normalize result by dividing by sqrt(norm) - this step is easiest // done using floating point operation dnorm = (double)norm; return (double)corr / sqrt(dnorm < 1e-9 ? 1.0 : dnorm); // Note: Warning about the missing EMMS instruction is harmless // as it'll be called elsewhere. } /// Update cross-correlation by accumulating "norm" coefficient by previously calculated value double TDStretchMMX::calcCrossCorrAccumulate(const short *pV1, const short *pV2, double &dnorm) { const __m64 *pVec1, *pVec2; __m64 shifter; __m64 accu; long corr, lnorm; int i; // cancel first normalizer tap from previous round lnorm = 0; for (i = 1; i <= channels; i ++) { lnorm -= (pV1[-i] * pV1[-i]) >> overlapDividerBitsNorm; } pVec1 = (__m64*)pV1; pVec2 = (__m64*)pV2; shifter = _m_from_int(overlapDividerBitsNorm); accu = _mm_setzero_si64(); // Process 4 parallel sets of 2 * stereo samples or 4 * mono samples // during each round for improved CPU-level parallellization. for (i = 0; i < channels * overlapLength / 16; i ++) { __m64 temp; // dictionary of instructions: // _m_pmaddwd : 4*16bit multiply-add, resulting two 32bits = [a0*b0+a1*b1 ; a2*b2+a3*b3] // _mm_add_pi32 : 2*32bit add // _m_psrad : 32bit right-shift temp = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[0], pVec2[0]), shifter), _mm_sra_pi32(_mm_madd_pi16(pVec1[1], pVec2[1]), shifter)); accu = _mm_add_pi32(accu, temp); temp = _mm_add_pi32(_mm_sra_pi32(_mm_madd_pi16(pVec1[2], pVec2[2]), shifter), _mm_sra_pi32(_mm_madd_pi16(pVec1[3], pVec2[3]), shifter)); accu = _mm_add_pi32(accu, temp); pVec1 += 4; pVec2 += 4; } // copy hi-dword of mm0 to lo-dword of mm1, then sum mmo+mm1 // and finally store the result into the variable "corr" accu = _mm_add_pi32(accu, _mm_srli_si64(accu, 32)); corr = _m_to_int(accu); // Clear MMS state _m_empty(); // update normalizer with last samples of this round pV1 = (short *)pVec1; for (int j = 1; j <= channels; j ++) { lnorm += (pV1[-j] * pV1[-j]) >> overlapDividerBitsNorm; } dnorm += (double)lnorm; if (lnorm > (long)maxnorm) { maxnorm = lnorm; } // Normalize result by dividing by sqrt(norm) - this step is easiest // done using floating point operation return (double)corr / sqrt((dnorm < 1e-9) ? 1.0 : dnorm); } void TDStretchMMX::clearCrossCorrState() { // Clear MMS state _m_empty(); //_asm EMMS; } // MMX-optimized version of the function overlapStereo void TDStretchMMX::overlapStereo(short *output, const short *input) const { const __m64 *pVinput, *pVMidBuf; __m64 *pVdest; __m64 mix1, mix2, adder, shifter; int i; pVinput = (const __m64*)input; pVMidBuf = (const __m64*)pMidBuffer; pVdest = (__m64*)output; // mix1 = mixer values for 1st stereo sample // mix1 = mixer values for 2nd stereo sample // adder = adder for updating mixer values after each round mix1 = _mm_set_pi16(0, overlapLength, 0, overlapLength); adder = _mm_set_pi16(1, -1, 1, -1); mix2 = _mm_add_pi16(mix1, adder); adder = _mm_add_pi16(adder, adder); // Overlaplength-division by shifter. "+1" is to account for "-1" deduced in // overlapDividerBits calculation earlier. shifter = _m_from_int(overlapDividerBitsPure + 1); for (i = 0; i < overlapLength / 4; i ++) { __m64 temp1, temp2; // load & shuffle data so that input & mixbuffer data samples are paired temp1 = _mm_unpacklo_pi16(pVMidBuf[0], pVinput[0]); // = i0l m0l i0r m0r temp2 = _mm_unpackhi_pi16(pVMidBuf[0], pVinput[0]); // = i1l m1l i1r m1r // temp = (temp .* mix) >> shifter temp1 = _mm_sra_pi32(_mm_madd_pi16(temp1, mix1), shifter); temp2 = _mm_sra_pi32(_mm_madd_pi16(temp2, mix2), shifter); pVdest[0] = _mm_packs_pi32(temp1, temp2); // pack 2*2*32bit => 4*16bit // update mix += adder mix1 = _mm_add_pi16(mix1, adder); mix2 = _mm_add_pi16(mix2, adder); // --- second round begins here --- // load & shuffle data so that input & mixbuffer data samples are paired temp1 = _mm_unpacklo_pi16(pVMidBuf[1], pVinput[1]); // = i2l m2l i2r m2r temp2 = _mm_unpackhi_pi16(pVMidBuf[1], pVinput[1]); // = i3l m3l i3r m3r // temp = (temp .* mix) >> shifter temp1 = _mm_sra_pi32(_mm_madd_pi16(temp1, mix1), shifter); temp2 = _mm_sra_pi32(_mm_madd_pi16(temp2, mix2), shifter); pVdest[1] = _mm_packs_pi32(temp1, temp2); // pack 2*2*32bit => 4*16bit // update mix += adder mix1 = _mm_add_pi16(mix1, adder); mix2 = _mm_add_pi16(mix2, adder); pVinput += 2; pVMidBuf += 2; pVdest += 2; } _m_empty(); // clear MMS state } ////////////////////////////////////////////////////////////////////////////// // // implementation of MMX optimized functions of class 'FIRFilter' // ////////////////////////////////////////////////////////////////////////////// #include "FIRFilter.h" FIRFilterMMX::FIRFilterMMX() : FIRFilter() { filterCoeffsAlign = NULL; filterCoeffsUnalign = NULL; } FIRFilterMMX::~FIRFilterMMX() { delete[] filterCoeffsUnalign; } // (overloaded) Calculates filter coefficients for MMX routine void FIRFilterMMX::setCoefficients(const short *coeffs, uint newLength, uint uResultDivFactor) { uint i; FIRFilter::setCoefficients(coeffs, newLength, uResultDivFactor); // Ensure that filter coeffs array is aligned to 16-byte boundary delete[] filterCoeffsUnalign; filterCoeffsUnalign = new short[2 * newLength + 8]; filterCoeffsAlign = (short *)SOUNDTOUCH_ALIGN_POINTER_16(filterCoeffsUnalign); // rearrange the filter coefficients for mmx routines for (i = 0;i < length; i += 4) { filterCoeffsAlign[2 * i + 0] = coeffs[i + 0]; filterCoeffsAlign[2 * i + 1] = coeffs[i + 2]; filterCoeffsAlign[2 * i + 2] = coeffs[i + 0]; filterCoeffsAlign[2 * i + 3] = coeffs[i + 2]; filterCoeffsAlign[2 * i + 4] = coeffs[i + 1]; filterCoeffsAlign[2 * i + 5] = coeffs[i + 3]; filterCoeffsAlign[2 * i + 6] = coeffs[i + 1]; filterCoeffsAlign[2 * i + 7] = coeffs[i + 3]; } } // mmx-optimized version of the filter routine for stereo sound uint FIRFilterMMX::evaluateFilterStereo(short *dest, const short *src, uint numSamples) const { // Create stack copies of the needed member variables for asm routines : uint i, j; __m64 *pVdest = (__m64*)dest; if (length < 2) return 0; for (i = 0; i < (numSamples - length) / 2; i ++) { __m64 accu1; __m64 accu2; const __m64 *pVsrc = (const __m64*)src; const __m64 *pVfilter = (const __m64*)filterCoeffsAlign; accu1 = accu2 = _mm_setzero_si64(); for (j = 0; j < lengthDiv8 * 2; j ++) { __m64 temp1, temp2; temp1 = _mm_unpacklo_pi16(pVsrc[0], pVsrc[1]); // = l2 l0 r2 r0 temp2 = _mm_unpackhi_pi16(pVsrc[0], pVsrc[1]); // = l3 l1 r3 r1 accu1 = _mm_add_pi32(accu1, _mm_madd_pi16(temp1, pVfilter[0])); // += l2*f2+l0*f0 r2*f2+r0*f0 accu1 = _mm_add_pi32(accu1, _mm_madd_pi16(temp2, pVfilter[1])); // += l3*f3+l1*f1 r3*f3+r1*f1 temp1 = _mm_unpacklo_pi16(pVsrc[1], pVsrc[2]); // = l4 l2 r4 r2 accu2 = _mm_add_pi32(accu2, _mm_madd_pi16(temp2, pVfilter[0])); // += l3*f2+l1*f0 r3*f2+r1*f0 accu2 = _mm_add_pi32(accu2, _mm_madd_pi16(temp1, pVfilter[1])); // += l4*f3+l2*f1 r4*f3+r2*f1 // accu1 += l2*f2+l0*f0 r2*f2+r0*f0 // += l3*f3+l1*f1 r3*f3+r1*f1 // accu2 += l3*f2+l1*f0 r3*f2+r1*f0 // l4*f3+l2*f1 r4*f3+r2*f1 pVfilter += 2; pVsrc += 2; } // accu >>= resultDivFactor accu1 = _mm_srai_pi32(accu1, resultDivFactor); accu2 = _mm_srai_pi32(accu2, resultDivFactor); // pack 2*2*32bits => 4*16 bits pVdest[0] = _mm_packs_pi32(accu1, accu2); src += 4; pVdest ++; } _m_empty(); // clear emms state return (numSamples & 0xfffffffe) - length; } #endif // SOUNDTOUCH_ALLOW_MMX soundtouch/source/Android-lib/0000775000175100017510000000000012577461413015737 5ustar vmdevvmdevsoundtouch/source/Android-lib/jni/0000775000175100017510000000000012577461413016517 5ustar vmdevvmdevsoundtouch/source/Android-lib/jni/soundtouch-jni.cpp0000664000175100017510000001724612577461413022206 0ustar vmdevvmdev//////////////////////////////////////////////////////////////////////////////// /// /// Example Interface class for SoundTouch native compilation /// /// Author : Copyright (c) Olli Parviainen /// Author e-mail : oparviai 'at' iki.fi /// WWW : http://www.surina.net /// //////////////////////////////////////////////////////////////////////////////// // // $Id: soundtouch-jni.cpp 212 2015-05-15 10:22:36Z oparviai $ // //////////////////////////////////////////////////////////////////////////////// #include #include #include #include using namespace std; #include "../../../include/SoundTouch.h" #include "../source/SoundStretch/WavFile.h" #define LOGV(...) __android_log_print((int)ANDROID_LOG_INFO, "SOUNDTOUCH", __VA_ARGS__) //#define LOGV(...) // String for keeping possible c++ exception error messages. Notice that this isn't // thread-safe but it's expected that exceptions are special situations that won't // occur in several threads in parallel. static string _errMsg = ""; #define DLL_PUBLIC __attribute__ ((visibility ("default"))) #define BUFF_SIZE 4096 using namespace soundtouch; // Set error message to return static void _setErrmsg(const char *msg) { _errMsg = msg; } #ifdef _OPENMP #include extern pthread_key_t gomp_tls_key; static void * _p_gomp_tls = NULL; /// Function to initialize threading for OpenMP. /// /// This is a workaround for bug in Android NDK v10 regarding OpenMP: OpenMP works only if /// called from the Android App main thread because in the main thread the gomp_tls storage is /// properly set, however, Android does not properly initialize gomp_tls storage for other threads. /// Thus if OpenMP routines are invoked from some other thread than the main thread, /// the OpenMP routine will crash the application due to NULL pointer access on uninitialized storage. /// /// This workaround stores the gomp_tls storage from main thread, and copies to other threads. /// In order this to work, the Application main thread needws to call at least "getVersionString" /// routine. static int _init_threading(bool warn) { void *ptr = pthread_getspecific(gomp_tls_key); LOGV("JNI thread-specific TLS storage %ld", (long)ptr); if (ptr == NULL) { LOGV("JNI set missing TLS storage to %ld", (long)_p_gomp_tls); pthread_setspecific(gomp_tls_key, _p_gomp_tls); } else { LOGV("JNI store this TLS storage"); _p_gomp_tls = ptr; } // Where critical, show warning if storage still not properly initialized if ((warn) && (_p_gomp_tls == NULL)) { _setErrmsg("Error - OpenMP threading not properly initialized: Call SoundTouch.getVersionString() from the App main thread!"); return -1; } return 0; } #else static int _init_threading(bool warn) { // do nothing if not OpenMP build return 0; } #endif // Processes the sound file static void _processFile(SoundTouch *pSoundTouch, const char *inFileName, const char *outFileName) { int nSamples; int nChannels; int buffSizeSamples; SAMPLETYPE sampleBuffer[BUFF_SIZE]; // open input file WavInFile inFile(inFileName); int sampleRate = inFile.getSampleRate(); int bits = inFile.getNumBits(); nChannels = inFile.getNumChannels(); // create output file WavOutFile outFile(outFileName, sampleRate, bits, nChannels); pSoundTouch->setSampleRate(sampleRate); pSoundTouch->setChannels(nChannels); assert(nChannels > 0); buffSizeSamples = BUFF_SIZE / nChannels; // Process samples read from the input file while (inFile.eof() == 0) { int num; // Read a chunk of samples from the input file num = inFile.read(sampleBuffer, BUFF_SIZE); nSamples = num / nChannels; // Feed the samples into SoundTouch processor pSoundTouch->putSamples(sampleBuffer, nSamples); // Read ready samples from SoundTouch processor & write them output file. // NOTES: // - 'receiveSamples' doesn't necessarily return any samples at all // during some rounds! // - On the other hand, during some round 'receiveSamples' may have more // ready samples than would fit into 'sampleBuffer', and for this reason // the 'receiveSamples' call is iterated for as many times as it // outputs samples. do { nSamples = pSoundTouch->receiveSamples(sampleBuffer, buffSizeSamples); outFile.write(sampleBuffer, nSamples * nChannels); } while (nSamples != 0); } // Now the input file is processed, yet 'flush' few last samples that are // hiding in the SoundTouch's internal processing pipeline. pSoundTouch->flush(); do { nSamples = pSoundTouch->receiveSamples(sampleBuffer, buffSizeSamples); outFile.write(sampleBuffer, nSamples * nChannels); } while (nSamples != 0); } extern "C" DLL_PUBLIC jstring Java_net_surina_soundtouch_SoundTouch_getVersionString(JNIEnv *env, jobject thiz) { const char *verStr; LOGV("JNI call SoundTouch.getVersionString"); // Call example SoundTouch routine verStr = SoundTouch::getVersionString(); /// gomp_tls storage bug workaround - see comments in _init_threading() function! _init_threading(false); int threads = 0; #pragma omp parallel { #pragma omp atomic threads ++; } LOGV("JNI thread count %d", threads); // return version as string return env->NewStringUTF(verStr); } extern "C" DLL_PUBLIC jlong Java_net_surina_soundtouch_SoundTouch_newInstance(JNIEnv *env, jobject thiz) { return (jlong)(new SoundTouch()); } extern "C" DLL_PUBLIC void Java_net_surina_soundtouch_SoundTouch_deleteInstance(JNIEnv *env, jobject thiz, jlong handle) { SoundTouch *ptr = (SoundTouch*)handle; delete ptr; } extern "C" DLL_PUBLIC void Java_net_surina_soundtouch_SoundTouch_setTempo(JNIEnv *env, jobject thiz, jlong handle, jfloat tempo) { SoundTouch *ptr = (SoundTouch*)handle; ptr->setTempo(tempo); } extern "C" DLL_PUBLIC void Java_net_surina_soundtouch_SoundTouch_setPitchSemiTones(JNIEnv *env, jobject thiz, jlong handle, jfloat pitch) { SoundTouch *ptr = (SoundTouch*)handle; ptr->setPitchSemiTones(pitch); } extern "C" DLL_PUBLIC void Java_net_surina_soundtouch_SoundTouch_setSpeed(JNIEnv *env, jobject thiz, jlong handle, jfloat speed) { SoundTouch *ptr = (SoundTouch*)handle; ptr->setRate(speed); } extern "C" DLL_PUBLIC jstring Java_net_surina_soundtouch_SoundTouch_getErrorString(JNIEnv *env, jobject thiz) { jstring result = env->NewStringUTF(_errMsg.c_str()); _errMsg.clear(); return result; } extern "C" DLL_PUBLIC int Java_net_surina_soundtouch_SoundTouch_processFile(JNIEnv *env, jobject thiz, jlong handle, jstring jinputFile, jstring joutputFile) { SoundTouch *ptr = (SoundTouch*)handle; const char *inputFile = env->GetStringUTFChars(jinputFile, 0); const char *outputFile = env->GetStringUTFChars(joutputFile, 0); LOGV("JNI process file %s", inputFile); /// gomp_tls storage bug workaround - see comments in _init_threading() function! if (_init_threading(true)) return -1; try { _processFile(ptr, inputFile, outputFile); } catch (const runtime_error &e) { const char *err = e.what(); // An exception occurred during processing, return the error message LOGV("JNI exception in SoundTouch::processFile: %s", err); _setErrmsg(err); return -1; } env->ReleaseStringUTFChars(jinputFile, inputFile); env->ReleaseStringUTFChars(joutputFile, outputFile); return 0; } soundtouch/source/Android-lib/jni/Application.mk0000664000175100017510000000042312577461413021312 0ustar vmdevvmdev# $Id: Application.mk 212 2015-05-15 10:22:36Z oparviai $ # # Build library bilaries for all supported architectures # APP_ABI := all #armeabi-v7a armeabi APP_OPTIM := release APP_STL := stlport_static APP_CPPFLAGS := -fexceptions # -D SOUNDTOUCH_DISABLE_X86_OPTIMIZATIONS soundtouch/source/Android-lib/jni/Android.mk0000664000175100017510000000400512577461413020427 0ustar vmdevvmdev# Copyright (C) 2010 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # $Id: Android.mk 216 2015-05-18 15:28:41Z oparviai $ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # *** Remember: Change -O0 into -O2 in add-applications.mk *** LOCAL_MODULE := soundtouch LOCAL_SRC_FILES := soundtouch-jni.cpp ../../SoundTouch/AAFilter.cpp ../../SoundTouch/FIFOSampleBuffer.cpp \ ../../SoundTouch/FIRFilter.cpp ../../SoundTouch/cpu_detect_x86.cpp \ ../../SoundTouch/sse_optimized.cpp ../../SoundStretch/WavFile.cpp \ ../../SoundTouch/RateTransposer.cpp ../../SoundTouch/SoundTouch.cpp \ ../../SoundTouch/InterpolateCubic.cpp ../../SoundTouch/InterpolateLinear.cpp \ ../../SoundTouch/InterpolateShannon.cpp ../../SoundTouch/TDStretch.cpp \ ../../SoundTouch/BPMDetect.cpp ../../SoundTouch/PeakFinder.cpp # for native audio LOCAL_SHARED_LIBRARIES += -lgcc # --whole-archive -lgcc # for logging LOCAL_LDLIBS += -llog # for native asset manager #LOCAL_LDLIBS += -landroid # Custom Flags: # -fvisibility=hidden : don't export all symbols LOCAL_CFLAGS += -fvisibility=hidden -I ../../../include -fdata-sections -ffunction-sections # OpenMP mode : enable these flags to enable using OpenMP for parallel computation #LOCAL_CFLAGS += -fopenmp #LOCAL_LDFLAGS += -fopenmp # Use ARM instruction set instead of Thumb for improved calculation performance in ARM CPUs LOCAL_ARM_MODE := arm include $(BUILD_SHARED_LIBRARY) soundtouch/source/Android-lib/.classpath0000664000175100017510000000072212577461413017723 0ustar vmdevvmdev soundtouch/source/Android-lib/res/0000775000175100017510000000000012577461413016530 5ustar vmdevvmdevsoundtouch/source/Android-lib/res/drawable-mdpi/0000775000175100017510000000000012577461413021240 5ustar vmdevvmdevsoundtouch/source/Android-lib/res/drawable-mdpi/ic_launcher.png0000664000175100017510000000730112577461413024223 0ustar vmdevvmdevPNG  IHDR00WIDATxYyTy/2t& qei6DGV]n}_[Q67[%QJ79d2SmQu{߯KKN=~m5={ zοq;U}a\9hݥy='nz֧7Z k&Dž&j;Z\e^%2_ }3(v>qxk "vݫXxl?I<*T4{ݗ8d< sot>]c}/-WaULJPn`J/No{0n:<\|Ќ;ܛ,ʸ;oz lu9c t\eo,6"k$* 1qןwxqZokZ/ځ]tt+e7lLo2 .aCT=h:5Q}؆c-/WǧEU9i6fl<#DY{4sD9'h>;Yt)GMGY5zP52t#ȓ~3{H{T~wmEe>o4L: f(ejA+dMn 8VkXxK35 qQXOx z/lxlh2ܖ]7g&m~:ScRB7|c.fB&p3N_5._KXL֡_p`8ad9b^!9e *ūZ[ιRQÌYY{gPY9LFSd4C\cnj/TU{Ėi>gO;I]\tr*;[WTqA)(ns@!6Y#%Z"oYQaE>3?r LAԵFLp2}_m?ڜT6e!F월!(ef(0Â8n 2+ΗwQp}Sd7& A^kq ١{ؔz>&1`{R`(!F0^!, ʦ1eBhi`]ήJT &w)7 s$d>|)Y'魔09쵭&8C&Pwn2 |j{,uCAR+tT^r]YZ&sbTjcbB5FIh9w@~ s()D $S|(2@GtwǓ*QrgVr 5;){pJ>\g@GtCFV fCUdBl;&Z&7y WoF((ט| 5 ʅ(P- #Kҧ諔ӎ (!9*ѧ#*X.KVcҚ%#aFOtMk"F{{{|G!V f QШ-v:-t ~ M>h:IV>y-a3"YCd[L1>-_B4x&‘sp HʍH 0ԖbDvF 'H E<`&Uhc%YR03P@Cx}Cݟ5Upz^P%^y_HdlpV{Y2F" B'dmCZ'U> u>x0`e s %X*)ԵZfnPvfu9XťLq66F.Vqj}2Gȭ%=GDz4FsbOQKJYlU%F!ؐ@8-hH( ыQz\0FH nqPJS83pD:@_w ȩzCL"p_['ᔉ @J%LآB`ecF\ G[HX[r; AJgY>~MrzrBTщ4#:JgW$ceZ!#bbr-_IC7:pA{w҉.yIª[RMqROiIC W=h%!1K 61, D+)JC"l+**e)CC] t D~<Y*~P\i&)l#]Z˔|| 5DPỌ!"[#<\DeY"Jx,椱l^]#kzʗ+)X켠Cz\KHz (ZJVqH6<7L."m,qzIr*D+ Ef3+I-4zq/Wº^kL)Wl`";hZQ0uΘ~M{x]=vW4ѥ\aa pQN”&xG"×nδyiJw]gWpHX CR,yZfsxA؉/8ه8-Kqt^'Kf c OъjDIENDB`soundtouch/source/Android-lib/res/values/0000775000175100017510000000000012577461413020027 5ustar vmdevvmdevsoundtouch/source/Android-lib/res/values/styles.xml0000664000175100017510000000125512577461413022077 0ustar vmdevvmdev soundtouch/source/Android-lib/res/values/strings.xml0000664000175100017510000000025712577461413022246 0ustar vmdevvmdev SoundTouch Example Hello world! soundtouch/source/Android-lib/res/layout/0000775000175100017510000000000012577461413020045 5ustar vmdevvmdevsoundtouch/source/Android-lib/res/layout/activity_example.xml0000664000175100017510000001112612577461413024137 0ustar vmdevvmdev