PipeWire 1.6.8

Loading...
Searching...
No Matches
audio-dsp-sink2.c

Audio sink using pw_filter

Audio sink using pw_filter

/* PipeWire */
/* SPDX-FileCopyrightText: Copyright © 2025 Wim Taymans */
/* SPDX-License-Identifier: MIT */
/*
[title]
Audio sink using \ref pw_filter "pw_filter"
[title]
*/
#include "config.h"
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <spa/pod/builder.h>
#include <pipewire/pipewire.h>
#include <pipewire/filter.h>
struct data;
struct port {
struct data *data;
};
struct data {
struct pw_main_loop *loop;
struct pw_filter *filter;
struct port *in_port;
bool move;
uint32_t quantum_limit;
};
/* our data processing function is in general:
*
* struct pw_buffer *b;
* out = pw_filter_dequeue_buffer(filter, in_port);
*
* .. consume data in the buffer ...
*
* pw_filter_queue_buffer(filter, in_port, out);
*
* For DSP ports, there is a shortcut to directly dequeue, get
* the data and requeue the buffer with pw_filter_get_dsp_buffer().
*/
static void on_process(void *userdata, struct spa_io_position *position)
{
struct data *data = userdata;
float *in, max;
struct port *in_port = data->in_port;
uint32_t i, n_samples = position->clock.duration, peak;
pw_log_trace("do process %d", n_samples);
in = pw_filter_get_dsp_buffer(in_port, n_samples);
if (in == NULL)
return;
/* move cursor up */
if (data->move)
fprintf(stdout, "%c[%dA", 0x1b, 2);
fprintf(stdout, "captured %d samples\n", n_samples);
max = 0.0f;
for (i = 0; i < n_samples; i++)
max = fmaxf(max, fabsf(in[i]));
peak = (uint32_t)SPA_CLAMPF(max * 30, 0.f, 39.f);
fprintf(stdout, "input: |%*s%*s| peak:%f\n", peak+1, "*", 40 - peak, "", max);
data->move = true;
fflush(stdout);
}
/* Check the buffer memory */
static void on_add_buffer(void *_data, void *_port_data, struct pw_buffer *buffer)
{
struct spa_buffer *buf = buffer->buffer;
struct spa_data *d;
pw_log_info("add buffer %p", buffer);
d = buf->datas;
if ((d[0].type != SPA_DATA_MemFd)) {
pw_log_error("unsupported data type %08x", d[0].type);
return;
}
}
static const struct pw_filter_events filter_events = {
PW_VERSION_FILTER_EVENTS,
.process = on_process,
.add_buffer = on_add_buffer,
};
static void do_quit(void *userdata, int signal_number)
{
struct data *data = userdata;
pw_main_loop_quit(data->loop);
}
int main(int argc, char *argv[])
{
struct data data = { 0, };
uint32_t flags, n_params = 0;
const struct spa_pod *params[1];
uint8_t buffer[1024];
struct spa_pod_builder b;
pw_init(&argc, &argv);
data.quantum_limit = 8192;
/* make a main loop. If you already have another main loop, you can add
* the fd of this pipewire mainloop to it. */
data.loop = pw_main_loop_new(NULL);
pw_loop_add_signal(pw_main_loop_get_loop(data.loop), SIGINT, do_quit, &data);
pw_loop_add_signal(pw_main_loop_get_loop(data.loop), SIGTERM, do_quit, &data);
/* Create a simple filter, the simple filter manages the core and remote
* objects for you if you don't need to deal with them.
*
* Pass your events and a user_data pointer as the last arguments. This
* will inform you about the filter state. The most important event
* you need to listen to is the process event where you need to process
* the data.
*/
data.filter = pw_filter_new_simple(
pw_main_loop_get_loop(data.loop),
"audio-dsp-sink2",
pw_properties_new(
PW_KEY_MEDIA_TYPE, "Audio",
PW_KEY_MEDIA_CATEGORY, "Sink",
PW_KEY_MEDIA_ROLE, "DSP",
PW_KEY_MEDIA_CLASS, "Stream/Input/Audio",
PW_KEY_NODE_AUTOCONNECT, "true",
NULL),
&filter_events,
&data);
flags = PW_FILTER_PORT_FLAG_MAP_BUFFERS;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
params[n_params++] = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers,
SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(1, 1, 16),
SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(1),
SPA_PARAM_BUFFERS_size, SPA_POD_Int(sizeof(float) * data.quantum_limit),
SPA_PARAM_BUFFERS_stride, SPA_POD_Int(sizeof(float)),
SPA_PARAM_BUFFERS_dataType, SPA_POD_CHOICE_FLAGS_Int(1<<SPA_DATA_MemFd));
/* make an audio DSP output port */
data.in_port = pw_filter_add_port(data.filter,
PW_DIRECTION_INPUT,
flags,
sizeof(struct port),
pw_properties_new(
PW_KEY_FORMAT_DSP, "32 bit float mono audio",
PW_KEY_PORT_NAME, "input",
NULL),
params, n_params);
/* Now connect this filter. We ask that our process function is
* called in a realtime thread. */
if (pw_filter_connect(data.filter,
PW_FILTER_FLAG_RT_PROCESS,
NULL, 0) < 0) {
fprintf(stderr, "can't connect\n");
return -1;
}
/* and wait while we let things run */
pw_main_loop_run(data.loop);
pw_filter_destroy(data.filter);
pw_main_loop_destroy(data.loop);
pw_deinit();
return 0;
}
builder.h
spa/pod/builder.h
pw_filter_connect
int pw_filter_connect(struct pw_filter *filter, enum pw_filter_flags flags, const struct spa_pod **params, uint32_t n_params)
Connect a filter for processing.
Definition filter.c:1573
pw_filter_add_port
void * pw_filter_add_port(struct pw_filter *filter, enum pw_direction direction, enum pw_filter_port_flags flags, size_t port_data_size, struct pw_properties *props, const struct spa_pod **params, uint32_t n_params)
add a port to the filter, returns user data of port_data_size.
Definition filter.c:1816
PW_VERSION_FILTER_EVENTS
#define PW_VERSION_FILTER_EVENTS
Definition filter.h:67
pw_filter_destroy
void pw_filter_destroy(struct pw_filter *filter)
Destroy a filter.
Definition filter.c:1421
pw_filter_get_dsp_buffer
void * pw_filter_get_dsp_buffer(void *port_data, uint32_t n_samples)
Get a data pointer to the buffer data.
Definition filter.c:2057
pw_filter_new_simple
struct pw_filter * pw_filter_new_simple(struct pw_loop *loop, const char *name, struct pw_properties *props, const struct pw_filter_events *events, void *data)
Definition filter.c:1295
PW_FILTER_FLAG_RT_PROCESS
@ PW_FILTER_FLAG_RT_PROCESS
call process from the realtime thread.
Definition filter.h:112
PW_FILTER_PORT_FLAG_MAP_BUFFERS
@ PW_FILTER_PORT_FLAG_MAP_BUFFERS
mmap the buffers except DmaBuf that is not explicitly marked as mappable.
Definition filter.h:134
PW_KEY_PORT_NAME
#define PW_KEY_PORT_NAME
port name
Definition keys.h:346
PW_KEY_MEDIA_TYPE
#define PW_KEY_MEDIA_TYPE
Media.
Definition keys.h:512
PW_KEY_NODE_AUTOCONNECT
#define PW_KEY_NODE_AUTOCONNECT
node wants to be automatically connected to a compatible node
Definition keys.h:238
PW_KEY_MEDIA_ROLE
#define PW_KEY_MEDIA_ROLE
Role: Movie, Music, Camera, Screen, Communication, Game, Notification, DSP, Production,...
Definition keys.h:518
PW_KEY_MEDIA_CATEGORY
#define PW_KEY_MEDIA_CATEGORY
Media Category: Playback, Capture, Duplex, Monitor, Manager.
Definition keys.h:515
PW_KEY_FORMAT_DSP
#define PW_KEY_FORMAT_DSP
format related properties
Definition keys.h:555
PW_KEY_MEDIA_CLASS
#define PW_KEY_MEDIA_CLASS
class Ex: "Video/Source"
Definition keys.h:523
pw_log_trace
#define pw_log_trace(...)
Definition log.h:182
pw_log_error
#define pw_log_error(...)
Definition log.h:178
pw_log_info
#define pw_log_info(...)
Definition log.h:180
pw_loop_add_signal
PW_API_LOOP_IMPL struct spa_source * pw_loop_add_signal(struct pw_loop *object, int signal_number, spa_source_signal_func_t func, void *data)
Definition loop.h:177
pw_main_loop_quit
int pw_main_loop_quit(struct pw_main_loop *loop)
Quit a main loop.
Definition main-loop.c:108
pw_main_loop_destroy
void pw_main_loop_destroy(struct pw_main_loop *loop)
Destroy a loop.
Definition main-loop.c:73
pw_main_loop_run
int pw_main_loop_run(struct pw_main_loop *loop)
Run a main loop.
Definition main-loop.c:122
pw_main_loop_new
struct pw_main_loop * pw_main_loop_new(const struct spa_dict *props)
Create a new main loop.
Definition main-loop.c:63
pw_main_loop_get_loop
struct pw_loop * pw_main_loop_get_loop(struct pw_main_loop *loop)
Get the loop implementation.
Definition main-loop.c:96
pw_init
void pw_init(int *argc, char **argv[])
Initialize PipeWire.
Definition pipewire.c:499
pw_deinit
void pw_deinit(void)
Deinitialize PipeWire.
Definition pipewire.c:614
PW_DIRECTION_INPUT
#define PW_DIRECTION_INPUT
Definition port.h:53
pw_properties_new
struct pw_properties * pw_properties_new(const char *key,...)
Make a new properties object.
Definition properties.c:97
SPA_DATA_MemFd
@ SPA_DATA_MemFd
memfd, mmap to get to memory.
Definition buffer.h:45
SPA_PARAM_Buffers
@ SPA_PARAM_Buffers
buffer configurations as SPA_TYPE_OBJECT_ParamBuffers
Definition param.h:35
SPA_PARAM_BUFFERS_dataType
@ SPA_PARAM_BUFFERS_dataType
possible memory types (flags choice Int, mask of enum spa_data_type)
Definition buffers.h:32
SPA_PARAM_BUFFERS_size
@ SPA_PARAM_BUFFERS_size
size of a data block memory (Int)
Definition buffers.h:29
SPA_PARAM_BUFFERS_stride
@ SPA_PARAM_BUFFERS_stride
stride of data block memory (Int)
Definition buffers.h:30
SPA_PARAM_BUFFERS_blocks
@ SPA_PARAM_BUFFERS_blocks
number of data blocks per buffer (Int)
Definition buffers.h:28
SPA_PARAM_BUFFERS_buffers
@ SPA_PARAM_BUFFERS_buffers
number of buffers (Int)
Definition buffers.h:27
SPA_POD_CHOICE_RANGE_Int
#define SPA_POD_CHOICE_RANGE_Int(def, min, max)
Definition vararg.h:62
spa_pod_builder_init
SPA_API_POD_BUILDER void spa_pod_builder_init(struct spa_pod_builder *builder, void *data, uint32_t size)
Definition builder.h:102
spa_pod_builder_add_object
#define spa_pod_builder_add_object(b, type, id,...)
Definition builder.h:725
SPA_POD_Int
#define SPA_POD_Int(val)
Definition vararg.h:58
SPA_POD_CHOICE_FLAGS_Int
#define SPA_POD_CHOICE_FLAGS_Int(flags)
Definition vararg.h:66
SPA_TYPE_OBJECT_ParamBuffers
@ SPA_TYPE_OBJECT_ParamBuffers
Definition type.h:88
SPA_CLAMPF
#define SPA_CLAMPF(v, low, high)
Definition defs.h:185
pipewire.h
pipewire/pipewire.h
filter.h
pipewire/filter.h
pw_buffer
a buffer structure obtained from pw_stream_dequeue_buffer().
Definition stream.h:261
pw_filter_events
Events for a filter.
Definition filter.h:65
pw_filter
pw_main_loop
A main loop object.
spa_buffer
A Buffer.
Definition buffer.h:110
spa_buffer::datas
struct spa_data * datas
array of data members
Definition buffer.h:114
spa_data
Data for a buffer this stays constant for a buffer.
Definition buffer.h:78
spa_data::type
uint32_t type
memory type, one of enum spa_data_type, when allocating memory, the type contains a bitmask of allowe...
Definition buffer.h:79
spa_io_clock::duration
uint64_t duration
Duration of current cycle, in samples @ rate.
Definition io.h:193
spa_io_position
The position information adds extra meaning to the raw clock times.
Definition io.h:353
spa_io_position::clock
struct spa_io_clock clock
clock position of driver, always valid and read only
Definition io.h:354
spa_pod_builder
Definition builder.h:63
spa_pod
Definition pod.h:57