Skip to content

Commit

Permalink
CLAP latency reporting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Piolat committed Oct 28, 2024
1 parent 1379f30 commit 81477fd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
43 changes: 40 additions & 3 deletions clap/dplug/clap/client.d
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ private:
bool _mustReset;

// Last hint at sampleRate, -1 if not specified yet
float _sampleRate = -1;
double _sampleRate = -1;

// Current latency in samples.
int _latencySamples;

// Max frames in block., -1 if not specified yet.
int _maxFrames = -1;
Expand Down Expand Up @@ -236,6 +239,9 @@ private:
activated = true;
clientReset();

// Set latency. Tells the host to check latency immediately.
_latencySamples = _client.latencySamples(_sampleRate);
_hostCommand.notifyLatencyChanged();
return true;
}

Expand Down Expand Up @@ -427,7 +433,15 @@ private:
api.hide = &plugin_gui_hide;
return &api;
}
// no extension support

if (strcmp(name, "clap.latency") == 0)
{
__gshared clap_plugin_latency_t api;
api.get = &plugin_latency_get;
return &api;
}

// extension not supported
return null;
}

Expand Down Expand Up @@ -1182,6 +1196,15 @@ extern(C) static
mixin(ClientCallback);
return client.gui_hide();
}

// latency implem
uint plugin_latency_get(const(clap_plugin_t)* plugin)
{
mixin(ClientCallback);
int samples = client._latencySamples;
assert(samples >= 0);
return samples;
}
}

// CLAP host commands
Expand All @@ -1193,8 +1216,9 @@ nothrow @nogc:
{
_host = host;
_host_gui = cast(clap_host_gui_t*) host.get_extension(host, "clap.gui".ptr);
_host_latency = cast(clap_host_latency_t*) host.get_extension(host, "clap.latency".ptr);
}

/// Notifies the host that editing of a parameter has begun from UI side.
override void beginParamEdit(int paramIndex)
{
Expand Down Expand Up @@ -1237,6 +1261,18 @@ nothrow @nogc:
return false;
}

// Tell the host the latency changed while activated.
bool notifyLatencyChanged()
{
if (_host_latency)
{
_host_latency.changed(_host);
return true;
}
else
return false;
}

DAW getDAW()
{
char[128] dawStr;
Expand All @@ -1259,5 +1295,6 @@ nothrow @nogc:

const(clap_host_t)* _host;
const(clap_host_gui_t)* _host_gui;
const(clap_host_latency_t)* _host_latency;
}

22 changes: 21 additions & 1 deletion clap/dplug/clap/types.d
Original file line number Diff line number Diff line change
Expand Up @@ -1324,4 +1324,24 @@ extern(C) nothrow @nogc:
// Request the host to schedule a call to plugin->on_main_thread(plugin) on the main thread.
// [thread-safe]
void function(const(clap_host_t)*host) request_callback;
}
}

// latency.h

struct clap_plugin_latency_t
{
extern(C) nothrow @nogc:
// Returns the plugin latency in samples.
// [main-thread & (being-activated | active)]
uint function(const(clap_plugin_t)* plugin) get;
}

struct clap_host_latency_t
{
extern(C) nothrow @nogc:
// Tell the host that the latency changed.
// The latency is only allowed to change during plugin->activate.
// If the plugin is activated, call host->request_restart()
// [main-thread & being-activated]
void function(const(clap_host_t)* host) changed;
}

0 comments on commit 81477fd

Please sign in to comment.