i wanted to use this c example code for the PipeWire API to generate a simple tone. For different reasons I want to use C++ for my project, so my plan was to refactor the code to work with C++.
So first of all I followed the getting stated page and installed the pipewire dev headers apt install libpipewire-0.3-dev
.
Testing the C code
Then I created a project in c to test if the code is working as expected.
The CMakeLists file I use
cmake_minimum_required(VERSION 3.29)
project(pipewire_ctest C)
set(CMAKE_C_STANDARD 11)
find_package(PkgConfig REQUIRED)
pkg_check_modules(PIPEWIRE REQUIRED libpipewire-0.3)
include_directories(${PIPEWIRE_INCLUDE_DIRS})
add_executable(pipewire_ctest main.c)
target_link_libraries(pipewire_ctest ${PIPEWIRE_LIBRARIES} m)
The main.c file
My main.c file contains the code which was also given with the link above
Compiler
gcc --version
gcc (Ubuntu 13.2.0-23ubuntu4) 13.2.0
With the CLion IDE I built and executed the code, which works without errors and results in a played sound.
Trying with c++
Now I tried to refactor the code to c++.
The CMakeLists file I use for C++
cmake_minimum_required(VERSION 3.29)
project(pipewire_test)
set(CMAKE_CXX_STANDARD 23)
find_package(PkgConfig REQUIRED)
pkg_check_modules(PIPEWIRE REQUIRED libpipewire-0.3)
include_directories(${PIPEWIRE_INCLUDE_DIRS})
add_executable(pipewire_test main.cpp)
target_link_libraries(pipewire_test ${PIPEWIRE_LIBRARIES})
The main.cpp file
changes I made:
- mark the headers as extern C
- static_cast for void pointers to structs
- removed designated initializer clauses
- added a separated struct audio_info
#include <math.h>
extern "C"
{
#include <spa/param/audio/format-utils.h>
#include <pipewire/pipewire.h>
}
#define M_PI_M2 ( M_PI + M_PI )
#define DEFAULT_RATE 44100
#define DEFAULT_CHANNELS 2
#define DEFAULT_VOLUME 0.7
struct data {
pw_main_loop *loop;
pw_stream *stream;
double accumulator;
};
/* [on_process] */
static void on_process(void *userdata)
{
struct data *data = static_cast<struct data*>(userdata);
struct pw_buffer *b;
struct spa_buffer *buf;
int i, c, n_frames, stride;
int16_t *dst, val;
if ((b = pw_stream_dequeue_buffer(data->stream)) == NULL) {
pw_log_warn("out of buffers: %m");
return;
}
buf = b->buffer;
if ((dst = static_cast<int16_t*>(buf->datas[0].data)) == NULL)
return;
stride = sizeof(int16_t) * DEFAULT_CHANNELS;
n_frames = buf->datas[0].maxsize / stride;
if (b->requested)
n_frames = SPA_MIN(b->requested, n_frames);
for (i = 0; i < n_frames; i++) {
data->accumulator += M_PI_M2 * 440 / DEFAULT_RATE;
if (data->accumulator >= M_PI_M2)
data->accumulator -= M_PI_M2;
/* sin() gives a value between -1.0 and 1.0, we first apply
* the volume and then scale with 32767.0 to get a 16 bits value
* between [-32767 32767].
* Another common method to convert a double to
* 16 bits is to multiply by 32768.0 and then clamp to
* [-32768 32767] to get the full 16 bits range. */
val = sin(data->accumulator) * DEFAULT_VOLUME * 32767.0;
for (c = 0; c < DEFAULT_CHANNELS; c++)
*dst++ = val;
}
buf->datas[0].chunk->offset = 0;
buf->datas[0].chunk->stride = stride;
buf->datas[0].chunk->size = n_frames * stride;
pw_stream_queue_buffer(data->stream, b);
}
/* [on_process] */
static const struct pw_stream_events stream_events = {
PW_VERSION_STREAM_EVENTS,
on_process,
};
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);
data.loop = pw_main_loop_new(NULL);
data.stream = pw_stream_new_simple(
pw_main_loop_get_loop(data.loop),
"audio-src",
pw_properties_new(
PW_KEY_MEDIA_TYPE, "Audio",
PW_KEY_MEDIA_CATEGORY, "Playback",
PW_KEY_MEDIA_ROLE, "Music",
NULL),
&stream_events,
&data);
struct spa_audio_info_raw audio_info = SPA_AUDIO_INFO_RAW_INIT(
SPA_AUDIO_FORMAT_S16,
DEFAULT_CHANNELS,
DEFAULT_RATE
);
params[0] = spa_format_audio_raw_build(&b, SPA_PARAM_EnumFormat, &audio_info);
pw_stream_connect(data.stream,
PW_DIRECTION_OUTPUT,
PW_ID_ANY,
static_cast<pw_stream_flags>(PW_STREAM_FLAG_AUTOCONNECT |
PW_STREAM_FLAG_MAP_BUFFERS |
PW_STREAM_FLAG_RT_PROCESS),
params, 1);
pw_main_loop_run(data.loop);
pw_stream_destroy(data.stream);
pw_main_loop_destroy(data.loop);
return 0;
}
Also compiled and executed, there is no error, but no sound is played. However, using the qpwgraph software, I can see the audio-src
node that is successfully created. What am I doing wrong?
6