diff --git a/include/STTypes.h b/include/STTypes.h index e9ce847..e648bc8 100644 --- a/include/STTypes.h +++ b/include/STTypes.h @@ -143,8 +143,20 @@ namespace soundtouch #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 + #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. diff --git a/source/SoundStretch/RunParameters.cpp b/source/SoundStretch/RunParameters.cpp index 2e0c0c6..e5f569e 100644 --- a/source/SoundStretch/RunParameters.cpp +++ b/source/SoundStretch/RunParameters.cpp @@ -36,7 +36,6 @@ // //////////////////////////////////////////////////////////////////////////////// -#include #include #include @@ -120,7 +119,7 @@ RunParameters::RunParameters(const int nParams, const char * const paramStr[]) } string msg = whatText; msg += usage; - throw runtime_error(msg.c_str()); + ST_THROW_RT_ERROR(msg.c_str()); } inFileName = NULL; @@ -200,14 +199,14 @@ void RunParameters::throwIllegalParamExp(const string &str) const msg += str; msg += "\".\n\n"; msg += usage; - throw runtime_error(msg.c_str()); + ST_THROW_RT_ERROR(msg.c_str()); } void RunParameters::throwLicense() const { - throw runtime_error(licenseText); + ST_THROW_RT_ERROR(licenseText); } diff --git a/source/SoundStretch/WavFile.cpp b/source/SoundStretch/WavFile.cpp index 31760c9..c02092b 100644 --- a/source/SoundStretch/WavFile.cpp +++ b/source/SoundStretch/WavFile.cpp @@ -46,7 +46,6 @@ //////////////////////////////////////////////////////////////////////////////// #include -#include #include #include #include @@ -54,6 +53,7 @@ #include #include "WavFile.h" +#include "STTypes.h" using namespace std; @@ -149,7 +149,7 @@ WavInFile::WavInFile(const char *fileName) string msg = "Error : Unable to open file \""; msg += fileName; msg += "\" for reading."; - throw runtime_error(msg); + ST_THROW_RT_ERROR(msg.c_str()); } init(); @@ -164,7 +164,7 @@ WavInFile::WavInFile(FILE *file) { // didn't succeed string msg = "Error : Unable to access input stream for reading"; - throw runtime_error(msg); + ST_THROW_RT_ERROR(msg.c_str()); } init(); @@ -185,13 +185,13 @@ void WavInFile::init() { // Something didn't match in the wav file headers string msg = "Input file is corrupt or not a WAV file"; - throw runtime_error(msg); + ST_THROW_RT_ERROR(msg.c_str()); } if (header.format.fixed != 1) { string msg = "Input file uses unsupported encoding."; - throw runtime_error(msg); + ST_THROW_RT_ERROR(msg.c_str()); } dataRead = 0; @@ -237,7 +237,7 @@ int WavInFile::read(char *buffer, int maxElems) // ensure it's 8 bit format if (header.format.bits_per_sample != 8) { - throw runtime_error("Error: WavInFile::read(char*, int) works only with 8bit samples."); + ST_THROW_RT_ERROR("Error: WavInFile::read(char*, int) works only with 8bit samples."); } assert(sizeof(char) == 1); @@ -288,7 +288,7 @@ int WavInFile::read(short *buffer, int maxElems) ss << "\nOnly 8/16 bit sample WAV files supported. Can't open WAV file with "; ss << (int)header.format.bits_per_sample; ss << " bit sample format. "; - throw runtime_error(ss.str()); + ST_THROW_RT_ERROR(ss.str().c_str()); } assert(sizeof(short) == 2); @@ -556,7 +556,7 @@ WavOutFile::WavOutFile(const char *fileName, int sampleRate, int bits, int chann msg += fileName; msg += "\" for writing."; //pmsg = msg.c_str; - throw runtime_error(msg); + ST_THROW_RT_ERROR(msg.c_str()); } fillInHeader(sampleRate, bits, channels); @@ -571,7 +571,7 @@ WavOutFile::WavOutFile(FILE *file, int sampleRate, int bits, int channels) if (fptr == NULL) { string msg = "Error : Unable to access output file stream."; - throw runtime_error(msg); + ST_THROW_RT_ERROR(msg.c_str()); } fillInHeader(sampleRate, bits, channels); @@ -657,7 +657,7 @@ void WavOutFile::writeHeader() res = (int)fwrite(&hdrTemp, sizeof(hdrTemp), 1, fptr); if (res != 1) { - throw runtime_error("Error while writing to a wav file."); + ST_THROW_RT_ERROR("Error while writing to a wav file."); } // jump back to the end of the file @@ -672,14 +672,14 @@ void WavOutFile::write(const char *buffer, int numElems) if (header.format.bits_per_sample != 8) { - throw runtime_error("Error: WavOutFile::write(const char*, int) accepts only 8bit samples."); + 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) { - throw runtime_error("Error while writing to a wav file."); + ST_THROW_RT_ERROR("Error while writing to a wav file."); } bytesWritten += numElems; @@ -717,7 +717,7 @@ void WavOutFile::write(const short *buffer, int numElems) ss << "\nOnly 8/16 bit sample WAV files supported. Can't open WAV file with "; ss << (int)header.format.bits_per_sample; ss << " bit sample format. "; - throw runtime_error(ss.str()); + ST_THROW_RT_ERROR(ss.str().c_str()); } // allocate temp buffer to swap byte order if necessary @@ -730,7 +730,7 @@ void WavOutFile::write(const short *buffer, int numElems) if (res != numElems) { - throw runtime_error("Error while writing to a wav file."); + ST_THROW_RT_ERROR("Error while writing to a wav file."); } bytesWritten += 2 * numElems; } diff --git a/source/SoundTouch/FIFOSampleBuffer.cpp b/source/SoundTouch/FIFOSampleBuffer.cpp index 8393f7b..f9efee6 100644 --- a/source/SoundTouch/FIFOSampleBuffer.cpp +++ b/source/SoundTouch/FIFOSampleBuffer.cpp @@ -47,7 +47,6 @@ #include #include #include -#include #include "FIFOSampleBuffer.h" @@ -175,7 +174,7 @@ void FIFOSampleBuffer::ensureCapacity(uint capacityRequirement) tempUnaligned = new SAMPLETYPE[sizeInBytes / sizeof(SAMPLETYPE) + 16 / sizeof(SAMPLETYPE)]; if (tempUnaligned == NULL) { - throw std::runtime_error("Couldn't allocate memory!\n"); + ST_THROW_RT_ERROR("Couldn't allocate memory!\n"); } // Align the buffer to begin at 16byte cache line boundary for optimal performance temp = (SAMPLETYPE *)(((ulong)tempUnaligned + 15) & (ulong)-16); diff --git a/source/SoundTouch/FIRFilter.cpp b/source/SoundTouch/FIRFilter.cpp index a274562..f882f7e 100644 --- a/source/SoundTouch/FIRFilter.cpp +++ b/source/SoundTouch/FIRFilter.cpp @@ -43,7 +43,6 @@ #include #include #include -#include #include "FIRFilter.h" #include "cpu_detect.h" @@ -174,7 +173,7 @@ uint FIRFilter::evaluateFilterMono(SAMPLETYPE *dest, const SAMPLETYPE *src, uint void FIRFilter::setCoefficients(const SAMPLETYPE *coeffs, uint newLength, uint uResultDivFactor) { assert(newLength > 0); - if (newLength % 8) throw std::runtime_error("FIR filter length not divisible by 8"); + if (newLength % 8) ST_THROW_RT_ERROR("FIR filter length not divisible by 8"); lengthDiv8 = newLength / 8; length = lengthDiv8 * 8; @@ -222,8 +221,8 @@ uint FIRFilter::evaluate(SAMPLETYPE *dest, const SAMPLETYPE *src, uint numSample void * FIRFilter::operator new(size_t s) { // Notice! don't use "new FIRFilter" directly, use "newInstance" to create a new instance instead! - throw std::runtime_error("Error in FIRFilter::new: Don't use 'new FIRFilter', use 'newInstance' member instead!"); - return NULL; + ST_THROW_RT_ERROR("Error in FIRFilter::new: Don't use 'new FIRFilter', use 'newInstance' member instead!"); + return newInstance(); } diff --git a/source/SoundTouch/RateTransposer.cpp b/source/SoundTouch/RateTransposer.cpp index 2afc187..772e95b 100644 --- a/source/SoundTouch/RateTransposer.cpp +++ b/source/SoundTouch/RateTransposer.cpp @@ -42,11 +42,9 @@ #include #include #include -#include #include "RateTransposer.h" #include "AAFilter.h" -using namespace std; using namespace soundtouch; @@ -108,8 +106,8 @@ public: // depending on if we've a MMX/SSE/etc-capable CPU available or not. void * RateTransposer::operator new(size_t s) { - throw runtime_error("Error in RateTransoser::new: don't use \"new TDStretch\" directly, use \"newInstance\" to create a new instance instead!"); - return NULL; + ST_THROW_RT_ERROR("Error in RateTransoser::new: don't use \"new TDStretch\" directly, use \"newInstance\" to create a new instance instead!"); + return newInstance(); } diff --git a/source/SoundTouch/SoundTouch.cpp b/source/SoundTouch/SoundTouch.cpp index 6f7b9a8..b67dd2c 100644 --- a/source/SoundTouch/SoundTouch.cpp +++ b/source/SoundTouch/SoundTouch.cpp @@ -73,7 +73,6 @@ #include #include #include -#include #include #include "SoundTouch.h" @@ -146,7 +145,7 @@ void SoundTouch::setChannels(uint numChannels) { if (numChannels != 1 && numChannels != 2) { - throw std::runtime_error("Illegal number of channels"); + ST_THROW_RT_ERROR("Illegal number of channels"); } channels = numChannels; pRateTransposer->setChannels((int)numChannels); @@ -295,11 +294,11 @@ void SoundTouch::putSamples(const SAMPLETYPE *samples, uint nSamples) { if (bSrateSet == FALSE) { - throw std::runtime_error("SoundTouch : Sample rate not defined"); + ST_THROW_RT_ERROR("SoundTouch : Sample rate not defined"); } else if (channels == 0) { - throw std::runtime_error("SoundTouch : Number of channels not defined"); + ST_THROW_RT_ERROR("SoundTouch : Number of channels not defined"); } // Transpose the rate of the new samples if necessary diff --git a/source/SoundTouch/TDStretch.cpp b/source/SoundTouch/TDStretch.cpp index 3ef7798..b8dc488 100644 --- a/source/SoundTouch/TDStretch.cpp +++ b/source/SoundTouch/TDStretch.cpp @@ -46,7 +46,6 @@ #include #include #include -#include #include "STTypes.h" #include "cpu_detect.h" @@ -731,8 +730,8 @@ void TDStretch::acceptNewOverlapLength(int newOverlapLength) void * TDStretch::operator new(size_t s) { // Notice! don't use "new TDStretch" directly, use "newInstance" to create a new instance instead! - throw std::runtime_error("Error in TDStretch::new: Don't use 'new TDStretch' directly, use 'newInstance' member instead!"); - return NULL; + ST_THROW_RT_ERROR("Error in TDStretch::new: Don't use 'new TDStretch' directly, use 'newInstance' member instead!"); + return newInstance(); } diff --git a/source/SoundTouch/cpu_detect_x86_gcc.cpp b/source/SoundTouch/cpu_detect_x86_gcc.cpp index a56d6cb..55c4651 100644 --- a/source/SoundTouch/cpu_detect_x86_gcc.cpp +++ b/source/SoundTouch/cpu_detect_x86_gcc.cpp @@ -39,15 +39,9 @@ // //////////////////////////////////////////////////////////////////////////////// -#include -#include #include "cpu_detect.h" #include "STTypes.h" -using namespace std; - -#include - ////////////////////////////////////////////////////////////////////////////// // // processor instructions extension detection routines