cliprec: add a class-level semaphore to wake thread

This commit is contained in:
Paul Davis 2023-01-28 17:39:40 -07:00
parent f657a7f826
commit 17e14f090e
2 changed files with 49 additions and 0 deletions

View File

@ -21,11 +21,13 @@
#include "pbd/ringbufferNPT.h"
#include "pbd/signals.h"
#include "ardour/processor.h"
namespace PBD {
class Thread;
class Semaphore;
}
namespace ARDOUR {
@ -43,8 +45,13 @@ class LIBARDOUR_API ClipRecProcessor : public Processor
void run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample, double speed, pframes_t nframes, bool result_required);
bool can_support_io_configuration (const ChanCount& in, ChanCount& out);
bool armed() const { return _armed; }
void set_armed (bool yn);
PBD::Signal0<void> ArmedChanged;
private:
Track& _track;
bool _armed;
/** Information about one audio channel, playback or capture
* (depending on the derived class)
@ -79,8 +86,12 @@ class LIBARDOUR_API ClipRecProcessor : public Processor
/* private (to class) butler thread */
static PBD::Thread* _thread;
static PBD::Semaphore* _semaphore;
static bool thread_should_run;
static void thread_work ();
static ClipRecProcessor* currently_recording;
void pull_data ();
};
} /* namespace */

View File

@ -19,36 +19,70 @@
#include "pbd/compose.h"
#include "pbd/debug.h"
#include "pbd/pthread_utils.h"
#include "pbd/semutils.h"
#include "ardour/audio_buffer.h"
#include "ardour/cliprec.h"
#include "ardour/debug.h"
#include "ardour/midi_track.h"
#include "pbd/i18n.h"
using namespace ARDOUR;
using namespace PBD;
PBD::Thread* ClipRecProcessor::_thread (0);
bool ClipRecProcessor::thread_should_run (false);
PBD::Semaphore* ClipRecProcessor::_semaphore (0);
ClipRecProcessor::ClipRecProcessor (Session& s, Track& t, std::string const & name)
: Processor (s, name, Temporal::BeatTime)
, _track (t)
, _armed (false)
, channels (new ChannelList)
{
if (!_thread) {
thread_should_run = true;
_semaphore = new PBD::Semaphore (X_("cliprec"), 0);
_thread = PBD::Thread::create (&ClipRecProcessor::thread_work);
}
}
void
ClipRecProcessor::set_armed (bool yn)
{
if (_armed == yn) {
assert (currently_recording == this);
return;
}
if (yn) {
if (currently_recording) {
currently_recording->set_armed (false);
currently_recording = 0;
}
_armed = true;
currently_recording = this;
}
}
void
ClipRecProcessor::thread_work ()
{
while (thread_should_run) {
_semaphore->wait ();
ClipRecProcessor* crp = currently_recording;
if (crp) {
crp->pull_data ();
}
}
}
void
ClipRecProcessor::pull_data ()
{
}
bool
ClipRecProcessor::can_support_io_configuration (const ChanCount& in, ChanCount& out)
{
@ -109,6 +143,10 @@ ClipRecProcessor::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t en
}
chaninfo->buf->increment_write_ptr (nframes);
if (chaninfo->buf->read_space() > 10) {
_semaphore->signal ();
}
}
}