From adb4277630aed4fdacd68f81326d7b0befed8a34 Mon Sep 17 00:00:00 2001 From: josh Date: Wed, 21 Aug 2024 22:04:27 -0400 Subject: [PATCH] Work In Progress pacat_advanced program --- CMakeLists.txt | 6 ++- pacat_advanced.cpp | 111 +++++++++++++++++++++++++++++++++++++++++++++ pacat_simple.cpp | 27 +++++++---- 3 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 pacat_advanced.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 81df204..bea0a8c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,8 +49,12 @@ endif() target_link_libraries(ReMixer-Test PUBLIC ReMixer) -add_executable(pacat pacat_simple.cpp) +add_executable(pacat pacat_simple.cpp + pacat_advanced.cpp) target_link_libraries(pacat pulse-simple pulse) add_executable(vorbis_decode vorbis_decode.cpp) target_link_libraries(vorbis_decode vorbisfile vorbis) + +add_executable(pacat_advanced pacat_advanced.cpp) +target_link_libraries(pacat_advanced pulse) diff --git a/pacat_advanced.cpp b/pacat_advanced.cpp new file mode 100644 index 0000000..8fb4601 --- /dev/null +++ b/pacat_advanced.cpp @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include +#include +#include + +// https://github.com/pulseaudio/pulseaudio/blob/master/src/pulse/simple.c + +static void context_state_callback(pa_context *c, void *userdata) +{ + switch(pa_context_get_state(c)) { + case PA_CONTEXT_READY: + case PA_CONTEXT_TERMINATED: + case PA_CONTEXT_FAILED: + case PA_CONTEXT_UNCONNECTED: + case PA_CONTEXT_CONNECTING: + case PA_CONTEXT_AUTHORIZING: + case PA_CONTEXT_SETTING_NAME: + default: + std::cerr << "The fuck?" << std::endl; + break; + } +} + + +int main(int argc, char* argv[]) { + if (argc == 1 || argv[1] == "-h" || argv[1] == "help") { + std::cout << "pacat - Tests playback of raw audio data via PulseAudio API." << std::endl; + std::cout << "usage: pacat (presumably output.raw)\n" << std::endl; + } + + static const pa_sample_spec ss = { + .format = PA_SAMPLE_S16LE, + .rate = 44100, + .channels = 2 + }; + + + int errno; + + pa_mainloop *maneloop; + pa_context *ctx; + pa_stream *stream; + const pa_channel_map* map = NULL; + const pa_buffer_attr* attr = NULL; + + const char* server = NULL; + const char* name = "This Dick"; + pa_stream_direction_t dir = PA_STREAM_PLAYBACK; + const char* dev = NULL; + const char* stream_name = "playback"; + + + if (!(maneloop = pa_mainloop_new())) + throw std::runtime_error("Failed to create a PulseAudio mainloop"); + + +#pragma region Context Creation + if (!(ctx = pa_context_new( + pa_mainloop_get_api(maneloop), + "Sound Channel?"))) + throw std::runtime_error("Failed to create a context"); + + pa_context_flags flags = pa_context_flags::PA_CONTEXT_NOAUTOSPAWN; + + if (pa_context_connect(ctx, server, flags, NULL) < 0) + { + errno = pa_context_errno(ctx); + throw std::runtime_error(std::format("Failed to connect a context: {}", errno)); + } + + pa_context_set_state_callback(ctx, context_state_callback, NULL); + + for (;;) + { + pa_context_state_t state; + state = pa_context_get_state(ctx); + + if (state == PA_CONTEXT_READY) + break; + + if (!PA_CONTEXT_IS_GOOD(state)) { + errno = pa_context_errno(ctx); + throw std::runtime_error(std::format("Context was not good! {}", errno)); + } + + // Wait until the context is ready + //pa_mainloop_wait(maneloop); + + } +#pragma endregion + + auto state = pa_context_get_state(ctx); + + if (state != PA_CONTEXT_READY) { + + } + + if (!(stream = pa_stream_new(ctx, "This Dick", &ss, map))) + { + errno = pa_context_errno(ctx); + throw std::runtime_error(std::format("Failed to create a stream. Error code {}", errno)); + } + + //pa_stream_set_state_callback(stream, stream_state) + + + return 0; +} \ No newline at end of file diff --git a/pacat_simple.cpp b/pacat_simple.cpp index 7389260..3ec6b8c 100644 --- a/pacat_simple.cpp +++ b/pacat_simple.cpp @@ -1,22 +1,32 @@ +/// ReMixer +/// A Public Domain C++ Audio Playback Library +/// By william @ RedactedSoftware. Thanks to Dawsh & Maxine. +/// (c) 2024 Redacted Software +/// This work is explicitly dedicated to the public domain, for the hopeful betterment of the software industry. + +/// @file pacat_simple.cpp +/// @desc A CLI program that plays back raw PCM data (given as a file) over the PulseAudio service. +/// @edit 2024-08-19 + #include -extern "C" -{ - -#include +#include #include -#include -#include +#include +#include #include #include #include +#include +#include #define BUFSIZE 1024 + int main(int argc, char* argv[]) { if (argc == 1 || argv[1] == "-h" || argv[1] == "help") { - printf("pacat - Tests playback of raw audio data via PulseAudio API.\n"); - printf("usage: pacat (presumably output.raw)\n"); + std::cout << "pacat - Tests playback of raw audio data via PulseAudio API." << std::endl; + std::cout << "usage: pacat (presumably output.raw)\n" << std::endl; } static const pa_sample_spec ss = { @@ -94,4 +104,3 @@ int main(int argc, char* argv[]) return ret; } -} \ No newline at end of file