1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-05-05 22:28:57 +02:00

Merge commit '1281f1b14b03355f694cd104d6b4d9919de107a6'

* commit '1281f1b14b03355f694cd104d6b4d9919de107a6':
  Common function limits buffer size to sampleCount known to be small
This commit is contained in:
Paul Licameli 2016-08-23 12:46:37 -04:00
commit 0d3c00662e
31 changed files with 161 additions and 226 deletions

View File

@ -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<size_t> (
std::min( sampleCount( bufferSize ), std::max( sampleCount(0), limit ) )
);
}
// ----------------------------------------------------------------------------
// Supported sample formats
// ----------------------------------------------------------------------------

View File

@ -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

View File

@ -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];
}

View File

@ -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)

View File

@ -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<samplePtr>(&frequencySnappingData[0]),

View File

@ -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++;

View File

@ -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);

View File

@ -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<size_t>( 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);

View File

@ -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)
{

View File

@ -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);

View File

@ -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)
{

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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;

View File

@ -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; i<blockSize; i++) {
for(decltype(+blockSize) i=0; i<blockSize; i++) {
r->buf[i][0] = r->leftBuffer[i];
r->buf[i][1] = r->rightBuffer[i];
}
@ -376,12 +375,9 @@ bool EffectSBSMS::Process()
// process
while(pos<samplesOut && outputCount) {
long frames;
if(pos+SBSMSOutBlockSize>samplesOut) {
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];

View File

@ -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);

View File

@ -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];
}

View File

@ -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];

View File

@ -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;

View File

@ -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

View File

@ -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,

View File

@ -222,7 +222,7 @@ private:
SampleBuffer mCurBuffer[2];
sampleCount mCurBufferStart[2];
sampleCount mCurBufferLen[2];
size_t mCurBufferLen[2];
std::unique_ptr<WaveTrack> mOutputTrack[2];

View File

@ -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;
}

View File

@ -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);

View File

@ -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)

View File

@ -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)

View File

@ -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_count_t>(sf_readf_short, sndFile.get(), (short *)srcbuffer.ptr(), block);
result = SFCall<sf_count_t>(sf_readf_short, sndFile.get(), (short *)srcbuffer.ptr(), block);
else
block = SFCall<sf_count_t>(sf_readf_float, sndFile.get(), (float *)srcbuffer.ptr(), block);
result = SFCall<sf_count_t>(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<numChannels; ++iter, ++c) {
if (format==int16Sample) {
for(int j=0; j<block; j++)
for(decltype(block) j=0; j<block; j++)
((short *)buffer.ptr())[j] =
((short *)srcbuffer.ptr())[numChannels*j+c];
}
else {
for(int j=0; j<block; j++)
for(decltype(block) j=0; j<block; j++)
((float *)buffer.ptr())[j] =
((float *)srcbuffer.ptr())[numChannels*j+c];
}
@ -270,11 +285,7 @@ void ImportRaw(wxWindow *parent, const wxString &fileName,
} while (block > 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;
}