Use GCD for TapStep.

ms-pipeline-optim-2
Mooneer Salem 2025-08-15 09:20:09 -07:00
parent 35ca8c06bb
commit e1522a7ab3
3 changed files with 34 additions and 10 deletions

View File

@ -129,7 +129,7 @@ short* ResampleStep::execute(short* inputSamples, int numInputSamples, int* numO
*numOutputSamples = 0;
auto inputPtr = inputSamples;
//auto inputPtr = inputSamples;
auto outputPtr = outputSamples_.get();
#if 0
while (numInputSamples > 0)

View File

@ -36,11 +36,13 @@ TapStep::TapStep(int sampleRate, IPipelineStep* tapStep)
: tapStep_(tapStep)
, sampleRate_(sampleRate)
#if defined(TAP_STEP_USE_THREADING)
, endingTapThread_(false)
, tapThreadInput_(sampleRate)
#if !defined(__APPLE__)
, endingTapThread_(false)
#endif // !defined(__APPLE__)
#endif // defined(TAP_STEP_USE_THREADING)
{
#if defined(TAP_STEP_USE_THREADING)
#if defined(TAP_STEP_USE_THREADING) && !defined(__APPLE__)
tapThread_ = std::thread([&]() {
const int SAMPLE_RATE_AT_10MS = sampleRate_ / 100;
short* fifoInput = new short[SAMPLE_RATE_AT_10MS];
@ -64,16 +66,16 @@ TapStep::TapStep(int sampleRate, IPipelineStep* tapStep)
delete[] fifoInput;
});
#endif // defined(TAP_STEP_USE_THREADING)
#endif // defined(TAP_STEP_USE_THREADING) && !defined(__APPLE__)
}
TapStep::~TapStep()
{
#if defined(TAP_STEP_USE_THREADING)
#if defined(TAP_STEP_USE_THREADING) && !defined(__APPLE__)
endingTapThread_ = true;
sem_.signal();
tapThread_.join();
#endif // defined(TAP_STEP_USE_THREADING)
#endif // defined(TAP_STEP_USE_THREADING) && !defined(__APPLE__)
}
int TapStep::getInputSampleRate() const
@ -94,8 +96,25 @@ short* TapStep::execute(short* inputSamples, int numInputSamples, int* numOutput
tapThreadInput_.write(inputSamples, numInputSamples);
if (tapThreadInput_.numUsed() > (100 * sampleRate_ / 1000))
{
#if !defined(__APPLE__)
sem_.signal();
#else
enqueue_([&]() {
const int SAMPLE_RATE_AT_10MS = sampleRate_ / 100;
short* fifoInput = new short[SAMPLE_RATE_AT_10MS];
assert(fifoInput != nullptr);
while (tapThreadInput_.numUsed() >= SAMPLE_RATE_AT_10MS)
{
int temp = 0;
tapThreadInput_.read(fifoInput, SAMPLE_RATE_AT_10MS);
tapStep_->execute(fifoInput, SAMPLE_RATE_AT_10MS, &temp);
}
delete[] fifoInput;
});
}
#endif // !defined(__APPLE__)
#else
int temp = 0;
tapStep_->execute(inputSamples, numInputSamples, &temp);

View File

@ -26,14 +26,17 @@
#include <memory>
#include <thread>
#include "../util/GenericFIFO.h"
#include "../util/Semaphore.h"
#include "../util/ThreadedObject.h"
#include "IPipelineStep.h"
// Creates a separate thread for each TapStep instance if uncommented.
//#define TAP_STEP_USE_THREADING
#define TAP_STEP_USE_THREADING
class TapStep : public IPipelineStep
#if defined(TAP_STEP_USE_THREADING) && defined(__APPLE__)
, public ThreadedObject
#endif // defined(TAP_STEP_USE_THREADING) && defined(__APPLE__)
{
public:
TapStep(int inputSampleRate, IPipelineStep* tapStep);
@ -47,11 +50,13 @@ private:
std::unique_ptr<IPipelineStep> tapStep_;
int sampleRate_;
#if defined(TAP_STEP_USE_THREADING)
#if defined(TAP_STEP_USE_THREADING)
GenericFIFO<short> tapThreadInput_;
#if !defined(__APPLE__)
std::thread tapThread_;
bool endingTapThread_;
GenericFIFO<short> tapThreadInput_;
Semaphore sem_;
#endif // !defined(__APPLE__)
#endif // defined(TAP_STEP_USE_THREADING)
};