Replaced illegal-number-of-channel assertions with run-time exception

This commit is contained in:
oparviainen 2018-08-12 20:00:56 +03:00
parent 5c168a55ff
commit 107f2c5d20
6 changed files with 23 additions and 13 deletions

View File

@ -51,6 +51,18 @@ namespace soundtouch
/// Abstract base class for FIFO (first-in-first-out) sample processing classes. /// Abstract base class for FIFO (first-in-first-out) sample processing classes.
class FIFOSamplePipe class FIFOSamplePipe
{ {
protected:
bool verifyNumberOfChannels(int nChannels) const
{
if ((nChannels > 0) && (nChannels <= SOUNDTOUCH_MAX_CHANNELS))
{
return true;
}
ST_THROW_RT_ERROR("Error: Illegal number of channels");
return false;
}
public: public:
// virtual default destructor // virtual default destructor
virtual ~FIFOSamplePipe() {} virtual ~FIFOSamplePipe() {}

View File

@ -56,6 +56,9 @@ typedef unsigned long ulong;
namespace soundtouch namespace soundtouch
{ {
/// Max allowed number of channels
#define SOUNDTOUCH_MAX_CHANNELS 16
/// Activate these undef's to overrule the possible sampletype /// Activate these undef's to overrule the possible sampletype
/// setting inherited from some other header file: /// setting inherited from some other header file:
//#undef SOUNDTOUCH_INTEGER_SAMPLES //#undef SOUNDTOUCH_INTEGER_SAMPLES

View File

@ -73,7 +73,8 @@ void FIFOSampleBuffer::setChannels(int numChannels)
{ {
uint usedBytes; uint usedBytes;
assert(numChannels > 0); if (!verifyNumberOfChannels(numChannels)) return;
usedBytes = channels * samplesInBuffer; usedBytes = channels * samplesInBuffer;
channels = (uint)numChannels; channels = (uint)numChannels;
samplesInBuffer = usedBytes / channels; samplesInBuffer = usedBytes / channels;

View File

@ -179,11 +179,10 @@ void RateTransposer::processSamples(const SAMPLETYPE *src, uint nSamples)
// Sets the number of channels, 1 = mono, 2 = stereo // Sets the number of channels, 1 = mono, 2 = stereo
void RateTransposer::setChannels(int nChannels) void RateTransposer::setChannels(int nChannels)
{ {
assert(nChannels > 0); if (!verifyNumberOfChannels(nChannels) ||
(pTransposer->numChannels == nChannels)) return;
if (pTransposer->numChannels == nChannels) return;
pTransposer->setChannels(nChannels); pTransposer->setChannels(nChannels);
inputBuffer.setChannels(nChannels); inputBuffer.setChannels(nChannels);
midBuffer.setChannels(nChannels); midBuffer.setChannels(nChannels);
outputBuffer.setChannels(nChannels); outputBuffer.setChannels(nChannels);

View File

@ -139,18 +139,14 @@ uint SoundTouch::getVersionId()
// Sets the number of channels, 1 = mono, 2 = stereo // Sets the number of channels, 1 = mono, 2 = stereo
void SoundTouch::setChannels(uint numChannels) void SoundTouch::setChannels(uint numChannels)
{ {
/*if (numChannels != 1 && numChannels != 2) if (!verifyNumberOfChannels(numChannels)) return;
{
//ST_THROW_RT_ERROR("Illegal number of channels");
return;
}*/
channels = numChannels; channels = numChannels;
pRateTransposer->setChannels((int)numChannels); pRateTransposer->setChannels((int)numChannels);
pTDStretch->setChannels((int)numChannels); pTDStretch->setChannels((int)numChannels);
} }
// Sets new rate control value. Normal rate = 1.0, smaller values // Sets new rate control value. Normal rate = 1.0, smaller values
// represent slower rate, larger faster rates. // represent slower rate, larger faster rates.
void SoundTouch::setRate(double newRate) void SoundTouch::setRate(double newRate)

View File

@ -588,9 +588,8 @@ void TDStretch::setTempo(double newTempo)
// Sets the number of channels, 1 = mono, 2 = stereo // Sets the number of channels, 1 = mono, 2 = stereo
void TDStretch::setChannels(int numChannels) void TDStretch::setChannels(int numChannels)
{ {
assert(numChannels > 0); if (!verifyNumberOfChannels(numChannels) ||
if (channels == numChannels) return; (channels == numChannels)) return;
// assert(numChannels == 1 || numChannels == 2);
channels = numChannels; channels = numChannels;
inputBuffer.setChannels(channels); inputBuffer.setChannels(channels);