Browse Source

Optimize audio file player

Signed-off-by: falkTX <falktx@falktx.com>
tags/v2.1-rc1
falkTX 6 years ago
parent
commit
041658b0a1
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
2 changed files with 38 additions and 23 deletions
  1. +21
    -8
      source/native-plugins/audio-base.hpp
  2. +17
    -15
      source/native-plugins/audio-file.cpp

+ 21
- 8
source/native-plugins/audio-base.hpp View File

@@ -64,7 +64,7 @@ struct AudioFilePool {
CARLA_ASSERT(startFrame == 0); CARLA_ASSERT(startFrame == 0);
CARLA_ASSERT(size == 0); CARLA_ASSERT(size == 0);


size = srate * 8;
size = srate * 60; // buffer of 60 secs
sampleRate = srate; sampleRate = srate;


buffer[0] = new float[size]; 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()) if (! fMutex.tryLock())
return;
return false;


//if (pool.startFrame != fPool.startFrame || pool.buffer[0] != fPool.buffer[0] || pool.buffer[1] != fPool.buffer[1]) //if (pool.startFrame != fPool.startFrame || pool.buffer[0] != fPool.buffer[0] || pool.buffer[1] != fPool.buffer[1])
{ {
pool.startFrame = fPool.startFrame; 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(); fMutex.unlock();
return true;
} }


void readPoll() void readPoll()
@@ -417,8 +430,8 @@ protected:


if (fNeedsRead || lastFrame < fPool.startFrame || (lastFrame - fPool.startFrame >= fPool.size*3/4 && loopedFrame < fMaxPlayerFrame)) if (fNeedsRead || lastFrame < fPool.startFrame || (lastFrame - fPool.startFrame >= fPool.size*3/4 && loopedFrame < fMaxPlayerFrame))
readPoll(); readPoll();
else
carla_msleep(50);
carla_msleep(50);
} }
} }




+ 17
- 15
source/native-plugins/audio-file.cpp View File

@@ -149,8 +149,6 @@ protected:
return; return;
} }


fThread.tryPutData(fPool);

// out of reach // out of reach
if (timePos->frame + frames < fPool.startFrame || (timePos->frame >= fMaxFrame && !fLoopMode)) if (timePos->frame + frames < fPool.startFrame || (timePos->frame >= fMaxFrame && !fLoopMode))
{ {
@@ -169,26 +167,30 @@ protected:
} }


const uint32_t poolSize = fPool.size; const uint32_t poolSize = fPool.size;
int64_t poolFrame = static_cast<uint32_t>(timePos->frame - fPool.startFrame);
float* const bufferL = fPool.buffer[0]; float* const bufferL = fPool.buffer[0];
float* const bufferR = fPool.buffer[1]; float* const bufferR = fPool.buffer[1];


for (uint32_t i=0; i < frames; ++i, ++poolFrame)
int64_t poolFrame = static_cast<int64_t>(timePos->frame - fPool.startFrame);

if (poolFrame >= 0 && poolFrame < poolSize && fThread.tryPutData(fPool, static_cast<uint32_t>(poolFrame), frames))
{ {
if (poolFrame >= 0 && poolFrame < poolSize)
{
out1[i] = bufferL[poolFrame];
out2[i] = bufferR[poolFrame];
const uint32_t framesToCopy = std::min(frames, static_cast<uint32_t>(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; fLastFrame = timePos->frame;


Loading…
Cancel
Save