Scaffold OBS audio stem source
This commit is contained in:
parent
0c39921f96
commit
71234f04ce
12 changed files with 522 additions and 9 deletions
|
|
@ -157,9 +157,11 @@ a named invariant that the native runtime cannot protect yet.
|
|||
aligned-source stem names with the actuator program.
|
||||
`MimirObsStemPublicationState` consumes the Fensalir stem bus without copying
|
||||
audio and validates ready/missing/unconfigured lanes against the
|
||||
alignment-actuator OBS stem profile. Actual OBS device/plugin routing remains
|
||||
the next output cut before those stems become separately mixable program
|
||||
outputs.
|
||||
alignment-actuator OBS stem profile. `MimirObsStemSharedMemoryPublisher` now
|
||||
publishes those validated stems into a fixed Windows shared-memory ABI, and
|
||||
`native/obs_stem_source` is the first OBS source plugin that reads one named
|
||||
stem and submits it to OBS as an audio source. Local plugin compilation still
|
||||
needs an OBS/libobs SDK installation.
|
||||
- `MimirRuntime` no longer submits the legacy direct `AquariumSplineFrame`
|
||||
spectrum dashboard. Live spectrum visualization authority is the
|
||||
`AquariumFieldEvidenceFrame`: Mimir declares the rolling resource, emits a
|
||||
|
|
|
|||
45
docs/obs-stem-plugin.md
Normal file
45
docs/obs-stem-plugin.md
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# OBS Stem Plugin
|
||||
|
||||
The production audio path is a Mimir/Fensalir-owned stem bus plus a narrow OBS
|
||||
source plugin.
|
||||
|
||||
## Authority
|
||||
|
||||
- Mimir/Fensalir own timing, alignment, DSP, and stem identity.
|
||||
- OBS owns broadcast mixing and scene composition.
|
||||
- The plugin reads already-processed stem samples. It does not synchronize raw
|
||||
sources, estimate clocks, or decide which stem is true.
|
||||
|
||||
## Current Contract
|
||||
|
||||
`MimirObsStemSharedMemoryPublisher` writes the latest validated
|
||||
`MimirObsStemPublicationSnapshot` into a Windows shared-memory map named
|
||||
`Local\MimirObsStemBus` by default. The native OBS plugin in
|
||||
`native/obs_stem_source` exposes one audio input source. Add one source per stem
|
||||
and set:
|
||||
|
||||
```text
|
||||
Shared memory map: Local\MimirObsStemBus
|
||||
Stem id: aligned_source_0
|
||||
```
|
||||
|
||||
Use `aligned_source_0` through `aligned_source_5` for the first actuator-stage
|
||||
source lanes.
|
||||
|
||||
## Build
|
||||
|
||||
The plugin requires an OBS/libobs development package that provides
|
||||
`libobsConfig.cmake`.
|
||||
|
||||
```powershell
|
||||
cmake -S .\native\obs_stem_source -B .\native\obs_stem_source\build -Dlibobs_DIR=<path-to-libobs-cmake>
|
||||
cmake --build .\native\obs_stem_source\build --config Release
|
||||
```
|
||||
|
||||
This workstation currently does not have the libobs SDK installed, so CMake
|
||||
configuration stops at `find_package(libobs)`. The managed shared-memory
|
||||
publisher is still verified by:
|
||||
|
||||
```powershell
|
||||
dotnet run --no-build --project .\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --obs-stem-shared-memory-smoke
|
||||
```
|
||||
15
native/obs_stem_source/CMakeLists.txt
Normal file
15
native/obs_stem_source/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
project(mimir_obs_stem_source LANGUAGES CXX)
|
||||
|
||||
find_package(libobs REQUIRED CONFIG)
|
||||
|
||||
add_library(mimir_obs_stem_source MODULE
|
||||
mimir_obs_stem_source.cpp)
|
||||
|
||||
target_compile_features(mimir_obs_stem_source PRIVATE cxx_std_20)
|
||||
target_compile_definitions(mimir_obs_stem_source PRIVATE
|
||||
WIN32_LEAN_AND_MEAN
|
||||
NOMINMAX
|
||||
_WIN32_WINNT=0x0A00)
|
||||
target_link_libraries(mimir_obs_stem_source PRIVATE OBS::libobs)
|
||||
43
native/obs_stem_source/mimir_obs_stem_ipc.h
Normal file
43
native/obs_stem_source/mimir_obs_stem_ipc.h
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
constexpr uint64_t MIMIR_OBS_STEM_MAGIC = 0x4D545352494D494Dull;
|
||||
constexpr int MIMIR_OBS_STEM_VERSION = 1;
|
||||
constexpr int MIMIR_OBS_STEM_MAX_STEMS = 8;
|
||||
constexpr int MIMIR_OBS_STEM_MAX_FRAMES = 4096;
|
||||
constexpr int MIMIR_OBS_STEM_ID_BYTES = 64;
|
||||
constexpr int MIMIR_OBS_STEM_DISPLAY_BYTES = 64;
|
||||
constexpr int MIMIR_OBS_STEM_SOURCE_BYTES = 64;
|
||||
constexpr int MIMIR_OBS_STEM_HEADER_BYTES = 64;
|
||||
constexpr int MIMIR_OBS_STEM_RECORD_BYTES = 224;
|
||||
constexpr int MIMIR_OBS_STEM_MAP_BYTES =
|
||||
MIMIR_OBS_STEM_HEADER_BYTES +
|
||||
(MIMIR_OBS_STEM_MAX_STEMS * MIMIR_OBS_STEM_RECORD_BYTES) +
|
||||
(MIMIR_OBS_STEM_MAX_STEMS * MIMIR_OBS_STEM_MAX_FRAMES * static_cast<int>(sizeof(float)));
|
||||
|
||||
struct MimirObsStemHeader {
|
||||
uint64_t magic;
|
||||
int32_t version;
|
||||
uint32_t generation;
|
||||
int32_t stem_count;
|
||||
int32_t max_stems;
|
||||
int32_t max_frames;
|
||||
int32_t header_bytes;
|
||||
int32_t record_bytes;
|
||||
int32_t map_bytes;
|
||||
};
|
||||
|
||||
struct MimirObsStemRecord {
|
||||
int32_t channel_index;
|
||||
int32_t configured;
|
||||
int32_t sample_rate;
|
||||
int32_t frame_count;
|
||||
int32_t sample_offset;
|
||||
int32_t reserved0;
|
||||
int32_t reserved1;
|
||||
int32_t reserved2;
|
||||
char stem_id[MIMIR_OBS_STEM_ID_BYTES];
|
||||
char display_name[MIMIR_OBS_STEM_DISPLAY_BYTES];
|
||||
char source_id[MIMIR_OBS_STEM_SOURCE_BYTES];
|
||||
};
|
||||
212
native/obs_stem_source/mimir_obs_stem_source.cpp
Normal file
212
native/obs_stem_source/mimir_obs_stem_source.cpp
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
#include <obs-module.h>
|
||||
#include <util/platform.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
#include "mimir_obs_stem_ipc.h"
|
||||
|
||||
OBS_DECLARE_MODULE()
|
||||
OBS_MODULE_USE_DEFAULT_LOCALE("mimir-obs-stem-source", "en-US")
|
||||
|
||||
struct MimirStemSource {
|
||||
obs_source_t *source = nullptr;
|
||||
std::string map_name = "Local\\MimirObsStemBus";
|
||||
std::string stem_id = "aligned_source_0";
|
||||
HANDLE mapping = nullptr;
|
||||
const uint8_t *view = nullptr;
|
||||
std::thread worker;
|
||||
std::atomic_bool running = false;
|
||||
uint32_t last_generation = 0;
|
||||
};
|
||||
|
||||
static std::string obs_string(obs_data_t *settings, const char *key, const char *fallback)
|
||||
{
|
||||
const char *value = obs_data_get_string(settings, key);
|
||||
return value && *value ? std::string(value) : std::string(fallback);
|
||||
}
|
||||
|
||||
static void close_mapping(MimirStemSource *ctx)
|
||||
{
|
||||
if (ctx->view) {
|
||||
UnmapViewOfFile(ctx->view);
|
||||
ctx->view = nullptr;
|
||||
}
|
||||
|
||||
if (ctx->mapping) {
|
||||
CloseHandle(ctx->mapping);
|
||||
ctx->mapping = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
static bool open_mapping(MimirStemSource *ctx)
|
||||
{
|
||||
if (ctx->mapping) {
|
||||
return true;
|
||||
}
|
||||
|
||||
ctx->mapping = OpenFileMappingA(FILE_MAP_READ, FALSE, ctx->map_name.c_str());
|
||||
if (!ctx->mapping) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx->view = static_cast<const uint8_t *>(MapViewOfFile(ctx->mapping, FILE_MAP_READ, 0, 0, MIMIR_OBS_STEM_MAP_BYTES));
|
||||
if (!ctx->view) {
|
||||
close_mapping(ctx);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static const MimirObsStemRecord *find_record(const uint8_t *view, const std::string &stem_id, int stem_count)
|
||||
{
|
||||
const auto count = std::min(stem_count, MIMIR_OBS_STEM_MAX_STEMS);
|
||||
for (int index = 0; index < count; ++index) {
|
||||
const auto *record = reinterpret_cast<const MimirObsStemRecord *>(view + MIMIR_OBS_STEM_HEADER_BYTES + (index * MIMIR_OBS_STEM_RECORD_BYTES));
|
||||
if (record->frame_count <= 0 || record->sample_rate <= 0 || record->sample_offset <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (stem_id == record->stem_id) {
|
||||
return record;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void publish_if_available(MimirStemSource *ctx)
|
||||
{
|
||||
if (!open_mapping(ctx)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto *header = reinterpret_cast<const MimirObsStemHeader *>(ctx->view);
|
||||
const auto generation_a = header->generation;
|
||||
if ((generation_a & 1u) != 0 || generation_a == ctx->last_generation) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (header->magic != MIMIR_OBS_STEM_MAGIC ||
|
||||
header->version != MIMIR_OBS_STEM_VERSION ||
|
||||
header->map_bytes != MIMIR_OBS_STEM_MAP_BYTES) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto *record = find_record(ctx->view, ctx->stem_id, header->stem_count);
|
||||
if (!record) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto frame_count = std::min(record->frame_count, MIMIR_OBS_STEM_MAX_FRAMES);
|
||||
const auto sample_rate = record->sample_rate;
|
||||
const auto byte_count = static_cast<size_t>(frame_count) * sizeof(float);
|
||||
if (record->sample_offset < 0 || record->sample_offset + static_cast<int>(byte_count) > MIMIR_OBS_STEM_MAP_BYTES) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<float> samples(frame_count);
|
||||
std::memcpy(samples.data(), ctx->view + record->sample_offset, byte_count);
|
||||
|
||||
const auto generation_b = header->generation;
|
||||
if (generation_a != generation_b || (generation_b & 1u) != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
ctx->last_generation = generation_b;
|
||||
uint8_t *planes[MAX_AV_PLANES] = {};
|
||||
planes[0] = reinterpret_cast<uint8_t *>(samples.data());
|
||||
obs_source_audio audio = {};
|
||||
audio.data[0] = planes[0];
|
||||
audio.frames = static_cast<uint32_t>(frame_count);
|
||||
audio.speakers = SPEAKERS_MONO;
|
||||
audio.samples_per_sec = static_cast<uint32_t>(sample_rate);
|
||||
audio.format = AUDIO_FORMAT_FLOAT_PLANAR;
|
||||
audio.timestamp = os_gettime_ns();
|
||||
obs_source_output_audio(ctx->source, &audio);
|
||||
}
|
||||
|
||||
static void worker_loop(MimirStemSource *ctx)
|
||||
{
|
||||
while (ctx->running.load()) {
|
||||
publish_if_available(ctx);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
}
|
||||
}
|
||||
|
||||
static const char *mimir_stem_get_name(void *)
|
||||
{
|
||||
return "Mimir Audio Stem";
|
||||
}
|
||||
|
||||
static void mimir_stem_update(void *data, obs_data_t *settings)
|
||||
{
|
||||
auto *ctx = static_cast<MimirStemSource *>(data);
|
||||
ctx->map_name = obs_string(settings, "map_name", "Local\\MimirObsStemBus");
|
||||
ctx->stem_id = obs_string(settings, "stem_id", "aligned_source_0");
|
||||
ctx->last_generation = 0;
|
||||
close_mapping(ctx);
|
||||
}
|
||||
|
||||
static void *mimir_stem_create(obs_data_t *settings, obs_source_t *source)
|
||||
{
|
||||
auto *ctx = new MimirStemSource();
|
||||
ctx->source = source;
|
||||
mimir_stem_update(ctx, settings);
|
||||
ctx->running = true;
|
||||
ctx->worker = std::thread(worker_loop, ctx);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void mimir_stem_destroy(void *data)
|
||||
{
|
||||
auto *ctx = static_cast<MimirStemSource *>(data);
|
||||
ctx->running = false;
|
||||
if (ctx->worker.joinable()) {
|
||||
ctx->worker.join();
|
||||
}
|
||||
|
||||
close_mapping(ctx);
|
||||
delete ctx;
|
||||
}
|
||||
|
||||
static void mimir_stem_defaults(obs_data_t *settings)
|
||||
{
|
||||
obs_data_set_default_string(settings, "map_name", "Local\\MimirObsStemBus");
|
||||
obs_data_set_default_string(settings, "stem_id", "aligned_source_0");
|
||||
}
|
||||
|
||||
static obs_properties_t *mimir_stem_properties(void *)
|
||||
{
|
||||
obs_properties_t *props = obs_properties_create();
|
||||
obs_properties_add_text(props, "map_name", "Shared memory map", OBS_TEXT_DEFAULT);
|
||||
obs_properties_add_text(props, "stem_id", "Stem id", OBS_TEXT_DEFAULT);
|
||||
return props;
|
||||
}
|
||||
|
||||
static obs_source_info mimir_stem_source_info = {
|
||||
.id = "mimir_audio_stem_source",
|
||||
.type = OBS_SOURCE_TYPE_INPUT,
|
||||
.output_flags = OBS_SOURCE_AUDIO,
|
||||
.get_name = mimir_stem_get_name,
|
||||
.create = mimir_stem_create,
|
||||
.destroy = mimir_stem_destroy,
|
||||
.get_defaults = mimir_stem_defaults,
|
||||
.get_properties = mimir_stem_properties,
|
||||
.update = mimir_stem_update,
|
||||
};
|
||||
|
||||
bool obs_module_load(void)
|
||||
{
|
||||
obs_register_source(&mimir_stem_source_info);
|
||||
blog(LOG_INFO, "Mimir OBS audio stem source loaded");
|
||||
return true;
|
||||
}
|
||||
|
|
@ -318,8 +318,11 @@ actuator program declaring deterministic aligned-source stem names. OBS-facing
|
|||
publication now has a first readiness surface:
|
||||
`MimirObsStemPublicationState` consumes those frames without copying audio and
|
||||
reports ready, missing, and unconfigured stems against the alignment actuator
|
||||
stem-bus profile. Actual device/plugin routing for OBS still has to consume the
|
||||
same validated stem stream as separately mixable program audio.
|
||||
stem-bus profile. `MimirObsStemSharedMemoryPublisher` writes the validated stem
|
||||
frames into `Local\MimirObsStemBus`, and `native/obs_stem_source` is the first
|
||||
OBS source plugin: one OBS source reads one named stem from that map and submits
|
||||
it through libobs audio. Building it locally still requires an OBS/libobs SDK
|
||||
that provides `libobsConfig.cmake`.
|
||||
|
||||
## Visual Fusion
|
||||
|
||||
|
|
|
|||
|
|
@ -79,6 +79,11 @@ if (args.Any(arg => string.Equals(arg, "--audio-stem-publication-smoke", StringC
|
|||
return RunAudioStemPublicationSmoke();
|
||||
}
|
||||
|
||||
if (args.Any(arg => string.Equals(arg, "--obs-stem-shared-memory-smoke", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return RunObsStemSharedMemorySmoke();
|
||||
}
|
||||
|
||||
if (args.Any(arg => string.Equals(arg, "--complex-contour-tracker-self-test", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return RunComplexContourTrackerSelfTest(
|
||||
|
|
@ -1917,6 +1922,49 @@ static int RunAudioStemPublicationSmoke()
|
|||
: 1;
|
||||
}
|
||||
|
||||
static int RunObsStemSharedMemorySmoke()
|
||||
{
|
||||
if (!OperatingSystem.IsWindows())
|
||||
{
|
||||
Console.WriteLine("obs-stem-shared-memory-smoke skipped=non-windows");
|
||||
return 0;
|
||||
}
|
||||
|
||||
var mapName = $"Local\\MimirObsStemBusSmoke{Environment.ProcessId}";
|
||||
var publication = new MimirObsStemPublicationState(MimirObsPublicationConfigurations.AlignmentActuatorStemBus);
|
||||
publication.Consume(new AquariumAudioStemFrame(
|
||||
MimirAlignmentActuatorProfile.SixSourceFaust.Id,
|
||||
[new AquariumAudioStemChannel(0, "aligned_source_0", "Aligned source 0", "mic-0", [0.125f, -0.25f, 0.5f])],
|
||||
FrameCount: 3,
|
||||
SampleRate: 48_000,
|
||||
Sequence: 11));
|
||||
|
||||
using var publisher = new MimirObsStemSharedMemoryPublisher(mapName);
|
||||
publisher.Publish(publication.Capture());
|
||||
using var file = System.IO.MemoryMappedFiles.MemoryMappedFile.OpenExisting(mapName, System.IO.MemoryMappedFiles.MemoryMappedFileRights.Read);
|
||||
using var view = file.CreateViewAccessor(0, MimirObsStemSharedMemoryPublisher.MapBytes, System.IO.MemoryMappedFiles.MemoryMappedFileAccess.Read);
|
||||
var magic = view.ReadUInt64(0);
|
||||
var generation = view.ReadUInt32(12);
|
||||
var stemCount = view.ReadInt32(16);
|
||||
var sampleRate = view.ReadInt32(64 + 8);
|
||||
var frameCount = view.ReadInt32(64 + 12);
|
||||
var sampleOffset = view.ReadInt32(64 + 16);
|
||||
var firstSample = view.ReadSingle(sampleOffset);
|
||||
|
||||
Console.WriteLine(
|
||||
$"obs-stem-shared-memory-smoke stems={stemCount} generation={generation} sampleRate={sampleRate} frames={frameCount} first={firstSample:0.000}");
|
||||
|
||||
return magic == 0x4D545352494D494DUL &&
|
||||
generation > 0 &&
|
||||
(generation & 1u) == 0 &&
|
||||
stemCount == 1 &&
|
||||
sampleRate == 48_000 &&
|
||||
frameCount == 3 &&
|
||||
Math.Abs(firstSample - 0.125f) < 0.0001f
|
||||
? 0
|
||||
: 1;
|
||||
}
|
||||
|
||||
static MimirAudioSynchronizationState NewSyncState(string sourceId, double delaySamples, double confidence) =>
|
||||
new(
|
||||
"loopback-scarlett-speakers",
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
private readonly MimirAudioSpectrumAnalyzer spectrumAnalyzer;
|
||||
private readonly MimirAlignmentActuatorBank audioActuatorBank = new();
|
||||
private readonly MimirObsStemPublicationState obsStemPublication = new(MimirObsPublicationConfigurations.AlignmentActuatorStemBus);
|
||||
private readonly MimirObsStemSharedMemoryPublisher? obsStemPublisher;
|
||||
private readonly IReadOnlyList<MimirStreamSourceFactory> sourceFactories;
|
||||
private readonly AquariumUiDocument ui;
|
||||
private readonly AquariumAudioDocument audio = new();
|
||||
|
|
@ -130,6 +131,7 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
calibrationGain = settings.Audio.CalibrationGain;
|
||||
watermarkGain = settings.Audio.WatermarkGain;
|
||||
syntheticSpectrumPreview = IsTruthy(Environment.GetEnvironmentVariable("MIMIR_SYNTHETIC_SPECTRUM_PREVIEW"));
|
||||
obsStemPublisher = CreateObsStemPublisher();
|
||||
complexContourWitness = settings.Audio.EnableComplexContourRuntime
|
||||
? new MimirBioacousticContestantRenderer(
|
||||
MimirBioacousticContestants.BuiltIn.FirstOrDefault(profile =>
|
||||
|
|
@ -689,6 +691,7 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
|
||||
public void Dispose()
|
||||
{
|
||||
obsStemPublisher?.Dispose();
|
||||
synchronization.Dispose();
|
||||
}
|
||||
|
||||
|
|
@ -1051,6 +1054,7 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
if (consumed)
|
||||
{
|
||||
lastObsStemPublication = obsStemPublication.Capture();
|
||||
obsStemPublisher?.Publish(lastObsStemPublication);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1169,6 +1173,27 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
: 0.0f;
|
||||
}
|
||||
|
||||
private static MimirObsStemSharedMemoryPublisher? CreateObsStemPublisher()
|
||||
{
|
||||
if (!OperatingSystem.IsWindows())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var mapName = Environment.GetEnvironmentVariable("MIMIR_OBS_STEM_MAP");
|
||||
try
|
||||
{
|
||||
return new MimirObsStemSharedMemoryPublisher(string.IsNullOrWhiteSpace(mapName)
|
||||
? MimirObsStemSharedMemoryPublisher.DefaultMapName
|
||||
: mapName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Mimir OBS stem shared-memory publisher disabled: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static string? ResolveRuntimeAssetPath(string relativePath)
|
||||
{
|
||||
if (File.Exists(relativePath))
|
||||
|
|
|
|||
|
|
@ -43,7 +43,8 @@ public sealed record MimirObsPublishedAudioStem(
|
|||
int FrameCount,
|
||||
int SampleRate,
|
||||
long Sequence,
|
||||
bool Configured);
|
||||
bool Configured,
|
||||
float[] Samples);
|
||||
|
||||
public sealed record MimirObsStemPublicationSnapshot(
|
||||
string ConfigurationId,
|
||||
|
|
@ -80,7 +81,8 @@ public sealed class MimirObsStemPublicationState(MimirObsPublicationConfiguratio
|
|||
frame.FrameCount,
|
||||
frame.SampleRate,
|
||||
frame.Sequence,
|
||||
isConfigured);
|
||||
isConfigured,
|
||||
channel.Samples);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
113
src/Mimir.Runtime/Synchronization/MimirObsStemSharedMemory.cs
Normal file
113
src/Mimir.Runtime/Synchronization/MimirObsStemSharedMemory.cs
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
using System.IO.MemoryMappedFiles;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Text;
|
||||
|
||||
namespace Mimir.Runtime.Synchronization;
|
||||
|
||||
public sealed class MimirObsStemSharedMemoryPublisher : IDisposable
|
||||
{
|
||||
public const string DefaultMapName = "Local\\MimirObsStemBus";
|
||||
public const int MaxStems = 8;
|
||||
public const int MaxFramesPerStem = 4096;
|
||||
public const int StemIdBytes = 64;
|
||||
public const int DisplayNameBytes = 64;
|
||||
public const int SourceIdBytes = 64;
|
||||
public const int HeaderBytes = 64;
|
||||
public const int StemRecordBytes = 224;
|
||||
public const int MapBytes = HeaderBytes + (MaxStems * StemRecordBytes) + (MaxStems * MaxFramesPerStem * sizeof(float));
|
||||
private const ulong Magic = 0x4D545352494D494DUL;
|
||||
private const int Version = 1;
|
||||
|
||||
private readonly MemoryMappedFile file;
|
||||
private readonly MemoryMappedViewAccessor view;
|
||||
private uint generation;
|
||||
private bool disposed;
|
||||
|
||||
[SupportedOSPlatform("windows")]
|
||||
public MimirObsStemSharedMemoryPublisher(string mapName = DefaultMapName)
|
||||
{
|
||||
file = MemoryMappedFile.CreateOrOpen(mapName, MapBytes, MemoryMappedFileAccess.ReadWrite);
|
||||
view = file.CreateViewAccessor(0, MapBytes, MemoryMappedFileAccess.ReadWrite);
|
||||
view.Write(0, Magic);
|
||||
view.Write(8, Version);
|
||||
view.Write(20, MaxStems);
|
||||
view.Write(24, MaxFramesPerStem);
|
||||
view.Write(28, HeaderBytes);
|
||||
view.Write(32, StemRecordBytes);
|
||||
view.Write(36, MapBytes);
|
||||
}
|
||||
|
||||
public void Publish(MimirObsStemPublicationSnapshot snapshot)
|
||||
{
|
||||
if (disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var stems = snapshot.ReadyStems
|
||||
.Where(stem => stem.FrameCount > 0 && stem.SampleRate > 0)
|
||||
.OrderBy(stem => stem.StemId, StringComparer.Ordinal)
|
||||
.Take(MaxStems)
|
||||
.ToArray();
|
||||
var beginGeneration = unchecked(++generation) | 1u;
|
||||
view.Write(12, beginGeneration);
|
||||
view.Write(16, stems.Length);
|
||||
var sampleOffset = HeaderBytes + (MaxStems * StemRecordBytes);
|
||||
for (var index = 0; index < MaxStems; index++)
|
||||
{
|
||||
var recordOffset = HeaderBytes + (index * StemRecordBytes);
|
||||
if (index >= stems.Length)
|
||||
{
|
||||
ClearRecord(recordOffset);
|
||||
continue;
|
||||
}
|
||||
|
||||
var stem = stems[index];
|
||||
var frameCount = Math.Min(stem.FrameCount, MaxFramesPerStem);
|
||||
WriteRecord(recordOffset, stem, sampleOffset, frameCount);
|
||||
view.WriteArray(sampleOffset, stem.Samples, 0, Math.Min(frameCount, stem.Samples.Length));
|
||||
sampleOffset += MaxFramesPerStem * sizeof(float);
|
||||
}
|
||||
|
||||
view.Write(12, unchecked(beginGeneration + 1u));
|
||||
}
|
||||
|
||||
private void ClearRecord(int offset)
|
||||
{
|
||||
view.Write(offset, -1);
|
||||
view.Write(offset + 4, 0);
|
||||
view.Write(offset + 8, 0);
|
||||
view.Write(offset + 12, 0);
|
||||
view.Write(offset + 16, 0);
|
||||
WriteFixedString(offset + 32, StemIdBytes, "");
|
||||
WriteFixedString(offset + 96, DisplayNameBytes, "");
|
||||
WriteFixedString(offset + 160, SourceIdBytes, "");
|
||||
}
|
||||
|
||||
private void WriteRecord(int offset, MimirObsPublishedAudioStem stem, int sampleOffset, int frameCount)
|
||||
{
|
||||
view.Write(offset, stem.ChannelIndex);
|
||||
view.Write(offset + 4, stem.Configured ? 1 : 0);
|
||||
view.Write(offset + 8, stem.SampleRate);
|
||||
view.Write(offset + 12, frameCount);
|
||||
view.Write(offset + 16, sampleOffset);
|
||||
WriteFixedString(offset + 32, StemIdBytes, stem.StemId);
|
||||
WriteFixedString(offset + 96, DisplayNameBytes, stem.DisplayName);
|
||||
WriteFixedString(offset + 160, SourceIdBytes, stem.SourceId);
|
||||
}
|
||||
|
||||
private void WriteFixedString(int offset, int byteCount, string value)
|
||||
{
|
||||
var buffer = new byte[byteCount];
|
||||
var bytes = Encoding.UTF8.GetBytes(value);
|
||||
Array.Copy(bytes, buffer, Math.Min(bytes.Length, byteCount - 1));
|
||||
view.WriteArray(offset, buffer, 0, byteCount);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
disposed = true;
|
||||
view.Dispose();
|
||||
file.Dispose();
|
||||
}
|
||||
}
|
||||
|
|
@ -189,3 +189,4 @@
|
|||
{"ts": "2026-05-27T20:18:50+01:00", "type": "verification", "status": "ok", "note": "MimirRuntime now queues sample-bearing AquariumStreamingAudioBlock lanes for actuator source buffers after publishing the control frame. Channel indices are derived from the bank-assigned Faust sourceN control path, so source disappearance does not reshuffle lanes. Mimir build passed; Fensalir tests prove declared blocks enter the streaming Faust DSP host."}
|
||||
{"ts": "2026-05-27T20:28:43+01:00", "type": "verification", "status": "ok", "note": "Fensalir now publishes processed streaming Faust DSP output lanes as AquariumAudioStemFrame records through an engine-owned IAquariumAudioStemBus. Mimir declares deterministic aligned-source output stems when queuing faust/mimir_alignment_actuator.dsp, so processed actuator lanes have stable stem names before OBS-facing publication consumes them. Fensalir build and Aquarium.Engine.Tests passed; Mimir build and --audio-actuator-bank-smoke passed."}
|
||||
{"ts": "2026-05-27T20:33:41+01:00", "type": "verification", "status": "ok", "note": "Mimir now has an OBS-facing stem publication state for the Fensalir Faust stem bus. It consumes AquariumAudioStemFrame records without copying audio, validates them against the alignment-actuator stem profile, reports ready/missing/unconfigured stems, and telemetry can print publication readiness. --audio-stem-publication-smoke passed; Mimir build passed."}
|
||||
{"ts": "2026-05-27T20:55:23+01:00", "type": "verification", "status": "blocked", "note": "OBS stem plugin path is now chosen and scaffolded. Mimir publishes validated stem snapshots into a fixed Windows shared-memory ABI (Local\\\\MimirObsStemBus), and native/obs_stem_source defines the first libobs audio source plugin that reads one named stem and calls obs_source_output_audio. dotnet build and --obs-stem-shared-memory-smoke passed. Local native plugin build is blocked until an OBS/libobs SDK with libobsConfig.cmake is installed or supplied via libobs_DIR."}
|
||||
|
|
|
|||
|
|
@ -194,8 +194,12 @@ accepted_design:
|
|||
Mimir declares deterministic aligned-source stem names with the actuator
|
||||
program. Mimir now consumes that bus through `MimirObsStemPublicationState`,
|
||||
validating ready/missing/unconfigured lanes against the alignment actuator
|
||||
stem-bus profile without copying audio; actual OBS device/plugin routing is
|
||||
the remaining program-audio cut. The synthetic
|
||||
stem-bus profile without copying audio. `MimirObsStemSharedMemoryPublisher`
|
||||
writes validated stems into the `Local\MimirObsStemBus` Windows
|
||||
shared-memory ABI, and `native/obs_stem_source` is the first OBS source
|
||||
plugin that reads one named stem from that map and submits it as libobs
|
||||
audio. Local native plugin compilation still needs an OBS/libobs SDK that
|
||||
provides `libobsConfig.cmake`. The synthetic
|
||||
codebook proof is `dotnet run --project
|
||||
.\src\Mimir.BufferSmoke\Mimir.BufferSmoke.csproj -- --bioacoustic-self-test`.
|
||||
`Mimir.BufferSmoke --render-chirp-bin-f32` plus
|
||||
|
|
|
|||
Loading…
Reference in a new issue