From 321d5259a279f9dc8c40c5bbb65a44660c34fde8 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Mon, 1 Feb 2016 10:16:00 -0500 Subject: [PATCH] More uses of SampleBuffer, eliminating explicit DeleteSamples calls --- src/AudioIO.cpp | 40 +++++++-------------- src/AudioIO.h | 3 +- src/BlockFile.cpp | 16 ++++----- src/Dependencies.cpp | 12 ++++--- src/Mix.cpp | 32 ++++++++--------- src/Mix.h | 4 +-- src/RingBuffer.cpp | 11 +++--- src/RingBuffer.h | 2 +- src/SampleFormat.h | 51 +++++++++++++++++++++------ src/WaveClip.cpp | 23 +++++------- src/WaveClip.h | 2 +- src/WaveTrack.h | 2 +- src/blockfile/LegacyBlockFile.cpp | 19 +++++----- src/blockfile/ODDecodeBlockFile.cpp | 5 ++- src/blockfile/ODPCMAliasBlockFile.cpp | 19 +++++----- src/blockfile/PCMAliasBlockFile.cpp | 11 +++--- src/blockfile/SimpleBlockFile.cpp | 8 ++--- src/import/ImportPCM.cpp | 23 ++++++------ src/ondemand/ODDecodeFFmpegTask.cpp | 8 ++--- src/ondemand/ODDecodeFlacTask.cpp | 6 ++-- src/ondemand/ODDecodeFlacTask.h | 2 +- src/ondemand/ODDecodeTask.h | 2 +- 22 files changed, 145 insertions(+), 156 deletions(-) diff --git a/src/AudioIO.cpp b/src/AudioIO.cpp index 4afc5384c..798438c97 100644 --- a/src/AudioIO.cpp +++ b/src/AudioIO.cpp @@ -879,9 +879,6 @@ AudioIO::AudioIO() #ifdef EXPERIMENTAL_AUTOMATED_INPUT_LEVEL_ADJUSTMENT mAILAActive = false; #endif - mSilentBuf = NULL; - mLastSilentBufSize = 0; - mStreamToken = 0; mLastPaError = paNoError; @@ -989,9 +986,6 @@ AudioIO::~AudioIO() mThread->Delete(); - if(mSilentBuf) - DeleteSamples(mSilentBuf); - delete mThread; #ifdef EXPERIMENTAL_SCRUBBING_SUPPORT @@ -3364,16 +3358,9 @@ void AudioIO::FillBuffers() // numbers of samples for all channels for this pass of the do-loop. if(processed < frames && mPlayMode != PLAY_STRAIGHT) { - if(mLastSilentBufSize < frames) - { - //delete old if necessary - if(mSilentBuf) - DeleteSamples(mSilentBuf); - mLastSilentBufSize = frames; - mSilentBuf = NewSamples(mLastSilentBufSize, floatSample); - ClearSamples(mSilentBuf, floatSample, 0, mLastSilentBufSize); - } - mPlaybackBuffers[i]->Put(mSilentBuf, floatSample, frames - processed); + mSilentBuf.Resize(frames, floatSample); + ClearSamples(mSilentBuf.ptr(), floatSample, 0, frames); + mPlaybackBuffers[i]->Put(mSilentBuf.ptr(), floatSample, frames - processed); } } @@ -3463,28 +3450,25 @@ void AudioIO::FillBuffers() if( mFactor == 1.0 ) { - samplePtr temp = NewSamples(avail, trackFormat); - mCaptureBuffers[i]->Get (temp, trackFormat, avail); - (*mCaptureTracks)[i]-> Append(temp, trackFormat, avail, 1, + SampleBuffer temp(avail, trackFormat); + mCaptureBuffers[i]->Get (temp.ptr(), trackFormat, avail); + (*mCaptureTracks)[i]-> Append(temp.ptr(), trackFormat, avail, 1, &appendLog); - DeleteSamples(temp); } else { int size = lrint(avail * mFactor); - samplePtr temp1 = NewSamples(avail, floatSample); - samplePtr temp2 = NewSamples(size, floatSample); - mCaptureBuffers[i]->Get(temp1, floatSample, avail); + SampleBuffer temp1(avail, floatSample); + SampleBuffer temp2(size, floatSample); + mCaptureBuffers[i]->Get(temp1.ptr(), floatSample, avail); /* we are re-sampling on the fly. The last resampling call * must flush any samples left in the rate conversion buffer * so that they get recorded */ - size = mResample[i]->Process(mFactor, (float *)temp1, avail, !IsStreamActive(), - &size, (float *)temp2, size); - (*mCaptureTracks)[i]-> Append(temp2, floatSample, size, 1, + size = mResample[i]->Process(mFactor, (float *)temp1.ptr(), avail, !IsStreamActive(), + &size, (float *)temp2.ptr(), size); + (*mCaptureTracks)[i]-> Append(temp2.ptr(), floatSample, size, 1, &appendLog); - DeleteSamples(temp1); - DeleteSamples(temp2); } if (!appendLog.IsEmpty()) diff --git a/src/AudioIO.h b/src/AudioIO.h index 9aac65a86..a66580742 100644 --- a/src/AudioIO.h +++ b/src/AudioIO.h @@ -648,8 +648,7 @@ private: double mCutPreviewGapStart; double mCutPreviewGapLen; - samplePtr mSilentBuf; - sampleCount mLastSilentBufSize; + GrowableSampleBuffer mSilentBuf; AudioIOListener* mListener; diff --git a/src/BlockFile.cpp b/src/BlockFile.cpp index 1c434a2df..cbd3a259e 100644 --- a/src/BlockFile.cpp +++ b/src/BlockFile.cpp @@ -386,8 +386,8 @@ void BlockFile::GetMinMax(sampleCount start, sampleCount len, float *outMin, float *outMax, float *outRMS) { // TODO: actually use summaries - samplePtr blockData = NewSamples(len, floatSample); - this->ReadData(blockData, floatSample, start, len); + SampleBuffer blockData(len, floatSample); + this->ReadData(blockData.ptr(), floatSample, start, len); float min = FLT_MAX; float max = -FLT_MAX; @@ -395,7 +395,7 @@ void BlockFile::GetMinMax(sampleCount start, sampleCount len, for( int i = 0; i < len; i++ ) { - float sample = ((float*)blockData)[i]; + float sample = ((float*)blockData.ptr())[i]; if( sample > max ) max = sample; @@ -404,8 +404,6 @@ void BlockFile::GetMinMax(sampleCount start, sampleCount len, sumsq += (sample*sample); } - DeleteSamples(blockData); - *outMin = min; *outMax = max; *outRMS = sqrt(sumsq/len); @@ -579,14 +577,12 @@ void AliasBlockFile::WriteSummary() // To build the summary data, call ReadData (implemented by the // derived classes) to get the sample data - samplePtr sampleData = NewSamples(mLen, floatSample); - this->ReadData(sampleData, floatSample, 0, mLen); + SampleBuffer sampleData(mLen, floatSample); + this->ReadData(sampleData.ptr(), floatSample, 0, mLen); - void *summaryData = BlockFile::CalcSummary(sampleData, mLen, + void *summaryData = BlockFile::CalcSummary(sampleData.ptr(), mLen, floatSample); summaryFile.Write(summaryData, mSummaryInfo.totalSummaryBytes); - - DeleteSamples(sampleData); } AliasBlockFile::~AliasBlockFile() diff --git a/src/Dependencies.cpp b/src/Dependencies.cpp index 3a3df38e1..77cf4fce9 100644 --- a/src/Dependencies.cpp +++ b/src/Dependencies.cpp @@ -202,11 +202,13 @@ static void RemoveDependencies(AudacityProject *project, // Convert it from an aliased file to an actual file in the project. unsigned int len = aliasBlockFile->GetLength(); - samplePtr buffer = NewSamples(len, format); - f->ReadData(buffer, format, 0, len); - BlockFile *newBlockFile = - dirManager->NewSimpleBlockFile(buffer, len, format); - DeleteSamples(buffer); + BlockFile *newBlockFile; + { + SampleBuffer buffer(len, format); + f->ReadData(buffer.ptr(), format, 0, len); + newBlockFile = + dirManager->NewSimpleBlockFile(buffer.ptr(), len, format); + } // Update our hash so we know what block files we've done blockFileHash[f] = newBlockFile; diff --git a/src/Mix.cpp b/src/Mix.cpp index 8d67540a2..4ae7dc562 100644 --- a/src/Mix.cpp +++ b/src/Mix.cpp @@ -294,11 +294,11 @@ Mixer::Mixer(int numInputTracks, WaveTrack **inputTracks, mInterleavedBufferSize = mBufferSize; } - mBuffer = new samplePtr[mNumBuffers]; - mTemp = new samplePtr[mNumBuffers]; + mBuffer = new SampleBuffer[mNumBuffers]; + mTemp = new SampleBuffer[mNumBuffers]; for (int c = 0; c < mNumBuffers; c++) { - mBuffer[c] = NewSamples(mInterleavedBufferSize, mFormat); - mTemp[c] = NewSamples(mInterleavedBufferSize, floatSample); + mBuffer[c].Allocate(mInterleavedBufferSize, mFormat); + mTemp[c].Allocate(mInterleavedBufferSize, floatSample); } mFloatBuffer = new float[mInterleavedBufferSize]; @@ -356,10 +356,6 @@ Mixer::~Mixer() { int i; - for (i = 0; i < mNumBuffers; i++) { - DeleteSamples(mBuffer[i]); - DeleteSamples(mTemp[i]); - } delete[] mBuffer; delete[] mTemp; delete[] mInputTrack; @@ -386,12 +382,12 @@ void Mixer::ApplyTrackGains(bool apply) void Mixer::Clear() { for (int c = 0; c < mNumBuffers; c++) { - memset(mTemp[c], 0, mInterleavedBufferSize * SAMPLE_SIZE(floatSample)); + memset(mTemp[c].ptr(), 0, mInterleavedBufferSize * SAMPLE_SIZE(floatSample)); } } void MixBuffers(int numChannels, int *channelFlags, float *gains, - samplePtr src, samplePtr *dests, + samplePtr src, SampleBuffer *dests, int len, bool interleaved) { for (int c = 0; c < numChannels; c++) { @@ -402,10 +398,10 @@ void MixBuffers(int numChannels, int *channelFlags, float *gains, int skip; if (interleaved) { - destPtr = dests[0] + c*SAMPLE_SIZE(floatSample); + destPtr = dests[0].ptr() + c*SAMPLE_SIZE(floatSample); skip = numChannels; } else { - destPtr = dests[c]; + destPtr = dests[c].ptr(); skip = 1; } @@ -689,9 +685,9 @@ sampleCount Mixer::Process(sampleCount maxToProcess) } if(mInterleaved) { for(int c=0; c 64 ? size : 64) + , mBuffer(mBufferSize, mFormat) { - mFormat = format; - mBufferSize = (size > 64? size: 64); mStart = 0; mEnd = 0; - mBuffer = NewSamples(mBufferSize, mFormat); } RingBuffer::~RingBuffer() { - DeleteSamples(mBuffer); } int RingBuffer::Len() @@ -74,7 +73,7 @@ int RingBuffer::Put(samplePtr buffer, sampleFormat format, block = mBufferSize - pos; CopySamples(src, format, - mBuffer + pos * SAMPLE_SIZE(mFormat), mFormat, + mBuffer.ptr() + pos * SAMPLE_SIZE(mFormat), mFormat, block); src += block * SAMPLE_SIZE(format); @@ -116,7 +115,7 @@ int RingBuffer::Get(samplePtr buffer, sampleFormat format, if (block > mBufferSize - mStart) block = mBufferSize - mStart; - CopySamples(mBuffer + mStart * SAMPLE_SIZE(mFormat), mFormat, + CopySamples(mBuffer.ptr() + mStart * SAMPLE_SIZE(mFormat), mFormat, dest, format, block); diff --git a/src/RingBuffer.h b/src/RingBuffer.h index 6712129be..8806d713c 100644 --- a/src/RingBuffer.h +++ b/src/RingBuffer.h @@ -40,7 +40,7 @@ class RingBuffer { int mStart; int mEnd; int mBufferSize; - samplePtr mBuffer; + SampleBuffer mBuffer; }; #endif /* __AUDACITY_RING_BUFFER__ */ diff --git a/src/SampleFormat.h b/src/SampleFormat.h index 7123abcd4..ed1546877 100644 --- a/src/SampleFormat.h +++ b/src/SampleFormat.h @@ -56,10 +56,10 @@ class SampleBuffer { public: SampleBuffer() - : mCount(0), mPtr(0) + : mPtr(0) {} SampleBuffer(int count, sampleFormat format) - : mCount(count), mPtr(NewSamples(mCount, format)) + : mPtr(NewSamples(count, format)) {} ~SampleBuffer() { @@ -67,30 +67,61 @@ public: } // WARNING! May not preserve contents. - void Resize(int count, sampleFormat format) + SampleBuffer &Allocate(int count, sampleFormat format) { - if (mCount < count) { - Free(); - mPtr = NewSamples(count, format); - mCount = count; - } + Free(); + mPtr = NewSamples(count, format); + return *this; } + void Free() { DeleteSamples(mPtr); mPtr = 0; - mCount = 0; } samplePtr ptr() const { return mPtr; } private: - int mCount; samplePtr mPtr; }; +class GrowableSampleBuffer : private SampleBuffer +{ +public: + GrowableSampleBuffer() + : SampleBuffer() + , mCount(0) + {} + + GrowableSampleBuffer(int count, sampleFormat format) + : SampleBuffer(count, format) + , mCount(count) + {} + + GrowableSampleBuffer &Resize(int count, sampleFormat format) + { + if (!ptr() || mCount < count) { + Allocate(count, format); + mCount = count; + } + return *this; + } + + void Free() + { + SampleBuffer::Free(); + mCount = 0; + } + + using SampleBuffer::ptr; + +private: + int mCount; +}; + // // Copying, Converting and Clearing Samples // diff --git a/src/WaveClip.cpp b/src/WaveClip.cpp index f55bb15e5..a8ddbb95a 100644 --- a/src/WaveClip.cpp +++ b/src/WaveClip.cpp @@ -302,7 +302,6 @@ WaveClip::WaveClip(DirManager *projDirManager, sampleFormat format, int rate) mWaveCache = new WaveCache(); mSpecCache = new SpecCache(); mSpecPxCache = new SpecPxCache(1); - mAppendBuffer = NULL; mAppendBufferLen = 0; mDirty = 0; mIsPlaceholder = false; @@ -328,7 +327,6 @@ WaveClip::WaveClip(const WaveClip& orig, DirManager *projDirManager) for (WaveClipList::compatibility_iterator it=orig.mCutLines.GetFirst(); it; it=it->GetNext()) mCutLines.Append(new WaveClip(*it->GetData(), projDirManager)); - mAppendBuffer = NULL; mAppendBufferLen = 0; mDirty = 0; mIsPlaceholder = orig.GetIsPlaceholder(); @@ -345,9 +343,6 @@ WaveClip::~WaveClip() delete mSpecCache; delete mSpecPxCache; - if (mAppendBuffer) - DeleteSamples(mAppendBuffer); - mCutLines.DeleteContents(true); mCutLines.Clear(); } @@ -668,10 +663,10 @@ bool WaveClip::GetWaveDisplay(WaveDisplay &display, double t0, sampleCount j; if (seqFormat == floatSample) - b = &((float *)mAppendBuffer)[left]; + b = &((float *)mAppendBuffer.ptr())[left]; else { b = new float[len]; - CopySamples(mAppendBuffer + left*SAMPLE_SIZE(seqFormat), + CopySamples(mAppendBuffer.ptr() + left*SAMPLE_SIZE(seqFormat), seqFormat, (samplePtr)b, floatSample, len); } @@ -1263,18 +1258,18 @@ bool WaveClip::Append(samplePtr buffer, sampleFormat format, sampleCount blockSize = mSequence->GetIdealAppendLen(); sampleFormat seqFormat = mSequence->GetSampleFormat(); - if (!mAppendBuffer) - mAppendBuffer = NewSamples(maxBlockSize, seqFormat); + if (!mAppendBuffer.ptr()) + mAppendBuffer.Allocate(maxBlockSize, seqFormat); for(;;) { if (mAppendBufferLen >= blockSize) { bool success = - mSequence->Append(mAppendBuffer, seqFormat, blockSize, + mSequence->Append(mAppendBuffer.ptr(), seqFormat, blockSize, blockFileLog); if (!success) return false; - memmove(mAppendBuffer, - mAppendBuffer + blockSize * SAMPLE_SIZE(seqFormat), + memmove(mAppendBuffer.ptr(), + mAppendBuffer.ptr() + blockSize * SAMPLE_SIZE(seqFormat), (mAppendBufferLen - blockSize) * SAMPLE_SIZE(seqFormat)); mAppendBufferLen -= blockSize; blockSize = mSequence->GetIdealAppendLen(); @@ -1288,7 +1283,7 @@ bool WaveClip::Append(samplePtr buffer, sampleFormat format, toCopy = len; CopySamples(buffer, format, - mAppendBuffer + mAppendBufferLen * SAMPLE_SIZE(seqFormat), + mAppendBuffer.ptr() + mAppendBufferLen * SAMPLE_SIZE(seqFormat), seqFormat, toCopy, true, // high quality @@ -1337,7 +1332,7 @@ bool WaveClip::Flush() bool success = true; if (mAppendBufferLen > 0) { - success = mSequence->Append(mAppendBuffer, mSequence->GetSampleFormat(), mAppendBufferLen); + success = mSequence->Append(mAppendBuffer.ptr(), mSequence->GetSampleFormat(), mAppendBufferLen); if (success) { mAppendBufferLen = 0; UpdateEnvelopeTrackLen(); diff --git a/src/WaveClip.h b/src/WaveClip.h index 35dc76fc5..ab3d5b0da 100644 --- a/src/WaveClip.h +++ b/src/WaveClip.h @@ -392,7 +392,7 @@ protected: WaveCache *mWaveCache; ODLock mWaveCacheMutex; SpecCache *mSpecCache; - samplePtr mAppendBuffer; + SampleBuffer mAppendBuffer; sampleCount mAppendBufferLen; // Cut Lines are nothing more than ordinary wave clips, with the diff --git a/src/WaveTrack.h b/src/WaveTrack.h index fc6fdbece..7c4b6d298 100644 --- a/src/WaveTrack.h +++ b/src/WaveTrack.h @@ -536,7 +536,7 @@ private: const WaveTrack *mPTrack; sampleCount mBufferSize; Buffer mBuffers[2]; - SampleBuffer mOverlapBuffer; + GrowableSampleBuffer mOverlapBuffer; int mNValidBuffers; }; diff --git a/src/blockfile/LegacyBlockFile.cpp b/src/blockfile/LegacyBlockFile.cpp index bd9d57e2d..319cfefab 100644 --- a/src/blockfile/LegacyBlockFile.cpp +++ b/src/blockfile/LegacyBlockFile.cpp @@ -66,8 +66,8 @@ void ComputeLegacySummaryInfo(wxFileName fileName, // float *summary = new float[info->frames64K * fields]; - samplePtr data = NewSamples(info->frames64K * fields, - info->format); + SampleBuffer data(info->frames64K * fields, + info->format); wxLogNull *silence=0; wxFFile summaryFile(fileName.GetFullPath(), wxT("rb")); @@ -79,10 +79,10 @@ void ComputeLegacySummaryInfo(wxFileName fileName, fileName.GetFullPath().c_str()); read=info->frames64K * info->bytesPerFrame; - memset(data,0,read); + memset(data.ptr(), 0, read); }else{ summaryFile.Seek(info->offset64K); - read = summaryFile.Read(data, + read = summaryFile.Read(data.ptr(), info->frames64K * info->bytesPerFrame); } @@ -91,7 +91,7 @@ void ComputeLegacySummaryInfo(wxFileName fileName, int count = read / info->bytesPerFrame; - CopySamples(data, info->format, + CopySamples(data.ptr(), info->format, (samplePtr)summary, floatSample, count); (*min) = FLT_MAX; @@ -111,7 +111,6 @@ void ComputeLegacySummaryInfo(wxFileName fileName, else (*rms) = 0; - DeleteSamples(data); delete[] summary; } @@ -235,7 +234,7 @@ int LegacyBlockFile::ReadData(samplePtr data, sampleFormat format, (mSummaryInfo.totalSummaryBytes / SAMPLE_SIZE(mFormat)); sf_seek(sf, seekstart , SEEK_SET); - samplePtr buffer = NewSamples(len, floatSample); + SampleBuffer buffer(len, floatSample); int framesRead = 0; // If both the src and dest formats are integer formats, @@ -258,15 +257,13 @@ int LegacyBlockFile::ReadData(samplePtr data, sampleFormat format, // Otherwise, let libsndfile handle the conversion and // scaling, and pass us normalized data as floats. We can // then convert to whatever format we want. - framesRead = sf_readf_float(sf, (float *)buffer, len); - CopySamples(buffer, floatSample, + framesRead = sf_readf_float(sf, (float *)buffer.ptr(), len); + CopySamples(buffer.ptr(), floatSample, (samplePtr)data, format, framesRead); } sf_close(sf); - DeleteSamples(buffer); - return framesRead; } diff --git a/src/blockfile/ODDecodeBlockFile.cpp b/src/blockfile/ODDecodeBlockFile.cpp index d6cf17a5a..f7fda8b66 100644 --- a/src/blockfile/ODDecodeBlockFile.cpp +++ b/src/blockfile/ODDecodeBlockFile.cpp @@ -308,7 +308,7 @@ int ODDecodeBlockFile::WriteODDecodeBlockFile() // To build the summary data, call ReadData (implemented by the // derived classes) to get the sample data - samplePtr sampleData;// = NewSamples(mLen, floatSample); + SampleBuffer sampleData;// = NewSamples(mLen, floatSample); int ret; //use the decoder here. mDecoderMutex.Lock(); @@ -334,7 +334,7 @@ int ODDecodeBlockFile::WriteODDecodeBlockFile() //TODO: we may need to write a version of WriteSimpleBlockFile that uses threadsafe FILE vs wxFile bool bSuccess = WriteSimpleBlockFile( - sampleData, + sampleData.ptr(), mLen, mFormat, NULL);//summaryData); @@ -342,7 +342,6 @@ int ODDecodeBlockFile::WriteODDecodeBlockFile() mFileNameMutex.Unlock(); - DeleteSamples(sampleData); // delete [] (char *) summaryData; diff --git a/src/blockfile/ODPCMAliasBlockFile.cpp b/src/blockfile/ODPCMAliasBlockFile.cpp index 1c7a2b274..9e26dce4c 100644 --- a/src/blockfile/ODPCMAliasBlockFile.cpp +++ b/src/blockfile/ODPCMAliasBlockFile.cpp @@ -421,16 +421,15 @@ void ODPCMAliasBlockFile::WriteSummary() // To build the summary data, call ReadData (implemented by the // derived classes) to get the sample data - samplePtr sampleData = NewSamples(mLen, floatSample); - this->ReadData(sampleData, floatSample, 0, mLen); + SampleBuffer sampleData(mLen, floatSample); + this->ReadData(sampleData.ptr(), floatSample, 0, mLen); - void *summaryData = CalcSummary(sampleData, mLen, + void *summaryData = CalcSummary(sampleData.ptr(), mLen, floatSample); //summaryFile.Write(summaryData, mSummaryInfo.totalSummaryBytes); fwrite(summaryData, 1, mSummaryInfo.totalSummaryBytes, summaryFile); fclose(summaryFile); - DeleteSamples(sampleData); delete [] (char *) summaryData; @@ -645,7 +644,7 @@ int ODPCMAliasBlockFile::ReadData(samplePtr data, sampleFormat format, sf_seek(sf, mAliasStart + start, SEEK_SET); ODManager::UnlockLibSndFileMutex(); - samplePtr buffer = NewSamples(len * info.channels, floatSample); + SampleBuffer buffer(len * info.channels, floatSample); int framesRead = 0; @@ -656,28 +655,26 @@ int ODPCMAliasBlockFile::ReadData(samplePtr data, sampleFormat format, // read 16-bit data directly. This is a pretty common // case, as most audio files are 16-bit. ODManager::LockLibSndFileMutex(); - framesRead = sf_readf_short(sf, (short *)buffer, len); + framesRead = sf_readf_short(sf, (short *)buffer.ptr(), len); ODManager::UnlockLibSndFileMutex(); for (int i = 0; i < framesRead; i++) ((short *)data)[i] = - ((short *)buffer)[(info.channels * i) + mAliasChannel]; + ((short *)buffer.ptr())[(info.channels * i) + mAliasChannel]; } else { // Otherwise, let libsndfile handle the conversion and // scaling, and pass us normalized data as floats. We can // then convert to whatever format we want. ODManager::LockLibSndFileMutex(); - framesRead = sf_readf_float(sf, (float *)buffer, len); + framesRead = sf_readf_float(sf, (float *)buffer.ptr(), len); ODManager::UnlockLibSndFileMutex(); - float *bufferPtr = &((float *)buffer)[mAliasChannel]; + float *bufferPtr = &((float *)buffer.ptr())[mAliasChannel]; CopySamples((samplePtr)bufferPtr, floatSample, (samplePtr)data, format, framesRead, true, info.channels); } - DeleteSamples(buffer); - ODManager::LockLibSndFileMutex(); sf_close(sf); ODManager::UnlockLibSndFileMutex(); diff --git a/src/blockfile/PCMAliasBlockFile.cpp b/src/blockfile/PCMAliasBlockFile.cpp index c4e2c24da..5deaece2d 100644 --- a/src/blockfile/PCMAliasBlockFile.cpp +++ b/src/blockfile/PCMAliasBlockFile.cpp @@ -116,7 +116,7 @@ int PCMAliasBlockFile::ReadData(samplePtr data, sampleFormat format, ODManager::LockLibSndFileMutex(); sf_seek(sf, mAliasStart + start, SEEK_SET); ODManager::UnlockLibSndFileMutex(); - samplePtr buffer = NewSamples(len * info.channels, floatSample); + SampleBuffer buffer(len * info.channels, floatSample); int framesRead = 0; @@ -127,26 +127,25 @@ int PCMAliasBlockFile::ReadData(samplePtr data, sampleFormat format, // read 16-bit data directly. This is a pretty common // case, as most audio files are 16-bit. ODManager::LockLibSndFileMutex(); - framesRead = sf_readf_short(sf, (short *)buffer, len); + framesRead = sf_readf_short(sf, (short *)buffer.ptr(), len); ODManager::UnlockLibSndFileMutex(); for (int i = 0; i < framesRead; i++) ((short *)data)[i] = - ((short *)buffer)[(info.channels * i) + mAliasChannel]; + ((short *)buffer.ptr())[(info.channels * i) + mAliasChannel]; } else { // Otherwise, let libsndfile handle the conversion and // scaling, and pass us normalized data as floats. We can // then convert to whatever format we want. ODManager::LockLibSndFileMutex(); - framesRead = sf_readf_float(sf, (float *)buffer, len); + framesRead = sf_readf_float(sf, (float *)buffer.ptr(), len); ODManager::UnlockLibSndFileMutex(); - float *bufferPtr = &((float *)buffer)[mAliasChannel]; + float *bufferPtr = &((float *)buffer.ptr())[mAliasChannel]; CopySamples((samplePtr)bufferPtr, floatSample, (samplePtr)data, format, framesRead, true, info.channels); } - DeleteSamples(buffer); ODManager::LockLibSndFileMutex(); sf_close(sf); ODManager::UnlockLibSndFileMutex(); diff --git a/src/blockfile/SimpleBlockFile.cpp b/src/blockfile/SimpleBlockFile.cpp index 807bf23e9..de3717498 100644 --- a/src/blockfile/SimpleBlockFile.cpp +++ b/src/blockfile/SimpleBlockFile.cpp @@ -428,7 +428,7 @@ int SimpleBlockFile::ReadData(samplePtr data, sampleFormat format, mSilentLog=FALSE; sf_seek(sf, start, SEEK_SET); - samplePtr buffer = NewSamples(len, floatSample); + SampleBuffer buffer(len, floatSample); int framesRead = 0; @@ -456,13 +456,11 @@ int SimpleBlockFile::ReadData(samplePtr data, sampleFormat format, // Otherwise, let libsndfile handle the conversion and // scaling, and pass us normalized data as floats. We can // then convert to whatever format we want. - framesRead = sf_readf_float(sf, (float *)buffer, len); - CopySamples(buffer, floatSample, + framesRead = sf_readf_float(sf, (float *)buffer.ptr(), len); + CopySamples(buffer.ptr(), floatSample, (samplePtr)data, format, framesRead); } - DeleteSamples(buffer); - sf_close(sf); return framesRead; diff --git a/src/import/ImportPCM.cpp b/src/import/ImportPCM.cpp index 0bb449a94..34df7e035 100644 --- a/src/import/ImportPCM.cpp +++ b/src/import/ImportPCM.cpp @@ -423,15 +423,15 @@ int PCMImportFileHandle::Import(TrackFactory *trackFactory, if (maxBlock < 1) return eProgressFailed; - samplePtr srcbuffer; - while (NULL == (srcbuffer = NewSamples(maxBlock * mInfo.channels, mFormat))) + SampleBuffer srcbuffer; + while (NULL == srcbuffer.Allocate(maxBlock * mInfo.channels, mFormat).ptr()) { maxBlock >>= 1; if (maxBlock < 1) return eProgressFailed; } - samplePtr buffer = NewSamples(maxBlock, mFormat); + SampleBuffer buffer(maxBlock, mFormat); unsigned long framescompleted = 0; @@ -440,25 +440,25 @@ int PCMImportFileHandle::Import(TrackFactory *trackFactory, block = maxBlock; if (mFormat == int16Sample) - block = sf_readf_short(mFile, (short *)srcbuffer, block); + block = sf_readf_short(mFile, (short *)srcbuffer.ptr(), block); //import 24 bit int as float and have the append function convert it. This is how PCMAliasBlockFile works too. else - block = sf_readf_float(mFile, (float *)srcbuffer, block); + block = sf_readf_float(mFile, (float *)srcbuffer.ptr(), block); if (block) { for(c=0; cAppend(buffer, (mFormat == int16Sample)?int16Sample:floatSample, block); + channels[c]->Append(buffer.ptr(), (mFormat == int16Sample)?int16Sample:floatSample, block); } framescompleted += block; } @@ -469,9 +469,6 @@ int PCMImportFileHandle::Import(TrackFactory *trackFactory, break; } while (block > 0); - - DeleteSamples(buffer); - DeleteSamples(srcbuffer); } if (updateResult == eProgressFailed || updateResult == eProgressCancelled) { diff --git a/src/ondemand/ODDecodeFFmpegTask.cpp b/src/ondemand/ODDecodeFFmpegTask.cpp index 431ca1f24..3f779c295 100644 --- a/src/ondemand/ODDecodeFFmpegTask.cpp +++ b/src/ondemand/ODDecodeFFmpegTask.cpp @@ -68,7 +68,7 @@ public: ///this->ReadData(sampleData, floatSample, 0, mLen); ///This class should call ReadHeader() first, so it knows the length, and can prepare ///the file object if it needs to. - virtual int Decode(samplePtr & data, sampleFormat & format, sampleCount start, sampleCount len, unsigned int channel); + virtual int Decode(SampleBuffer & data, sampleFormat & format, sampleCount start, sampleCount len, unsigned int channel); ///This is a must implement abstract virtual in the superclass. ///However it doesn't do anything because ImportFFMpeg does all that for us. @@ -296,12 +296,12 @@ ODFFmpegDecoder::~ODFFmpegDecoder() #define kDecodeSampleAllowance 400000 //number of jump backwards seeks #define kMaxSeekRewindAttempts 8 -int ODFFmpegDecoder::Decode(samplePtr & data, sampleFormat & format, sampleCount start, sampleCount len, unsigned int channel) +int ODFFmpegDecoder::Decode(SampleBuffer & data, sampleFormat & format, sampleCount start, sampleCount len, unsigned int channel) { format = mScs[mStreamIndex]->m_osamplefmt; - data = NewSamples(len, format); - samplePtr bufStart = data; + data.Allocate(len, format); + samplePtr bufStart = data.ptr(); streamContext* sc = NULL; // printf("start %llu len %llu\n", start, len); diff --git a/src/ondemand/ODDecodeFlacTask.cpp b/src/ondemand/ODDecodeFlacTask.cpp index 37c03b033..b42fffa1d 100644 --- a/src/ondemand/ODDecodeFlacTask.cpp +++ b/src/ondemand/ODDecodeFlacTask.cpp @@ -166,7 +166,7 @@ FLAC__StreamDecoderWriteStatus ODFLACFile::write_callback(const FLAC__Frame *fra ///this->ReadData(sampleData, floatSample, 0, mLen); ///This class should call ReadHeader() first, so it knows the length, and can prepare ///the file object if it needs to. -int ODFlacDecoder::Decode(samplePtr & data, sampleFormat & format, sampleCount start, sampleCount len, unsigned int channel) +int ODFlacDecoder::Decode(SampleBuffer & data, sampleFormat & format, sampleCount start, sampleCount len, unsigned int channel) { //we need to lock this so the target stays fixed over the seek/write callback. @@ -182,8 +182,8 @@ int ODFlacDecoder::Decode(samplePtr & data, sampleFormat & format, sampleCount s mDecodeBufferWritePosition=0; mDecodeBufferLen = len; - data = NewSamples(len, mFormat); - mDecodeBuffer=data; + data.Allocate(len, mFormat); + mDecodeBuffer = data.ptr(); format = mFormat; mTargetChannel=channel; diff --git a/src/ondemand/ODDecodeFlacTask.h b/src/ondemand/ODDecodeFlacTask.h index 19375954b..7f4bc3b55 100644 --- a/src/ondemand/ODDecodeFlacTask.h +++ b/src/ondemand/ODDecodeFlacTask.h @@ -106,7 +106,7 @@ public: ///this->ReadData(sampleData, floatSample, 0, mLen); ///This class should call ReadHeader() first, so it knows the length, and can prepare ///the file object if it needs to. - virtual int Decode(samplePtr & data, sampleFormat & format, sampleCount start, sampleCount len, unsigned int channel); + virtual int Decode(SampleBuffer & data, sampleFormat & format, sampleCount start, sampleCount len, unsigned int channel); ///Read header. Subclasses must override. Probably should save the info somewhere. diff --git a/src/ondemand/ODDecodeTask.h b/src/ondemand/ODDecodeTask.h index 33b410e92..4cfac2407 100644 --- a/src/ondemand/ODDecodeTask.h +++ b/src/ondemand/ODDecodeTask.h @@ -117,7 +117,7 @@ public: ///This class should call ReadHeader() first, so it knows the length, and can prepare ///the file object if it needs to. ///returns negative value for failure, 0 or positive value for success. - virtual int Decode(samplePtr & data, sampleFormat & format, sampleCount start, sampleCount len, unsigned int channel)=0; + virtual int Decode(SampleBuffer & data, sampleFormat & format, sampleCount start, sampleCount len, unsigned int channel)=0; wxString GetFileName(){return mFName;}