Skip to content

Commit

Permalink
Merge branch 'audio-engine-job-cleanups'
Browse files Browse the repository at this point in the history
* audio-engine-job-cleanups:
  ASE: engine.hh: document engine lifetime
  ASE: engine: remove BorrowedPtr, simplify EngineJobImpl
  ASE: clapplugin.cc: use main_rt_jobs queue to delete events from audio_thread
  ASE: clapplugin.cc: rename ClapAudioProcessor
  ASE: project.cc: do not (auto) destroy all projects during atexit
  ASE: main.cc: shutdown: unset engine project and handle main loop callbacks
  ASE: engine: make transport a reference, move job queues into AudioEngine
  ASE: engine: rename AudioEngineThread

Signed-off-by: Tim Janik <[email protected]>
  • Loading branch information
tim-janik committed Oct 1, 2023
2 parents a5f9ca9 + d0ec859 commit 8067acc
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 210 deletions.
63 changes: 34 additions & 29 deletions ase/clapplugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace Ase {

ASE_CLASS_DECLS (ClapAudioWrapper);
ASE_CLASS_DECLS (ClapAudioProcessor);
ASE_CLASS_DECLS (ClapPluginHandleImpl);
using ClapEventParamS = std::vector<clap_event_param_value>;
union ClapEventUnion;
Expand Down Expand Up @@ -220,8 +220,8 @@ union ClapEventUnion {
clap_event_midi2_t midi2; // CLAP_NOTE_DIALECT_MIDI2
};

// == ClapAudioWrapper ==
class ClapAudioWrapper : public AudioProcessor {
// == ClapAudioProcessor ==
class ClapAudioProcessor : public AudioProcessor {
ClapPluginHandle *handle_ = nullptr;
const clap_plugin *clapplugin_ = nullptr;
IBusId ibusid = {};
Expand All @@ -236,17 +236,17 @@ class ClapAudioWrapper : public AudioProcessor {
static void
static_info (AudioProcessorInfo &info)
{
info.label = "Anklang.Devices.ClapAudioWrapper";
info.label = "Anklang.Devices.ClapAudioProcessor";
}
ClapAudioWrapper (AudioEngine &engine) :
ClapAudioProcessor (AudioEngine &engine) :
AudioProcessor (engine)
{}
~ClapAudioWrapper()
~ClapAudioProcessor()
{
while (enqueued_events_.size()) {
BorrowedPtr<ClapEventParamS> pevents_b = enqueued_events_.back();
ClapEventParamS *pevents = enqueued_events_.back();
enqueued_events_.pop_back();
pevents_b.dispose (engine_);
main_rt_jobs += RtCall (delete_clap_event_params, pevents); // delete in main_thread
}
}
void
Expand Down Expand Up @@ -312,7 +312,7 @@ class ClapAudioWrapper : public AudioProcessor {
static uint32_t
input_events_size (const clap_input_events *evlist)
{
ClapAudioWrapper *self = (ClapAudioWrapper*) evlist->ctx;
ClapAudioProcessor *self = (ClapAudioProcessor*) evlist->ctx;
size_t param_events_size = 0;
for (const auto &pevents_b : self->enqueued_events_)
param_events_size += pevents_b->size();
Expand All @@ -321,7 +321,7 @@ class ClapAudioWrapper : public AudioProcessor {
static const clap_event_header_t*
input_events_get (const clap_input_events *evlist, uint32_t index)
{
ClapAudioWrapper *self = (ClapAudioWrapper*) evlist->ctx;
ClapAudioProcessor *self = (ClapAudioProcessor*) evlist->ctx;
for (const auto &pevents_b : self->enqueued_events_) {
if (index < pevents_b->size())
return &(*pevents_b)[index].header;
Expand All @@ -332,35 +332,35 @@ class ClapAudioWrapper : public AudioProcessor {
static bool
output_events_try_push (const clap_output_events *evlist, const clap_event_header_t *event)
{
ClapAudioWrapper *self = (ClapAudioWrapper*) evlist->ctx;
ClapAudioProcessor *self = (ClapAudioProcessor*) evlist->ctx;
return event_unions_try_push (self->output_events_, event);
}
const clap_input_events_t plugin_input_events = {
.ctx = (ClapAudioWrapper*) this,
.ctx = (ClapAudioProcessor*) this,
.size = input_events_size,
.get = input_events_get,
};
const clap_output_events_t plugin_output_events = {
.ctx = (ClapAudioWrapper*) this,
.ctx = (ClapAudioProcessor*) this,
.try_push = output_events_try_push,
};
const ClapParamInfoMap *param_info_map_ = nullptr;
const ClapParamInfoImpl *param_info_map_start_ = nullptr;
clap_process_t processinfo = { 0, };
clap_event_transport_t transportinfo = { { 0, }, };
std::vector<BorrowedPtr<ClapEventParamS>> enqueued_events_;
std::vector<ClapEventParamS*> enqueued_events_;
void
enqueue_events (BorrowedPtr<ClapEventParamS> pevents_b)
enqueue_events (ClapEventParamS *pevents)
{
// insert 0-time event list *before* other events
if (pevents_b && pevents_b->size() && pevents_b->back().header.time == 0)
if (pevents && pevents->size() && pevents->back().header.time == 0)
for (size_t i = 0; i < enqueued_events_.size(); i++)
if (enqueued_events_[i]->size() && enqueued_events_[i]->back().header.time > 0) {
enqueued_events_.insert (enqueued_events_.begin() + i, pevents_b);
enqueued_events_.insert (enqueued_events_.begin() + i, pevents);
return;
}
// or add to existing queue
enqueued_events_.push_back (pevents_b);
enqueued_events_.push_back (pevents);
}
bool
start_processing (const ClapParamInfoMap *param_info_map, const ClapParamInfoImpl *map_start, size_t map_size)
Expand Down Expand Up @@ -487,16 +487,22 @@ class ClapAudioWrapper : public AudioProcessor {
// TODO: need proper time stamp handling
bool need_wakeup = false;
while (enqueued_events_.size() && (enqueued_events_[0]->empty() || enqueued_events_[0]->back().header.time < nframes)) {
BorrowedPtr<ClapEventParamS> pevents_b = enqueued_events_[0];
ClapEventParamS *const pevents = enqueued_events_[0];
enqueued_events_.erase (enqueued_events_.begin());
for (const auto &e : *pevents_b)
for (const auto &e : *pevents)
need_wakeup |= apply_param_value_event (e);
pevents_b.dispose (engine_);
main_rt_jobs += RtCall (delete_clap_event_params, pevents); // delete in main_thread
}
return need_wakeup;
}
static void
delete_clap_event_params (ClapEventParamS *p)
{
assert_return (this_thread_is_ase());
delete p;
}
};
static CString clap_audio_wrapper_aseid = register_audio_processor<ClapAudioWrapper>();
static CString clap_audio_wrapper_aseid = register_audio_processor<ClapAudioProcessor>();

static inline clap_event_midi*
setup_midi1 (ClapEventUnion *evunion, uint32_t time, uint16_t port_index)
Expand Down Expand Up @@ -538,7 +544,7 @@ setup_expression (ClapEventUnion *evunion, uint32_t time, uint16_t port_index)
}

void
ClapAudioWrapper::convert_clap_events (const clap_process_t &process, const bool as_clapnotes)
ClapAudioProcessor::convert_clap_events (const clap_process_t &process, const bool as_clapnotes)
{
MidiEventRange erange = get_event_input();
if (input_events_.capacity() < erange.events_pending())
Expand Down Expand Up @@ -651,7 +657,7 @@ class ClapPluginHandleImpl : public ClapPluginHandle {
.request_process = host_request_process_mt,
.request_callback = host_request_callback_mt,
};
ClapAudioWrapperP proc_;
ClapAudioProcessorP proc_;
const clap_plugin_t *plugin_ = nullptr;
const clap_plugin_gui *plugin_gui = nullptr;
const clap_plugin_state *plugin_state = nullptr;
Expand All @@ -663,7 +669,7 @@ class ClapPluginHandleImpl : public ClapPluginHandle {
const clap_plugin_note_ports *plugin_note_ports = nullptr;
const clap_plugin_posix_fd_support *plugin_posix_fd_support = nullptr;
ClapPluginHandleImpl (const ClapPluginDescriptor &descriptor_, AudioProcessorP aproc) :
ClapPluginHandle (descriptor_), proc_ (shared_ptr_cast<ClapAudioWrapper> (aproc))
ClapPluginHandle (descriptor_), proc_ (shared_ptr_cast<ClapAudioProcessor> (aproc))
{
assert_return (proc_ != nullptr);
const clap_plugin_entry *pluginentry = descriptor.entry();
Expand Down Expand Up @@ -932,10 +938,9 @@ class ClapPluginHandleImpl : public ClapPluginHandle {
{
ClapPluginHandleImplP selfp = shared_ptr_cast<ClapPluginHandleImpl> (this);
return_unless (clap_activated(), false);
ClapEventParamS *pevents = convert_param_updates (updates);
BorrowedPtr<ClapEventParamS> pevents_b (pevents); // moves ownership
proc_->engine().async_jobs += [selfp, pevents_b] () {
selfp->proc_->enqueue_events (pevents_b); // use BorrowedPtr in audio-thread, dispose in main-thread
ClapEventParamS *pevents = convert_param_updates (updates); // allocated in main_thread
proc_->engine().async_jobs += [selfp, pevents] () {
selfp->proc_->enqueue_events (pevents);
};
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions ase/clapplugin.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Ase {

class ClapFileHandle;
class ClapAudioWrapper;
class ClapAudioProcessor;

// == ClapPluginDescriptor ==
class ClapPluginDescriptor {
Expand Down Expand Up @@ -88,7 +88,7 @@ public:
virtual AudioProcessorP audio_processor () = 0;
static ClapPluginHandleP make_clap_handle (const ClapPluginDescriptor &descriptor, AudioProcessorP audio_processor);
static CString audio_processor_type();
friend class ClapAudioWrapper;
friend class ClapAudioProcessor;
};

// == CLAP utilities ==
Expand Down
2 changes: 1 addition & 1 deletion ase/defs.hh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ ASE_STRUCT_DECLS (UserNote);
ASE_CLASS_DECLS (AudioChain);
ASE_CLASS_DECLS (AudioCombo);
ASE_CLASS_DECLS (AudioCombo);
ASE_CLASS_DECLS (AudioEngineImpl);
ASE_CLASS_DECLS (AudioEngineThread);
ASE_CLASS_DECLS (AudioProcessor);
ASE_CLASS_DECLS (ClapDeviceImpl);
ASE_CLASS_DECLS (ClapPluginHandle);
Expand Down
Loading

0 comments on commit 8067acc

Please sign in to comment.