Each row on Playback, Output Devices and Input Devices gets a level bar under its slider. A meter is a small capture pw_stream on WirePlumber's pw_core with the properties pipewire-pulse gives pavucontrol's peak streams (stream.monitor, target.object, stream.capture.sink for sinks); its process callback folds samples into a peak that a 40 ms GUI timer reads, maps with the slider's cubic curve and decays smoothly. - Only the visible tab is metered; output/stream meters are passive. - stream.monitor nodes (ours and pavucontrol's) are hidden from the tabs. - moonmix-probe gains `meter <id>`; the smoke test now plays noise and checks the stream and sink meters see it. - Screenshot mode holds the meters still before capturing, since a constantly redrawing window never yields a frame under broadway. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
611 lines
20 KiB
C
611 lines
20 KiB
C
/*
|
|
* moonmix: thin C layer over libwireplumber-0.5.
|
|
*
|
|
* Exactly what wpctl does internally, without spawning wpctl:
|
|
* - a WpObjectManager watching audio WpNodes, WpLinks and the "default"
|
|
* WpMetadata;
|
|
* - the "mixer-api" plugin for per-channel volume and mute (it handles
|
|
* device routes for hardware sinks, so it is what pavucontrol's numbers
|
|
* correspond to);
|
|
* - the "default-nodes-api" plugin for the default sink/source;
|
|
* - peak meters: a small capture pw_stream per monitored node, created on
|
|
* WirePlumber's own pw_core (the same connection), like the peak streams
|
|
* pipewire-pulse creates for pavucontrol.
|
|
*
|
|
* MoonBit gets a single callback (kind, id) for every change and pulls the
|
|
* details it needs through small accessor functions. All calls happen on the
|
|
* GLib default main context (shared with GTK), so no locking is needed.
|
|
*/
|
|
#include <string.h>
|
|
#include <wp/wp.h>
|
|
#include <pipewire/keys.h>
|
|
#include <pipewire/stream.h>
|
|
#include <spa/param/audio/format-utils.h>
|
|
#include "moonbit.h"
|
|
|
|
enum {
|
|
MM_EV_NODE_ADDED = 1,
|
|
MM_EV_NODE_CHANGED = 2,
|
|
MM_EV_NODE_REMOVED = 3,
|
|
MM_EV_LINKS_CHANGED = 4,
|
|
MM_EV_DEFAULTS_CHANGED = 5,
|
|
MM_EV_READY = 6,
|
|
MM_EV_DISCONNECTED = 7,
|
|
MM_EV_ERROR = 8,
|
|
MM_EV_TIMEOUT = 9,
|
|
};
|
|
|
|
typedef void (*mm_call_t)(void *closure, int32_t kind, int32_t id);
|
|
|
|
static mm_call_t g_call;
|
|
static void *g_closure;
|
|
static WpCore *g_core;
|
|
static WpObjectManager *g_om;
|
|
static WpPlugin *g_mixer;
|
|
static WpPlugin *g_defaults;
|
|
static GMainLoop *g_loop;
|
|
static int g_pending;
|
|
static gboolean g_om_installed;
|
|
static gboolean g_ready_sent;
|
|
|
|
static void emit(int32_t kind, int32_t id) {
|
|
if (g_call) {
|
|
/* MoonBit functions take ownership of their arguments. */
|
|
moonbit_incref(g_closure);
|
|
g_call(g_closure, kind, id);
|
|
}
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT void mm_wp_set_handler(mm_call_t call, void *closure) {
|
|
if (g_closure)
|
|
moonbit_decref(g_closure);
|
|
g_call = call;
|
|
g_closure = closure; /* owned, kept for the program lifetime */
|
|
}
|
|
|
|
/* ---- helpers ------------------------------------------------------------ */
|
|
|
|
static moonbit_bytes_t to_bytes(const char *s) {
|
|
size_t n = s ? strlen(s) : 0;
|
|
moonbit_bytes_t b = moonbit_make_bytes((int32_t)n, 0);
|
|
if (n)
|
|
memcpy(b, s, n);
|
|
return b;
|
|
}
|
|
|
|
static WpNode *lookup_node(guint32 id) {
|
|
if (!g_om)
|
|
return NULL;
|
|
return wp_object_manager_lookup(g_om, WP_TYPE_NODE,
|
|
WP_CONSTRAINT_TYPE_G_PROPERTY, "bound-id",
|
|
"=u", id, NULL);
|
|
}
|
|
|
|
static WpMetadata *lookup_default_metadata(void) {
|
|
if (!g_om)
|
|
return NULL;
|
|
return wp_object_manager_lookup(g_om, WP_TYPE_METADATA,
|
|
WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY,
|
|
"metadata.name", "=s", "default", NULL);
|
|
}
|
|
|
|
static void maybe_ready(void) {
|
|
if (!g_ready_sent && g_pending == 0 && g_om_installed) {
|
|
g_ready_sent = TRUE;
|
|
emit(MM_EV_READY, 0);
|
|
}
|
|
}
|
|
|
|
/* ---- signal handlers ---------------------------------------------------- */
|
|
|
|
static void on_node_props(GObject *obj, GParamSpec *pspec, gpointer data) {
|
|
(void)pspec;
|
|
(void)data;
|
|
emit(MM_EV_NODE_CHANGED, (int32_t)wp_proxy_get_bound_id(WP_PROXY(obj)));
|
|
}
|
|
|
|
static void on_object_added(WpObjectManager *om, GObject *obj, gpointer d) {
|
|
(void)om;
|
|
(void)d;
|
|
if (WP_IS_NODE(obj)) {
|
|
g_signal_connect(obj, "notify::properties", G_CALLBACK(on_node_props),
|
|
NULL);
|
|
emit(MM_EV_NODE_ADDED, (int32_t)wp_proxy_get_bound_id(WP_PROXY(obj)));
|
|
} else if (WP_IS_LINK(obj)) {
|
|
emit(MM_EV_LINKS_CHANGED, 0);
|
|
}
|
|
}
|
|
|
|
static void on_object_removed(WpObjectManager *om, GObject *obj, gpointer d) {
|
|
(void)om;
|
|
(void)d;
|
|
if (WP_IS_NODE(obj)) {
|
|
g_signal_handlers_disconnect_by_func(obj, on_node_props, NULL);
|
|
emit(MM_EV_NODE_REMOVED, (int32_t)wp_proxy_get_bound_id(WP_PROXY(obj)));
|
|
} else if (WP_IS_LINK(obj)) {
|
|
emit(MM_EV_LINKS_CHANGED, 0);
|
|
}
|
|
}
|
|
|
|
static void on_installed(WpObjectManager *om, gpointer d) {
|
|
(void)om;
|
|
(void)d;
|
|
g_om_installed = TRUE;
|
|
maybe_ready();
|
|
}
|
|
|
|
static void on_mixer_changed(WpPlugin *p, guint id, gpointer d) {
|
|
(void)p;
|
|
(void)d;
|
|
emit(MM_EV_NODE_CHANGED, (int32_t)id);
|
|
}
|
|
|
|
static void on_defaults_changed(WpPlugin *p, gpointer d) {
|
|
(void)p;
|
|
(void)d;
|
|
emit(MM_EV_DEFAULTS_CHANGED, 0);
|
|
}
|
|
|
|
static void forget_meters(void);
|
|
|
|
static void on_disconnected(WpCore *core, gpointer d) {
|
|
(void)core;
|
|
(void)d;
|
|
forget_meters();
|
|
emit(MM_EV_DISCONNECTED, 0);
|
|
}
|
|
|
|
static void on_plugin_loaded(GObject *src, GAsyncResult *res, gpointer d) {
|
|
const char *plugin_name = d;
|
|
g_autoptr(GError) error = NULL;
|
|
if (!wp_core_load_component_finish(WP_CORE(src), res, &error)) {
|
|
g_warning("moonmix: failed to load %s: %s", plugin_name,
|
|
error ? error->message : "?");
|
|
emit(MM_EV_ERROR, 1);
|
|
return;
|
|
}
|
|
if (g_str_equal(plugin_name, "mixer-api")) {
|
|
g_mixer = wp_plugin_find(g_core, "mixer-api");
|
|
if (g_mixer) {
|
|
/* 0 = linear: report raw channelVolumes, the MoonBit side does the
|
|
* cubic (pavucontrol) mapping. */
|
|
g_object_set(g_mixer, "scale", 0, NULL);
|
|
g_signal_connect(g_mixer, "changed", G_CALLBACK(on_mixer_changed),
|
|
NULL);
|
|
}
|
|
} else {
|
|
g_defaults = wp_plugin_find(g_core, "default-nodes-api");
|
|
if (g_defaults)
|
|
g_signal_connect(g_defaults, "changed", G_CALLBACK(on_defaults_changed),
|
|
NULL);
|
|
}
|
|
g_pending--;
|
|
if (g_pending == 0) {
|
|
/* Install the object manager only after the plugins are up, like wpctl
|
|
* does, so the first full sync already has volumes and defaults. */
|
|
wp_core_install_object_manager(g_core, g_om);
|
|
}
|
|
maybe_ready();
|
|
}
|
|
|
|
/* ---- lifecycle ---------------------------------------------------------- */
|
|
|
|
/* Returns 0 on success, -1 if PipeWire is not reachable. Events follow on
|
|
* the main loop; MM_EV_READY once the initial state is known. */
|
|
MOONBIT_FFI_EXPORT int32_t mm_wp_start(void) {
|
|
if (g_core)
|
|
return 0;
|
|
wp_init(WP_INIT_PIPEWIRE | WP_INIT_SPA_TYPES | WP_INIT_SET_GLIB_LOG);
|
|
|
|
g_autoptr(WpProperties) props =
|
|
wp_properties_new(PW_KEY_APP_NAME, "moonmix", NULL);
|
|
g_core = wp_core_new(NULL, NULL, g_steal_pointer(&props));
|
|
if (!wp_core_connect(g_core)) {
|
|
g_clear_object(&g_core);
|
|
return -1;
|
|
}
|
|
g_signal_connect(g_core, "disconnected", G_CALLBACK(on_disconnected), NULL);
|
|
|
|
g_om = wp_object_manager_new();
|
|
wp_object_manager_add_interest(g_om, WP_TYPE_NODE,
|
|
WP_CONSTRAINT_TYPE_PW_PROPERTY, "media.class",
|
|
"#s", "*Audio*", NULL);
|
|
wp_object_manager_add_interest(g_om, WP_TYPE_LINK, NULL);
|
|
wp_object_manager_add_interest(g_om, WP_TYPE_METADATA,
|
|
WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY,
|
|
"metadata.name", "=s", "default", NULL);
|
|
wp_object_manager_request_object_features(
|
|
g_om, WP_TYPE_GLOBAL_PROXY, WP_PIPEWIRE_OBJECT_FEATURES_MINIMAL);
|
|
wp_object_manager_request_object_features(g_om, WP_TYPE_METADATA,
|
|
WP_OBJECT_FEATURES_ALL);
|
|
g_signal_connect(g_om, "object-added", G_CALLBACK(on_object_added), NULL);
|
|
g_signal_connect(g_om, "object-removed", G_CALLBACK(on_object_removed),
|
|
NULL);
|
|
g_signal_connect(g_om, "installed", G_CALLBACK(on_installed), NULL);
|
|
|
|
g_pending = 2;
|
|
wp_core_load_component(g_core, "libwireplumber-module-mixer-api", "module",
|
|
NULL, NULL, NULL,
|
|
(GAsyncReadyCallback)on_plugin_loaded,
|
|
(gpointer) "mixer-api");
|
|
wp_core_load_component(g_core, "libwireplumber-module-default-nodes-api",
|
|
"module", NULL, NULL, NULL,
|
|
(GAsyncReadyCallback)on_plugin_loaded,
|
|
(gpointer) "default-nodes-api");
|
|
return 0;
|
|
}
|
|
|
|
static gboolean on_timeout(gpointer d) {
|
|
emit(MM_EV_TIMEOUT, GPOINTER_TO_INT(d));
|
|
return G_SOURCE_REMOVE;
|
|
}
|
|
|
|
/* One-shot timer delivering MM_EV_TIMEOUT with `tag` as id. */
|
|
MOONBIT_FFI_EXPORT void mm_wp_timeout(int32_t ms, int32_t tag) {
|
|
g_timeout_add((guint)ms, on_timeout, GINT_TO_POINTER(tag));
|
|
}
|
|
|
|
/* Standalone main loop for the headless probe (the GUI uses GTK's). */
|
|
MOONBIT_FFI_EXPORT void mm_wp_run(void) {
|
|
if (!g_loop)
|
|
g_loop = g_main_loop_new(NULL, FALSE);
|
|
g_main_loop_run(g_loop);
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT void mm_wp_quit(void) {
|
|
if (g_loop)
|
|
g_main_loop_quit(g_loop);
|
|
}
|
|
|
|
/* ---- queries ------------------------------------------------------------ */
|
|
|
|
static GArray *g_ids; /* snapshot for mm_wp_node_count/mm_wp_node_id_at */
|
|
|
|
MOONBIT_FFI_EXPORT int32_t mm_wp_node_count(void) {
|
|
if (!g_ids)
|
|
g_ids = g_array_new(FALSE, FALSE, sizeof(guint32));
|
|
g_array_set_size(g_ids, 0);
|
|
if (!g_om)
|
|
return 0;
|
|
g_autoptr(WpIterator) it = wp_object_manager_new_filtered_iterator(
|
|
g_om, WP_TYPE_NODE, NULL);
|
|
g_auto(GValue) val = G_VALUE_INIT;
|
|
for (; wp_iterator_next(it, &val); g_value_unset(&val)) {
|
|
guint32 id = wp_proxy_get_bound_id(WP_PROXY(g_value_get_object(&val)));
|
|
g_array_append_val(g_ids, id);
|
|
}
|
|
return (int32_t)g_ids->len;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT int32_t mm_wp_node_id_at(int32_t i) {
|
|
if (!g_ids || i < 0 || (guint)i >= g_ids->len)
|
|
return -1;
|
|
return (int32_t)g_array_index(g_ids, guint32, i);
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT int32_t mm_wp_node_exists(int32_t id) {
|
|
g_autoptr(WpNode) node = lookup_node((guint32)id);
|
|
return node != NULL;
|
|
}
|
|
|
|
/* A PipeWire property of a node ("node.name", "media.class", ...), "" if
|
|
* unset. `key` is NUL-terminated UTF-8. */
|
|
MOONBIT_FFI_EXPORT moonbit_bytes_t mm_wp_node_prop(int32_t id,
|
|
moonbit_bytes_t key) {
|
|
g_autoptr(WpNode) node = lookup_node((guint32)id);
|
|
const char *v = NULL;
|
|
if (node)
|
|
v = wp_pipewire_object_get_property(WP_PIPEWIRE_OBJECT(node),
|
|
(const char *)key);
|
|
return to_bytes(v);
|
|
}
|
|
|
|
/* Volume of the last queried node. */
|
|
#define MM_MAX_CH 64
|
|
static double g_vol[MM_MAX_CH];
|
|
static int g_nch;
|
|
static int g_mute;
|
|
|
|
/* Asks mixer-api for a node's volume. Returns the channel count (0 if the
|
|
* node has no volume control yet), then read mm_wp_volume_at / mm_wp_mute. */
|
|
MOONBIT_FFI_EXPORT int32_t mm_wp_query_volume(int32_t id) {
|
|
g_nch = 0;
|
|
g_mute = 0;
|
|
if (!g_mixer)
|
|
return 0;
|
|
GVariant *v = NULL;
|
|
g_signal_emit_by_name(g_mixer, "get-volume", (guint)id, &v);
|
|
if (!v)
|
|
return 0;
|
|
gboolean mute = FALSE;
|
|
g_variant_lookup(v, "mute", "b", &mute);
|
|
g_mute = mute;
|
|
g_autoptr(GVariant) chans =
|
|
g_variant_lookup_value(v, "channelVolumes", G_VARIANT_TYPE_VARDICT);
|
|
if (chans) {
|
|
GVariantIter iter;
|
|
const char *key;
|
|
GVariant *ch;
|
|
g_variant_iter_init(&iter, chans);
|
|
while (g_variant_iter_loop(&iter, "{&sv}", &key, &ch)) {
|
|
int idx = atoi(key);
|
|
double vol = 0;
|
|
if (idx >= 0 && idx < MM_MAX_CH && g_variant_lookup(ch, "volume", "d", &vol)) {
|
|
g_vol[idx] = vol;
|
|
if (idx + 1 > g_nch)
|
|
g_nch = idx + 1;
|
|
}
|
|
}
|
|
} else {
|
|
double vol;
|
|
if (g_variant_lookup(v, "volume", "d", &vol)) {
|
|
g_vol[0] = vol;
|
|
g_nch = 1;
|
|
}
|
|
}
|
|
g_variant_unref(v);
|
|
return g_nch;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT double mm_wp_volume_at(int32_t ch) {
|
|
return (ch >= 0 && ch < g_nch) ? g_vol[ch] : 0.0;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT int32_t mm_wp_mute(void) { return g_mute; }
|
|
|
|
/* Links snapshot. */
|
|
static GArray *g_links;
|
|
|
|
MOONBIT_FFI_EXPORT int32_t mm_wp_link_count(void) {
|
|
if (!g_links)
|
|
g_links = g_array_new(FALSE, FALSE, sizeof(guint32) * 2);
|
|
g_array_set_size(g_links, 0);
|
|
if (!g_om)
|
|
return 0;
|
|
g_autoptr(WpIterator) it = wp_object_manager_new_filtered_iterator(
|
|
g_om, WP_TYPE_LINK, NULL);
|
|
g_auto(GValue) val = G_VALUE_INIT;
|
|
for (; wp_iterator_next(it, &val); g_value_unset(&val)) {
|
|
guint32 pair[2] = {0, 0};
|
|
guint32 out_port, in_port;
|
|
wp_link_get_linked_object_ids(WP_LINK(g_value_get_object(&val)), &pair[0],
|
|
&out_port, &pair[1], &in_port);
|
|
g_array_append_val(g_links, pair);
|
|
}
|
|
return (int32_t)g_links->len;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT int32_t mm_wp_link_end(int32_t i, int32_t input) {
|
|
if (!g_links || i < 0 || (guint)i >= g_links->len)
|
|
return -1;
|
|
guint32 *pair = &g_array_index(g_links, guint32, i * 2);
|
|
return (int32_t)pair[input ? 1 : 0];
|
|
}
|
|
|
|
static const char *class_for(int32_t kind) {
|
|
return kind == 0 ? "Audio/Sink" : "Audio/Source";
|
|
}
|
|
|
|
/* Default node id for kind 0 = sink, 1 = source; -1 if none. */
|
|
MOONBIT_FFI_EXPORT int32_t mm_wp_default_node(int32_t kind) {
|
|
if (!g_defaults)
|
|
return -1;
|
|
guint32 id = SPA_ID_INVALID;
|
|
g_signal_emit_by_name(g_defaults, "get-default-node", class_for(kind), &id);
|
|
return id == SPA_ID_INVALID ? -1 : (int32_t)id;
|
|
}
|
|
|
|
/* ---- actions ------------------------------------------------------------ */
|
|
|
|
/* Set linear per-channel volumes. */
|
|
MOONBIT_FFI_EXPORT int32_t mm_wp_set_volumes(int32_t id, double *vols,
|
|
int32_t n) {
|
|
if (!g_mixer || n <= 0)
|
|
return 0;
|
|
GVariantBuilder chans;
|
|
g_variant_builder_init(&chans, G_VARIANT_TYPE_VARDICT);
|
|
for (int i = 0; i < n; i++) {
|
|
GVariantBuilder ch;
|
|
g_variant_builder_init(&ch, G_VARIANT_TYPE_VARDICT);
|
|
g_variant_builder_add(&ch, "{sv}", "volume", g_variant_new_double(vols[i]));
|
|
char key[16];
|
|
g_snprintf(key, sizeof key, "%d", i);
|
|
g_variant_builder_add(&chans, "{sv}", key, g_variant_builder_end(&ch));
|
|
}
|
|
GVariantBuilder b;
|
|
g_variant_builder_init(&b, G_VARIANT_TYPE_VARDICT);
|
|
g_variant_builder_add(&b, "{sv}", "channelVolumes",
|
|
g_variant_builder_end(&chans));
|
|
gboolean ok = FALSE;
|
|
g_signal_emit_by_name(g_mixer, "set-volume", (guint)id,
|
|
g_variant_builder_end(&b), &ok);
|
|
return ok;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT int32_t mm_wp_set_mute(int32_t id, int32_t mute) {
|
|
if (!g_mixer)
|
|
return 0;
|
|
GVariantBuilder b;
|
|
g_variant_builder_init(&b, G_VARIANT_TYPE_VARDICT);
|
|
g_variant_builder_add(&b, "{sv}", "mute", g_variant_new_boolean(mute != 0));
|
|
gboolean ok = FALSE;
|
|
g_signal_emit_by_name(g_mixer, "set-volume", (guint)id,
|
|
g_variant_builder_end(&b), &ok);
|
|
return ok;
|
|
}
|
|
|
|
/* Make node `id` the configured default sink (kind 0) or source (kind 1). */
|
|
MOONBIT_FFI_EXPORT int32_t mm_wp_set_default(int32_t kind, int32_t id) {
|
|
if (!g_defaults)
|
|
return 0;
|
|
g_autoptr(WpNode) node = lookup_node((guint32)id);
|
|
if (!node)
|
|
return 0;
|
|
const char *name = wp_pipewire_object_get_property(WP_PIPEWIRE_OBJECT(node),
|
|
PW_KEY_NODE_NAME);
|
|
if (!name)
|
|
return 0;
|
|
gboolean ok = FALSE;
|
|
g_signal_emit_by_name(g_defaults, "set-default-configured-node-name",
|
|
class_for(kind), name, &ok);
|
|
return ok;
|
|
}
|
|
|
|
/* Route a stream to another device, the way pipewire-pulse implements
|
|
* pavucontrol's move: set "target.object" (the device's object.serial) on
|
|
* the stream in the "default" metadata; WirePlumber relinks it. */
|
|
MOONBIT_FFI_EXPORT int32_t mm_wp_move_stream(int32_t stream_id,
|
|
int32_t device_id) {
|
|
g_autoptr(WpMetadata) m = lookup_default_metadata();
|
|
g_autoptr(WpNode) dev = lookup_node((guint32)device_id);
|
|
if (!m || !dev)
|
|
return 0;
|
|
const char *serial = wp_pipewire_object_get_property(
|
|
WP_PIPEWIRE_OBJECT(dev), PW_KEY_OBJECT_SERIAL);
|
|
if (!serial)
|
|
return 0;
|
|
wp_metadata_set(m, (guint32)stream_id, "target.node", NULL, NULL);
|
|
wp_metadata_set(m, (guint32)stream_id, "target.object", "Spa:Id", serial);
|
|
return 1;
|
|
}
|
|
|
|
/* ---- peak meters ---------------------------------------------------------- */
|
|
|
|
/* One capture stream per monitored node. Not RT_PROCESS, so `process` runs
|
|
* on the main loop; it only folds the buffer into `peak`, which the GUI reads
|
|
* (and resets) from a timer. */
|
|
typedef struct {
|
|
struct pw_stream *stream;
|
|
struct spa_hook listener;
|
|
float peak;
|
|
} MmMeter;
|
|
|
|
static GHashTable *g_meters; /* node id -> MmMeter* */
|
|
|
|
static void meter_process(void *data) {
|
|
MmMeter *m = data;
|
|
struct pw_buffer *b = pw_stream_dequeue_buffer(m->stream);
|
|
if (!b)
|
|
return;
|
|
struct spa_buffer *buf = b->buffer;
|
|
if (buf->n_datas > 0 && buf->datas[0].data) {
|
|
struct spa_data *d = &buf->datas[0];
|
|
uint32_t off = SPA_MIN(d->chunk->offset, d->maxsize);
|
|
uint32_t size = SPA_MIN(d->chunk->size, d->maxsize - off);
|
|
const float *f = SPA_PTROFF(d->data, off, const float);
|
|
float peak = m->peak;
|
|
for (uint32_t i = 0; i < size / sizeof(float); i++) {
|
|
float v = f[i] < 0 ? -f[i] : f[i];
|
|
if (v > peak)
|
|
peak = v;
|
|
}
|
|
m->peak = peak;
|
|
}
|
|
pw_stream_queue_buffer(m->stream, b);
|
|
}
|
|
|
|
static const struct pw_stream_events meter_events = {
|
|
PW_VERSION_STREAM_EVENTS,
|
|
.process = meter_process,
|
|
};
|
|
|
|
static void meter_free(gpointer p) {
|
|
MmMeter *m = p;
|
|
if (m->stream)
|
|
pw_stream_destroy(m->stream);
|
|
g_free(m);
|
|
}
|
|
|
|
/* Start a peak meter on node `id`. `kind`: 0 = sink (captures its monitor),
|
|
* 1 = source, 2 = playback stream. Returns 1 if a meter runs (or already
|
|
* ran) for it. */
|
|
MOONBIT_FFI_EXPORT int32_t mm_wp_meter_start(int32_t id, int32_t kind) {
|
|
if (!g_core || !wp_core_is_connected(g_core))
|
|
return 0;
|
|
if (!g_meters)
|
|
g_meters = g_hash_table_new_full(NULL, NULL, NULL, meter_free);
|
|
if (g_hash_table_contains(g_meters, GINT_TO_POINTER(id)))
|
|
return 1;
|
|
g_autoptr(WpNode) node = lookup_node((guint32)id);
|
|
if (!node)
|
|
return 0;
|
|
const char *serial = wp_pipewire_object_get_property(
|
|
WP_PIPEWIRE_OBJECT(node), PW_KEY_OBJECT_SERIAL);
|
|
struct pw_core *pwc = wp_core_get_pw_core(g_core);
|
|
if (!serial || !pwc)
|
|
return 0;
|
|
|
|
struct pw_properties *props = pw_properties_new(
|
|
PW_KEY_MEDIA_TYPE, "Audio", PW_KEY_MEDIA_CATEGORY, "Capture",
|
|
PW_KEY_MEDIA_ROLE, "DSP", PW_KEY_NODE_NAME, "moonmix-peak",
|
|
PW_KEY_APP_NAME, "moonmix", PW_KEY_STREAM_MONITOR, "true",
|
|
PW_KEY_TARGET_OBJECT, serial, PW_KEY_NODE_DONT_RECONNECT, "true",
|
|
NULL);
|
|
if (kind == 0)
|
|
pw_properties_set(props, PW_KEY_STREAM_CAPTURE_SINK, "true");
|
|
/* Don't keep outputs running just to meter them; a source is opened so
|
|
* its level shows, like pavucontrol does. */
|
|
if (kind != 1)
|
|
pw_properties_set(props, PW_KEY_NODE_PASSIVE, "true");
|
|
|
|
MmMeter *m = g_new0(MmMeter, 1);
|
|
m->stream = pw_stream_new(pwc, "moonmix peak meter", props);
|
|
if (!m->stream) {
|
|
g_free(m);
|
|
return 0;
|
|
}
|
|
pw_stream_add_listener(m->stream, &m->listener, &meter_events, m);
|
|
|
|
uint8_t buffer[512];
|
|
struct spa_pod_builder pb = SPA_POD_BUILDER_INIT(buffer, sizeof buffer);
|
|
struct spa_audio_info_raw info = {.format = SPA_AUDIO_FORMAT_F32,
|
|
.channels = 1};
|
|
const struct spa_pod *params[1] = {
|
|
spa_format_audio_raw_build(&pb, SPA_PARAM_EnumFormat, &info)};
|
|
if (pw_stream_connect(m->stream, PW_DIRECTION_INPUT, PW_ID_ANY,
|
|
PW_STREAM_FLAG_AUTOCONNECT |
|
|
PW_STREAM_FLAG_MAP_BUFFERS |
|
|
PW_STREAM_FLAG_DONT_RECONNECT,
|
|
params, 1) < 0) {
|
|
meter_free(m);
|
|
return 0;
|
|
}
|
|
g_hash_table_insert(g_meters, GINT_TO_POINTER(id), m);
|
|
return 1;
|
|
}
|
|
|
|
/* The streams belong to the pw_core, which libpipewire tears down (with
|
|
* them) on disconnect: drop our records without touching the streams. */
|
|
static void forget_meters(void) {
|
|
if (!g_meters)
|
|
return;
|
|
GHashTableIter it;
|
|
gpointer v;
|
|
g_hash_table_iter_init(&it, g_meters);
|
|
while (g_hash_table_iter_next(&it, NULL, &v))
|
|
((MmMeter *)v)->stream = NULL;
|
|
g_hash_table_remove_all(g_meters);
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT void mm_wp_meter_stop(int32_t id) {
|
|
if (g_meters)
|
|
g_hash_table_remove(g_meters, GINT_TO_POINTER(id));
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT void mm_wp_meter_stop_all(void) {
|
|
if (g_meters)
|
|
g_hash_table_remove_all(g_meters);
|
|
}
|
|
|
|
/* Highest absolute sample since the last call (linear, 0..1+), then resets;
|
|
* -1 if node `id` has no meter. */
|
|
MOONBIT_FFI_EXPORT double mm_wp_meter_take(int32_t id) {
|
|
MmMeter *m = g_meters ? g_hash_table_lookup(g_meters, GINT_TO_POINTER(id))
|
|
: NULL;
|
|
if (!m)
|
|
return -1.0;
|
|
double p = m->peak;
|
|
m->peak = 0;
|
|
return p;
|
|
}
|