Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multithreaded rubberband #13143

Merged
merged 9 commits into from
Jun 8, 2024
Prev Previous commit
Next Next commit
Replace the waitcond and mutex with sema on RBTask
  • Loading branch information
acolombier committed Jun 4, 2024
commit 5996927e72adfc9b4adb2a832413c686eb20df00
16 changes: 5 additions & 11 deletions src/engine/bufferscalers/rubberbandtask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RubberBandTask::RubberBandTask(
size_t sampleRate, size_t channels, Options options)
: RubberBand::RubberBandStretcher(sampleRate, channels, options),
QRunnable(),
m_completed(false),
m_completedSema(0),
m_input(nullptr),
m_samples(0),
m_isFinal(false) {
Expand All @@ -18,31 +18,25 @@ RubberBandTask::RubberBandTask(
void RubberBandTask::set(const float* const* input,
size_t samples,
bool isFinal) {
auto locker = lockMutex(&m_waitLock);
m_completed = false;
DEBUG_ASSERT(m_completedSema.available() == 0);
m_input = input;
m_samples = samples;
m_isFinal = isFinal;
}

void RubberBandTask::waitReady() {
auto locker = lockMutex(&m_waitLock);
VERIFY_OR_DEBUG_ASSERT(m_input && m_samples) {
return;
};
while (!m_completed) {
m_waitCondition.wait(&m_waitLock);
}
m_completedSema.acquire();
}

void RubberBandTask::run() {
auto locker = lockMutex(&m_waitLock);
VERIFY_OR_DEBUG_ASSERT(!m_completed && m_input && m_samples) {
VERIFY_OR_DEBUG_ASSERT(m_completedSema.available() == 0 && m_input && m_samples) {
return;
};
process(m_input,
m_samples,
m_isFinal);
m_completed = true;
m_waitCondition.wakeOne();
m_completedSema.release();
}
8 changes: 2 additions & 6 deletions src/engine/bufferscalers/rubberbandtask.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

#include <rubberband/RubberBandStretcher.h>

#include <QMutex>
#include <QRunnable>
#include <QWaitCondition>
#include <QSemaphore>
#include <atomic>

#include "audio/types.h"
Expand Down Expand Up @@ -33,11 +32,8 @@ class RubberBandTask : public RubberBandStretcher, public QRunnable {
void run();

private:
// Used to schedule the thread
QMutex m_waitLock;
QWaitCondition m_waitCondition;
// Whether or not the scheduled job as completed
bool m_completed;
QSemaphore m_completedSema;

const float* const* m_input;
size_t m_samples;
Expand Down