From bb4d527cbe78fcf2fa187bc6fcb3a5f3b4753c9d Mon Sep 17 00:00:00 2001 From: JohannesItten Date: Wed, 1 Jul 2026 13:26:22 +0300 Subject: [PATCH] feat: sine tone audio in testpattern node testpattern now optionally writes an audio flow when audio_flow_id is wired in the graph. Each channel generates a sine wave at (c+1)*1000 Hz (ch0=1kHz, ch1=2kHz, ...) at -18 dBFS broadcast reference level. Phase is derived from the absolute audio_index so there are no clicks at frame boundaries. One audio chunk per video frame (1920 samples at 48kHz/25fps), written via mxlFlowWriterOpenSamples with ring-buffer fragment and channel stride handling matching ndiin. build_graph: separate UUIDs for testpattern and ndiin flows; wire testpattern audio_flow_id (no sink yet). Co-Authored-By: Claude Sonnet 4.6 --- nodes/testpattern/main.cpp | 110 +++++++++++++++++++++++++++++-------- studio-manager/main.cpp | 14 +++-- 2 files changed, 96 insertions(+), 28 deletions(-) diff --git a/nodes/testpattern/main.cpp b/nodes/testpattern/main.cpp index 954620d..a8f43e7 100644 --- a/nodes/testpattern/main.cpp +++ b/nodes/testpattern/main.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -7,43 +9,77 @@ class TestPatternNode : public dmf::NodeBase { void run() override { + // --- video flow --- const auto flow_info = config().at("flow_id"); const auto flow_id = flow_info.at("id").get(); const int width = flow_info.value("width", 1920); const int height = flow_info.value("height", 1080); const int fps_num = flow_info.value("fps_num", 25); const int fps_den = flow_info.value("fps_den", 1); + const auto pattern = config().value("pattern", "bars"); - log("flow=%s %dx%d @ %d/%d fps", flow_id.c_str(), width, height, fps_num, fps_den); - - const std::string flow_def = - dmf::make_video_flow_def(flow_id, node_id(), width, height, fps_num, fps_den); + log("flow=%s %dx%d @ %d/%d fps pattern=%s", + flow_id.c_str(), width, height, fps_num, fps_den, pattern.c_str()); mxlFlowWriter writer{}; mxlFlowConfigInfo cfg_info{}; bool created = false; - mxlStatus st = mxlCreateFlowWriter( - instance(), flow_def.c_str(), nullptr, &writer, &cfg_info, &created); - if (st != MXL_STATUS_OK) { - log("mxlCreateFlowWriter failed (status=%d)", st); - return; + instance(), + dmf::make_video_flow_def(flow_id, node_id(), width, height, fps_num, fps_den).c_str(), + nullptr, &writer, &cfg_info, &created); + if (st != MXL_STATUS_OK) { log("video mxlCreateFlowWriter failed (status=%d)", st); return; } + + const uint32_t stride = cfg_info.discrete.sliceSizes[0]; + log("video stride=%u B/line grain=%u B ring=%u grains", + stride, stride * static_cast(height), cfg_info.discrete.grainCount); + + // --- audio flow (optional) --- + mxlFlowWriter audio_writer{}; + mxlFlowConfigInfo audio_cfg{}; + int sample_rate = 48000; + int channels = 2; + bool has_audio = config().contains("audio_flow_id"); + + if (has_audio) { + const auto audio_info = config().at("audio_flow_id"); + const auto audio_id = audio_info.at("id").get(); + sample_rate = audio_info.value("sample_rate", 48000); + channels = audio_info.value("channels", 2); + + log("audio flow=%s %d Hz %dch", audio_id.c_str(), sample_rate, channels); + + mxlStatus ast = mxlCreateFlowWriter( + instance(), + dmf::make_audio_flow_def(audio_id, node_id(), sample_rate, channels, 32, + fps_num, fps_den).c_str(), + nullptr, &audio_writer, &audio_cfg, &created); + if (ast != MXL_STATUS_OK) { + log("audio mxlCreateFlowWriter failed (status=%d) — continuing without audio", ast); + has_audio = false; + } else { + log("audio channels=%u buffer=%u samples", + audio_cfg.continuous.channelCount, audio_cfg.continuous.bufferLength); + } } - const uint32_t stride = cfg_info.discrete.sliceSizes[0]; - const auto pattern = config().value("pattern", "bars"); - log("stride=%u B/line grain=%u B ring=%u grains pattern=%s", - stride, stride * static_cast(height), cfg_info.discrete.grainCount, - pattern.c_str()); + // --- main loop --- + const mxlRational video_rate = {fps_num, fps_den}; + const mxlRational audio_rate = {sample_rate, 1}; + const size_t samples_per_frame = + static_cast(sample_rate) * static_cast(fps_den) / static_cast(fps_num); - const mxlRational rate = {fps_num, fps_den}; - uint64_t index = mxlGetCurrentIndex(&rate); - log("start index=%llu", index); + // -18 dBFS broadcast reference level + const float amplitude = static_cast(std::pow(10.0, -18.0 / 20.0)); + + uint64_t index = mxlGetCurrentIndex(&video_rate); + uint64_t audio_index = has_audio ? mxlGetCurrentIndex(&audio_rate) : 0; + log("start video_index=%llu", index); while (dmf::g_running.load(std::memory_order_relaxed)) { + // --- video grain --- mxlGrainInfo grain{}; uint8_t* buf = nullptr; - st = mxlFlowWriterOpenGrain(writer, index, &grain, &buf); if (st != MXL_STATUS_OK) { log("OpenGrain failed (status=%d), skipping index=%llu", st, index); @@ -56,17 +92,47 @@ class TestPatternNode : public dmf::NodeBase { else if (pattern == "black") dmf::v210::fill_black (buf, width, height, stride); else if (pattern == "white") dmf::v210::fill_white (buf, width, height, stride); else dmf::v210::fill_colorbars (buf, width, height, stride); - grain.flags = 0; - grain.validSlices = grain.totalSlices; // mark grain complete so readers can consume it + grain.flags = 0; + grain.validSlices = grain.totalSlices; mxlFlowWriterCommitGrain(writer, &grain); - const uint64_t ns = mxlGetNsUntilIndex(index + 1, &rate); + // --- audio: sine tones, channel c = (c+1) * 1000 Hz --- + if (has_audio) { + mxlMutableWrappedMultiBufferSlice slice{}; + if (mxlFlowWriterOpenSamples(audio_writer, audio_index, + samples_per_frame, &slice) == MXL_STATUS_OK) { + const size_t frag0 = slice.base.fragments[0].size / sizeof(float); + const size_t frag1 = slice.base.fragments[1].size / sizeof(float); + for (int c = 0; c < channels; ++c) { + const double freq = 1000.0 * (c + 1); + const double period = static_cast(sample_rate) / freq; + auto write_samples = [&](float* dst, size_t count, size_t offset) { + for (size_t s = 0; s < count; ++s) + dst[s] = amplitude * static_cast( + std::sin(2.0 * std::numbers::pi * (audio_index + offset + s) / period)); + }; + auto* dst0 = reinterpret_cast( + static_cast(slice.base.fragments[0].pointer) + c * slice.stride); + write_samples(dst0, frag0, 0); + if (frag1 > 0) { + auto* dst1 = reinterpret_cast( + static_cast(slice.base.fragments[1].pointer) + c * slice.stride); + write_samples(dst1, frag1, frag0); + } + } + mxlFlowWriterCommitSamples(audio_writer); + } + audio_index += samples_per_frame; + } + + const uint64_t ns = mxlGetNsUntilIndex(index + 1, &video_rate); if (ns > 0 && ns < 2'000'000'000ULL) mxlSleepForNs(ns); index++; } - log("stopped at index=%llu", index); + log("stopped at video_index=%llu", index); mxlReleaseFlowWriter(instance(), writer); + if (has_audio) mxlReleaseFlowWriter(instance(), audio_writer); } }; diff --git a/studio-manager/main.cpp b/studio-manager/main.cpp index 582ff64..5d37016 100644 --- a/studio-manager/main.cpp +++ b/studio-manager/main.cpp @@ -38,8 +38,10 @@ static std::string gen_uuid() { static dmf::FlowGraph build_graph() { dmf::FlowGraph g; - const std::string video_flow = gen_uuid(); - const std::string audio_flow = gen_uuid(); + const std::string tp_video_flow = gen_uuid(); + const std::string tp_audio_flow = gen_uuid(); + const std::string ndi_video_flow = gen_uuid(); + const std::string ndi_audio_flow = gen_uuid(); g.nodes = { { "testpattern", "testpattern", {{"pattern", "bars"}} }, { "ndiin", "ndiin", {} }, @@ -53,10 +55,10 @@ static dmf::FlowGraph build_graph() { {"kind","audio"}, {"sample_rate",48000}, {"channels",2}, {"bit_depth",32} }; g.edges = { - { video_flow, "ndiin", "video_flow_id", "fakesink", "flow_id", video_fmt }, - { video_flow, "ndiin", "video_flow_id", "ndiout", "flow_id", video_fmt }, - // audio_flow_id wired to ndiin only — no sink node yet, readers added later - { audio_flow, "ndiin", "audio_flow_id", "", "", audio_fmt }, + { tp_video_flow, "testpattern", "flow_id", "fakesink", "flow_id", video_fmt }, + { tp_audio_flow, "testpattern", "audio_flow_id", "", "", audio_fmt }, + { ndi_video_flow, "ndiin", "video_flow_id", "ndiout", "flow_id", video_fmt }, + { ndi_audio_flow, "ndiin", "audio_flow_id", "", "", audio_fmt }, }; return g; }