PipeWire 1.0.5
Loading...
Searching...
No Matches
audio-dsp-filter.c

Audio filter using pw_filter.

Audio filter using pw_filter.

/* PipeWire */
/* SPDX-FileCopyrightText: Copyright © 2019 Wim Taymans */
/* SPDX-License-Identifier: MIT */
/*
[title]
Audio filter using \ref pw_filter "pw_filter".
[title]
*/
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <signal.h>
struct data;
struct port {
struct data *data;
};
struct data {
struct pw_main_loop *loop;
struct pw_filter *filter;
struct port *in_port;
struct port *out_port;
};
/* our data processing function is in general:
*
* struct pw_buffer *b;
* in = pw_filter_dequeue_buffer(filter, in_port);
* out = pw_filter_dequeue_buffer(filter, out_port);
*
* .. do stuff with buffers ...
*
* pw_filter_queue_buffer(filter, in_port, in);
* pw_filter_queue_buffer(filter, out_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, *out;
uint32_t n_samples = position->clock.duration;
pw_log_trace("do process %d", n_samples);
in = pw_filter_get_dsp_buffer(data->in_port, n_samples);
out = pw_filter_get_dsp_buffer(data->out_port, n_samples);
if (in == NULL || out == NULL)
return;
memcpy(out, in, n_samples * sizeof(float));
}
static const struct pw_filter_events filter_events = {
.process = on_process,
};
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, };
const struct spa_pod *params[1];
uint8_t buffer[1024];
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
pw_init(&argc, &argv);
/* 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(
"audio-filter",
NULL),
&filter_events,
&data);
/* make an audio DSP input port */
data.in_port = pw_filter_add_port(data.filter,
sizeof(struct port),
PW_KEY_FORMAT_DSP, "32 bit float mono audio",
PW_KEY_PORT_NAME, "input",
NULL),
NULL, 0);
/* make an audio DSP output port */
data.out_port = pw_filter_add_port(data.filter,
sizeof(struct port),
PW_KEY_FORMAT_DSP, "32 bit float mono audio",
PW_KEY_PORT_NAME, "output",
NULL),
NULL, 0);
params[0] = spa_process_latency_build(&b,
.ns = 10 * SPA_NSEC_PER_MSEC
));
/* Now connect this filter. We ask that our process function is
* called in a realtime thread. */
if (pw_filter_connect(data.filter,
params, 1) < 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);
return 0;
}
spa/pod/builder.h
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:1595
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:1826
#define PW_VERSION_FILTER_EVENTS
Definition filter.h:66
void pw_filter_destroy(struct pw_filter *filter)
Destroy a filter
Definition filter.c:1432
void * pw_filter_get_dsp_buffer(void *port_data, uint32_t n_samples)
Get a data pointer to the buffer data.
Definition filter.c:2039
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:1328
@ PW_FILTER_FLAG_RT_PROCESS
call process from the realtime thread
Definition filter.h:108
@ PW_FILTER_PORT_FLAG_MAP_BUFFERS
mmap the buffers except DmaBuf that is not explicitly marked as mappable.
Definition filter.h:129
#define PW_KEY_PORT_NAME
port name
Definition keys.h:282
#define PW_KEY_MEDIA_TYPE
Media.
Definition keys.h:438
#define PW_KEY_MEDIA_ROLE
Role: Movie, Music, Camera, Screen, Communication, Game, Notification, DSP, Production,...
Definition keys.h:444
#define PW_KEY_MEDIA_CATEGORY
Media Category: Playback, Capture, Duplex, Monitor, Manager.
Definition keys.h:441
#define PW_KEY_FORMAT_DSP
format related properties
Definition keys.h:481
#define pw_log_trace(...)
Definition log.h:135
#define pw_loop_add_signal(l,...)
Definition loop.h:63
int pw_main_loop_quit(struct pw_main_loop *loop)
Quit a main loop.
Definition main-loop.c:106
void pw_main_loop_destroy(struct pw_main_loop *loop)
Destroy a loop.
Definition main-loop.c:71
int pw_main_loop_run(struct pw_main_loop *loop)
Run a main loop.
Definition main-loop.c:120
struct pw_main_loop * pw_main_loop_new(const struct spa_dict *props)
Create a new main loop.
Definition main-loop.c:61
struct pw_loop * pw_main_loop_get_loop(struct pw_main_loop *loop)
Get the loop implementation.
Definition main-loop.c:94
void pw_init(int *argc, char **argv[])
Initialize PipeWire.
Definition pipewire.c:556
void pw_deinit(void)
Deinitialize PipeWire.
Definition pipewire.c:669
#define PW_DIRECTION_OUTPUT
Definition port.h:50
#define PW_DIRECTION_INPUT
Definition port.h:48
struct pw_properties * pw_properties_new(const char *key,...)
Make a new properties object.
Definition properties.c:85
static struct spa_pod * spa_process_latency_build(struct spa_pod_builder *builder, uint32_t id, const struct spa_process_latency_info *info)
Definition latency-utils.h:131
#define SPA_PROCESS_LATENCY_INFO_INIT(...)
Definition latency.h:66
@ SPA_PARAM_ProcessLatency
processing latency, a SPA_TYPE_OBJECT_ParamProcessLatency
Definition param.h:46
#define SPA_POD_BUILDER_INIT(buffer, size)
Definition builder.h:62
#define SPA_NSEC_PER_MSEC
Definition defs.h:237
pipewire/pipewire.h
pipewire/filter.h
Events for a filter.
Definition filter.h:64
void(* process)(void *data, struct spa_io_position *position)
do processing.
Definition filter.h:89
A main loop object.
uint64_t duration
duration of current cycle
Definition io.h:135
The position information adds extra meaning to the raw clock times.
Definition io.h:279
struct spa_io_clock clock
clock position of driver, always valid and read only
Definition io.h:280
Definition builder.h:53
Definition pod.h:43