diff --git a/include/audacity/Types.h b/include/audacity/Types.h index bf8b6651f..b212b117a 100644 --- a/include/audacity/Types.h +++ b/include/audacity/Types.h @@ -60,6 +60,18 @@ typedef __int64 sampleCount; typedef long long sampleCount; #endif +// ---------------------------------------------------------------------------- +// Function returning the minimum of a sampleCount and a size_t, +// hiding the casts +// ---------------------------------------------------------------------------- + +inline size_t limitSampleBufferSize( size_t bufferSize, sampleCount limit ) +{ + return static_cast ( + std::min( sampleCount( bufferSize ), std::max( sampleCount(0), limit ) ) + ); +} + // ---------------------------------------------------------------------------- // Supported sample formats // ---------------------------------------------------------------------------- diff --git a/src/Experimental.h b/src/Experimental.h index 364ee066a..bcea63fa1 100644 --- a/src/Experimental.h +++ b/src/Experimental.h @@ -96,10 +96,10 @@ // Use on-demand importing for FLAC. Has issues with opening projects that // have not been fully imported in builds without FLAC support, so disabled for // 2.0 release -//#define EXPERIMENTAL_OD_FLAC +#define EXPERIMENTAL_OD_FLAC // similarly for FFmpeg: // Won't build on Fedora 17 or Windows VC++, per http://bugzilla.audacityteam.org/show_bug.cgi?id=539. -//#define EXPERIMENTAL_OD_FFMPEG 1 +#define EXPERIMENTAL_OD_FFMPEG 1 // Paul Licameli (PRL) 5 Oct 2014 #define EXPERIMENTAL_SPECTRAL_EDITING diff --git a/src/Mix.cpp b/src/Mix.cpp index 638808b16..3811164fb 100644 --- a/src/Mix.cpp +++ b/src/Mix.cpp @@ -448,9 +448,10 @@ sampleCount Mixer::MixVariableRates(int *channelFlags, WaveTrackCache &cache, memmove(queue, &queue[*queueStart], (*queueLen) * sampleSize); *queueStart = 0; - int getLen = - std::min((backwards ? *pos - endPos : endPos - *pos), - sampleCount(mQueueMaxLen - *queueLen)); + const auto getLen = limitSampleBufferSize( + mQueueMaxLen - *queueLen, + backwards ? *pos - endPos : endPos - *pos + ); // Nothing to do if past end of play interval if (getLen > 0) { @@ -475,7 +476,7 @@ sampleCount Mixer::MixVariableRates(int *channelFlags, WaveTrackCache &cache, *pos += getLen; } - for (int i = 0; i < getLen; i++) { + for (decltype(+getLen) i = 0; i < getLen; i++) { queue[(*queueLen) + i] *= mEnvValues[i]; } diff --git a/src/Sequence.cpp b/src/Sequence.cpp index 8da4fbb2e..0536b058d 100644 --- a/src/Sequence.cpp +++ b/src/Sequence.cpp @@ -239,7 +239,7 @@ bool Sequence::GetMinMax(sampleCount start, sampleCount len, unsigned int block0 = FindBlock(start); unsigned int block1 = FindBlock(start + len - 1); - sampleCount s0, l0, maxl0; + sampleCount s0; // First calculate the min/max of the blocks in the middle of this region; // this is very fast because we have the min/max of every entire block @@ -267,11 +267,9 @@ bool Sequence::GetMinMax(sampleCount start, sampleCount len, if (block0Min < min || block0Max > max) { s0 = start - theBlock.start; - l0 = len; - maxl0 = theBlock.start + theFile->GetLength() - start; + const auto maxl0 = theBlock.start + theFile->GetLength() - start; wxASSERT(maxl0 <= mMaxSamples); // Vaughan, 2011-10-19 - if (l0 > maxl0) - l0 = maxl0; + const auto l0 = limitSampleBufferSize ( maxl0, len ); float partialMin, partialMax, partialRMS; theFile->GetMinMax(s0, l0, @@ -293,7 +291,7 @@ bool Sequence::GetMinMax(sampleCount start, sampleCount len, if (block1Min < min || block1Max > max) { s0 = 0; - l0 = (start + len) - theBlock.start; + const auto l0 = (start + len) - theBlock.start; wxASSERT(l0 <= mMaxSamples); // Vaughan, 2011-10-19 float partialMin, partialMax, partialRMS; @@ -328,7 +326,7 @@ bool Sequence::GetRMS(sampleCount start, sampleCount len, unsigned int block0 = FindBlock(start); unsigned int block1 = FindBlock(start + len - 1); - sampleCount s0, l0, maxl0; + sampleCount s0; // First calculate the rms of the blocks in the middle of this region; // this is very fast because we have the rms of every entire block @@ -351,11 +349,9 @@ bool Sequence::GetRMS(sampleCount start, sampleCount len, const SeqBlock &theBlock = mBlock[block0]; const auto &theFile = theBlock.f; s0 = start - theBlock.start; - l0 = len; - maxl0 = theBlock.start + theFile->GetLength() - start; + const auto maxl0 = theBlock.start + theFile->GetLength() - start; wxASSERT(maxl0 <= mMaxSamples); // Vaughan, 2011-10-19 - if (l0 > maxl0) - l0 = maxl0; + const auto l0 = limitSampleBufferSize( maxl0, len ); float partialMin, partialMax, partialRMS; theFile->GetMinMax(s0, l0, &partialMin, &partialMax, &partialRMS); @@ -369,7 +365,7 @@ bool Sequence::GetRMS(sampleCount start, sampleCount len, const auto &theFile = theBlock.f; s0 = 0; - l0 = (start + len) - theBlock.start; + const auto l0 = (start + len) - theBlock.start; wxASSERT(l0 <= mMaxSamples); // PRL: I think Vaughan missed this float partialMin, partialMax, partialRMS; @@ -1181,7 +1177,8 @@ bool Sequence::Set(samplePtr buffer, sampleFormat format, SampleBuffer temp; if (buffer && format != mSampleFormat) { - temp.Allocate(std::min(len, mMaxSamples), mSampleFormat); + const auto size = limitSampleBufferSize( mMaxSamples, len ); + temp.Allocate(size, mSampleFormat); } int b = FindBlock(start); @@ -1190,8 +1187,7 @@ bool Sequence::Set(samplePtr buffer, sampleFormat format, SeqBlock &block = mBlock[b]; const sampleCount bstart = start - block.start; const sampleCount fileLength = block.f->GetLength(); - const int blen = - std::min(len, fileLength - bstart); + const auto blen = limitSampleBufferSize( fileLength - bstart, len ); if (buffer) { if (format == mSampleFormat) diff --git a/src/TrackPanel.cpp b/src/TrackPanel.cpp index 547fbef63..bd089fba6 100644 --- a/src/TrackPanel.cpp +++ b/src/TrackPanel.cpp @@ -2336,7 +2336,7 @@ void TrackPanel::SnapCenterOnce(const WaveTrack *pTrack, bool up) void TrackPanel::StartSnappingFreqSelection (const WaveTrack *pTrack) { - static const sampleCount minLength = 8; + static const size_t minLength = 8; const double rate = pTrack->GetRate(); @@ -2346,11 +2346,11 @@ void TrackPanel::StartSnappingFreqSelection (const WaveTrack *pTrack) pTrack->TimeToLongSamples(mViewInfo->selectedRegion.t0()); const sampleCount end = pTrack->TimeToLongSamples(mViewInfo->selectedRegion.t1()); - const sampleCount length = - std::min(sampleCount(frequencySnappingData.max_size()), - std::min(sampleCount(10485760), // as in FreqWindow.cpp - end - start)); - const sampleCount effectiveLength = std::max(minLength, length); + const auto length = + std::min(frequencySnappingData.max_size(), + limitSampleBufferSize( 10485760, // as in FreqWindow.cpp + end - start )); + const auto effectiveLength = std::max(minLength, length); frequencySnappingData.resize(effectiveLength, 0.0f); pTrack->Get( reinterpret_cast(&frequencySnappingData[0]), diff --git a/src/VoiceKey.cpp b/src/VoiceKey.cpp index 6ab738f48..0c5ba01e5 100644 --- a/src/VoiceKey.cpp +++ b/src/VoiceKey.cpp @@ -110,7 +110,6 @@ sampleCount VoiceKey::OnForward (WaveTrack & t, sampleCount start, sampleCount l auto samplesleft = len - WindowSizeInt; //Indexes the number of samples remaining in the selection lastsubthresholdsample = start; //start this off at the selection start int blockruns=0; //keeps track of the number of consecutive above-threshold blocks - int blocksize; //The final block may be smaller than WindowSizeInt, so use this //This loop goes through the selection a block at a time. If a long enough run @@ -122,12 +121,7 @@ sampleCount VoiceKey::OnForward (WaveTrack & t, sampleCount start, sampleCount l i += (WindowSizeInt - 1) , samplesleft -= (WindowSizeInt - 1)) { //Set blocksize so that it is the right size - if((unsigned int)samplesleft < WindowSizeInt){ - blocksize = samplesleft; - } - else { - blocksize = WindowSizeInt; - } + const auto blocksize = limitSampleBufferSize( WindowSizeInt, samplesleft); //Test whether we are above threshold (the number of stats) if(AboveThreshold(t,i,blocksize)) @@ -263,7 +257,6 @@ sampleCount VoiceKey::OnBackward (WaveTrack & t, sampleCount end, sampleCount le auto samplesleft = len - WindowSizeInt; //Indexes the number of samples remaining in the selection lastsubthresholdsample = end; //start this off at the end int blockruns=0; //keeps track of the number of consecutive above-threshold blocks - int blocksize; //The final block may be smaller than WindowSizeInt, so use this //This loop goes through the selection a block at a time in reverse order. If a long enough run @@ -274,12 +267,8 @@ sampleCount VoiceKey::OnBackward (WaveTrack & t, sampleCount end, sampleCount le i -= (WindowSizeInt - 1) , samplesleft -= (WindowSizeInt - 1)) { //Set blocksize so that it is the right size - if(samplesleft < (int)WindowSizeInt){ - blocksize = samplesleft; - } - else { - blocksize = WindowSizeInt; - } + + const auto blocksize = limitSampleBufferSize( WindowSizeInt, samplesleft); //Test whether we are above threshold @@ -406,7 +395,6 @@ sampleCount VoiceKey::OffForward (WaveTrack & t, sampleCount start, sampleCount auto samplesleft = len - WindowSizeInt; //Indexes the number of samples remaining in the selection lastsubthresholdsample = start; //start this off at the selection start int blockruns=0; //keeps track of the number of consecutive above-threshold blocks - int blocksize; //The final block may be smaller than WindowSizeInt, so use this //This loop goes through the selection a block at a time. If a long enough run //of above-threshold blocks occur, we return to the last sub-threshold block and @@ -416,12 +404,7 @@ sampleCount VoiceKey::OffForward (WaveTrack & t, sampleCount start, sampleCount i += (WindowSizeInt - 1) , samplesleft -= (WindowSizeInt - 1)) { //Set blocksize so that it is the right size - if(samplesleft < (int)WindowSizeInt){ - blocksize = samplesleft; - } - else { - blocksize = WindowSizeInt; - } + const auto blocksize = limitSampleBufferSize( WindowSizeInt, samplesleft); if(!AboveThreshold(t,i,blocksize)) { @@ -547,7 +530,6 @@ sampleCount VoiceKey::OffBackward (WaveTrack & t, sampleCount end, sampleCount l auto samplesleft = len - WindowSizeInt; //Indexes the number of samples remaining in the selection lastsubthresholdsample = end; //start this off at the end int blockruns=0; //keeps track of the number of consecutive above-threshold blocks - int blocksize; //The final block may be smaller than WindowSizeInt, so use this //This loop goes through the selection a block at a time in reverse order. If a long enough run //of above-threshold blocks occur, we return to the last sub-threshold block and @@ -557,12 +539,7 @@ sampleCount VoiceKey::OffBackward (WaveTrack & t, sampleCount end, sampleCount l i -= (WindowSizeInt - 1), samplesleft -= (WindowSizeInt -1 )) { //Set blocksize so that it is the right size - if(samplesleft < (int)WindowSizeInt){ - blocksize = samplesleft; - } - else { - blocksize = WindowSizeInt; - } + const auto blocksize = limitSampleBufferSize( WindowSizeInt, samplesleft); if(!AboveThreshold(t,i,blocksize)) { @@ -797,7 +774,6 @@ void VoiceKey::CalibrateNoise(WaveTrack & t, sampleCount start, sampleCount len) // int n = len - WindowSizeInt; //This is how many samples we have auto samplesleft = len - WindowSizeInt; - int blocksize; int samples=0; for(auto i = start; samplesleft >= 10; @@ -807,14 +783,7 @@ void VoiceKey::CalibrateNoise(WaveTrack & t, sampleCount start, sampleCount len) //samples left) take a chunk that eats the rest of the samples. samples++; //Increment the number of samples we have - if(samplesleft < (int)WindowSizeInt) - { - blocksize = samplesleft; - } - else - { - blocksize = WindowSizeInt; - } + const auto blocksize = limitSampleBufferSize( WindowSizeInt, samplesleft); erg = TestEnergy(t, i, blocksize); sumerg +=(double)erg; @@ -872,22 +841,19 @@ double VoiceKey::TestEnergy (WaveTrack & t, sampleCount start, sampleCount len) double sum = 1; sampleCount s = start; //Keep track of start sampleCount originalLen = len; //Keep track of the length of block to process (its not the length of t) - sampleCount blockSize = t.GetMaxBlockSize(); //Determine size of sampling buffer - - - if( blockSize > len) - blockSize = len; + const auto blockSize = limitSampleBufferSize( + t.GetMaxBlockSize(), len); //Determine size of sampling buffer float *buffer = new float[blockSize]; //Get a sampling buffer while(len > 0) { - sampleCount block = t.GetBestBlockSize(s); //Figure out how much to grab - if(block > len) block = len; //Don't grab too much! + //Figure out how much to grab + const auto block = limitSampleBufferSize ( t.GetBestBlockSize(s), len ); t.Get((samplePtr)buffer,floatSample, s,block); //grab the block; //Now, go through the block and calculate energy - for(int i = 0; i< block; i++) + for(decltype(+block) i = 0; i< block; i++) { sum += buffer[i]*buffer[i]; } @@ -916,19 +882,16 @@ double VoiceKey::TestSignChanges(WaveTrack & t, sampleCount start, sampleCount l sampleCount s = start; //Keep track of start sampleCount originalLen = len; //Keep track of the length of block to process (its not the length of t) - sampleCount blockSize = t.GetMaxBlockSize(); //Determine size of sampling buffer + const auto blockSize = limitSampleBufferSize( + t.GetMaxBlockSize(), len); //Determine size of sampling buffer unsigned long signchanges = 1; int currentsign=0; - - if( blockSize > len) - blockSize = len; float *buffer = new float[blockSize]; //Get a sampling buffer while(len > 0) { - - sampleCount block = t.GetBestBlockSize(s); //Figure out how much to grab - if(block > len) block = len; //Don't grab too much! + //Figure out how much to grab + const auto block = limitSampleBufferSize ( t.GetBestBlockSize(s), len ); t.Get((samplePtr)buffer,floatSample, s,block); //grab the block; @@ -940,7 +903,7 @@ double VoiceKey::TestSignChanges(WaveTrack & t, sampleCount start, sampleCount l //Now, go through the block and calculate zero crossings - for(int i = 0; i< block; i++) + for(decltype(+block) i = 0; i< block; i++) { if( sgn(buffer[i]) != currentsign) { @@ -975,19 +938,17 @@ double VoiceKey::TestDirectionChanges(WaveTrack & t, sampleCount start, sampleCo sampleCount s = start; //Keep track of start sampleCount originalLen = len; //Keep track of the length of block to process (its not the length of t) - sampleCount blockSize = t.GetMaxBlockSize(); //Determine size of sampling buffer + const auto blockSize = limitSampleBufferSize( + t.GetMaxBlockSize(), len); //Determine size of sampling buffer unsigned long directionchanges = 1; float lastval=float(0); int lastdirection=1; - if( blockSize > len) - blockSize = len; float *buffer = new float[blockSize]; //Get a sampling buffer while(len > 0) { - - sampleCount block = t.GetBestBlockSize(s); //Figure out how much to grab - if(block > len) block = len; //Don't grab too much! + //Figure out how much to grab + const auto block = limitSampleBufferSize ( t.GetBestBlockSize(s), len ); t.Get((samplePtr)buffer,floatSample, s,block); //grab the block; @@ -999,7 +960,7 @@ double VoiceKey::TestDirectionChanges(WaveTrack & t, sampleCount start, sampleCo //Now, go through the block and calculate zero crossings - for(int i = 0; i< block; i++){ + for(decltype(+block) i = 0; i< block; i++){ if( sgn(buffer[i]-lastval) != lastdirection) { directionchanges++; diff --git a/src/WaveClip.cpp b/src/WaveClip.cpp index a63956523..2512e6592 100644 --- a/src/WaveClip.cpp +++ b/src/WaveClip.cpp @@ -1717,9 +1717,7 @@ bool WaveClip::Resample(int rate, ProgressDialog *progress) */ while (pos < numSamples || outGenerated > 0) { - int inLen = numSamples - pos; - if (inLen > bufsize) - inLen = bufsize; + const auto inLen = limitSampleBufferSize( bufsize, numSamples - pos ); bool isLast = ((pos + inLen) == numSamples); diff --git a/src/WaveTrack.cpp b/src/WaveTrack.cpp index d78dba52a..9d26cdc5e 100644 --- a/src/WaveTrack.cpp +++ b/src/WaveTrack.cpp @@ -1459,13 +1459,11 @@ bool WaveTrack::Disjoin(double t0, double t1) sampleCount len = ( end - start ); for( sampleCount done = 0; done < len; done += maxAtOnce ) { - sampleCount numSamples = maxAtOnce; - if( done + maxAtOnce > len ) - numSamples = len - done; + const auto numSamples = limitSampleBufferSize( maxAtOnce, len - done ); clip->GetSamples( ( samplePtr )buffer, floatSample, start + done, numSamples ); - for( sampleCount i = 0; i < numSamples; i++ ) + for( decltype(+numSamples) i = 0; i < numSamples; i++ ) { sampleCount curSamplePos = start + done + i; @@ -2148,7 +2146,7 @@ void WaveTrack::GetEnvelopeValues(double *buffer, size_t bufferLen, // This conditional prevents the previous write past the buffer end, in clip->GetEnvelope() call. // Never increase rlen here. // PRL bug 827: rewrote it again - rlen = static_cast( std::min(sampleCount( rlen ), nClipLen) ); + rlen = limitSampleBufferSize( rlen, nClipLen ); rlen = std::min(rlen, size_t(floor(0.5 + (dClipEndTime - rt0) / tstep))); } clip->GetEnvelope()->GetValues(rbuf, rlen, rt0, tstep); diff --git a/src/commands/CompareAudioCommand.cpp b/src/commands/CompareAudioCommand.cpp index 5e78002d9..48d6bf044 100644 --- a/src/commands/CompareAudioCommand.cpp +++ b/src/commands/CompareAudioCommand.cpp @@ -110,16 +110,13 @@ bool CompareAudioCommand::Apply(CommandExecutionContext context) while (position < s1) { // Get a block of data into the buffers - sampleCount block = mTrack0->GetBestBlockSize(position); - if (position + block > s1) - { - block = s1 - position; - } + const auto block = limitSampleBufferSize( + mTrack0->GetBestBlockSize(position), s1 - position + ); mTrack0->Get((samplePtr)buff0, floatSample, position, block); mTrack1->Get((samplePtr)buff1, floatSample, position, block); - int buffPos = 0; - for (buffPos = 0; buffPos < block; ++buffPos) + for (decltype(+block) buffPos = 0; buffPos < block; ++buffPos) { if (CompareSample(buff0[buffPos], buff1[buffPos]) > errorThreshold) { diff --git a/src/effects/AutoDuck.cpp b/src/effects/AutoDuck.cpp index c62d92f90..fb1396a33 100644 --- a/src/effects/AutoDuck.cpp +++ b/src/effects/AutoDuck.cpp @@ -306,9 +306,7 @@ bool EffectAutoDuck::Process() while (pos < end) { - sampleCount len = end - pos; - if (len > kBufSize) - len = kBufSize; + const auto len = limitSampleBufferSize( kBufSize, end - pos ); mControlTrack->Get((samplePtr)buf, floatSample, pos, (sampleCount)len); @@ -536,9 +534,7 @@ bool EffectAutoDuck::ApplyDuckFade(int trackNumber, WaveTrack* t, while (pos < end) { - sampleCount len = end - pos; - if (len > kBufSize) - len = kBufSize; + const auto len = limitSampleBufferSize( kBufSize, end - pos ); t->Get((samplePtr)buf, floatSample, pos, len); diff --git a/src/effects/DtmfGen.cpp b/src/effects/DtmfGen.cpp index 346f24f99..53fca7129 100644 --- a/src/effects/DtmfGen.cpp +++ b/src/effects/DtmfGen.cpp @@ -194,7 +194,7 @@ sampleCount EffectDtmf::ProcessBlock(float **WXUNUSED(inbuf), float **outbuf, sa numRemaining += (diff-- > 0 ? 1 : 0); } - sampleCount len = wxMin(numRemaining, size); + const auto len = limitSampleBufferSize( size, numRemaining ); if (isTone) { diff --git a/src/effects/Effect.cpp b/src/effects/Effect.cpp index cf77766ed..ffe178a9f 100644 --- a/src/effects/Effect.cpp +++ b/src/effects/Effect.cpp @@ -1546,7 +1546,7 @@ bool Effect::ProcessTrack(int count, sampleCount curBlockSize = 0; sampleCount curDelay = 0; - sampleCount inputBufferCnt = 0; + size_t inputBufferCnt = 0; sampleCount outputBufferCnt = 0; bool cleared = false; @@ -1589,11 +1589,8 @@ bool Effect::ProcessTrack(int count, if (inputBufferCnt == 0) { // Calculate the number of samples to get - inputBufferCnt = mBufferSize; - if (inputBufferCnt > inputRemaining) - { - inputBufferCnt = inputRemaining; - } + inputBufferCnt = + limitSampleBufferSize( mBufferSize, inputRemaining ); // Fill the input buffers left->Get((samplePtr) mInBuffer[0], floatSample, inLeftPos, inputBufferCnt); diff --git a/src/effects/FindClipping.cpp b/src/effects/FindClipping.cpp index 1e80ff14a..bc067e7ab 100644 --- a/src/effects/FindClipping.cpp +++ b/src/effects/FindClipping.cpp @@ -167,7 +167,7 @@ bool EffectFindClipping::ProcessOne(LabelTrack * lt, sampleCount startrun = 0; sampleCount stoprun = 0; sampleCount samps = 0; - sampleCount block = 0; + size_t block = 0; double startTime = -1.0; while (s < len) { @@ -177,7 +177,7 @@ bool EffectFindClipping::ProcessOne(LabelTrack * lt, break; } - block = s + blockSize > len ? len - s : blockSize; + block = limitSampleBufferSize( blockSize, len - s ); wt->Get((samplePtr)buffer, floatSample, start + s, block); ptr = buffer; diff --git a/src/effects/Generator.cpp b/src/effects/Generator.cpp index 4d70e284f..61a3dcd44 100644 --- a/src/effects/Generator.cpp +++ b/src/effects/Generator.cpp @@ -121,12 +121,10 @@ bool BlockGenerator::GenerateTrack(WaveTrack *tmp, numSamples = track.TimeToLongSamples(GetDuration()); sampleCount i = 0; float *data = new float[tmp->GetMaxBlockSize()]; - sampleCount block = 0; while ((i < numSamples) && bGoodResult) { - block = tmp->GetBestBlockSize(i); - if (block > (numSamples - i)) - block = numSamples - i; + const auto block = + limitSampleBufferSize( tmp->GetBestBlockSize(i), numSamples - i ); GenerateBlock(data, track, block); diff --git a/src/effects/NoiseReduction.cpp b/src/effects/NoiseReduction.cpp index 15a1b1396..2081acc21 100644 --- a/src/effects/NoiseReduction.cpp +++ b/src/effects/NoiseReduction.cpp @@ -1295,11 +1295,13 @@ bool EffectNoiseReduction::Worker::ProcessOne FloatVector buffer(bufferSize); bool bLoopSuccess = true; - sampleCount blockSize; sampleCount samplePos = start; while (bLoopSuccess && samplePos < start + len) { //Get a blockSize of samples (smaller than the size of the buffer) - blockSize = std::min(start + len - samplePos, track->GetBestBlockSize(samplePos)); + const auto blockSize = limitSampleBufferSize( + track->GetBestBlockSize(samplePos), + start + len - samplePos + ); //Get the samples from the track and put them in the buffer track->Get((samplePtr)&buffer[0], floatSample, samplePos, blockSize); diff --git a/src/effects/NoiseRemoval.cpp b/src/effects/NoiseRemoval.cpp index 793fe7360..9a7fce27e 100644 --- a/src/effects/NoiseRemoval.cpp +++ b/src/effects/NoiseRemoval.cpp @@ -576,15 +576,14 @@ bool EffectNoiseRemoval::ProcessOne(int count, WaveTrack * track, float *buffer = new float[bufferSize]; bool bLoopSuccess = true; - sampleCount blockSize; sampleCount samplePos = start; while (samplePos < start + len) { //Get a blockSize of samples (smaller than the size of the buffer) - blockSize = track->GetBestBlockSize(samplePos); - //Adjust the block size if it is the final block in the track - if (samplePos + blockSize > start + len) - blockSize = start + len - samplePos; + const auto blockSize = limitSampleBufferSize( + track->GetBestBlockSize(samplePos), + start + len - samplePos + ); //Get the samples from the track and put them in the buffer track->Get((samplePtr)buffer, floatSample, samplePos, blockSize); diff --git a/src/effects/Normalize.cpp b/src/effects/Normalize.cpp index 481515d1a..c4422fbd0 100644 --- a/src/effects/Normalize.cpp +++ b/src/effects/Normalize.cpp @@ -392,11 +392,11 @@ bool EffectNormalize::AnalyseDC(WaveTrack * track, const wxString &msg) s = start; while (s < end) { //Get a block of samples (smaller than the size of the buffer) - sampleCount block = track->GetBestBlockSize(s); - //Adjust the block size if it is the final block in the track - if (s + block > end) - block = end - s; + const auto block = limitSampleBufferSize( + track->GetBestBlockSize(s), + end - s + ); //Get the samples from the track and put them in the buffer track->Get((samplePtr) buffer, floatSample, s, block); @@ -450,11 +450,11 @@ bool EffectNormalize::ProcessOne(WaveTrack * track, const wxString &msg) s = start; while (s < end) { //Get a block of samples (smaller than the size of the buffer) - sampleCount block = track->GetBestBlockSize(s); - //Adjust the block size if it is the final block in the track - if (s + block > end) - block = end - s; + const auto block = limitSampleBufferSize( + track->GetBestBlockSize(s), + end - s + ); //Get the samples from the track and put them in the buffer track->Get((samplePtr) buffer, floatSample, s, block); diff --git a/src/effects/Reverse.cpp b/src/effects/Reverse.cpp index 4333efdd9..d0ddebb65 100644 --- a/src/effects/Reverse.cpp +++ b/src/effects/Reverse.cpp @@ -229,14 +229,13 @@ bool EffectReverse::ProcessOneClip(int count, WaveTrack *track, sampleCount originalLen = (sampleCount)originalEnd-originalStart; while (len > 1) { - sampleCount block = track->GetBestBlockSize(first); - if (block > len / 2) - block = len / 2; + const auto block = + limitSampleBufferSize( track->GetBestBlockSize(first), len / 2 ); second = first + (len - block); track->Get((samplePtr)buffer1, floatSample, first, block); track->Get((samplePtr)buffer2, floatSample, second, block); - for (int i = 0; i < block; i++) { + for (decltype(+block) i = 0; i < block; i++) { tmp = buffer1[i]; buffer1[i] = buffer2[block-i-1]; buffer2[block-i-1] = tmp; diff --git a/src/effects/SBSMSEffect.cpp b/src/effects/SBSMSEffect.cpp index 4464effb1..595ff5587 100644 --- a/src/effects/SBSMSEffect.cpp +++ b/src/effects/SBSMSEffect.cpp @@ -98,18 +98,17 @@ long resampleCB(void *cb_data, SBSMSFrame *data) { ResampleBuf *r = (ResampleBuf*) cb_data; - long blockSize = r->leftTrack->GetBestBlockSize(r->offset); - - //Adjust the block size if it is the final block in the track - if (r->offset + blockSize > r->end) - blockSize = r->end - r->offset; + const auto blockSize = limitSampleBufferSize( + r->leftTrack->GetBestBlockSize(r->offset), + r->end - r->offset + ); // Get the samples from the tracks and put them in the buffers. r->leftTrack->Get((samplePtr)(r->leftBuffer), floatSample, r->offset, blockSize); r->rightTrack->Get((samplePtr)(r->rightBuffer), floatSample, r->offset, blockSize); // convert to sbsms audio format - for(int i=0; ibuf[i][0] = r->leftBuffer[i]; r->buf[i][1] = r->rightBuffer[i]; } @@ -376,12 +375,9 @@ bool EffectSBSMS::Process() // process while(possamplesOut) { - frames = samplesOut - pos; - } else { - frames = SBSMSOutBlockSize; - } + const auto frames = + limitSampleBufferSize( SBSMSOutBlockSize, samplesOut - pos ); + outputCount = resampler.read(outBuf,frames); for(int i = 0; i < outputCount; i++) { outBufLeft[i] = outBuf[i][0]; diff --git a/src/effects/SimpleMono.cpp b/src/effects/SimpleMono.cpp index 25468ff33..a6a81774d 100644 --- a/src/effects/SimpleMono.cpp +++ b/src/effects/SimpleMono.cpp @@ -96,11 +96,9 @@ bool EffectSimpleMono::ProcessOne(WaveTrack * track, s = start; while (s < end) { //Get a block of samples (smaller than the size of the buffer) - sampleCount block = track->GetBestBlockSize(s); - //Adjust the block size if it is the final block in the track - if (s + block > end) - block = end - s; + const auto block = + limitSampleBufferSize( track->GetBestBlockSize(s), end - s ); //Get the samples from the track and put them in the buffer track->Get((samplePtr) buffer, floatSample, s, block); diff --git a/src/effects/SoundTouchEffect.cpp b/src/effects/SoundTouchEffect.cpp index 6f7dc52eb..6db19d725 100644 --- a/src/effects/SoundTouchEffect.cpp +++ b/src/effects/SoundTouchEffect.cpp @@ -195,11 +195,8 @@ bool EffectSoundTouch::ProcessOne(WaveTrack *track, s = start; while (s < end) { //Get a block of samples (smaller than the size of the buffer) - sampleCount block = track->GetBestBlockSize(s); - - //Adjust the block size if it is the final block in the track - if (s + block > end) - block = end - s; + const auto block = + limitSampleBufferSize( track->GetBestBlockSize(s), end - s ); //Get the samples from the track and put them in the buffer track->Get((samplePtr) buffer, floatSample, s, block); @@ -283,18 +280,18 @@ bool EffectSoundTouch::ProcessStereo(WaveTrack* leftTrack, WaveTrack* rightTrack sampleCount sourceSampleCount = start; while (sourceSampleCount < end) { //Get a block of samples (smaller than the size of the buffer) - sampleCount blockSize = leftTrack->GetBestBlockSize(sourceSampleCount); - //Adjust the block size if it is the final block in the track - if (sourceSampleCount + blockSize > end) - blockSize = end - sourceSampleCount; + const auto blockSize = limitSampleBufferSize( + leftTrack->GetBestBlockSize(sourceSampleCount), + end - sourceSampleCount + ); // Get the samples from the tracks and put them in the buffers. leftTrack->Get((samplePtr)(leftBuffer), floatSample, sourceSampleCount, blockSize); rightTrack->Get((samplePtr)(rightBuffer), floatSample, sourceSampleCount, blockSize); // Interleave into soundTouchBuffer. - for (int index = 0; index < blockSize; index++) { + for (decltype(+blockSize) index = 0; index < blockSize; index++) { soundTouchBuffer[index*2] = leftBuffer[index]; soundTouchBuffer[(index*2)+1] = rightBuffer[index]; } diff --git a/src/effects/StereoToMono.cpp b/src/effects/StereoToMono.cpp index 892990d8e..b79d63d97 100644 --- a/src/effects/StereoToMono.cpp +++ b/src/effects/StereoToMono.cpp @@ -144,11 +144,8 @@ bool EffectStereoToMono::ProcessOne(int count) while (index < mEnd) { bResult &= mLeftTrack->Get((samplePtr)leftBuffer, floatSample, index, idealBlockLen); bResult &= mRightTrack->Get((samplePtr)rightBuffer, floatSample, index, idealBlockLen); - sampleCount limit = idealBlockLen; - if ((index + idealBlockLen) > mEnd) { - limit = mEnd - index; - } - for (sampleCount i = 0; i < limit; ++i) { + const auto limit = limitSampleBufferSize( idealBlockLen, mEnd - index ); + for (decltype(+limit) i = 0; i < limit; ++i) { index++; curLeftFrame = leftBuffer[i]; curRightFrame = rightBuffer[i]; diff --git a/src/effects/TruncSilence.cpp b/src/effects/TruncSilence.cpp index c97d8b266..dd554593e 100644 --- a/src/effects/TruncSilence.cpp +++ b/src/effects/TruncSilence.cpp @@ -624,16 +624,13 @@ bool EffectTruncSilence::Analyze(RegionList& silenceList, // End of optimization // Limit size of current block if we've reached the end - sampleCount count = blockLen; - if ((*index + count) > end) { - count = end - *index; - } + const auto count = limitSampleBufferSize( blockLen, end - *index ); // Fill buffer wt->Get((samplePtr)(buffer), floatSample, *index, count); // Look for silenceList in current block - for (sampleCount i = 0; i < count; ++i) { + for (decltype(+count) i = 0; i < count; ++i) { if (inputLength && ((outLength >= previewLen) || (outLength > wt->TimeToLongSamples(*minInputLength)))) { *inputLength = wt->LongSamplesToTime(*index + i) - wt->LongSamplesToTime(start); break; diff --git a/src/effects/TwoPassSimpleMono.cpp b/src/effects/TwoPassSimpleMono.cpp index 4c3362d2f..8037853f9 100644 --- a/src/effects/TwoPassSimpleMono.cpp +++ b/src/effects/TwoPassSimpleMono.cpp @@ -100,7 +100,7 @@ bool EffectTwoPassSimpleMono::ProcessOne(WaveTrack * track, sampleCount start, sampleCount end) { bool ret; - sampleCount s, samples1, samples2, tmpcount; + sampleCount s; float *tmpfloat; //Get the length of the buffer (as double). len is @@ -113,12 +113,8 @@ bool EffectTwoPassSimpleMono::ProcessOne(WaveTrack * track, //be shorter than the length of the track being processed. float *buffer1 = new float[maxblock]; float *buffer2 = new float[maxblock]; - samples1 = track->GetBestBlockSize(start); - if(start + samples1 > end) - samples1 = end - start; - - if(samples1 > maxblock) - samples1 = maxblock; + auto samples1 = limitSampleBufferSize( + std::min( maxblock, track->GetBestBlockSize(start) ), end - start ); //Get the samples from the track and put them in the buffer track->Get((samplePtr) buffer1, floatSample, start, samples1); @@ -141,14 +137,10 @@ bool EffectTwoPassSimpleMono::ProcessOne(WaveTrack * track, s = start + samples1; while (s < end) { //Get a block of samples (smaller than the size of the buffer) - samples2 = track->GetBestBlockSize(s); - - if(samples2 > maxblock) - samples2 = maxblock; - //Adjust the block size if it is the final block in the track - if (s + samples2 > end) - samples2 = end - s; + auto samples2 = limitSampleBufferSize( + std::min( track->GetBestBlockSize(s), maxblock ), end - s + ); //Get the samples from the track and put them in the buffer track->Get((samplePtr) buffer2, floatSample, s, samples2); @@ -189,9 +181,7 @@ bool EffectTwoPassSimpleMono::ProcessOne(WaveTrack * track, buffer1 = buffer2; buffer2 = tmpfloat; - tmpcount = samples1; - samples1 = samples2; - samples2 = tmpcount; + std::swap(samples1, samples2); } // Send the last buffer with a NULL pointer for the current buffer diff --git a/src/effects/nyquist/Nyquist.cpp b/src/effects/nyquist/Nyquist.cpp index cce95014f..a2c90968e 100644 --- a/src/effects/nyquist/Nyquist.cpp +++ b/src/effects/nyquist/Nyquist.cpp @@ -1740,9 +1740,9 @@ int NyquistEffect::GetCallback(float *buffer, int ch, mCurBufferLen[ch] = mCurTrack[ch]->GetIdealBlockSize(); } - if (mCurBufferStart[ch] + mCurBufferLen[ch] > mCurStart[ch] + mCurLen) { - mCurBufferLen[ch] = mCurStart[ch] + mCurLen - mCurBufferStart[ch]; - } + mCurBufferLen[ch] = + limitSampleBufferSize( mCurBufferLen[ch], + mCurStart[ch] + mCurLen - mCurBufferStart[ch] ); mCurBuffer[ch].Allocate(mCurBufferLen[ch], floatSample); if (!mCurTrack[ch]->Get(mCurBuffer[ch].ptr(), floatSample, diff --git a/src/effects/nyquist/Nyquist.h b/src/effects/nyquist/Nyquist.h index 3334034c2..237efe71d 100644 --- a/src/effects/nyquist/Nyquist.h +++ b/src/effects/nyquist/Nyquist.h @@ -222,7 +222,7 @@ private: SampleBuffer mCurBuffer[2]; sampleCount mCurBufferStart[2]; - sampleCount mCurBufferLen[2]; + size_t mCurBufferLen[2]; std::unique_ptr mOutputTrack[2]; diff --git a/src/effects/vamp/VampEffect.cpp b/src/effects/vamp/VampEffect.cpp index eb2a65e89..249f08812 100644 --- a/src/effects/vamp/VampEffect.cpp +++ b/src/effects/vamp/VampEffect.cpp @@ -498,8 +498,7 @@ bool VampEffect::Process() while (len != 0) { - int request = block; - if (request > len) request = len; + const auto request = limitSampleBufferSize( block, len ); if (left) { @@ -511,11 +510,11 @@ bool VampEffect::Process() right->Get((samplePtr)data[1], floatSample, rs, request); } - if (request < (int)block) + if (request < block) { for (int c = 0; c < channels; ++c) { - for (int i = request; i < (int)block; ++i) + for (decltype(block) i = request; i < block; ++i) { data[c][i] = 0.f; } diff --git a/src/import/ImportFFmpeg.cpp b/src/import/ImportFFmpeg.cpp index 9c7a42350..76e6d9998 100644 --- a/src/import/ImportFFmpeg.cpp +++ b/src/import/ImportFFmpeg.cpp @@ -606,9 +606,8 @@ int FFmpegImportFileHandle::Import(TrackFactory *trackFactory, sampleCount maxBlockSize = t->GetMaxBlockSize(); //use the maximum blockfile size to divide the sections (about 11secs per blockfile at 44.1khz) for (sampleCount i = 0; i < sampleDuration; i += maxBlockSize) { - sampleCount blockLen = maxBlockSize; - if (i + blockLen > sampleDuration) - blockLen = sampleDuration - i; + const auto blockLen = + limitSampleBufferSize( maxBlockSize, sampleDuration - i ); t->AppendCoded(mFilename, i, blockLen, c, ODTask::eODFFMPEG); diff --git a/src/import/ImportFLAC.cpp b/src/import/ImportFLAC.cpp index 3934dda76..f61745409 100644 --- a/src/import/ImportFLAC.cpp +++ b/src/import/ImportFLAC.cpp @@ -487,12 +487,11 @@ int FLACImportFileHandle::Import(TrackFactory *trackFactory, //add the task to the ODManager if(useOD) { - sampleCount fileTotalFrames = mNumSamples; + sampleCount fileTotalFrames = (sampleCount)mNumSamples; sampleCount maxBlockSize = mChannels.begin()->get()->GetMaxBlockSize(); for (sampleCount i = 0; i < fileTotalFrames; i += maxBlockSize) { - sampleCount blockLen = maxBlockSize; - if (i + blockLen > fileTotalFrames) - blockLen = fileTotalFrames - i; + const auto blockLen = + limitSampleBufferSize( maxBlockSize, fileTotalFrames - i ); auto iter = mChannels.begin(); for (int c = 0; c < mNumChannels; ++c, ++iter) diff --git a/src/import/ImportPCM.cpp b/src/import/ImportPCM.cpp index 3bd7ebc6e..8feadedf0 100644 --- a/src/import/ImportPCM.cpp +++ b/src/import/ImportPCM.cpp @@ -388,10 +388,8 @@ int PCMImportFileHandle::Import(TrackFactory *trackFactory, int updateCounter = 0; for (sampleCount i = 0; i < fileTotalFrames; i += maxBlockSize) { - - sampleCount blockLen = maxBlockSize; - if (i + blockLen > fileTotalFrames) - blockLen = fileTotalFrames - i; + const auto blockLen = + limitSampleBufferSize( maxBlockSize, fileTotalFrames - i ); auto iter = channels.begin(); for (int c = 0; c < mInfo.channels; ++iter, ++c) diff --git a/src/import/ImportRaw.cpp b/src/import/ImportRaw.cpp index 6380cacd8..fb81bb2d6 100644 --- a/src/import/ImportRaw.cpp +++ b/src/import/ImportRaw.cpp @@ -105,7 +105,7 @@ void ImportRaw(wxWindow *parent, const wxString &fileName, double percent = 100.0; TrackHolders channels; int updateResult = eProgressSuccess; - long block; + { SF_INFO sndInfo; int result; @@ -224,6 +224,10 @@ void ImportRaw(wxWindow *parent, const wxString &fileName, SampleBuffer buffer(maxBlockSize, format); sampleCount framescompleted = 0; + if (totalFrames < 0) { + wxASSERT(false); + totalFrames = 0; + } wxString msg; @@ -232,27 +236,38 @@ void ImportRaw(wxWindow *parent, const wxString &fileName, /* i18n-hint: 'Raw' means 'unprocessed' here and should usually be tanslated.*/ ProgressDialog progress(_("Import Raw"), msg); + size_t block; do { - block = maxBlockSize; - - if (block + framescompleted > totalFrames) - block = totalFrames - framescompleted; + block = + limitSampleBufferSize( maxBlockSize, totalFrames - framescompleted ); + sf_count_t result; if (format == int16Sample) - block = SFCall(sf_readf_short, sndFile.get(), (short *)srcbuffer.ptr(), block); + result = SFCall(sf_readf_short, sndFile.get(), (short *)srcbuffer.ptr(), block); else - block = SFCall(sf_readf_float, sndFile.get(), (float *)srcbuffer.ptr(), block); + result = SFCall(sf_readf_float, sndFile.get(), (float *)srcbuffer.ptr(), block); + + if (result >= 0) { + block = result; + } + else { + // This is not supposed to happen, sndfile.h says result is always + // a count, not an invalid value for error + wxASSERT(false); + updateResult = eProgressFailed; + break; + } if (block) { auto iter = channels.begin(); for(int c=0; c 0 && framescompleted < totalFrames); } - int res = updateResult; - if (block < 0) - res = eProgressFailed; - - if (res == eProgressFailed || res == eProgressCancelled) { + if (updateResult == eProgressFailed || updateResult == eProgressCancelled) { // It's a shame we can't return proper error code return; }