PipeWire 0.3.65
audio-dsp-src.c

Audio source using pw_filter

/* PipeWire
*
* Copyright © 2020 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*
[title]
Audio source using \ref pw_filter "pw_filter"
[title]
*/
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <signal.h>
#define M_PI_M2 ( M_PI + M_PI )
#define DEFAULT_RATE 44100
#define DEFAULT_FREQ 440
#define DEFAULT_VOLUME 0.7
struct data;
struct port {
struct data *data;
double accumulator;
};
struct data {
struct pw_main_loop *loop;
struct pw_filter *filter;
struct port *out_port;
};
/* our data processing function is in general:
*
* struct pw_buffer *b;
* out = pw_filter_dequeue_buffer(filter, out_port);
*
* .. generate data in the buffer ...
*
* 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 *out;
struct port *out_port = data->out_port;
uint32_t i, n_samples = position->clock.duration;
pw_log_trace("do process %d", n_samples);
out = pw_filter_get_dsp_buffer(out_port, n_samples);
if (out == NULL)
return;
for (i = 0; i < n_samples; i++) {
out_port->accumulator += M_PI_M2 * DEFAULT_FREQ / DEFAULT_RATE;
if (out_port->accumulator >= M_PI_M2)
out_port->accumulator -= M_PI_M2;
*out++ = sin(out_port->accumulator) * DEFAULT_VOLUME;
}
}
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, };
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-dsp-src",
PW_KEY_MEDIA_CLASS, "Stream/Output/Audio",
NULL),
&filter_events,
&data);
/* 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);
/* Now connect this filter. We ask that our process function is
* called in a realtime thread. */
if (pw_filter_connect(data.filter,
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);
return 0;
}
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:1501
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:1685
#define PW_VERSION_FILTER_EVENTS
Definition: src/pipewire/filter.h:86
void pw_filter_destroy(struct pw_filter *filter)
Destroy a filter
Definition: filter.c:1370
void * pw_filter_get_dsp_buffer(void *port_data, uint32_t n_samples)
Get a data pointer to the buffer data.
Definition: filter.c:1901
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:1307
@ PW_FILTER_FLAG_RT_PROCESS
call process from the realtime thread
Definition: src/pipewire/filter.h:128
@ PW_FILTER_PORT_FLAG_MAP_BUFFERS
mmap the buffers except DmaBuf
Definition: src/pipewire/filter.h:137
#define PW_KEY_PORT_NAME
port name
Definition: src/pipewire/keys.h:292
#define PW_KEY_MEDIA_TYPE
Media.
Definition: src/pipewire/keys.h:441
#define PW_KEY_NODE_AUTOCONNECT
node wants to be automatically connected to a compatible node
Definition: src/pipewire/keys.h:219
#define PW_KEY_MEDIA_ROLE
Role: Movie, Music, Camera, Screen, Communication, Game, Notification, DSP, Production,...
Definition: src/pipewire/keys.h:447
#define PW_KEY_MEDIA_CATEGORY
Media Category: Playback, Capture, Duplex, Monitor, Manager.
Definition: src/pipewire/keys.h:444
#define PW_KEY_FORMAT_DSP
format related properties
Definition: src/pipewire/keys.h:482
#define PW_KEY_MEDIA_CLASS
class Ex: "Video/Source"
Definition: src/pipewire/keys.h:452
#define pw_log_trace(...)
Definition: src/pipewire/log.h:166
#define pw_loop_add_signal(l,...)
Definition: src/pipewire/loop.h:83
int pw_main_loop_quit(struct pw_main_loop *loop)
Quit a main loop.
Definition: main-loop.c:125
void pw_main_loop_destroy(struct pw_main_loop *loop)
Destroy a loop.
Definition: main-loop.c:90
int pw_main_loop_run(struct pw_main_loop *loop)
Run a main loop.
Definition: main-loop.c:139
struct pw_main_loop * pw_main_loop_new(const struct spa_dict *props)
Create a new main loop.
Definition: main-loop.c:80
struct pw_loop * pw_main_loop_get_loop(struct pw_main_loop *loop)
Get the loop implementation.
Definition: main-loop.c:113
void pw_init(int *argc, char **argv[])
Initialize PipeWire.
Definition: pipewire.c:578
void pw_deinit(void)
Deinitialize PipeWire.
Definition: pipewire.c:686
#define PW_DIRECTION_OUTPUT
Definition: port.h:67
struct pw_properties * pw_properties_new(const char *key,...) 1
Make a new properties object.
Definition: properties.c:102
pipewire/pipewire.h
pipewire/filter.h
Events for a filter.
Definition: src/pipewire/filter.h:84
A main loop object.
uint64_t duration
duration of current cycle
Definition: io.h:155
The position information adds extra meaning to the raw clock times.
Definition: io.h:293
struct spa_io_clock clock
clock position of driver, always valid and read only
Definition: io.h:294