From 041658b0a12d12143d5ef137956d1010b79d1988 Mon Sep 17 00:00:00 2001 From: falkTX Date: Mon, 1 Apr 2019 15:56:36 +0900 Subject: [PATCH] Optimize audio file player Signed-off-by: falkTX --- source/native-plugins/audio-base.hpp | 29 ++++++++++++++++++------- source/native-plugins/audio-file.cpp | 32 +++++++++++++++------------- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/source/native-plugins/audio-base.hpp b/source/native-plugins/audio-base.hpp index 57f569924..9ce950b0b 100644 --- a/source/native-plugins/audio-base.hpp +++ b/source/native-plugins/audio-base.hpp @@ -64,7 +64,7 @@ struct AudioFilePool { CARLA_ASSERT(startFrame == 0); CARLA_ASSERT(size == 0); - size = srate * 8; + size = srate * 60; // buffer of 60 secs sampleRate = srate; buffer[0] = new float[size]; @@ -268,21 +268,34 @@ public: } } - void tryPutData(AudioFilePool& pool) + bool tryPutData(AudioFilePool& pool, const uint32_t framePos, const uint32_t frames) { - CARLA_SAFE_ASSERT_RETURN(pool.size == fPool.size,); + CARLA_SAFE_ASSERT_RETURN(pool.size == fPool.size, false); if (! fMutex.tryLock()) - return; + return false; //if (pool.startFrame != fPool.startFrame || pool.buffer[0] != fPool.buffer[0] || pool.buffer[1] != fPool.buffer[1]) { pool.startFrame = fPool.startFrame; - carla_copyFloats(pool.buffer[0], fPool.buffer[0], fPool.size); - carla_copyFloats(pool.buffer[1], fPool.buffer[1], fPool.size); + + (void)frames; + if (frames == 0) + { + carla_copyFloats(pool.buffer[0], fPool.buffer[0], fPool.size); + carla_copyFloats(pool.buffer[1], fPool.buffer[1], fPool.size); + } + else + { + CARLA_SAFE_ASSERT_UINT2_RETURN(framePos + frames < fPool.size, framePos, fPool.size, false); + + carla_copyFloats(pool.buffer[0] + framePos, fPool.buffer[0] + framePos, frames); + carla_copyFloats(pool.buffer[1] + framePos, fPool.buffer[1] + framePos, frames); + } } fMutex.unlock(); + return true; } void readPoll() @@ -417,8 +430,8 @@ protected: if (fNeedsRead || lastFrame < fPool.startFrame || (lastFrame - fPool.startFrame >= fPool.size*3/4 && loopedFrame < fMaxPlayerFrame)) readPoll(); - else - carla_msleep(50); + + carla_msleep(50); } } diff --git a/source/native-plugins/audio-file.cpp b/source/native-plugins/audio-file.cpp index 5a2622691..4833675e7 100644 --- a/source/native-plugins/audio-file.cpp +++ b/source/native-plugins/audio-file.cpp @@ -149,8 +149,6 @@ protected: return; } - fThread.tryPutData(fPool); - // out of reach if (timePos->frame + frames < fPool.startFrame || (timePos->frame >= fMaxFrame && !fLoopMode)) { @@ -169,26 +167,30 @@ protected: } const uint32_t poolSize = fPool.size; - int64_t poolFrame = static_cast(timePos->frame - fPool.startFrame); float* const bufferL = fPool.buffer[0]; float* const bufferR = fPool.buffer[1]; - for (uint32_t i=0; i < frames; ++i, ++poolFrame) + int64_t poolFrame = static_cast(timePos->frame - fPool.startFrame); + + if (poolFrame >= 0 && poolFrame < poolSize && fThread.tryPutData(fPool, static_cast(poolFrame), frames)) { - if (poolFrame >= 0 && poolFrame < poolSize) - { - out1[i] = bufferL[poolFrame]; - out2[i] = bufferR[poolFrame]; + const uint32_t framesToCopy = std::min(frames, static_cast(poolSize - poolFrame)); + carla_copyFloats(out1, bufferL + poolFrame, framesToCopy); + carla_copyFloats(out2, bufferR + poolFrame, framesToCopy); - // reset - bufferL[poolFrame] = 0.0f; - bufferR[poolFrame] = 0.0f; - } - else + if (const uint32_t remainingFrames = frames - framesToCopy) { - out1[i] = 0.0f; - out2[i] = 0.0f; + carla_zeroFloats(out1 + framesToCopy, remainingFrames); + carla_zeroFloats(out2 + framesToCopy, remainingFrames); } + + carla_zeroFloats(bufferL + poolFrame, framesToCopy); + carla_zeroFloats(bufferR + poolFrame, framesToCopy); + } + else + { + carla_zeroFloats(out1, frames); + carla_zeroFloats(out2, frames); } fLastFrame = timePos->frame;