PipeWire 1.2.7
Loading...
Searching...
No Matches
midi-src.c

MIDI source using pw_filter.

MIDI source using pw_filter.


/* PipeWire */
/* SPDX-FileCopyrightText: Copyright © 2024 Pauli Virtanen */
/* SPDX-License-Identifier: MIT */
/*
[title]
MIDI source using \ref pw_filter "pw_filter".
[title]
*/
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include <signal.h>
#include <getopt.h>
#define PERIOD_NSEC (SPA_NSEC_PER_SEC/8)
struct port {
};
struct data {
struct pw_main_loop *loop;
struct pw_filter *filter;
struct port *port;
uint32_t clock_id;
int64_t offset;
uint64_t position;
};
static void on_process(void *userdata, struct spa_io_position *position)
{
struct data *data = userdata;
struct port *port = data->port;
struct pw_buffer *buf;
struct spa_data *d;
struct spa_pod_builder builder;
struct spa_pod_frame frame;
uint64_t sample_offset, sample_period, sample_position, cycle;
/*
* Use the clock sample position.
*
* If the playback switches to using a different clock, we reset
* playback as the sample position can then be discontinuous.
*/
if (data->clock_id != position->clock.id) {
pw_log_info("switch to clock %u", position->clock.id);
data->offset = position->clock.position - data->position;
data->clock_id = position->clock.id;
}
sample_position = position->clock.position - data->offset;
data->position = sample_position + position->clock.duration;
/*
* Produce note on/off every `PERIOD_NSEC` nanoseconds (rounded down to
* samples, for simplicity).
*
* We want to place the notes on the playback timeline, so we use sample
* positions (not real time!).
*/
sample_period = PERIOD_NSEC * position->clock.rate.denom
/ position->clock.rate.num / SPA_NSEC_PER_SEC;
cycle = sample_position / sample_period;
if (sample_position % sample_period != 0)
++cycle;
sample_offset = cycle*sample_period - sample_position;
if (sample_offset >= position->clock.duration)
return; /* don't need to produce anything yet */
/* Get output buffer */
if ((buf = pw_filter_dequeue_buffer(port)) == NULL)
return;
/* Midi buffers always have exactly one data block */
spa_assert(buf->buffer->n_datas == 1);
d = &buf->buffer->datas[0];
d->chunk->offset = 0;
d->chunk->size = 0;
d->chunk->stride = 1;
d->chunk->flags = 0;
/*
* MIDI buffers contain a SPA POD with a sequence of
* control messages and their raw MIDI data.
*/
spa_pod_builder_init(&builder, d->data, d->maxsize);
spa_pod_builder_push_sequence(&builder, &frame, 0);
while (sample_offset < position->clock.duration) {
if (cycle % 2 == 0) {
/* MIDI note on, channel 0, middle C, max velocity */
uint8_t buf[] = { 0x90, 0x3c, 0x7f };
/* The time position of the message in the graph cycle
* is given as offset from the cycle start, in
* samples. The cycle has duration of `clock.duration`
* samples, and the sample offset should satisfy
* 0 <= sample_offset < position->clock.duration.
*/
spa_pod_builder_control(&builder, sample_offset, SPA_CONTROL_Midi);
/* Raw MIDI data for the message */
spa_pod_builder_bytes(&builder, buf, sizeof(buf));
pw_log_info("note on at %"PRIu64, sample_position + sample_offset);
} else {
/* MIDI note off, channel 0, middle C, max velocity */
uint8_t buf[] = { 0x80, 0x3c, 0x7f };
spa_pod_builder_control(&builder, sample_offset, SPA_CONTROL_Midi);
spa_pod_builder_bytes(&builder, buf, sizeof(buf));
pw_log_info("note off at %"PRIu64, sample_position + sample_offset);
}
sample_offset += sample_period;
++cycle;
}
/*
* Finish the sequence and queue buffer to output.
*/
spa_pod_builder_pop(&builder, &frame);
d->chunk->size = builder.state.offset;
pw_log_trace("produced %u/%u bytes", d->chunk->size, d->maxsize);
}
static void state_changed(void *userdata, enum pw_filter_state old,
enum pw_filter_state state, const char *error)
{
struct data *data = userdata;
switch (state) {
/* reset playback position */
pw_log_info("start playback");
data->clock_id = SPA_ID_INVALID;
data->offset = 0;
data->position = 0;
break;
default:
break;
}
}
static const struct pw_filter_events filter_events = {
.process = on_process,
.state_changed = state_changed,
};
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 = {};
uint8_t buffer[1024];
struct spa_pod_builder builder;
struct spa_pod *params[1];
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(
"midi-src",
PW_KEY_MEDIA_CATEGORY, "Playback",
PW_KEY_MEDIA_CLASS, "Midi/Source",
NULL),
&filter_events,
&data);
/* Make a midi output port */
data.port = pw_filter_add_port(data.filter,
sizeof(struct port),
PW_KEY_FORMAT_DSP, "8 bit raw midi",
PW_KEY_PORT_NAME, "output",
NULL),
NULL, 0);
/* Update SPA_PARAM_Buffers to request a specific sizes and counts.
* This is not mandatory: if you skip this, you'll get default sized
* buffers, usually 4k or 32k bytes or so.
*
* We'll here ask for 4096 bytes as that's enough.
*/
spa_pod_builder_init(&builder, buffer, sizeof(buffer));
params[0] = spa_pod_builder_add_object(&builder,
/* POD Object for the buffer parameter */
/* Default 1 buffer, minimum of 1, max of 32 buffers.
* We can do with 1 buffer as we dequeue and queue in the same
* cycle.
*/
/* MIDI buffers always have 1 data block */
/* Buffer size: request default 4096 bytes, min 4096, no maximum */
/* MIDI buffers have stride 1 */
pw_filter_update_params(data.filter, data.port,
(const struct spa_pod **)params, SPA_N_ELEMENTS(params));
/* 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;
}
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:1548
int pw_filter_queue_buffer(void *port_data, struct pw_buffer *buffer)
Submit a buffer for playback or recycle a buffer for capture.
Definition filter.c:1994
pw_filter_state
The state of a filter
Definition filter.h:42
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:1780
#define PW_VERSION_FILTER_EVENTS
Definition filter.h:66
struct pw_buffer * pw_filter_dequeue_buffer(void *port_data)
Get a buffer that can be filled for output ports or consumed for input ports.
Definition filter.c:1976
int pw_filter_update_params(struct pw_filter *filter, void *port_data, const struct spa_pod **params, uint32_t n_params)
Update params, use NULL port_data for global filter params.
Definition filter.c:1892
void pw_filter_destroy(struct pw_filter *filter)
Destroy a filter
Definition filter.c:1385
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:1281
@ 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
@ PW_FILTER_STATE_STREAMING
filter is streaming
Definition filter.h:47
#define PW_KEY_PORT_NAME
port name
Definition keys.h:335
#define PW_KEY_MEDIA_TYPE
Media.
Definition keys.h:497
#define PW_KEY_MEDIA_CATEGORY
Media Category: Playback, Capture, Duplex, Monitor, Manager.
Definition keys.h:500
#define PW_KEY_FORMAT_DSP
format related properties
Definition keys.h:540
#define PW_KEY_MEDIA_CLASS
class Ex: "Video/Source"
Definition keys.h:508
#define pw_log_trace(...)
Definition log.h:182
#define pw_log_info(...)
Definition log.h:180
#define pw_loop_add_signal(l,...)
Definition loop.h:68
int pw_main_loop_quit(struct pw_main_loop *loop)
Quit a main loop.
Definition main-loop.c:108
void pw_main_loop_destroy(struct pw_main_loop *loop)
Destroy a loop.
Definition main-loop.c:73
int pw_main_loop_run(struct pw_main_loop *loop)
Run a main loop.
Definition main-loop.c:122
struct pw_main_loop * pw_main_loop_new(const struct spa_dict *props)
Create a new main loop.
Definition main-loop.c:63
struct pw_loop * pw_main_loop_get_loop(struct pw_main_loop *loop)
Get the loop implementation.
Definition main-loop.c:96
void pw_init(int *argc, char **argv[])
Initialize PipeWire.
Definition pipewire.c:488
void pw_deinit(void)
Deinitialize PipeWire.
Definition pipewire.c:600
#define PW_DIRECTION_OUTPUT
Definition port.h:50
struct pw_properties * pw_properties_new(const char *key,...)
Make a new properties object.
Definition properties.c:96
@ SPA_CONTROL_Midi
data contains a spa_pod_bytes with raw midi data
Definition control.h:33
@ SPA_PARAM_Buffers
buffer configurations as SPA_TYPE_OBJECT_ParamBuffers
Definition param.h:35
@ SPA_PARAM_BUFFERS_size
size of a data block memory (Int)
Definition buffers.h:29
@ SPA_PARAM_BUFFERS_stride
stride of data block memory (Int)
Definition buffers.h:30
@ SPA_PARAM_BUFFERS_blocks
number of data blocks per buffer (Int)
Definition buffers.h:28
@ SPA_PARAM_BUFFERS_buffers
number of buffers (Int)
Definition buffers.h:27
#define SPA_POD_CHOICE_RANGE_Int(def, min, max)
Definition vararg.h:58
static int spa_pod_builder_push_sequence(struct spa_pod_builder *builder, struct spa_pod_frame *frame, uint32_t unit)
Definition builder.h:460
static void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:168
static int spa_pod_builder_control(struct spa_pod_builder *builder, uint32_t offset, uint32_t type)
Definition builder.h:471
#define spa_pod_builder_add_object(b, type, id,...)
Definition builder.h:659
static int spa_pod_builder_bytes(struct spa_pod_builder *builder, const void *bytes, uint32_t len)
Definition builder.h:315
static void spa_pod_builder_init(struct spa_pod_builder *builder, void *data, uint32_t size)
Definition builder.h:87
#define SPA_POD_Int(val)
Definition vararg.h:54
@ SPA_TYPE_OBJECT_ParamBuffers
Definition type.h:77
#define SPA_ID_INVALID
Definition defs.h:250
#define SPA_N_ELEMENTS(arr)
Definition defs.h:143
#define SPA_NSEC_PER_SEC
Definition defs.h:253
#define spa_assert(expr)
Definition defs.h:468
pipewire/pipewire.h
spa/control/control.h
pipewire/filter.h
a buffer structure obtained from pw_stream_dequeue_buffer().
Definition stream.h:212
struct spa_buffer * buffer
the spa buffer
Definition stream.h:213
Events for a filter.
Definition filter.h:64
void(* state_changed)(void *data, enum pw_filter_state old, enum pw_filter_state state, const char *error)
when the filter state changes
Definition filter.h:71
A main loop object.
struct spa_data * datas
array of data members
Definition buffer.h:104
uint32_t n_datas
number of data members
Definition buffer.h:102
int32_t stride
stride of valid data
Definition buffer.h:55
uint32_t size
size of valid data.
Definition buffer.h:53
int32_t flags
chunk flags
Definition buffer.h:64
uint32_t offset
offset of valid data.
Definition buffer.h:50
Data for a buffer this stays constant for a buffer.
Definition buffer.h:68
struct spa_chunk * chunk
valid chunk of memory
Definition buffer.h:96
void * data
optional data pointer
Definition buffer.h:95
uint32_t maxsize
max size of data
Definition buffer.h:94
uint32_t num
Definition defs.h:138
uint32_t denom
Definition defs.h:139
uint64_t position
Current position, in samples @ rate.
Definition io.h:155
struct spa_fraction rate
Rate for position/duration/delay/xrun.
Definition io.h:154
uint32_t id
Unique clock id, set by host application.
Definition io.h:147
uint64_t duration
Duration of current cycle, in samples @ rate.
Definition io.h:156
The position information adds extra meaning to the raw clock times.
Definition io.h:314
struct spa_io_clock clock
clock position of driver, always valid and read only
Definition io.h:315
Definition builder.h:53
Definition iter.h:27
Definition pod.h:43