PipeWire: midi-src.c

PipeWire 1.4.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>
#include <pipewire/pipewire.h>
#include <pipewire/filter.h>
#include <spa/pod/builder.h>
#include <spa/control/control.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 */
uint32_t event = 0x20903c7f;
/* 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_UMP);
/* Raw MIDI data for the message */
spa_pod_builder_bytes(&builder, &event, sizeof(event));
pw_log_info("note on at %"PRIu64, sample_position + sample_offset);
} else {
/* MIDI note off, channel 0, middle C, max velocity */
uint32_t event = 0x20803c7f;
spa_pod_builder_control(&builder, sample_offset, SPA_CONTROL_UMP);
spa_pod_builder_bytes(&builder, &event, sizeof(event));
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);
pw_filter_queue_buffer(port, buf);
}
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) {
case PW_FILTER_STATE_STREAMING:
/* 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 = {
PW_VERSION_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(
pw_main_loop_get_loop(data.loop),
"midi-src",
pw_properties_new(
PW_KEY_MEDIA_TYPE, "Midi",
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,
PW_DIRECTION_OUTPUT,
PW_FILTER_PORT_FLAG_MAP_BUFFERS,
sizeof(struct port),
pw_properties_new(
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 */
SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers,
/* 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.
*/
SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(1, 1, 32),
/* MIDI buffers always have 1 data block */
SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(1),
/* Buffer size: request default 4096 bytes, min 4096, no maximum */
SPA_PARAM_BUFFERS_size, SPA_POD_CHOICE_RANGE_Int(4096, 4096, INT32_MAX),
/* MIDI buffers have stride 1 */
SPA_PARAM_BUFFERS_stride, SPA_POD_Int(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,
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:1562
pw_filter_queue_buffer
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:2022
pw_filter_state
pw_filter_state
The state of a filter.
Definition filter.h:43
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:1803
PW_VERSION_FILTER_EVENTS
#define PW_VERSION_FILTER_EVENTS
Definition filter.h:67
pw_filter_dequeue_buffer
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:2004
pw_filter_update_params
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:1920
pw_filter_destroy
void pw_filter_destroy(struct pw_filter *filter)
Destroy a filter.
Definition filter.c:1397
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:1293
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_FILTER_STATE_STREAMING
@ PW_FILTER_STATE_STREAMING
filter is streaming
Definition filter.h:48
PW_KEY_PORT_NAME
#define PW_KEY_PORT_NAME
port name
Definition keys.h:337
PW_KEY_MEDIA_TYPE
#define PW_KEY_MEDIA_TYPE
Media.
Definition keys.h:499
PW_KEY_MEDIA_CATEGORY
#define PW_KEY_MEDIA_CATEGORY
Media Category: Playback, Capture, Duplex, Monitor, Manager.
Definition keys.h:502
PW_KEY_FORMAT_DSP
#define PW_KEY_FORMAT_DSP
format related properties
Definition keys.h:542
PW_KEY_MEDIA_CLASS
#define PW_KEY_MEDIA_CLASS
class Ex: "Video/Source"
Definition keys.h:510
pw_log_trace
#define pw_log_trace(...)
Definition log.h:182
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:141
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:488
pw_deinit
void pw_deinit(void)
Deinitialize PipeWire.
Definition pipewire.c:603
PW_DIRECTION_OUTPUT
#define PW_DIRECTION_OUTPUT
Definition port.h:55
pw_properties_new
struct pw_properties * pw_properties_new(const char *key,...)
Make a new properties object.
Definition properties.c:97
SPA_CONTROL_UMP
@ SPA_CONTROL_UMP
spa_pod_bytes with raw UMP (universal MIDI packet) data.
Definition control.h:35
SPA_PARAM_Buffers
@ SPA_PARAM_Buffers
buffer configurations as SPA_TYPE_OBJECT_ParamBuffers
Definition param.h:35
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_builder_pop
SPA_API_POD_BUILDER void * spa_pod_builder_pop(struct spa_pod_builder *builder, struct spa_pod_frame *frame)
Definition builder.h:187
SPA_POD_CHOICE_RANGE_Int
#define SPA_POD_CHOICE_RANGE_Int(def, min, max)
Definition vararg.h:58
spa_pod_builder_control
SPA_API_POD_BUILDER int spa_pod_builder_control(struct spa_pod_builder *builder, uint32_t offset, uint32_t type)
Definition builder.h:515
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:97
spa_pod_builder_bytes
SPA_API_POD_BUILDER int spa_pod_builder_bytes(struct spa_pod_builder *builder, const void *bytes, uint32_t len)
Definition builder.h:334
spa_pod_builder_add_object
#define spa_pod_builder_add_object(b, type, id,...)
Definition builder.h:703
spa_pod_builder_push_sequence
SPA_API_POD_BUILDER int spa_pod_builder_push_sequence(struct spa_pod_builder *builder, struct spa_pod_frame *frame, uint32_t unit)
Definition builder.h:504
SPA_POD_Int
#define SPA_POD_Int(val)
Definition vararg.h:54
SPA_TYPE_OBJECT_ParamBuffers
@ SPA_TYPE_OBJECT_ParamBuffers
Definition type.h:88
SPA_ID_INVALID
#define SPA_ID_INVALID
Definition defs.h:250
SPA_N_ELEMENTS
#define SPA_N_ELEMENTS(arr)
Definition defs.h:143
SPA_NSEC_PER_SEC
#define SPA_NSEC_PER_SEC
Definition defs.h:253
spa_assert
#define spa_assert(expr)
Definition defs.h:493
pipewire.h
pipewire/pipewire.h
control.h
spa/control/control.h
filter.h
pipewire/filter.h
pw_buffer
a buffer structure obtained from pw_stream_dequeue_buffer().
Definition stream.h:261
pw_buffer::buffer
struct spa_buffer * buffer
the spa buffer
Definition stream.h:262
pw_filter_events
Events for a filter.
Definition filter.h:65
pw_filter_events::state_changed
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:73
pw_filter
pw_main_loop
A main loop object.
spa_buffer::datas
struct spa_data * datas
array of data members
Definition buffer.h:114
spa_buffer::n_datas
uint32_t n_datas
number of data members
Definition buffer.h:112
spa_chunk::stride
int32_t stride
stride of valid data
Definition buffer.h:65
spa_chunk::size
uint32_t size
size of valid data.
Definition buffer.h:63
spa_chunk::flags
int32_t flags
chunk flags
Definition buffer.h:74
spa_chunk::offset
uint32_t offset
offset of valid data.
Definition buffer.h:60
spa_data
Data for a buffer this stays constant for a buffer.
Definition buffer.h:78
spa_data::chunk
struct spa_chunk * chunk
valid chunk of memory
Definition buffer.h:106
spa_data::data
void * data
optional data pointer
Definition buffer.h:105
spa_data::maxsize
uint32_t maxsize
max size of data
Definition buffer.h:104
spa_fraction::num
uint32_t num
Definition defs.h:138
spa_fraction::denom
uint32_t denom
Definition defs.h:139
spa_io_clock::position
uint64_t position
Current position, in samples @ rate.
Definition io.h:157
spa_io_clock::rate
struct spa_fraction rate
Rate for position/duration/delay/xrun.
Definition io.h:156
spa_io_clock::id
uint32_t id
Unique clock id, set by host application.
Definition io.h:149
spa_io_clock::duration
uint64_t duration
Duration of current cycle, in samples @ rate.
Definition io.h:158
spa_io_position
The position information adds extra meaning to the raw clock times.
Definition io.h:316
spa_io_position::clock
struct spa_io_clock clock
clock position of driver, always valid and read only
Definition io.h:317
spa_pod_builder
Definition builder.h:63
spa_pod_frame
Definition iter.h:37
spa_pod
Definition pod.h:43