1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-15 15:49:36 +02:00

A rewrite in Nyquist to make reinterpret_cast unnecessary

This commit is contained in:
Paul Licameli 2021-05-23 17:54:49 -04:00
parent f369b5109b
commit 9c70887c34
2 changed files with 13 additions and 11 deletions

View File

@ -1391,12 +1391,12 @@ bool NyquistEffect::ProcessOne()
// Put the fetch buffers in a clean initial state // Put the fetch buffers in a clean initial state
for (size_t i = 0; i < mCurNumChannels; i++) for (size_t i = 0; i < mCurNumChannels; i++)
mCurBuffer[i].Free(); mCurBuffer[i].reset();
// Guarantee release of memory when done // Guarantee release of memory when done
auto cleanup = finally( [&] { auto cleanup = finally( [&] {
for (size_t i = 0; i < mCurNumChannels; i++) for (size_t i = 0; i < mCurNumChannels; i++)
mCurBuffer[i].Free(); mCurBuffer[i].reset();
} ); } );
// Evaluate the expression, which may invoke the get callback, but often does // Evaluate the expression, which may invoke the get callback, but often does
@ -1564,7 +1564,7 @@ bool NyquistEffect::ProcessOne()
// Clean the initial buffer states again for the get callbacks // Clean the initial buffer states again for the get callbacks
// -- is this really needed? // -- is this really needed?
mCurBuffer[i].Free(); mCurBuffer[i].reset();
} }
// Now fully evaluate the sound // Now fully evaluate the sound
@ -2427,15 +2427,15 @@ int NyquistEffect::StaticGetCallback(float *buffer, int channel,
int NyquistEffect::GetCallback(float *buffer, int ch, int NyquistEffect::GetCallback(float *buffer, int ch,
int64_t start, int64_t len, int64_t WXUNUSED(totlen)) int64_t start, int64_t len, int64_t WXUNUSED(totlen))
{ {
if (mCurBuffer[ch].ptr()) { if (mCurBuffer[ch]) {
if ((mCurStart[ch] + start) < mCurBufferStart[ch] || if ((mCurStart[ch] + start) < mCurBufferStart[ch] ||
(mCurStart[ch] + start)+len > (mCurStart[ch] + start)+len >
mCurBufferStart[ch]+mCurBufferLen[ch]) { mCurBufferStart[ch]+mCurBufferLen[ch]) {
mCurBuffer[ch].Free(); mCurBuffer[ch].reset();
} }
} }
if (!mCurBuffer[ch].ptr()) { if (!mCurBuffer[ch]) {
mCurBufferStart[ch] = (mCurStart[ch] + start); mCurBufferStart[ch] = (mCurStart[ch] + start);
mCurBufferLen[ch] = mCurTrack[ch]->GetBestBlockSize(mCurBufferStart[ch]); mCurBufferLen[ch] = mCurTrack[ch]->GetBestBlockSize(mCurBufferStart[ch]);
@ -2447,10 +2447,11 @@ int NyquistEffect::GetCallback(float *buffer, int ch,
limitSampleBufferSize( mCurBufferLen[ch], limitSampleBufferSize( mCurBufferLen[ch],
mCurStart[ch] + mCurLen - mCurBufferStart[ch] ); mCurStart[ch] + mCurLen - mCurBufferStart[ch] );
mCurBuffer[ch].Allocate(mCurBufferLen[ch], floatSample); // C++20
// mCurBuffer[ch] = std::make_unique_for_overwrite(mCurBufferLen[ch]);
mCurBuffer[ch] = Buffer{ safenew float[ mCurBufferLen[ch] ] };
try { try {
mCurTrack[ch]->GetFloats( mCurTrack[ch]->GetFloats( mCurBuffer[ch].get(),
reinterpret_cast<float*>(mCurBuffer[ch].ptr()),
mCurBufferStart[ch], mCurBufferLen[ch]); mCurBufferStart[ch], mCurBufferLen[ch]);
} }
catch ( ... ) { catch ( ... ) {
@ -2463,7 +2464,7 @@ int NyquistEffect::GetCallback(float *buffer, int ch,
// We have guaranteed above that this is nonnegative and bounded by // We have guaranteed above that this is nonnegative and bounded by
// mCurBufferLen[ch]: // mCurBufferLen[ch]:
auto offset = ( mCurStart[ch] + start - mCurBufferStart[ch] ).as_size_t(); auto offset = ( mCurStart[ch] + start - mCurBufferStart[ch] ).as_size_t();
const void *src = mCurBuffer[ch].ptr() + offset*SAMPLE_SIZE(floatSample); const void *src = &mCurBuffer[ch][offset];
std::memcpy(buffer, src, len * sizeof(float)); std::memcpy(buffer, src, len * sizeof(float));
if (ch == 0) { if (ch == 0) {

View File

@ -267,7 +267,8 @@ private:
double mProgressTot; double mProgressTot;
double mScale; double mScale;
SampleBuffer mCurBuffer[2]; using Buffer = std::unique_ptr<float[]>;
Buffer mCurBuffer[2];
sampleCount mCurBufferStart[2]; sampleCount mCurBufferStart[2];
size_t mCurBufferLen[2]; size_t mCurBufferLen[2];