Add live peak meters (pavucontrol-style level bars)
CI / check (push) Successful in 10s
CI / native (push) Successful in 1m39s

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>
This commit is contained in:
nandiandClaude Opus 5.5 committed 2026-11-22 16:33:12 -08:00
1 parent 92ae5cdbfe
commit f08f241edb
17 files changed
+503 -18

No files matched your search

+149 -1
View File
@@ -7,7 +7,10 @@
* - 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.
* - 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
@@ -16,6 +19,8 @@
#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 {
@@ -141,9 +146,12 @@ static void on_defaults_changed(WpPlugin *p, gpointer 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);
}
@@ -460,3 +468,143 @@ MOONBIT_FFI_EXPORT int32_t mm_wp_move_stream(int32_t stream_id,
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;
}